1- use std:: collections:: HashMap ;
1+ use std:: {
2+ collections:: HashMap ,
3+ hash:: { DefaultHasher , Hash , Hasher } ,
4+ } ;
25
36use ast:: tree:: { ASTTreeNode , ASTTreeNodeKind } ;
47use astoir_hir:: {
58 ctx:: { HIRBranchedContext , HIRContext } ,
69 nodes:: { HIRNode , HIRNodeKind } ,
7- structs:: HIRStructContainer ,
810} ;
911use compiler_global_scope:: { entry:: GlobalStorageEntryType , key:: EntryKey } ;
1012use compiler_typing:: { raw:: RawType , structs:: RawStructTypeContainer } ;
11- use compiler_utils:: utils:: indexed:: IndexStorage ;
13+ use compiler_utils:: { hash :: HashedString , utils:: indexed:: IndexStorage } ;
1214use diagnostics:: { DiagnosticResult , builders:: make_already_in_scope} ;
1315
1416use crate :: { lower_ast_body, types:: lower_ast_type_struct, values:: lower_ast_value} ;
@@ -32,7 +34,8 @@ fn lower_ast_struct_function_decl(
3234 context : & mut HIRContext ,
3335 node : Box < ASTTreeNode > ,
3436 container : & mut RawStructTypeContainer ,
35- ) -> DiagnosticResult < Box < HIRNode > > {
37+ ty : RawType ,
38+ ) -> DiagnosticResult < ( Box < HIRNode > , usize ) > {
3639 if let ASTTreeNodeKind :: FunctionDeclaration {
3740 func_name,
3841 args,
@@ -66,18 +69,47 @@ fn lower_ast_struct_function_decl(
6669 . functions
6770 . append ( func_name. hash , ( arguments. clone ( ) , ret_type. clone ( ) ) ) ;
6871
69- return Ok ( Box :: new ( HIRNode :: new (
72+ let implementation = Box :: new ( HIRNode :: new (
7073 HIRNodeKind :: StructFunctionDeclaration {
7174 func_name : ind,
72- arguments,
73- return_type : ret_type,
75+ arguments : arguments . clone ( ) ,
76+ return_type : ret_type. clone ( ) ,
7477 body,
75- ctx : curr_ctx,
78+ ctx : curr_ctx. clone ( ) ,
7679 requires_this,
7780 } ,
7881 & node. start ,
7982 & node. end ,
80- ) ) ) ;
83+ ) ) ;
84+
85+ let mut hasher = DefaultHasher :: new ( ) ;
86+ ty. hash ( & mut hasher) ;
87+
88+ let fnname = format ! ( "{}$${}" , hasher. finish( ) , func_name. hash) ;
89+
90+ let ret_type2;
91+ let mut arguments2 = vec ! [ ] ;
92+
93+ if let Some ( v) = ret_type {
94+ ret_type2 = Some ( v. as_resolved ( ) ) // TODO: This unsupports generics, maybe fix later
95+ } else {
96+ ret_type2 = None ;
97+ }
98+
99+ for arg in & arguments {
100+ arguments2. push ( ( arg. 0 , arg. 1 . clone ( ) . as_resolved ( ) ) ) ;
101+ }
102+
103+ context. global_scope . append_struct_function (
104+ EntryKey {
105+ name_hash : HashedString :: new ( fnname. clone ( ) ) . hash ,
106+ } ,
107+ ( ret_type2, arguments2, fnname) ,
108+ implementation,
109+ curr_ctx,
110+ ty,
111+ & * node,
112+ ) ?;
81113 }
82114
83115 panic ! ( "Invalid node type" )
@@ -98,10 +130,10 @@ pub fn lower_ast_struct_declaration(
98130 fields : IndexStorage :: new ( ) ,
99131 functions : IndexStorage :: new ( ) ,
100132 type_params,
133+ function_ids : vec ! [ ] ,
134+ self_ref : context. global_scope . scope . entries . len ( ) ,
101135 } ;
102136
103- let mut func_impls = vec ! [ ] ;
104-
105137 let base = RawType :: Struct ( layout, container. clone ( ) ) ;
106138
107139 let ind = match context. global_scope . append (
@@ -124,25 +156,23 @@ pub fn lower_ast_struct_declaration(
124156 GlobalStorageEntryType :: Type ( RawType :: Struct ( layout, container. clone ( ) ) ) ;
125157 }
126158 & ASTTreeNodeKind :: FunctionDeclaration { .. } => {
127- let body = lower_ast_struct_function_decl ( context, member, & mut container) ?;
159+ let body = lower_ast_struct_function_decl (
160+ context,
161+ member,
162+ & mut container,
163+ context. global_scope . scope . entries [ ind] . as_type_unsafe ( ) ,
164+ ) ?;
165+
166+ container. function_ids . push ( body. 1 ) ;
128167
129168 context. global_scope . scope . entries [ ind] . entry_type =
130169 GlobalStorageEntryType :: Type ( RawType :: Struct ( layout, container. clone ( ) ) ) ;
131-
132- func_impls. push ( body) ;
133170 }
134171
135172 _ => panic ! ( "Invalid node type" ) ,
136173 } ;
137174 }
138175
139- context. struct_func_impls . insert (
140- ind,
141- HIRStructContainer {
142- function_impls : func_impls,
143- } ,
144- ) ;
145-
146176 return Ok ( Box :: new ( HIRNode :: new (
147177 HIRNodeKind :: StructDeclaration {
148178 type_name : ind,
0 commit comments