11//! The nodes inside of the AstoIR HIR.
22
3+ use std:: collections:: HashMap ;
4+
35use compiler_typing:: { raw:: RawType , references:: TypeReference , storage:: { BOOLEAN_TYPE , STATIC_STR } , structs:: RawStructTypeContainer , transmutation:: array:: can_transmute_inner, tree:: Type } ;
4- use compiler_utils:: Position ;
6+ use compiler_utils:: { Position , hash :: SelfHash } ;
57use diagnostics:: { DiagnosticSpanOrigin , builders:: { make_diff_type, make_diff_type_val} , diagnostic:: { Diagnostic , Span , SpanKind , SpanPosition } , unsure_panic} ;
68use lexer:: toks:: { comp:: ComparingOperator , math:: MathOperator } ;
79
8- use crate :: { ctx:: { HIRBranchedContext , HIRContext } , structs:: { HIRIfBranch , StructLRUStep } } ;
10+ use crate :: { ctx:: { HIRBranchedContext , HIRContext } , resolve :: resolve_to_type , structs:: { HIRIfBranch , StructLRUStep } } ;
911
1012#[ derive( Debug , Clone ) ]
1113pub struct HIRNode {
@@ -62,6 +64,8 @@ pub enum HIRNodeKind {
6264
6365 StructLRU { steps : Vec < StructLRUStep > , last : Type } ,
6466
67+ EnumParentCast { val : Box < HIRNode > , parent : Type } ,
68+
6569 StructDeclaration { type_name : usize , container : RawStructTypeContainer , layout : bool } ,
6670 StructFunctionDeclaration { func_name : usize , arguments : Vec < ( u64 , TypeReference ) > , return_type : Option < TypeReference > , body : Vec < Box < HIRNode > > , ctx : HIRBranchedContext , requires_this : bool } ,
6771
@@ -71,7 +75,9 @@ pub enum HIRNodeKind {
7175 ArrayIndexAccess { val : Box < HIRNode > , index : Box < HIRNode > } ,
7276 ArrayIndexModify { array : Box < HIRNode > , index : Box < HIRNode > , new_val : Box < HIRNode > } ,
7377
74- StructVariableInitializerValue { t : Type , fields : Vec < Box < HIRNode > > } ,
78+ /// Before transmutation
79+ StructInitializer { fields : HashMap < SelfHash , Box < HIRNode > > } ,
80+ StructInitializerTyped { t : Type , fields : Vec < Box < HIRNode > > } ,
7581
7682 FunctionDeclaration { func_name : usize , arguments : Vec < ( u64 , Type ) > , return_type : Option < Type > , body : Vec < Box < HIRNode > > , ctx : HIRBranchedContext , requires_this : bool } ,
7783
@@ -132,6 +138,10 @@ impl HIRNode {
132138 }
133139
134140 pub fn use_as < K : DiagnosticSpanOrigin > ( & self , context : & HIRContext , curr_ctx : & HIRBranchedContext , t : Type , origin : & K , var_origin : Option < & K > ) -> Result < HIRNode , ( ) > {
141+ if self . is_intederminately_typed ( ) {
142+ return Ok ( resolve_to_type ( Box :: new ( self . clone ( ) ) , t. clone ( ) , context, curr_ctx, origin) ?. use_as ( context, curr_ctx, t, origin, var_origin) ?) ;
143+ }
144+
135145 let self_type = match self . get_node_type ( context, curr_ctx) {
136146 Some ( v) => v,
137147 _ => panic ! ( "Tried using a typeless node in use_as: {:#?}" , self )
@@ -146,7 +156,7 @@ impl HIRNode {
146156 HIRNodeKind :: IntegerLiteral { value, int_type : _ } => {
147157 return Ok ( self . with ( HIRNodeKind :: IntegerLiteral { value : * value, int_type : t } ) ) ;
148158 } ,
149-
159+
150160 HIRNodeKind :: ArrayVariableInitializerValue { vals } => {
151161 if can_transmute_inner ( & self_type, & t, & context. type_storage ) {
152162 let mut new_vals = vec ! [ ] ;
@@ -181,6 +191,14 @@ impl HIRNode {
181191 return Err ( make_diff_type_val ( origin, & t. faulty_lowering_generic ( & context. type_storage ) , & self . get_node_type ( context, curr_ctx) . unwrap ( ) . faulty_lowering_generic ( & context. type_storage ) ) . into ( ) )
182192 }
183193
194+ pub fn is_intederminately_typed ( & self ) -> bool {
195+ match self . kind {
196+ HIRNodeKind :: StructInitializer { .. } => true ,
197+
198+ _ => false
199+ }
200+ }
201+
184202 pub fn get_node_type ( & self , context : & HIRContext , curr_ctx : & HIRBranchedContext ) -> Option < Type > {
185203 match & self . kind {
186204 HIRNodeKind :: VariableReference { index, is_static } => {
@@ -218,12 +236,7 @@ impl HIRNode {
218236 } ,
219237
220238 HIRNodeKind :: StringLiteral { value : _ } => {
221- let ind = match context. type_storage . types . get_index ( STATIC_STR ) {
222- Some ( v) => v,
223- None => return None
224- } ;
225-
226- return Some ( Type :: Generic ( RawType :: Boolean , vec ! [ ] , vec ! [ ] ) )
239+ return Some ( Type :: Generic ( RawType :: StaticString , vec ! [ ] , vec ! [ ] ) )
227240 } ,
228241
229242 HIRNodeKind :: ArrayVariableInitializerValue { vals } => return Some ( Type :: Array ( vals. len ( ) , Box :: new ( vals[ 0 ] . get_node_type ( context, curr_ctx) . unwrap ( ) ) ) ) ,
@@ -241,9 +254,7 @@ impl HIRNode {
241254 return Some ( Type :: Generic ( RawType :: Boolean , vec ! [ ] , vec ! [ ] ) )
242255 } ,
243256
244- HIRNodeKind :: StructVariableInitializerValue { t, fields : _ } => {
245- return Some ( t. clone ( ) )
246- }
257+ HIRNodeKind :: StructInitializerTyped { t, fields : _ } => Some ( t. clone ( ) ) ,
247258
248259 HIRNodeKind :: FunctionCall { func_name, arguments : _ } => {
249260 let f = context. functions . vals [ * func_name] . 0 . clone ( ) ;
0 commit comments