@@ -350,7 +350,9 @@ def gen_decl(self, decl):
350
350
fn_dec_ret_type_str = str (fn_decl .ret_typ )
351
351
if fn_dec_ret_type_str == "void" or fn_dec_ret_type_str == "_R6Result_R4void" :
352
352
self .gen_defer_stmts (scope = decl .scope )
353
- if fn_dec_ret_type_str == "_R6Result_R4void" and len (fn_decl .instrs ) == 0 :
353
+ if fn_dec_ret_type_str == "_R6Result_R4void" and len (
354
+ fn_decl .instrs
355
+ ) == 0 :
354
356
self .cur_func .add_ret (self .result_void (decl .ret_typ ))
355
357
elif fn_dec_ret_type_str != "void" and not (
356
358
len (fn_decl .instrs ) > 0
@@ -641,12 +643,19 @@ def gen_expr_with_cast(self, expected_typ_, expr, custom_tmp = None):
641
643
tmp = self .cur_func .local_name ()
642
644
tmp_t = expected_typ
643
645
variant_idx = expr_sym .info .get_variant_by_type (expected_typ_ ).value
644
- self .cur_func .add_call ("_R4core16tagged_enum_castF" , [
645
- ir .Selector (ir .UINT_T , res_expr , ir .Name ("_idx_" )),
646
- variant_idx
647
- ])
648
- obj_f = ir .Selector (ir .Type (cg_utils .mangle_symbol (expr_sym )+ "6_Union" ), res_expr , ir .Name ("obj" ))
649
- value = ir .Selector (self .ir_type (expr .typ ), obj_f , ir .Name (f"v{ variant_idx } " ))
646
+ self .cur_func .add_call (
647
+ "_R4core16tagged_enum_castF" , [
648
+ ir .Selector (ir .UINT_T , res_expr , ir .Name ("_idx_" )),
649
+ variant_idx
650
+ ]
651
+ )
652
+ obj_f = ir .Selector (
653
+ ir .Type (cg_utils .mangle_symbol (expr_sym ) + "6_Union" ), res_expr ,
654
+ ir .Name ("obj" )
655
+ )
656
+ value = ir .Selector (
657
+ self .ir_type (expr .typ ), obj_f , ir .Name (f"v{ variant_idx } " )
658
+ )
650
659
self .cur_func .inline_alloca (tmp_t , tmp , value )
651
660
res_expr = ir .Ident (tmp_t , tmp )
652
661
@@ -805,13 +814,23 @@ def gen_expr(self, expr, custom_tmp = None):
805
814
elif typ_sym .kind == TypeKind .Enum and typ_sym .info .is_tagged :
806
815
tmp = self .cur_func .local_name ()
807
816
tmp_t = ir_typ
808
- variant_idx = typ_sym .info .get_variant_by_type (expr .typ ).value
809
- self .cur_func .add_call ("_R4core16tagged_enum_castF" , [
810
- ir .Selector (ir .UINT_T , res , ir .Name ("_idx_" )),
811
- variant_idx
812
- ])
813
- obj_f = ir .Selector (ir .Type (cg_utils .mangle_symbol (typ_sym )+ "6_Union" ), res , ir .Name ("obj" ))
814
- value = ir .Selector (self .ir_type (expr .typ ), obj_f , ir .Name (f"v{ variant_idx } " ))
817
+ variant_idx = typ_sym .info .get_variant_by_type (
818
+ expr .typ
819
+ ).value
820
+ self .cur_func .add_call (
821
+ "_R4core16tagged_enum_castF" , [
822
+ ir .Selector (ir .UINT_T , res , ir .Name ("_idx_" )),
823
+ variant_idx
824
+ ]
825
+ )
826
+ obj_f = ir .Selector (
827
+ ir .Type (cg_utils .mangle_symbol (typ_sym ) + "6_Union" ),
828
+ res , ir .Name ("obj" )
829
+ )
830
+ value = ir .Selector (
831
+ self .ir_type (expr .typ ), obj_f ,
832
+ ir .Name (f"v{ variant_idx } " )
833
+ )
815
834
self .cur_func .inline_alloca (tmp_t , tmp , value )
816
835
return ir .Ident (tmp_t , tmp )
817
836
tmp = self .cur_func .local_name ()
@@ -980,7 +999,11 @@ def gen_expr(self, expr, custom_tmp = None):
980
999
)
981
1000
elif typ_sym .kind == TypeKind .Enum :
982
1001
if expr .is_enum_variant :
983
- tmp = self .stacked_instance (ir .Type (cg_utils .mangle_symbol (expr .enum_variant_sym )))
1002
+ tmp = self .stacked_instance (
1003
+ ir .Type (
1004
+ cg_utils .mangle_symbol (expr .enum_variant_sym )
1005
+ )
1006
+ )
984
1007
initted_fields = []
985
1008
type_fields = expr .enum_variant_sym .full_fields ()
986
1009
for i , f in enumerate (expr .args ):
@@ -1347,7 +1370,9 @@ def gen_expr(self, expr, custom_tmp = None):
1347
1370
)
1348
1371
self .cur_func .add_ret_void ()
1349
1372
else :
1350
- tmp2 = self .stacked_instance (self .ir_type (self .cur_func_ret_typ ))
1373
+ tmp2 = self .stacked_instance (
1374
+ self .ir_type (self .cur_func_ret_typ )
1375
+ )
1351
1376
self .cur_func .store (
1352
1377
ir .Selector (ir .BOOL_T , tmp2 , ir .Name ("is_err" )),
1353
1378
ir .IntLit (ir .BOOL_T , "1" )
@@ -1551,7 +1576,9 @@ def gen_expr(self, expr, custom_tmp = None):
1551
1576
typ_sym = expr .typ .symbol ()
1552
1577
if len (expr .elems ) == 0 :
1553
1578
if expr .is_dyn :
1554
- return self .stacked_instance (self .ir_type (expr .typ ), self .empty_dyn_array (typ_sym ))
1579
+ return self .stacked_instance (
1580
+ self .ir_type (expr .typ ), self .empty_dyn_array (typ_sym )
1581
+ )
1555
1582
return self .default_value (expr .typ )
1556
1583
elem_typ = typ_sym .info .elem_typ
1557
1584
size , _ = self .comp .type_size (elem_typ )
@@ -1750,7 +1777,9 @@ def gen_expr(self, expr, custom_tmp = None):
1750
1777
return tmp
1751
1778
elif expr .op in (Kind .LogicalAnd , Kind .LogicalOr ):
1752
1779
left = self .gen_expr_with_cast (expr_left_typ , expr .left )
1753
- tmp = self .stacked_instance (self .ir_type (self .comp .bool_t ), left )
1780
+ tmp = self .stacked_instance (
1781
+ self .ir_type (self .comp .bool_t ), left
1782
+ )
1754
1783
left_l = self .cur_func .local_name ()
1755
1784
exit_l = self .cur_func .local_name ()
1756
1785
if expr .op == Kind .LogicalAnd :
@@ -1827,22 +1856,25 @@ def gen_expr(self, expr, custom_tmp = None):
1827
1856
union_type = ir .Type (union_name )
1828
1857
obj_val = ir .Selector (union_type , left , ir .Name ("obj" ))
1829
1858
val = ir .Selector (
1830
- ir .Type (self .ir_type (expr .typ )), obj_val , ir .Name (f"v{ expr .right .variant_info .value } " )
1859
+ ir .Type (self .ir_type (expr .typ )), obj_val ,
1860
+ ir .Name (f"v{ expr .right .variant_info .value } " )
1831
1861
)
1832
- if expr .var .is_mut and not isinstance (var_t , ir .Pointer ):
1862
+ if expr .var .is_mut and not isinstance (
1863
+ var_t , ir .Pointer
1864
+ ):
1833
1865
val = ir .Inst (ir .InstKind .GetPtr , [val ], var_t2 )
1834
1866
var_t = var_t .ptr ()
1835
1867
else :
1836
1868
val = ir .Inst (
1837
1869
ir .InstKind .Cast , [
1838
- ir .Selector (ir .VOID_PTR_T , left , ir .Name ("obj" )),
1839
- var_t2
1870
+ ir .Selector (
1871
+ ir .VOID_PTR_T , left , ir .Name ("obj" )
1872
+ ), var_t2
1840
1873
]
1841
1874
)
1842
- if not (
1843
- (isinstance (var_t2 , ir .Pointer ) and var_t2 .is_managed )
1844
- or expr .var .is_mut
1845
- ):
1875
+ if not ((
1876
+ isinstance (var_t2 , ir .Pointer ) and var_t2 .is_managed
1877
+ ) or expr .var .is_mut ):
1846
1878
val = ir .Inst (ir .InstKind .LoadPtr , [val ], var_t2 )
1847
1879
unique_name = self .cur_func .unique_name (expr .var .name )
1848
1880
expr .scope .update_ir_name (expr .var .name , unique_name )
@@ -2186,13 +2218,16 @@ def gen_expr(self, expr, custom_tmp = None):
2186
2218
e_expr_typ_sym = expr .expr .typ .symbol ()
2187
2219
if e_expr_typ_sym .kind == TypeKind .Enum :
2188
2220
obj_f = ir .Selector (
2189
- e_expr_typ_sym .name + "6_Union" , match_expr , ir .Name ("obj" )
2221
+ e_expr_typ_sym .name + "6_Union" , match_expr ,
2222
+ ir .Name ("obj" )
2190
2223
)
2191
2224
val = ir .Selector (
2192
- self .ir_type (p .variant_info .typ ),
2193
- obj_f , ir .Name (f"v{ p .variant_info .value } " )
2225
+ self .ir_type (p .variant_info .typ ), obj_f ,
2226
+ ir .Name (f"v{ p .variant_info .value } " )
2194
2227
)
2195
- if b .var_is_mut and not isinstance (var_t , ir .Pointer ):
2228
+ if b .var_is_mut and not isinstance (
2229
+ var_t , ir .Pointer
2230
+ ):
2196
2231
val = ir .Inst (ir .InstKind .GetPtr , [val ])
2197
2232
else :
2198
2233
val = ir .Inst (
@@ -2323,7 +2358,9 @@ def gen_expr(self, expr, custom_tmp = None):
2323
2358
expr_ = self .gen_expr_with_cast (ret_typ , expr .expr )
2324
2359
if is_array and self .comp .prefs .target_backend == prefs .Backend .C :
2325
2360
size , _ = self .comp .type_size (ret_typ )
2326
- tmp = self .stacked_instance (ir .Type (self .cur_func .arr_ret_struct ))
2361
+ tmp = self .stacked_instance (
2362
+ ir .Type (self .cur_func .arr_ret_struct )
2363
+ )
2327
2364
self .cur_func .add_call (
2328
2365
"_R4core8mem_copyF" , [
2329
2366
ir .Selector (
@@ -2665,7 +2702,7 @@ def gen_string_literal(self, lit, size = None):
2665
2702
self .generated_string_literals [lit_hash ] = tmp .name
2666
2703
return tmp
2667
2704
2668
- def stacked_instance (self , typ , init_value = None ):
2705
+ def stacked_instance (self , typ , init_value = None ):
2669
2706
tmp = ir .Ident (typ , self .cur_func .local_name ())
2670
2707
if init_value :
2671
2708
self .cur_func .alloca (tmp , init_value )
@@ -2766,10 +2803,15 @@ def tagged_enum_value(
2766
2803
):
2767
2804
arg0 = self .gen_expr_with_cast (variant_info .typ , value )
2768
2805
size , _ = self .comp .type_size (variant_info .typ )
2769
- obj_f = ir .Selector (ir .Type (f"{ cg_utils .mangle_symbol (enum_sym )} 6_Union" ), tmp , ir .Name ("obj" ))
2806
+ obj_f = ir .Selector (
2807
+ ir .Type (f"{ cg_utils .mangle_symbol (enum_sym )} 6_Union" ), tmp ,
2808
+ ir .Name ("obj" )
2809
+ )
2770
2810
self .cur_func .store (
2771
- ir .Selector (self .ir_type (variant_info .typ ), obj_f , ir .Name (f"v{ variant_info .value } " )),
2772
- arg0
2811
+ ir .Selector (
2812
+ self .ir_type (variant_info .typ ), obj_f ,
2813
+ ir .Name (f"v{ variant_info .value } " )
2814
+ ), arg0
2773
2815
)
2774
2816
return tmp
2775
2817
@@ -2789,9 +2831,15 @@ def tagged_enum_variant_with_fields_value(
2789
2831
ir .Selector (ir .UINT_T , tmp , ir .Name ("_idx_" )),
2790
2832
ir .IntLit (ir .UINT_T , variant_info .value )
2791
2833
)
2792
- obj_f = ir .Selector (ir .Type (f"{ cg_utils .mangle_symbol (enum_sym )} 6_Union" ), tmp , ir .Name ("obj" ))
2834
+ obj_f = ir .Selector (
2835
+ ir .Type (f"{ cg_utils .mangle_symbol (enum_sym )} 6_Union" ), tmp ,
2836
+ ir .Name ("obj" )
2837
+ )
2793
2838
self .cur_func .store (
2794
- ir .Selector (self .ir_type (variant_info .typ ), obj_f , ir .Name (f"v{ variant_info .value } " )), value
2839
+ ir .Selector (
2840
+ self .ir_type (variant_info .typ ), obj_f ,
2841
+ ir .Name (f"v{ variant_info .value } " )
2842
+ ), value
2795
2843
)
2796
2844
return tmp
2797
2845
@@ -2981,7 +3029,9 @@ def gen_types(self):
2981
3029
fields = []
2982
3030
for v in ts .info .variants :
2983
3031
if v .has_typ :
2984
- fields .append (ir .Field (f"v{ v .value } " , self .ir_type (v .typ )))
3032
+ fields .append (
3033
+ ir .Field (f"v{ v .value } " , self .ir_type (v .typ ))
3034
+ )
2985
3035
union_name = mangled_name + "6_Union"
2986
3036
self .out_rir .types .append (ir .Union (union_name , fields ))
2987
3037
struct_fields = []
@@ -2991,7 +3041,9 @@ def gen_types(self):
2991
3041
ir .Field ("_idx_" , ir .UINT_T ),
2992
3042
ir .Field ("obj" , ir .Type (union_name ))
2993
3043
]
2994
- self .out_rir .types .append (ir .Struct (False , mangled_name , struct_fields ))
3044
+ self .out_rir .types .append (
3045
+ ir .Struct (False , mangled_name , struct_fields )
3046
+ )
2995
3047
elif ts .kind == TypeKind .Trait :
2996
3048
ts_name = cg_utils .mangle_symbol (ts )
2997
3049
fields = [
@@ -3133,7 +3185,8 @@ def sort_type_symbols(self, tss):
3133
3185
for variant in ts .info .variants :
3134
3186
if variant .has_typ :
3135
3187
variant_sym = variant .typ .symbol ()
3136
- if variant_sym .is_boxed () or isinstance (variant .typ , type .Option ):
3188
+ if variant_sym .is_boxed (
3189
+ ) or isinstance (variant .typ , type .Option ):
3137
3190
continue
3138
3191
dep = cg_utils .mangle_symbol (variant_sym )
3139
3192
if dep not in typ_names or dep in field_deps :
@@ -3156,7 +3209,8 @@ def sort_type_symbols(self, tss):
3156
3209
elif ts .kind == TypeKind .Struct :
3157
3210
for base in ts .info .bases :
3158
3211
dep = cg_utils .mangle_symbol (base )
3159
- if dep not in typ_names or dep in field_deps or base .is_boxed ():
3212
+ if dep not in typ_names or dep in field_deps or base .is_boxed (
3213
+ ):
3160
3214
continue
3161
3215
field_deps .append (dep )
3162
3216
for f in ts .fields :
0 commit comments