Skip to content

Commit 005a2fc

Browse files
committed
feat: lowered HIR functions to global scope
1 parent 7276b82 commit 005a2fc

2 files changed

Lines changed: 66 additions & 25 deletions

File tree

compiler/astoir_hir/src/scope.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use compiler_global_scope::key::EntryKey;
44
use compiler_typing::{TypedGlobalScope, TypedGlobalScopeEntry, raw::RawType, tree::Type};
5-
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin};
5+
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, builders::make_cannot_find};
66

77
use crate::{
88
ctx::{HIRBranchedContext, HIRFunction, HIRFunctionImpl},
@@ -126,6 +126,18 @@ impl HIRGlobalScopeStorage {
126126
self.scope.get_base(name, origin)
127127
}
128128

129+
pub fn get_ind<K: DiagnosticSpanOrigin>(
130+
&self,
131+
name: EntryKey,
132+
origin: &K,
133+
) -> DiagnosticResult<usize> {
134+
if self.scope.entry_to_ind.contains_key(&name) {
135+
return Ok(self.scope.entry_to_ind[&name]);
136+
}
137+
138+
return Err(make_cannot_find(origin, &name.name_hash).into());
139+
}
140+
129141
pub fn get_type<K: DiagnosticSpanOrigin>(
130142
&self,
131143
name: EntryKey,

compiler/astoir_hir_lowering/src/func.rs

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use astoir_hir::{
33
ctx::{HIRBranchedContext, HIRContext},
44
nodes::{HIRNode, HIRNodeKind},
55
};
6-
use diagnostics::{
7-
DiagnosticResult,
8-
builders::{make_already_in_scope, make_cannot_find_func},
9-
};
6+
use compiler_global_scope::key::EntryKey;
7+
use compiler_typing::TypedGlobalScopeEntry;
8+
use diagnostics::{DiagnosticResult, builders::make_already_in_scope};
109

1110
use crate::{lower_ast_body, types::lower_ast_type, values::lower_ast_value};
1211

@@ -16,12 +15,16 @@ pub fn lower_ast_function_call(
1615
node: Box<ASTTreeNode>,
1716
) -> DiagnosticResult<Box<HIRNode>> {
1817
if let ASTTreeNodeKind::FunctionCall { func, args } = node.kind.clone() {
19-
let f_ind = match context.functions.get_index(func.hash) {
20-
Some(v) => v,
21-
None => return Err(make_cannot_find_func(&*node, &func.hash).into()),
18+
let name = EntryKey {
19+
name_hash: func.hash,
2220
};
2321

24-
let func = &context.functions.vals[f_ind].clone();
22+
let func = context
23+
.global_scope
24+
.get_function_base(name.clone(), &*node)?;
25+
26+
let func_ind = context.global_scope.get_ind(name, &*node)?;
27+
2528
let mut hir_args = vec![];
2629
let mut ind = 0;
2730

@@ -37,7 +40,7 @@ pub fn lower_ast_function_call(
3740

3841
return Ok(Box::new(HIRNode::new(
3942
HIRNodeKind::FunctionCall {
40-
func_name: f_ind,
43+
func_name: func_ind,
4144
arguments: hir_args,
4245
},
4346
&node.start,
@@ -91,15 +94,21 @@ pub fn lower_ast_function_declaration(
9194
}
9295
}
9396

94-
let ind = context.functions.append(
95-
func_name.hash,
97+
let key = EntryKey {
98+
name_hash: func_name.hash,
99+
};
100+
101+
let entry =
102+
TypedGlobalScopeEntry::ImplLessFunction(context.global_scope.scope.descriptor_counter);
103+
104+
let ind = context.global_scope.append_implless_function(
105+
key.clone(),
96106
(ret_type.clone(), arguments.clone(), func_name.val.clone()),
97-
);
107+
&*node,
108+
)?;
98109

99110
let body = lower_ast_body(context, &mut curr_ctx, body, false)?;
100111

101-
context.function_contexts.push(Some(curr_ctx.clone()));
102-
103112
curr_ctx.end_branch(branch);
104113

105114
for var in 0..curr_ctx.variables.len() {
@@ -108,18 +117,37 @@ pub fn lower_ast_function_declaration(
108117
}
109118
}
110119

111-
return Ok(Box::new(HIRNode::new(
120+
let implementation = Box::new(HIRNode::new(
112121
HIRNodeKind::FunctionDeclaration {
113122
func_name: ind,
114-
arguments,
115-
return_type: ret_type,
123+
arguments: arguments.clone(),
124+
return_type: ret_type.clone(),
116125
body,
117-
ctx: curr_ctx,
126+
ctx: curr_ctx.clone(),
118127
requires_this,
119128
},
120129
&node.start,
121130
&node.end,
122-
)));
131+
));
132+
133+
// Remove old impless version
134+
context.global_scope.scope.entries.pop();
135+
context.global_scope.scope.entry_to_ind.remove(&key);
136+
context.global_scope.scope.value_to_ind.remove(&entry);
137+
context.global_scope.descriptors.pop();
138+
context.global_scope.scope.descriptor_counter -= 1;
139+
140+
// Append the new verison as a impl-containing function
141+
142+
context.global_scope.append_func(
143+
key,
144+
(ret_type.clone(), arguments.clone(), func_name.val.clone()),
145+
implementation.clone(),
146+
curr_ctx.clone(),
147+
&*node,
148+
);
149+
150+
return Ok(implementation);
123151
}
124152

125153
panic!("Invalid node passed!");
@@ -155,12 +183,13 @@ pub fn lower_ast_shadow_function_declaration(
155183
arguments.push((arg.name.hash, t));
156184
}
157185

158-
let ind = context.functions.append(
159-
func_name.hash,
186+
let ind = context.global_scope.append_implless_function(
187+
EntryKey {
188+
name_hash: func_name.hash,
189+
},
160190
(ret_type.clone(), arguments.clone(), func_name.val.clone()),
161-
);
162-
163-
context.function_contexts.push(None);
191+
&*node,
192+
)?;
164193

165194
return Ok(Box::new(HIRNode::new(
166195
HIRNodeKind::ShadowFunctionDeclaration {

0 commit comments

Comments
 (0)