Skip to content

Commit c9ecc43

Browse files
committed
feat: changed to use indexes for non type items
1 parent 2ae03b4 commit c9ecc43

2 files changed

Lines changed: 38 additions & 33 deletions

File tree

compiler/compiler_global_scope/src/entry.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,35 @@
22
33
use std::fmt::Display;
44

5-
use crate::{GlobalStorageIdentifier};
5+
use crate::GlobalStorageIdentifier;
66

77
#[derive(Clone, Debug)]
8-
pub enum GlobalStorageEntryType<T, R, F, I> {
9-
Function(F, I),
10-
ImplLessFunction(F),
8+
pub enum GlobalStorageEntryType<T, R> {
9+
Function { descriptor_ind: usize, impl_ind: usize },
10+
ImplLessFunction(usize),
11+
StructFunction { descriptor_ind: usize, impl_ind: usize, struct_type: GlobalStorageIdentifier },
12+
1113
StaticVariable(T),
1214

13-
StructFunction(F, I, GlobalStorageIdentifier),
14-
15+
TypeAlias(T),
1516
Type(R)
1617
}
1718

1819
#[derive(Debug)]
19-
pub struct GlobalStorageEntry<T, R, F, I> {
20-
pub entry_type: GlobalStorageEntryType<T, R, F, I>,
20+
pub struct GlobalStorageEntry<T, R> {
21+
pub entry_type: GlobalStorageEntryType<T, R>,
2122
pub parent_index: usize
2223
}
2324

24-
impl<T, R, F, I> Display for GlobalStorageEntryType<T, R, F, I> {
25+
impl<T, R> Display for GlobalStorageEntryType<T, R> {
2526
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2627
let s = match self {
27-
Self::Function(_, _) => "function",
28+
Self::Function { .. } => "function",
2829
Self::ImplLessFunction(_) => "function",
29-
Self::StructFunction(_, _, _) => "function",
30+
Self::StructFunction { .. } => "function",
3031
Self::StaticVariable(_) => "static variable",
31-
Self::Type(_) => "type"
32+
Self::Type(_) => "type",
33+
Self::TypeAlias(_) => "type (alias)"
3234
};
3335

3436
write!(f, "{}", s)?;

compiler/compiler_global_scope/src/lib.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ pub type GlobalStorageIdentifier = usize;
1111

1212

1313
#[derive(Debug)]
14-
pub struct GlobalScopeStorage<T, R, F, I> {
14+
pub struct GlobalScopeStorage<T, R> {
1515
pub entry_to_ind: HashMap<EntryKey, usize>,
16-
pub entries: Vec<GlobalStorageEntry<T, R, F, I>>,
16+
pub entries: Vec<GlobalStorageEntry<T, R>>,
17+
18+
pub descriptor_counter: usize,
19+
pub impl_counter: usize
1720
}
1821

1922

@@ -27,12 +30,12 @@ pub struct GlobalScopeStorage<T, R, F, I> {
2730
///
2831
/// # Safety
2932
/// The `GlobalScopeStorage` enforces correctness for global scope types and strictly allows only one entry per name. globally.
30-
impl<T: Clone, R: Clone, F: Clone, I: Clone> GlobalScopeStorage<T, R, F, I> {
33+
impl<T: Clone, R: Clone> GlobalScopeStorage<T, R> {
3134
pub fn new() -> Self {
32-
GlobalScopeStorage { entry_to_ind: HashMap::new(), entries: vec![] }
35+
GlobalScopeStorage { entry_to_ind: HashMap::new(), entries: vec![], descriptor_counter: 0, impl_counter: 0 }
3336
}
3437

35-
pub fn append<K: DiagnosticSpanOrigin>(&mut self, name: EntryKey, entry: GlobalStorageEntryType<T, R, F, I>, origin: &K) -> MaybeDiagnostic {
38+
pub fn append<K: DiagnosticSpanOrigin>(&mut self, name: EntryKey, entry: GlobalStorageEntryType<T, R>, origin: &K) -> MaybeDiagnostic {
3639
if self.entry_to_ind.contains_key(&name) {
3740
return Err(make_already_in_scope(origin, &name.name_hash).into())
3841
}
@@ -47,7 +50,7 @@ impl<T: Clone, R: Clone, F: Clone, I: Clone> GlobalScopeStorage<T, R, F, I> {
4750
Ok(())
4851
}
4952

50-
pub fn get_base<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<GlobalStorageEntryType<T, R, F, I>> {
53+
pub fn get_base<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<GlobalStorageEntryType<T, R>> {
5154
if !self.entry_to_ind.contains_key(&name) {
5255
return Err(make_cannot_find(origin, &name.name_hash).into());
5356
}
@@ -73,56 +76,56 @@ impl<T: Clone, R: Clone, F: Clone, I: Clone> GlobalScopeStorage<T, R, F, I> {
7376
};
7477
}
7578

76-
pub fn get_function_base<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<F> {
79+
pub fn get_function_base<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<usize> {
7780
let base = self.get_base(name, origin)?;
7881

7982
return match base {
80-
GlobalStorageEntryType::Function(hir, _) => Ok(hir.clone()),
81-
GlobalStorageEntryType::ImplLessFunction(hir) => Ok(hir.clone()),
82-
GlobalStorageEntryType::StructFunction(hir, _, _) => Ok(hir.clone()),
83+
GlobalStorageEntryType::Function { descriptor_ind, impl_ind: _ } => Ok(descriptor_ind),
84+
GlobalStorageEntryType::ImplLessFunction(descriptor_ind) => Ok(descriptor_ind),
85+
GlobalStorageEntryType::StructFunction { descriptor_ind, impl_ind: _, struct_type: _} => Ok(descriptor_ind),
8386

8487
_ => Err(make_expected_simple_error(origin, &"function".to_string(), &base).into())
8588
};
8689
}
8790

88-
pub fn get_function_impl<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<I> {
91+
pub fn get_function_impl<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<usize> {
8992
let base = self.get_base(name, origin)?;
9093

9194
return match base {
92-
GlobalStorageEntryType::Function(_, i) => Ok(i.clone()),
93-
GlobalStorageEntryType::StructFunction(_, i, _) => Ok(i.clone()),
95+
GlobalStorageEntryType::Function { descriptor_ind: _, impl_ind } => Ok(impl_ind),
96+
GlobalStorageEntryType::StructFunction { descriptor_ind: _, impl_ind, struct_type: _ } => Ok(impl_ind),
9497

9598
_ => Err(make_expected_simple_error(origin, &"function with implementation", &base).into())
9699
};
97100
}
98101

99-
pub fn get_implless_function<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<F> {
102+
pub fn get_implless_function<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<usize> {
100103
let base = self.get_base(name, origin)?;
101104

102105
return match base {
103-
GlobalStorageEntryType::ImplLessFunction(hir) => Ok(hir.clone()),
106+
GlobalStorageEntryType::ImplLessFunction(descriptor_ind) => Ok(descriptor_ind),
104107

105108
_ => Err(make_expected_simple_error(origin, &"function without implementation", &base).into())
106109
}
107110
}
108111

109-
pub fn get_exact_function<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<(F, I)> {
112+
pub fn get_exact_function<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<(usize, usize)> {
110113
let base = self.get_base(name, origin)?;
111114

112115
return match base {
113-
GlobalStorageEntryType::Function(hir, i) => Ok((hir.clone(), i.clone())),
116+
GlobalStorageEntryType::Function { descriptor_ind, impl_ind} => Ok((descriptor_ind, impl_ind)),
114117

115118
_ => Err(make_expected_simple_error(origin, &"function", &base).into())
116119
}
117120
}
118121

119-
pub fn get_exact_struct_function<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<(F, I, R)> {
122+
pub fn get_exact_struct_function<K: DiagnosticSpanOrigin>(&self, name: EntryKey, origin: &K) -> DiagnosticResult<(usize, usize, R)> {
120123
let base = self.get_base(name, origin)?;
121124

122125
return match base {
123-
GlobalStorageEntryType::StructFunction(hir, i, o) => {
124-
if let GlobalStorageEntryType::Type(t) = self.entries[o].entry_type.clone() {
125-
Ok((hir, i, t))
126+
GlobalStorageEntryType::StructFunction { descriptor_ind, impl_ind, struct_type } => {
127+
if let GlobalStorageEntryType::Type(t) = self.entries[struct_type].entry_type.clone() {
128+
Ok((descriptor_ind, impl_ind, t))
126129
} else {
127130
Err(make_expected_simple_error(origin, &"type", &self.entries[0].entry_type).into())
128131
}

0 commit comments

Comments
 (0)