@@ -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
1110use 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