Skip to content

Commit fb8bfdc

Browse files
committed
feat: added util conversion functions on GlobalStorageEntry
1 parent c9ecc43 commit fb8bfdc

4 files changed

Lines changed: 65 additions & 12 deletions

File tree

compiler/compiler_global_scope/src/entry.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
use std::fmt::Display;
44

5+
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, builders::make_expected_simple_error};
6+
57
use crate::GlobalStorageIdentifier;
68

79
#[derive(Clone, Debug)]
@@ -16,6 +18,56 @@ pub enum GlobalStorageEntryType<T, R> {
1618
Type(R)
1719
}
1820

21+
impl<T: Clone, R: Clone> GlobalStorageEntry<T, R> {
22+
pub fn as_function<K: DiagnosticSpanOrigin>(&self, origin: &K) -> DiagnosticResult<(usize, usize)> {
23+
match self.entry_type {
24+
GlobalStorageEntryType::Function { descriptor_ind, impl_ind } => Ok((descriptor_ind, impl_ind)),
25+
26+
_ => Err(make_expected_simple_error(origin, &"function".to_string(), &self.entry_type).into())
27+
}
28+
}
29+
30+
pub fn as_implless_function<K: DiagnosticSpanOrigin>(&self, origin: &K) -> DiagnosticResult<usize> {
31+
match self.entry_type {
32+
GlobalStorageEntryType::ImplLessFunction(ind) => Ok(ind),
33+
34+
_ => Err(make_expected_simple_error(origin, &"implless function".to_string(), &self.entry_type).into())
35+
}
36+
}
37+
38+
pub fn as_struct_function<K: DiagnosticSpanOrigin>(&self, origin: &K) -> DiagnosticResult<(usize, usize, GlobalStorageIdentifier)> {
39+
match self.entry_type {
40+
GlobalStorageEntryType::StructFunction { descriptor_ind, impl_ind, struct_type } => Ok((descriptor_ind, impl_ind, struct_type)),
41+
42+
_ => Err(make_expected_simple_error(origin, &"struct function".to_string(), &self.entry_type).into())
43+
}
44+
}
45+
46+
pub fn as_static_variable<K: DiagnosticSpanOrigin>(&self, origin: &K) -> DiagnosticResult<T> {
47+
match &self.entry_type {
48+
GlobalStorageEntryType::StaticVariable(t) => Ok(t.clone()),
49+
50+
_ => Err(make_expected_simple_error(origin, &"static variable".to_string(), &self.entry_type).into())
51+
}
52+
}
53+
54+
pub fn as_type_alias<K: DiagnosticSpanOrigin>(&self, origin: &K) -> DiagnosticResult<T> {
55+
match &self.entry_type {
56+
GlobalStorageEntryType::TypeAlias(t) => Ok(t.clone()),
57+
58+
_ => Err(make_expected_simple_error(origin, &"type alias".to_string(), &self.entry_type).into())
59+
}
60+
}
61+
62+
pub fn as_type<K: DiagnosticSpanOrigin>(&self, origin: &K) -> DiagnosticResult<R> {
63+
match &self.entry_type {
64+
GlobalStorageEntryType::Type(t) => Ok(t.clone()),
65+
66+
_ => Err(make_expected_simple_error(origin, &"type".to_string(), &self.entry_type).into())
67+
}
68+
}
69+
}
70+
1971
#[derive(Debug)]
2072
pub struct GlobalStorageEntry<T, R> {
2173
pub entry_type: GlobalStorageEntryType<T, R>,

compiler/compiler_typing/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod storage;
1818
pub mod transmutation;
1919
pub mod bounds;
2020

21-
pub type TypedGlobalScope<F, I> = GlobalScopeStorage<Type, RawType, F, I>;
21+
pub type TypedGlobalScope = GlobalScopeStorage<Type, RawType>;
2222

2323
/// A function contained within a type.
2424
pub type TypedFunction = (Vec<(u64, TypeReference)>, Option<TypeReference>);
@@ -31,7 +31,7 @@ pub type RawTypeReference = usize;
3131
/// Represents a basic type that has a size.
3232
pub trait SizedType {
3333
/// Obtains the size of the type. The `compacted_size` parameter determines if the compacted size should be returned or not
34-
fn get_size(&self, t: &Type, compacted_size: bool, storage: &TypeStorage) -> usize;
34+
fn get_size(&self, t: &Type, compacted_size: bool, storage: &TypedGlobalScope) -> usize;
3535
}
3636

3737
/// Represents a type that contains type parameters
@@ -46,20 +46,20 @@ pub trait TypeParamType {
4646
/// Represents types that can contain functions and more
4747
pub trait StructuredType {
4848
#[must_use = "Must set the diagnostic position beforehand"]
49-
fn get_function(&self, hash: u64, storage: &TypeStorage) -> DiagnosticResult<TypedFunction>;
49+
fn get_function(&self, hash: u64, storage: &TypedGlobalScope) -> DiagnosticResult<TypedFunction>;
5050

5151
#[must_use = "Must set the diagnostic position beforehand"]
52-
fn get_function_hash(&self, hash: u64, storage: &TypeStorage) -> DiagnosticResult<usize>;
52+
fn get_function_hash(&self, hash: u64, storage: &TypedGlobalScope) -> DiagnosticResult<usize>;
5353

5454
#[must_use = "Must set the diagnostic position beforehand"]
55-
fn get_field(&self, hash: u64, storage: &TypeStorage) -> DiagnosticResult<TypeReference>;
55+
fn get_field(&self, hash: u64, storage: &TypedGlobalScope) -> DiagnosticResult<TypeReference>;
5656

5757
#[must_use = "Must set the diagnostic position beforehand"]
58-
fn get_field_hash(&self, hash: u64, storage: &TypeStorage) -> DiagnosticResult<usize>;
58+
fn get_field_hash(&self, hash: u64, storage: &TypedGlobalScope) -> DiagnosticResult<usize>;
5959

6060
#[must_use = "Must set the diagnostic position beforehand"]
61-
fn get_fields(&self, storage: &TypeStorage) -> Vec<u64>;
61+
fn get_fields(&self, storage: &TypedGlobalScope) -> Vec<u64>;
6262

6363
#[must_use = "Must set the diagnostic position beforehand"]
64-
fn get_functions(&self, storage: &TypeStorage) -> Vec<u64>;
64+
fn get_functions(&self, storage: &TypedGlobalScope) -> Vec<u64>;
6565
}

compiler/compiler_typing/src/raw.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{fmt::Display, hash::Hash};
44

5-
use crate::{SizedType, bounds::traits::Trait, enums::{RawEnumEntryContainer, RawEnumTypeContainer}, storage::TypeStorage, structs::{LoweredStructTypeContainer, RawStructTypeContainer}, tree::Type, utils::get_pointer_size};
5+
use crate::{SizedType, TypedGlobalScope, bounds::traits::Trait, enums::{RawEnumEntryContainer, RawEnumTypeContainer}, storage::TypeStorage, structs::{LoweredStructTypeContainer, RawStructTypeContainer}, tree::Type, utils::get_pointer_size};
66

77
/// The raw types. Are also named generics
88
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -37,10 +37,10 @@ impl RawType {
3737
return RawType::Integer(bits, false)
3838
}
3939

40-
pub fn get_type_params_count(&self, storage: &TypeStorage) -> usize {
40+
pub fn get_type_params_count(&self, storage: &TypedGlobalScope) -> usize {
4141
match self {
4242
RawType::Enum(container) => container.type_params.len(),
43-
RawType::EnumEntry(container) => storage.types.vals[container.parent].get_type_params_count(storage),
43+
RawType::EnumEntry(container),
4444
RawType::Struct(_, container) => container.type_params.len(),
4545

4646
_ => 0
@@ -180,7 +180,7 @@ impl RawType {
180180
}
181181

182182
impl SizedType for RawType {
183-
fn get_size(&self, t: &Type, compacted_size: bool, storage: &TypeStorage) -> usize {
183+
fn get_size(&self, t: &Type, compacted_size: bool, storage: &TypedGlobalScope) -> usize {
184184
match self {
185185
RawType::Integer(size, _) => *size,
186186
RawType::Floating(size, _) => *size,

compiler/compiler_typing/src/storage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub const BOOLEAN_TYPE: u64 = hash!("bool");
5858
pub const RESULT_TYPE: u64 = hash!("result");
5959

6060
#[derive(Debug)]
61+
#[deprecated = "Deprecated to use global scope"]
6162
pub struct TypeStorage {
6263
pub types: IndexStorage<RawType>,
6364
pub type_to_ind: HashMap<RawType, usize>

0 commit comments

Comments
 (0)