@@ -3,7 +3,7 @@ use std::rc::Rc;
33use commons:: err:: { PositionlessError , PositionlessResult } ;
44use parser:: { ast:: { func, tree:: ASTTreeNode } , parse_ast_ctx} ;
55
6- use crate :: { conv:: { control:: { parse_for_statement_ir, parse_if_statement_ir} , val:: parse_ir_value} , ctx:: { IRContext , IRLocalContext } , irstruct:: { funcs:: IRFunction , ptr:: IRPointer } , refs:: IRValueRef , types:: typing:: IRType } ;
6+ use crate :: { conv:: { control:: { parse_for_statement_ir, parse_if_statement_ir} , val:: parse_ir_value} , ctx:: { IRContext , IRLocalContext } , irstruct:: { funcs:: IRFunction , ptr:: IRPointer } , refs:: IRValueRef , types:: typing:: IRType , values :: IRValue } ;
77
88pub fn parse_ir_shadow_function_decl ( ctx : & mut IRContext , node : Box < ASTTreeNode > ) -> PositionlessResult < Rc < IRFunction > > {
99 if let ASTTreeNode :: ShadowFunctionDeclaration { func_name, args, returnType } = * node {
@@ -12,22 +12,22 @@ pub fn parse_ir_shadow_function_decl(ctx: &mut IRContext, node: Box<ASTTreeNode>
1212 None => None
1313 } ;
1414
15- let mut arguments: Vec < Rc < IRType > > = vec ! [ ] ;
15+ let mut arguments: Vec < ( Rc < IRType > , u64 ) > = vec ! [ ] ;
1616
1717 for k in args {
1818 let t = match ctx. type_storage . get ( k. argument_type ) {
1919 Some ( v) => v,
2020 None => return Err ( PositionlessError :: new ( & format ! ( "Cannot get type with hash {} for argument {}!" , k. argument_type, k. name. val) ) )
2121 } ;
2222
23- arguments. push ( t ) ;
23+ arguments. push ( ( t , k . name . hash ) ) ;
2424 }
2525
26- let func = IRFunction :: create_shadow ( ctx, func_name. val . clone ( ) , & ctx. module , return_type, arguments) ?;
26+ let func = IRFunction :: create_shadow ( ctx, func_name. val . clone ( ) , func_name . hash , & ctx. module , return_type, arguments) ?;
2727
2828 ctx. add_function ( func_name. hash , func) ?;
2929
30- return Ok ( ctx. get_funtion ( func_name. hash ) ?) ;
30+ return Ok ( ctx. get_function ( func_name. hash ) ?) ;
3131 }
3232
3333 return Err ( PositionlessError :: new ( "Cannot parse ir shadow funtion decl as the node is incompatible!" ) ) ;
@@ -40,18 +40,26 @@ pub fn parse_ir_function_decl(ctx: &mut IRContext, node: Box<ASTTreeNode>) -> Po
4040 None => None
4141 } ;
4242
43- let mut arguments: Vec < Rc < IRType > > = vec ! [ ] ;
43+ let mut arguments: Vec < ( Rc < IRType > , u64 ) > = vec ! [ ] ;
4444
4545 for k in args {
4646 let t = match ctx. type_storage . get ( k. argument_type ) {
4747 Some ( v) => v,
4848 None => return Err ( PositionlessError :: new ( & format ! ( "Cannot get type with hash {} for argument {}!" , k. argument_type, k. name. val) ) )
4949 } ;
5050
51- arguments. push ( t ) ;
51+ arguments. push ( ( t , k . name . hash ) ) ;
5252 }
5353
54- let mut func = IRFunction :: create ( ctx, func_name. val , & ctx. module , return_type, arguments) ?;
54+ let mut func = IRFunction :: create ( ctx, func_name. val , func_name. hash , & ctx. module , return_type, arguments) ?;
55+
56+ let mut ind = 0 ;
57+ for argument in & func. args {
58+ let val = func. get_nth_arg ( ind) ?;
59+
60+ func. lctx . add_argument ( argument. 1 , IRValue :: new ( val, argument. 0 . clone ( ) ) ) ?;
61+ ind += 1 ;
62+ }
5563
5664 func. prepare_body_filling ( ctx) ;
5765 parse_ir_body ( ctx, & mut func, body, true ) ?;
@@ -63,9 +71,9 @@ pub fn parse_ir_function_decl(ctx: &mut IRContext, node: Box<ASTTreeNode>) -> Po
6371 } ;
6472 }
6573
66- ctx. add_function ( func_name. hash , func) ;
74+ ctx. add_function ( func_name. hash , func) ? ;
6775
68- return ctx. get_funtion ( func_name. hash ) ;
76+ return ctx. get_function ( func_name. hash ) ;
6977 }
7078
7179 return Err ( PositionlessError :: new ( "Given node in parse_ir_function_decl wasn't a function decl!" ) ) ;
@@ -83,21 +91,28 @@ pub fn parse_ir_body(ctx: &IRContext, func: &mut IRFunction, nodes: Vec<Box<ASTT
8391 return Ok ( true ) ;
8492}
8593
86- pub fn parse_ir_function_call ( ctx : & IRContext , lctx : & IRLocalContext , node : Box < ASTTreeNode > , owner : Option < IRPointer > , grab_result : bool ) -> PositionlessResult < Option < IRValueRef > > {
87- if let ASTTreeNode :: FunctionCall { func, args } = * node {
94+ pub fn parse_ir_function_call ( ctx : & IRContext , f : & IRFunction , node : Box < ASTTreeNode > , owner : Option < IRPointer > , grab_result : bool ) -> PositionlessResult < Option < IRValueRef > > {
95+ if let ASTTreeNode :: FunctionCall { func : ff , args } = * node {
8896 let mut arguments = vec ! [ ] ;
8997
9098 if owner. as_ref ( ) . is_some ( ) {
9199 arguments. push ( IRValueRef :: from_pointer ( owner. as_ref ( ) . unwrap ( ) . clone ( ) ) ) ;
92100 }
93101
94102 for v in args {
95- arguments. push ( parse_ir_value ( Some ( lctx ) , ctx, v, None , false ) ?) ;
103+ arguments. push ( parse_ir_value ( Some ( & f ) , ctx, v, None , false ) ?) ;
96104 }
97105
98- let func = ctx. get_funtion ( func. hash ) ?;
99106
100- let ret =func. call ( ctx, arguments, grab_result) ?;
107+ let ret;
108+
109+ if ff. hash == f. hash {
110+ ret = f. call ( ctx, arguments, grab_result) ?;
111+ } else {
112+ let func = ctx. get_function ( ff. hash ) ?;
113+
114+ ret = func. call ( ctx, arguments, grab_result) ?;
115+ }
101116
102117 if !grab_result || ret. is_none ( ) {
103118 return Ok ( None ) ;
@@ -120,7 +135,7 @@ pub fn parse_ir_function_body_member(ctx: &IRContext, func: &mut IRFunction, nod
120135 println ! ( "Var name: {}" , var_name. val. clone( ) ) ;
121136
122137 let initial = if let Some ( v) = value {
123- Some ( parse_ir_value ( Some ( & func. lctx ) , ctx, v, None , true ) ?)
138+ Some ( parse_ir_value ( Some ( & func) , ctx, v, None , true ) ?)
124139 } else {
125140 None
126141 } ;
@@ -135,19 +150,19 @@ pub fn parse_ir_function_body_member(ctx: &IRContext, func: &mut IRFunction, nod
135150 } ,
136151
137152 ASTTreeNode :: StructLRFunction { .. } => {
138- parse_ir_value ( Some ( & func. lctx ) , ctx, node, None , false ) ?;
153+ parse_ir_value ( Some ( & func) , ctx, node, None , false ) ?;
139154
140155 return Ok ( true )
141156 } ,
142157
143158 ASTTreeNode :: StructLRVariable { .. } => {
144- parse_ir_value ( Some ( & func. lctx ) , ctx, node, None , false ) ?;
159+ parse_ir_value ( Some ( & func) , ctx, node, None , false ) ?;
145160
146161 return Ok ( true )
147162 } ,
148163
149164 ASTTreeNode :: FunctionCall { .. } => {
150- parse_ir_function_call ( ctx, & func. lctx , node, None , false ) ?;
165+ parse_ir_function_call ( ctx, & func, node, None , false ) ?;
151166
152167 return Ok ( true )
153168 } ,
@@ -159,7 +174,7 @@ pub fn parse_ir_function_body_member(ctx: &IRContext, func: &mut IRFunction, nod
159174 return Ok ( true ) ;
160175 }
161176
162- let val = parse_ir_value ( Some ( & func. lctx ) , ctx, val. unwrap ( ) , None , true ) ?;
177+ let val = parse_ir_value ( Some ( & func) , ctx, val. unwrap ( ) , None , true ) ?;
163178
164179 ctx. builder . build_return ( Some ( & val. obtain ( ctx) ?. obtain ( ) . inner ) ) ;
165180
@@ -179,7 +194,7 @@ pub fn parse_ir_function_body_member(ctx: &IRContext, func: &mut IRFunction, nod
179194 return Err ( PositionlessError :: new ( "Cannot use a math expression in IR body if it is not assignments!" ) )
180195 }
181196
182- parse_ir_value ( Some ( & func. lctx ) , ctx, node, None , false ) ?;
197+ parse_ir_value ( Some ( & func) , ctx, node, None , false ) ?;
183198 return Ok ( true ) ;
184199 }
185200
0 commit comments