Skip to content

Commit cdda6e6

Browse files
committed
fix: fixed up global scope to work
1 parent fbbfd62 commit cdda6e6

13 files changed

Lines changed: 115 additions & 49 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/astoir_hir_lowering/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ diagnostics = { path = "../diagnostics" }
1111
compiler_utils = { path = "../compiler_utils" }
1212
lexer = { path = "../lexer" }
1313
ast_parser = { path = "../ast_parser" }
14-
prelude = { path = "../prelude" }
14+
prelude = { path = "../prelude" }
15+
compiler_global_scope = { path = "../compiler_global_scope" }

compiler/astoir_hir_lowering/src/enums.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use astoir_hir::{
33
ctx::HIRContext,
44
nodes::{HIRNode, HIRNodeKind},
55
};
6+
use compiler_global_scope::{entry::GlobalStorageEntryType, key::EntryKey};
67
use compiler_typing::{enums::RawEnumTypeContainer, raw::RawType};
78
use diagnostics::{DiagnosticResult, MaybeDiagnostic, builders::make_already_in_scope};
89

@@ -46,16 +47,19 @@ pub fn lower_ast_enum(
4647
} = node.kind.clone()
4748
{
4849
let mut container =
49-
RawEnumTypeContainer::new(context.type_storage.types.vals.len(), type_params);
50+
RawEnumTypeContainer::new(context.global_scope.entries.len(), type_params);
5051

5152
for entry in entries {
5253
lower_ast_enum_entry(context, entry, &mut container)?;
5354
}
5455

55-
let ind = match context
56-
.type_storage
57-
.append_with_hash(name.hash, RawType::Enum(container.clone()))
58-
{
56+
let ind = match context.global_scope.append(
57+
EntryKey {
58+
name_hash: name.hash,
59+
},
60+
GlobalStorageEntryType::Type(RawType::Enum(container.clone())),
61+
&*node,
62+
) {
5963
Ok(v) => v,
6064
Err(_) => return Err(make_already_in_scope(&*node, &name.val).into()),
6165
};

compiler/astoir_hir_lowering/src/literals.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use astoir_hir::{
33
ctx::HIRContext,
44
nodes::{HIRNode, HIRNodeKind},
55
};
6+
use compiler_global_scope::key::EntryKey;
67
use compiler_typing::tree::Type;
78
use diagnostics::{DiagnosticResult, builders::make_cannot_find_type};
89

@@ -12,7 +13,10 @@ pub fn lower_ast_literal(
1213
) -> DiagnosticResult<Box<HIRNode>> {
1314
match node.kind {
1415
ASTTreeNodeKind::IntegerLit { val, hash } => {
15-
let lit_type = match context.type_storage.get_type(hash) {
16+
let lit_type = match context
17+
.global_scope
18+
.get_type(EntryKey { name_hash: hash }, &*node)
19+
{
1620
Ok(v) => v,
1721
Err(_) => return Err(make_cannot_find_type(&*node, &hash).into()),
1822
};

compiler/astoir_hir_lowering/src/structs.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use astoir_hir::{
66
nodes::{HIRNode, HIRNodeKind},
77
structs::HIRStructContainer,
88
};
9+
use compiler_global_scope::{entry::GlobalStorageEntryType, key::EntryKey};
910
use compiler_typing::{raw::RawType, structs::RawStructTypeContainer};
1011
use compiler_utils::utils::indexed::IndexStorage;
1112
use diagnostics::{DiagnosticResult, builders::make_already_in_scope};
@@ -103,7 +104,13 @@ pub fn lower_ast_struct_declaration(
103104

104105
let base = RawType::Struct(layout, container.clone());
105106

106-
let ind = match context.type_storage.append(name.hash, base) {
107+
let ind = match context.global_scope.append(
108+
EntryKey {
109+
name_hash: name.hash,
110+
},
111+
GlobalStorageEntryType::Type(base),
112+
&*node,
113+
) {
107114
Ok(v) => v,
108115
Err(_) => return Err(make_already_in_scope(&*node, &name.val).into()),
109116
};
@@ -113,14 +120,14 @@ pub fn lower_ast_struct_declaration(
113120
&ASTTreeNodeKind::StructFieldMember { .. } => {
114121
lower_ast_struct_member(context, member, &mut container)?;
115122

116-
context.type_storage.types.vals[ind] =
117-
RawType::Struct(layout, container.clone());
123+
context.global_scope.entries[ind].entry_type =
124+
GlobalStorageEntryType::Type(RawType::Struct(layout, container.clone()));
118125
}
119126
&ASTTreeNodeKind::FunctionDeclaration { .. } => {
120127
let body = lower_ast_struct_function_decl(context, member, &mut container)?;
121128

122-
context.type_storage.types.vals[ind] =
123-
RawType::Struct(layout, container.clone());
129+
context.global_scope.entries[ind].entry_type =
130+
GlobalStorageEntryType::Type(RawType::Struct(layout, container.clone()));
124131

125132
func_impls.push(body);
126133
}

compiler/astoir_hir_lowering/src/types.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use ast::types::ASTType;
22
use astoir_hir::ctx::HIRContext;
3+
use compiler_global_scope::{entry::GlobalStorageEntryType, key::EntryKey};
34
use compiler_typing::{TypeParamType, raw::RawType, references::TypeReference, tree::Type};
45
use compiler_utils::hash::HashedString;
56
use diagnostics::{
@@ -19,7 +20,10 @@ pub fn lower_ast_type<K: DiagnosticSpanOrigin>(
1920
ASTType::Generic(type_id, type_params, size_params, specifier) => {
2021
let hash = HashedString::new(type_id).hash;
2122

22-
let mut t = match context.type_storage.get_type(hash) {
23+
let mut t = match context
24+
.global_scope
25+
.get_type(EntryKey { name_hash: hash }, origin)
26+
{
2327
Ok(v) => v,
2428
Err(_) => return Err(make_cannot_find_type(origin, &hash).into()),
2529
};
@@ -33,11 +37,11 @@ pub fn lower_ast_type<K: DiagnosticSpanOrigin>(
3337
t = container.get_entry(HashedString::new(specifier.unwrap()))?
3438
}
3539

36-
if t.get_type_params_count(&context.type_storage) != type_params.len() {
40+
if t.get_type_params_count(&context.global_scope, origin)? != type_params.len() {
3741
return Err(make_diff_type_specifiers(
3842
origin,
3943
&type_params.len(),
40-
&t.get_type_params_count(&context.type_storage),
44+
&t.get_type_params_count(&context.global_scope, origin)?,
4145
)
4246
.into());
4347
}
@@ -51,18 +55,26 @@ pub fn lower_ast_type<K: DiagnosticSpanOrigin>(
5155
let res = Type::Generic(t.clone(), t_params, size_params);
5256

5357
if t.is_sized() {
54-
let lower = lower_sized_base_type(context, &res, origin)?;
58+
let lower = lower_sized_base_type(&res, origin)?;
5559

56-
if context.type_storage.type_to_ind.contains_key(&lower) {
60+
if context
61+
.global_scope
62+
.value_to_ind
63+
.contains_key(&GlobalStorageEntryType::Type(lower.clone()))
64+
{
5765
return Ok(Type::Generic(t, vec![], vec![]));
5866
} else {
59-
let ind = match context.type_storage.append_with_hash(hash, lower) {
67+
let ind = match context.global_scope.append(
68+
EntryKey { name_hash: hash },
69+
GlobalStorageEntryType::Type(lower),
70+
origin,
71+
) {
6072
Ok(v) => v,
6173
Err(_) => panic!("Generic lowering type cannot be found on type_to_hash"),
6274
};
6375

6476
return Ok(Type::Generic(
65-
context.type_storage.types.vals[ind].clone(),
77+
context.global_scope.entries[ind].as_type_unsafe(),
6678
vec![],
6779
vec![],
6880
));
@@ -106,13 +118,12 @@ pub fn lower_ast_type_struct<K: DiagnosticSpanOrigin, T: TypeParamType>(
106118
}
107119

108120
pub fn lower_sized_base_type<K: DiagnosticSpanOrigin>(
109-
context: &HIRContext,
110121
t: &Type,
111122
origin: &K,
112123
) -> DiagnosticResult<RawType> {
113124
let data = t.get_generic_info();
114125

115-
match t.get_generic(&context.type_storage) {
126+
match t.get_generic() {
116127
RawType::SizedInteger(e) => {
117128
if data.1.len() != 1 {
118129
return Err(make_diff_size_specifiers(origin, &1, &data.1.len()).into());

compiler/astoir_hir_lowering/src/uses.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use astoir_hir::{
1010
ctx::HIRContext,
1111
nodes::{HIRNode, HIRNodeKind},
1212
};
13+
use compiler_global_scope::{entry::GlobalStorageEntryType, key::EntryKey};
1314
use compiler_typing::{raw::RawType, tree::Type};
1415
use compiler_utils::hash::HashedString;
1516
use diagnostics::{
@@ -135,7 +136,12 @@ pub fn gather_type_use<K: DiagnosticSpanOrigin>(
135136
pass: bool,
136137
ctx: &ParserCtx,
137138
) -> DiagnosticResult<RawType> {
138-
match context.type_storage.get_type(val.hash) {
139+
match context.global_scope.get_type(
140+
EntryKey {
141+
name_hash: val.hash,
142+
},
143+
origin,
144+
) {
139145
Ok(v) => return Ok(v),
140146
Err(_) => {
141147
if pass {
@@ -173,11 +179,11 @@ pub fn lower_ast_type_use_statement<K: DiagnosticSpanOrigin>(
173179
t = container.get_entry(HashedString::new(specifier.unwrap()))?
174180
}
175181

176-
if t.get_type_params_count(&context.type_storage) != type_params.len() {
182+
if t.get_type_params_count(&context.global_scope, origin)? != type_params.len() {
177183
return Err(make_diff_type_specifiers(
178184
origin,
179185
&type_params.len(),
180-
&t.get_type_params_count(&context.type_storage),
186+
&t.get_type_params_count(&context.global_scope, origin)?,
181187
)
182188
.into());
183189
}
@@ -191,18 +197,28 @@ pub fn lower_ast_type_use_statement<K: DiagnosticSpanOrigin>(
191197
let res = Type::Generic(t.clone(), t_params, size_params);
192198

193199
if t.is_sized() {
194-
let lower = lower_sized_base_type(context, &res, origin)?;
200+
let lower = lower_sized_base_type(&res, origin)?;
195201

196-
if context.type_storage.type_to_ind.contains_key(&lower) {
202+
if context
203+
.global_scope
204+
.value_to_ind
205+
.contains_key(&GlobalStorageEntryType::Type(lower.clone()))
206+
{
197207
return Ok(Type::Generic(t, vec![], vec![]));
198208
} else {
199-
let ind = match context.type_storage.append_with_hash(hash, lower) {
209+
let ind = match context.global_scope.append(
210+
EntryKey { name_hash: hash },
211+
GlobalStorageEntryType::Type(lower),
212+
origin,
213+
) {
200214
Ok(v) => v,
201-
Err(_) => panic!("Generic lowering type cannot be found on type_to_hash"),
215+
Err(_) => {
216+
panic!("Generic lowering type cannot be found on type_to_hash")
217+
}
202218
};
203219

204220
return Ok(Type::Generic(
205-
context.type_storage.types.vals[ind].clone(),
221+
context.global_scope.entries[ind].as_type_unsafe(),
206222
vec![],
207223
vec![],
208224
));

compiler/astoir_hir_lowering/src/values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(crate) fn lower_ast_lru_base(
3737
let ind: usize;
3838

3939
if let Some(curr_type_val) = curr_type {
40-
let res = match curr_type_val.get_function(&context.type_storage, func.hash) {
40+
let res = match curr_type_val.get_function(&context.global_scope, func.hash) {
4141
Ok(v) => v,
4242
Err(_) => {
4343
return Err(
@@ -108,7 +108,7 @@ pub(crate) fn lower_ast_lru_base(
108108
let ind: usize;
109109

110110
if let Some(curr_type_val) = curr_type {
111-
let res = match curr_type_val.get_field(&context.type_storage, str.hash) {
111+
let res = match curr_type_val.get_field(&context.global_scope, str.hash) {
112112
Ok(v) => v,
113113
Err(_) => {
114114
return Err(

compiler/compiler_global_scope/src/entry.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Definitions for entries
22
3-
use std::fmt::Display;
3+
use std::{fmt::Display, hash::Hash};
44

55
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, builders::make_expected_simple_error};
66

77
use crate::GlobalStorageIdentifier;
88

9-
#[derive(Clone, Debug)]
10-
pub enum GlobalStorageEntryType<T, R> {
9+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
10+
pub enum GlobalStorageEntryType<T: Hash, R: Hash> {
1111
Function {
1212
descriptor_ind: usize,
1313
impl_ind: usize,
@@ -25,7 +25,7 @@ pub enum GlobalStorageEntryType<T, R> {
2525
Type(R),
2626
}
2727

28-
impl<T: Clone, R: Clone> GlobalStorageEntry<T, R> {
28+
impl<T: Clone + Hash, R: Clone + Hash> GlobalStorageEntry<T, R> {
2929
pub fn as_function<K: DiagnosticSpanOrigin>(
3030
&self,
3131
origin: &K,
@@ -171,13 +171,13 @@ impl<T: Clone, R: Clone> GlobalStorageEntry<T, R> {
171171
}
172172
}
173173

174-
#[derive(Debug)]
175-
pub struct GlobalStorageEntry<T, R> {
174+
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
175+
pub struct GlobalStorageEntry<T: Hash, R: Hash> {
176176
pub entry_type: GlobalStorageEntryType<T, R>,
177177
pub parent_index: usize,
178178
}
179179

180-
impl<T, R> Display for GlobalStorageEntryType<T, R> {
180+
impl<T: Hash, R: Hash> Display for GlobalStorageEntryType<T, R> {
181181
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
182182
let s = match self {
183183
Self::Function { .. } => "function",

compiler/compiler_global_scope/src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, hash::Hash};
22

33
use diagnostics::{
4-
DiagnosticResult, DiagnosticSpanOrigin, MaybeDiagnostic,
4+
DiagnosticResult, DiagnosticSpanOrigin,
55
builders::{make_already_in_scope, make_cannot_find, make_expected_simple_error},
66
};
77

@@ -16,10 +16,12 @@ pub mod key;
1616
pub type GlobalStorageIdentifier = usize;
1717

1818
#[derive(Debug)]
19-
pub struct GlobalScopeStorage<T, R> {
19+
pub struct GlobalScopeStorage<T: Hash, R: Hash> {
2020
pub entry_to_ind: HashMap<EntryKey, usize>,
2121
pub entries: Vec<GlobalStorageEntry<T, R>>,
2222

23+
pub value_to_ind: HashMap<GlobalStorageEntryType<T, R>, usize>,
24+
2325
pub descriptor_counter: usize,
2426
pub impl_counter: usize,
2527
}
@@ -34,10 +36,11 @@ pub struct GlobalScopeStorage<T, R> {
3436
///
3537
/// # Safety
3638
/// The `GlobalScopeStorage` enforces correctness for global scope types and strictly allows only one entry per name. globally.
37-
impl<T: Clone, R: Clone> GlobalScopeStorage<T, R> {
39+
impl<T: Clone + Hash + Eq, R: Clone + Hash + Eq> GlobalScopeStorage<T, R> {
3840
pub fn new() -> Self {
3941
GlobalScopeStorage {
4042
entry_to_ind: HashMap::new(),
43+
value_to_ind: HashMap::new(),
4144
entries: vec![],
4245
descriptor_counter: 0,
4346
impl_counter: 0,
@@ -49,13 +52,15 @@ impl<T: Clone, R: Clone> GlobalScopeStorage<T, R> {
4952
name: EntryKey,
5053
entry: GlobalStorageEntryType<T, R>,
5154
origin: &K,
52-
) -> MaybeDiagnostic {
55+
) -> DiagnosticResult<usize> {
5356
if self.entry_to_ind.contains_key(&name) {
5457
return Err(make_already_in_scope(origin, &name.name_hash).into());
5558
}
5659

5760
let parent_index = self.entries.len();
5861

62+
self.value_to_ind.insert(entry.clone(), parent_index);
63+
5964
let entry = GlobalStorageEntry {
6065
entry_type: entry,
6166
parent_index,
@@ -64,7 +69,7 @@ impl<T: Clone, R: Clone> GlobalScopeStorage<T, R> {
6469
self.entries.push(entry);
6570
self.entry_to_ind.insert(name, parent_index);
6671

67-
Ok(())
72+
Ok(parent_index)
6873
}
6974

7075
pub fn get_base<K: DiagnosticSpanOrigin>(

0 commit comments

Comments
 (0)