-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
assign.v
1144 lines (1123 loc) · 33.9 KB
/
assign.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module c
import v.ast
import v.util
import v.token
fn (mut g Gen) expr_with_opt_or_block(expr ast.Expr, expr_typ ast.Type, var_expr ast.Expr, ret_typ ast.Type, in_heap bool) {
gen_or := expr is ast.Ident && expr.or_expr.kind != .absent
if gen_or {
old_inside_opt_or_res := g.inside_opt_or_res
g.inside_opt_or_res = true
g.expr_with_cast(expr, expr_typ, ret_typ)
if in_heap {
g.write('))')
}
g.writeln(';')
expr_var := if expr is ast.Ident && expr.kind == .constant {
g.get_const_name(expr)
} else if expr is ast.Ident && expr.is_auto_heap() {
'(*${expr.name})'
} else {
'${expr}'
}
g.writeln('if (${expr_var}.state != 0) { // assign')
if expr is ast.Ident && expr.or_expr.kind == .propagate_option {
g.writeln('\tpanic_option_not_set(_SLIT("none"));')
} else {
g.inside_or_block = true
defer {
g.inside_or_block = false
}
stmts := (expr as ast.Ident).or_expr.stmts
// handles stmt block which returns something
// e.g. { return none }
if stmts.len > 0 && stmts.last() is ast.ExprStmt && stmts.last().typ != ast.void_type {
g.gen_or_block_stmts(var_expr.str(), '', stmts, ret_typ, false)
} else {
// handles stmt block which doesn't returns value
// e.g. { return }
g.stmts(stmts)
if stmts.len > 0 && stmts.last() is ast.ExprStmt {
g.writeln(';')
}
}
}
g.writeln('}')
g.inside_opt_or_res = old_inside_opt_or_res
} else {
g.expr_with_opt(expr, expr_typ, ret_typ)
}
}
// expr_opt_with_cast is used in cast expr when converting compatible option types
// e.g. ?int(?u8(0))
fn (mut g Gen) expr_opt_with_cast(expr ast.Expr, expr_typ ast.Type, ret_typ ast.Type) string {
if !expr_typ.has_flag(.option) || !ret_typ.has_flag(.option) {
panic('cgen: expected expr_type and ret_typ to be options')
}
if expr_typ.idx() == ret_typ.idx() && g.table.sym(expr_typ).kind != .alias {
return g.expr_with_opt(expr, expr_typ, ret_typ)
} else {
past := g.past_tmp_var_new()
defer {
g.past_tmp_var_done(past)
}
styp := g.base_type(ret_typ)
decl_styp := g.typ(ret_typ).replace('*', '_ptr')
g.writeln('${decl_styp} ${past.tmp_var};')
g.write('_option_ok(&(${styp}[]) {')
if expr is ast.CastExpr && expr_typ.has_flag(.option) {
g.write('*((${g.base_type(expr_typ)}*)')
g.expr(expr)
g.write('.data)')
} else {
old_inside_opt_or_res := g.inside_opt_or_res
g.inside_opt_or_res = false
g.expr_with_cast(expr, expr_typ, ret_typ)
g.inside_opt_or_res = old_inside_opt_or_res
}
g.writeln(' }, (${option_name}*)(&${past.tmp_var}), sizeof(${styp}));')
return past.tmp_var
}
}
// expr_with_opt is used in assigning an expression to an `option` variable
// e.g. x = y (option lhs and rhs), mut x = ?int(123), y = none
fn (mut g Gen) expr_with_opt(expr ast.Expr, expr_typ ast.Type, ret_typ ast.Type) string {
old_inside_opt_or_res := g.inside_opt_or_res
g.inside_opt_or_res = true
defer {
g.inside_opt_or_res = old_inside_opt_or_res
}
if expr_typ.has_flag(.option) && ret_typ.has_flag(.option)
&& expr in [ast.SelectorExpr, ast.DumpExpr, ast.Ident, ast.ComptimeSelector, ast.AsCast, ast.CallExpr, ast.MatchExpr, ast.IfExpr, ast.IndexExpr, ast.UnsafeExpr, ast.CastExpr] {
if expr in [ast.Ident, ast.CastExpr] {
if expr_typ.idx() != ret_typ.idx() {
return g.expr_opt_with_cast(expr, expr_typ, ret_typ)
}
}
g.expr(expr)
if expr is ast.ComptimeSelector {
return '${expr.left.str()}.${g.comptime.comptime_for_field_value.name}'
} else {
return expr.str()
}
} else {
tmp_out_var := g.new_tmp_var()
g.expr_with_tmp_var(expr, expr_typ, ret_typ, tmp_out_var)
return tmp_out_var
}
return ''
}
fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
mut node := unsafe { node_ }
if node.is_static {
g.write('static ')
}
if node.is_volatile {
g.write('volatile ')
}
mut return_type := ast.void_type
is_decl := node.op == .decl_assign
g.assign_op = node.op
g.inside_assign = true
g.assign_ct_type = 0
defer {
g.assign_op = .unknown
g.inside_assign = false
g.assign_ct_type = 0
}
op := if is_decl { token.Kind.assign } else { node.op }
right_expr := node.right[0]
match right_expr {
ast.CallExpr { return_type = right_expr.return_type }
ast.LockExpr { return_type = right_expr.typ }
ast.MatchExpr { return_type = right_expr.return_type }
ast.IfExpr { return_type = right_expr.typ }
else {}
}
// Free the old value assigned to this string var (only if it's `str = [new value]`
// or `x.str = [new value]` )
mut af := g.is_autofree && !g.is_builtin_mod && node.op == .assign && node.left_types.len == 1
&& node.left[0] in [ast.Ident, ast.SelectorExpr]
mut sref_name := ''
mut type_to_free := ''
if af {
first_left_type := node.left_types[0]
first_left_sym := g.table.sym(node.left_types[0])
if first_left_type == ast.string_type || first_left_sym.kind == .array {
type_to_free = if first_left_type == ast.string_type { 'string' } else { 'array' }
mut ok := true
left0 := node.left[0]
if left0 is ast.Ident {
if left0.name == '_' {
ok = false
}
}
if ok {
sref_name = '_sref${node.pos.pos}'
g.write('${type_to_free} ${sref_name} = (') // TODO we are copying the entire string here, optimize
// we can't just do `.str` since we need the extra data from the string struct
// doing `&string` is also not an option since the stack memory with the data will be overwritten
g.expr(left0) // node.left[0])
g.writeln('); // free ${type_to_free} on re-assignment2')
defer {
if af {
g.writeln('${type_to_free}_free(&${sref_name});')
}
}
} else {
af = false
}
} else {
af = false
}
}
g.gen_assign_vars_autofree(node)
// json_test failed w/o this check
if return_type != ast.void_type && return_type != 0 {
sym := g.table.sym(return_type)
if sym.kind == .multi_return {
g.gen_multi_return_assign(node, return_type, sym)
return
}
}
// TODO: non idents on left (exprs)
if node.has_cross_var {
g.gen_cross_var_assign(node)
}
// `a := 1` | `a,b := 1,2`
if node.right.len < node.left.len {
g.checker_bug('node.right.len < node.left.len', node.pos)
}
if node.right_types.len < node.left.len {
g.checker_bug('node.right_types.len < node.left.len', node.pos)
}
if node.left_types.len < node.left.len {
g.checker_bug('node.left_types.len < node.left.len', node.pos)
}
for i, mut left in node.left {
mut is_auto_heap := false
mut var_type := node.left_types[i]
mut val_type := node.right_types[i]
val := node.right[i]
mut is_call := false
mut gen_or := false
mut blank_assign := false
mut ident := ast.Ident{
scope: unsafe { nil }
}
left_sym := g.table.sym(g.unwrap_generic(var_type))
if mut left is ast.Ident {
ident = left
// id_info := ident.var_info()
// var_type = id_info.typ
blank_assign = left.kind == .blank_ident
// TODO: temporary, remove this
left_info := left.info
if left_info is ast.IdentVar {
share := left_info.share
if share == .shared_t {
var_type = var_type.set_flag(.shared_f)
}
if share == .atomic_t {
var_type = var_type.set_flag(.atomic_f)
}
}
if mut left.obj is ast.Var {
if val is ast.Ident && g.comptime.is_comptime_var(val) {
ctyp := g.unwrap_generic(g.comptime.get_comptime_var_type(val))
if ctyp != ast.void_type {
var_type = ctyp
val_type = var_type
gen_or = val.or_expr.kind != .absent
if gen_or {
var_type = val_type.clear_flag(.option)
}
left.obj.typ = var_type
g.assign_ct_type = var_type
}
} else if val is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(val)
if key_str != '' {
if is_decl {
var_type = g.comptime.type_map[key_str] or { var_type }
val_type = var_type
left.obj.typ = var_type
} else {
val_type = g.comptime.type_map[key_str] or { var_type }
}
g.assign_ct_type = var_type
}
} else if val is ast.ComptimeCall {
key_str := '${val.method_name}.return_type'
var_type = g.comptime.type_map[key_str] or { var_type }
left.obj.typ = var_type
g.assign_ct_type = var_type
} else if is_decl && val is ast.Ident && val.info is ast.IdentVar {
val_info := (val as ast.Ident).info
gen_or = val.or_expr.kind != .absent
if val_info.is_option && gen_or {
var_type = val_type.clear_flag(.option)
left.obj.typ = var_type
}
} else if val is ast.DumpExpr && val.expr is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(val.expr as ast.ComptimeSelector)
if key_str != '' {
var_type = g.comptime.type_map[key_str] or { var_type }
val_type = var_type
left.obj.typ = var_type
}
g.assign_ct_type = var_type
} else if val is ast.IndexExpr {
if val.left is ast.Ident && g.is_generic_param_var(val.left) {
ctyp := g.unwrap_generic(g.get_gn_var_type(val.left))
if ctyp != ast.void_type {
var_type = ctyp
val_type = var_type
left.obj.typ = var_type
g.assign_ct_type = var_type
}
}
}
is_auto_heap = left.obj.is_auto_heap
}
} else if mut left is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(left)
if key_str != '' {
var_type = g.comptime.type_map[key_str] or { var_type }
}
if val is ast.ComptimeSelector {
key_str_right := g.comptime.get_comptime_selector_key_type(val)
if key_str_right != '' {
val_type = g.comptime.type_map[key_str_right] or { var_type }
}
}
g.assign_ct_type = var_type
} else if mut left is ast.IndexExpr && val is ast.ComptimeSelector {
key_str := g.comptime.get_comptime_selector_key_type(val)
if key_str != '' {
val_type = g.comptime.type_map[key_str] or { var_type }
}
g.assign_ct_type = val_type
}
mut styp := g.typ(var_type)
mut is_fixed_array_init := false
mut has_val := false
match val {
ast.ArrayInit {
is_fixed_array_init = val.is_fixed
has_val = val.has_val
}
ast.CallExpr {
is_call = true
if val.comptime_ret_val {
return_type = g.comptime.comptime_for_field_type
styp = g.typ(return_type)
} else {
return_type = val.return_type
}
}
// TODO: no buffer fiddling
ast.AnonFn {
if !var_type.has_option_or_result() {
if blank_assign {
g.write('{')
}
// if it's a decl assign (`:=`) or a blank assignment `_ =`/`_ :=` then generate `void (*ident) (args) =`
if (is_decl || blank_assign) && left is ast.Ident {
sig := g.fn_var_signature(val.decl.return_type, val.decl.params.map(it.typ),
ident.name)
g.write(sig + ' = ')
} else {
g.is_assign_lhs = true
g.assign_op = node.op
g.expr(left)
g.is_assign_lhs = false
g.is_arraymap_set = false
if mut left is ast.IndexExpr {
sym := g.table.sym(left.left_type)
if sym.kind in [.map, .array] {
g.expr(val)
g.writeln('});')
continue
}
}
g.write(' = ')
}
g.expr(val)
g.writeln(';')
if blank_assign {
g.write('}')
}
continue
}
}
else {}
}
unwrapped_val_type := g.unwrap_generic(val_type)
right_sym := g.table.sym(unwrapped_val_type)
unaliased_right_sym := g.table.final_sym(unwrapped_val_type)
is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit
&& (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr, ast.InfixExpr]
|| (val is ast.CastExpr && val.expr !is ast.ArrayInit)
|| (val is ast.PrefixExpr && val.op == .arrow)
|| (val is ast.UnsafeExpr && val.expr is ast.Ident)) && !g.pref.translated
g.is_assign_lhs = true
g.assign_op = node.op
g.left_is_opt = var_type.has_option_or_result()
g.right_is_opt = val_type.has_option_or_result()
defer {
g.left_is_opt = false
g.right_is_opt = false
}
if blank_assign {
if val is ast.IndexExpr {
g.assign_op = .decl_assign
}
g.is_assign_lhs = false
if is_call {
old_is_void_expr_stmt := g.is_void_expr_stmt
g.is_void_expr_stmt = true
g.expr(val)
g.is_void_expr_stmt = old_is_void_expr_stmt
} else if g.inside_for_c_stmt {
g.expr(val)
} else if var_type.has_flag(.option) {
g.expr_with_opt(val, val_type, var_type)
} else {
if left_sym.kind == .function {
g.write('{void* _ = ')
} else {
g.write('{${styp} _ = ')
}
g.expr(val)
g.writeln(';}')
}
} else if node.op == .assign && !g.pref.translated
&& (is_fixed_array_init || (right_sym.kind == .array_fixed && val is ast.Ident)) {
// Fixed arrays
if is_fixed_array_init && var_type.has_flag(.option) {
g.expr(left)
g.write(' = ')
g.expr_with_opt(val, val_type, var_type)
} else {
mut v_var := ''
arr_typ := styp.trim('*')
if is_fixed_array_init {
right := val as ast.ArrayInit
v_var = g.new_tmp_var()
g.write('${arr_typ} ${v_var} = ')
g.expr(right)
g.writeln(';')
} else {
right := val as ast.Ident
v_var = right.name
}
pos := g.out.len
g.expr(left)
if g.is_arraymap_set && g.arraymap_set_pos >= 0 {
if g.arraymap_set_pos > 0 {
g.go_back_to(g.arraymap_set_pos)
}
g.write(', &${v_var})')
g.is_arraymap_set = false
g.arraymap_set_pos = 0
} else {
g.go_back_to(pos)
is_var_mut := !is_decl && left.is_auto_deref_var()
addr_left := if is_var_mut { '' } else { '&' }
g.writeln('')
g.write('memcpy(${addr_left}')
g.expr(left)
addr_val := if is_fixed_array_var { '' } else { '&' }
g.writeln(', ${addr_val}${v_var}, sizeof(${arr_typ}));')
}
g.is_assign_lhs = false
}
} else {
is_inside_ternary := g.inside_ternary != 0
cur_line := if is_inside_ternary && is_decl {
g.register_ternary_name(ident.name)
g.empty_line = false
g.go_before_ternary()
} else {
''
}
mut str_add := false
mut op_overloaded := false
mut op_expected_left := ast.Type(0)
mut op_expected_right := ast.Type(0)
is_shared_re_assign := !is_decl && node.left_types[i].has_flag(.shared_f)
&& left is ast.Ident && left_sym.kind in [.array, .map, .struct_]
if node.op == .plus_assign && unaliased_right_sym.kind == .string {
if mut left is ast.IndexExpr {
if g.table.sym(left.left_type).kind == .array_fixed {
// strs[0] += str2 => `strs[0] = string__plus(strs[0], str2)`
g.expr(left)
g.write(' = string__plus(')
} else {
// a[0] += str => `array_set(&a, 0, &(string[]) {string__plus(...))})`
g.expr(left)
g.write('string__plus(')
}
} else {
// str += str2 => `str = string__plus(str, str2)`
g.expr(left)
g.write(' = string__plus(')
}
g.is_assign_lhs = false
str_add = true
}
// Assignment Operator Overloading
if ((left_sym.kind == .struct_ && right_sym.kind == .struct_)
|| (left_sym.kind == .alias && right_sym.kind == .alias))
&& node.op in [.plus_assign, .minus_assign, .div_assign, .mult_assign, .mod_assign] {
extracted_op := match node.op {
.plus_assign { '+' }
.minus_assign { '-' }
.div_assign { '/' }
.mod_assign { '%' }
.mult_assign { '*' }
else { 'unknown op' }
}
g.expr(left)
if left_sym.info is ast.Struct && left_sym.info.generic_types.len > 0 {
concrete_types := left_sym.info.concrete_types
mut method_name := left_sym.cname + '_' + util.replace_op(extracted_op)
method_name = g.generic_fn_name(concrete_types, method_name)
g.write(' = ${method_name}(')
g.expr(left)
g.write(', ')
g.expr(val)
g.writeln(');')
return
} else if left_sym.kind == .alias
&& g.table.final_sym(g.unwrap_generic(var_type)).is_number()
&& !left_sym.has_method(extracted_op) {
g.write(' = ')
g.expr(left)
g.write(' ${extracted_op} ')
g.expr(val)
g.write(';')
return
} else {
g.write(' = ${styp}_${util.replace_op(extracted_op)}(')
method := g.table.find_method(left_sym, extracted_op) or {
// the checker will most likely have found this, already...
g.error('assignment operator `${extracted_op}=` used but no `${extracted_op}` method defined',
node.pos)
ast.Fn{}
}
op_expected_left = method.params[0].typ
op_expected_right = method.params[1].typ
op_overloaded = true
}
}
if right_sym.info is ast.FnType && is_decl {
if is_inside_ternary {
g.out.write_string(util.tabs(g.indent - g.inside_ternary))
}
fn_name := c_fn_name(g.get_ternary_name(ident.name))
if val_type.has_flag(.option) && val is ast.SelectorExpr {
ret_styp := g.typ(g.unwrap_generic(val_type))
g.write('${ret_styp} ${fn_name}')
} else {
ret_styp := g.typ(right_sym.info.func.return_type)
mut call_conv := ''
mut msvc_call_conv := ''
for attr in right_sym.info.func.attrs {
match attr.name {
'callconv' {
if g.is_cc_msvc {
msvc_call_conv = '__${attr.arg} '
} else {
call_conv = '${attr.arg}'
}
}
else {}
}
}
call_conv_attribute_suffix := if call_conv.len != 0 {
'__attribute__((${call_conv}))'
} else {
''
}
g.write('${ret_styp} (${msvc_call_conv}*${fn_name}) (')
def_pos := g.definitions.len
g.fn_decl_params(right_sym.info.func.params, unsafe { nil }, false)
g.definitions.go_back(g.definitions.len - def_pos)
g.write(')${call_conv_attribute_suffix}')
}
} else {
if is_decl {
if is_inside_ternary {
g.out.write_string(util.tabs(g.indent - g.inside_ternary))
}
mut is_used_var_styp := false
if ident.name !in g.defer_vars {
val_sym := g.table.sym(val_type)
if val_sym.info is ast.Struct && val_sym.info.generic_types.len > 0 {
if val is ast.StructInit {
var_styp := g.typ(val.typ)
if var_type.has_flag(.shared_f) {
g.write('__shared__${var_styp}* ')
} else {
g.write('${var_styp} ')
}
is_used_var_styp = true
} else if val is ast.PrefixExpr {
if val.op == .amp && val.right is ast.StructInit {
var_styp := g.typ(val.right.typ.ref())
if var_type.has_flag(.shared_f) {
g.write('__shared__')
}
g.write('${var_styp} ')
is_used_var_styp = true
}
}
}
if !is_used_var_styp {
if !val_type.has_flag(.option) && left_sym.is_array_fixed() {
if left_sym.info is ast.Alias {
parent_sym := g.table.final_sym(left_sym.info.parent_type)
styp = g.typ(left_sym.info.parent_type)
if !parent_sym.is_array_fixed_ret() {
g.write('${styp} ')
} else {
g.write('${styp[3..]} ')
}
} else {
if !left_sym.is_array_fixed_ret() {
g.write('${styp} ')
} else {
g.write('${styp[3..]} ')
}
}
} else {
g.write('${styp} ')
}
}
if is_auto_heap && !(val_type.is_ptr() && val_type.has_flag(.option)) {
g.write('*')
}
}
}
if left in [ast.Ident, ast.SelectorExpr] {
g.prevent_sum_type_unwrapping_once = true
}
if !is_fixed_array_var || is_decl || is_shared_re_assign {
if op_overloaded {
g.op_arg(left, op_expected_left, var_type)
} else {
if !is_decl && !is_shared_re_assign && left.is_auto_deref_var() {
g.write('*')
}
g.expr(left)
if !is_decl && var_type.has_flag(.shared_f) {
g.write('->val') // don't reset the mutex, just change the value
}
}
}
}
if is_inside_ternary && is_decl {
g.write(';\n${cur_line}')
g.out.write_string(util.tabs(g.indent))
g.expr(left)
}
g.is_assign_lhs = false
if is_fixed_array_var {
if is_decl {
g.writeln(';')
}
} else if !g.is_arraymap_set && !str_add && !op_overloaded {
g.write(' ${op} ')
} else if str_add || op_overloaded {
g.write(', ')
}
mut cloned := false
if g.is_autofree && right_sym.kind in [.array, .string]
&& !unwrapped_val_type.has_flag(.shared_f) {
if g.gen_clone_assignment(val, unwrapped_val_type, false) {
cloned = true
}
}
if !cloned {
if g.comptime.comptime_for_field_var == ''
&& ((var_type.has_flag(.option) && !val_type.has_flag(.option))
|| (var_type.has_flag(.result) && !val_type.has_flag(.result))) {
old_inside_opt_or_res := g.inside_opt_or_res
defer {
g.inside_opt_or_res = old_inside_opt_or_res
}
g.inside_opt_or_res = true
if is_auto_heap && var_type.has_flag(.option) {
g.write('&')
}
tmp_var := g.new_tmp_var()
g.expr_with_tmp_var(val, val_type, var_type, tmp_var)
} else if is_fixed_array_var {
// TODO Instead of the translated check, check if it's a pointer already
// and don't generate memcpy &
typ_str := g.typ(val_type).trim('*')
final_typ_str := if is_fixed_array_var { '' } else { '(${typ_str}*)' }
final_ref_str := if is_fixed_array_var {
''
} else if val_type.is_ptr() {
'(byte*)'
} else {
'(byte*)&'
}
if val_type.has_flag(.option) {
g.expr(left)
g.write(' = ')
g.expr(val)
} else {
g.write('memcpy(${final_typ_str}')
g.expr(left)
g.write(', ${final_ref_str}')
g.expr(val)
g.write(', sizeof(${typ_str})) /*assign*/')
}
} else if is_decl {
g.is_shared = var_type.has_flag(.shared_f)
if is_fixed_array_init && !has_val {
if val is ast.ArrayInit {
g.array_init(val, c_name(ident.name))
} else {
g.write('{0}')
}
} else {
is_option_unwrapped := val is ast.Ident && val.or_expr.kind != .absent
is_option_auto_heap := is_auto_heap && is_option_unwrapped
if is_auto_heap {
g.write('HEAP(${styp}, (')
}
if val.is_auto_deref_var() && !is_option_unwrapped {
g.write('*')
}
if (var_type.has_flag(.option) && val !in [ast.Ident, ast.SelectorExpr])
|| gen_or {
g.expr_with_opt_or_block(val, val_type, left, var_type, is_option_auto_heap)
} else if val is ast.ArrayInit {
g.array_init(val, c_name(ident.name))
} else if val_type.has_flag(.shared_f) {
g.expr_with_cast(val, val_type, var_type)
} else {
g.expr(val)
}
if is_auto_heap && !is_option_auto_heap {
g.write('))')
}
}
} else {
// var = &auto_heap_var
old_is_auto_heap := g.is_option_auto_heap
g.is_option_auto_heap = val_type.has_flag(.option) && val is ast.PrefixExpr
&& val.right is ast.Ident && (val.right as ast.Ident).is_auto_heap()
defer {
g.is_option_auto_heap = old_is_auto_heap
}
if var_type.has_flag(.option) || gen_or {
g.expr_with_opt_or_block(val, val_type, left, var_type, false)
} else if node.has_cross_var {
g.gen_cross_tmp_variable(node.left, val)
} else {
if op_overloaded {
g.op_arg(val, op_expected_right, val_type)
} else {
exp_type := if var_type.is_ptr()
&& (left.is_auto_deref_var() || var_type.has_flag(.shared_f)) {
var_type.deref()
} else {
var_type
}.clear_flag(.shared_f) // don't reset the mutex, just change the value
g.expr_with_cast(val, val_type, exp_type)
}
}
}
}
if str_add || op_overloaded {
g.write(')')
}
if g.is_arraymap_set {
g.write(' })')
g.is_arraymap_set = false
}
g.is_shared = false
}
g.right_is_opt = false
if g.inside_ternary == 0 && (node.left.len > 1 || !node.is_simple) {
g.writeln(';')
}
}
}
fn (mut g Gen) gen_multi_return_assign(node &ast.AssignStmt, return_type ast.Type, return_sym ast.TypeSymbol) {
// multi return
// TODO Handle in if_expr
mr_var_name := 'mr_${node.pos.pos}'
mut is_option := return_type.has_flag(.option)
mut mr_styp := g.typ(return_type.clear_flag(.result))
if node.right[0] is ast.CallExpr && node.right[0].or_block.kind != .absent {
is_option = false
mr_styp = g.typ(return_type.clear_option_and_result())
}
g.write('${mr_styp} ${mr_var_name} = ')
g.expr(node.right[0])
g.writeln(';')
mr_types := (return_sym.info as ast.MultiReturn).types
for i, lx in node.left {
mut is_auto_heap := false
mut ident := ast.Ident{
scope: unsafe { nil }
}
if lx is ast.Ident {
ident = lx
if lx.kind == .blank_ident {
continue
}
if lx.obj is ast.Var {
is_auto_heap = lx.obj.is_auto_heap
}
}
styp := if ident.name in g.defer_vars { '' } else { g.typ(node.left_types[i]) }
if node.op == .decl_assign {
g.write('${styp} ')
}
if lx.is_auto_deref_var() {
g.write('*')
}
noscan := if is_auto_heap { g.check_noscan(return_type) } else { '' }
if node.left_types[i].has_flag(.option) {
base_typ := g.base_type(node.left_types[i])
tmp_var := if is_auto_heap {
'HEAP${noscan}(${styp}, ${mr_var_name}.arg${i})'
} else if is_option {
'(*((${g.base_type(return_type)}*)${mr_var_name}.data)).arg${i}'
} else {
'${mr_var_name}.arg${i}'
}
if mr_types[i].has_flag(.option) {
g.expr(lx)
g.write(' = ${tmp_var};')
} else {
g.write('_option_ok(&(${base_typ}[]) { ${tmp_var} }, (${option_name}*)(&')
tmp_left_is_opt := g.left_is_opt
g.left_is_opt = true
g.expr(lx)
g.left_is_opt = tmp_left_is_opt
g.writeln('), sizeof(${base_typ}));')
}
} else {
g.expr(lx)
sym := g.table.sym(node.left_types[i])
if sym.kind == .array_fixed {
g.writeln(';')
g.writeln('memcpy(&${g.expr_string(lx)}, &${mr_var_name}.arg${i}, sizeof(${styp}));')
} else {
if g.is_arraymap_set {
if is_auto_heap {
g.writeln('HEAP${noscan}(${styp}, ${mr_var_name}.arg${i}) });')
} else if is_option {
g.writeln('(*((${g.base_type(return_type)}*)${mr_var_name}.data)).arg${i} });')
} else {
g.writeln('${mr_var_name}.arg${i} });')
}
} else {
if is_auto_heap {
g.writeln(' = HEAP${noscan}(${styp}, ${mr_var_name}.arg${i});')
} else if is_option {
g.writeln(' = (*((${g.base_type(return_type)}*)${mr_var_name}.data)).arg${i};')
} else {
g.writeln(' = ${mr_var_name}.arg${i};')
}
}
}
}
}
if g.is_arraymap_set {
g.is_arraymap_set = false
}
}
fn (mut g Gen) gen_assign_vars_autofree(node &ast.AssignStmt) {
// Autofree tmp arg vars
// first_right := node.right[0]
// af := g.autofree && first_right is ast.CallExpr && !g.is_builtin_mod
// if af {
// g.autofree_call_pregen(first_right as ast.CallExpr)
// }
//
//
// Handle options. We need to declare a temp variable for them, that's why they are handled
// here, not in call_expr().
// `pos := s.index('x') or { return }`
// ==========>
// Option_int _t190 = string_index(s, _STR("x")); // _STR() no more used!!
// if (_t190.state != 2) {
// Error err = _t190.err;
// return;
// }
// int pos = *(int*)_t190.data;
// mut tmp_opt := ''
/*
is_option := false && g.is_autofree && (node.op in [.decl_assign, .assign])
&& node.left_types.len == 1 && node.right[0] is ast.CallExpr
if is_option {
// g.write('/* option assignment */')
call_expr := node.right[0] as ast.CallExpr
if call_expr.or_block.kind != .absent {
styp := g.typ(call_expr.return_type.set_flag(.option))
tmp_opt = g.new_tmp_var()
g.write('/*AF opt*/$styp $tmp_opt = ')
g.expr(node.right[0])
g.or_block(tmp_opt, call_expr.or_block, call_expr.return_type)
g.writeln('/*=============ret*/')
// if af && is_option {
// g.autofree_call_postgen()
// }
// return
}
}
*/
}
fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
for i, left in node.left {
left_is_auto_deref_var := left.is_auto_deref_var()
match left {
ast.Ident {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
mut anon_ctx := ''
if g.anon_fn {
obj := left.scope.find(left.name)
if obj is ast.Var && obj.is_inherited {
anon_ctx = '${closure_ctx}->'
}
}
if left_sym.kind == .function {
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.writeln(' = ${anon_ctx}${c_name(left.name)};')
} else if left_is_auto_deref_var {
styp := g.typ(left_typ).trim('*')
if left_sym.kind == .array {
g.writeln('${styp} _var_${left.pos.pos} = array_clone(${anon_ctx}${c_name(left.name)});')
} else {
g.writeln('${styp} _var_${left.pos.pos} = *${anon_ctx}${c_name(left.name)};')
}
} else {
styp := g.typ(left_typ)
if left_sym.kind == .array {
g.writeln('${styp} _var_${left.pos.pos} = array_clone(&${anon_ctx}${c_name(left.name)});')
} else {
g.writeln('${styp} _var_${left.pos.pos} = ${anon_ctx}${c_name(left.name)};')
}
}
}
ast.IndexExpr {
sym := g.table.sym(g.table.unaliased_type(left.left_type))
if sym.kind == .array {
info := sym.info as ast.Array
elem_typ := g.table.sym(info.elem_type)
needs_clone := info.elem_type == ast.string_type && g.is_autofree
if elem_typ.kind == .function {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write(' = *(voidptr*)array_get(')
} else {
styp := g.typ(info.elem_type)
string_clone := if needs_clone { '/*1*/string_clone(' } else { '' }
g.write('${styp} _var_${left.pos.pos} = ${string_clone}*(${styp}*)array_get(')
}
if left.left_type.is_ptr() {
g.write('*')
}
g.expr(left.left)
g.write(', ')
g.expr(left.index)
if needs_clone {
g.write(')')
}
g.writeln(');')
} else if sym.kind == .array_fixed {
info := sym.info as ast.ArrayFixed
elem_typ := g.table.sym(info.elem_type)
if elem_typ.kind == .function {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write(' = *(voidptr*)')
} else {
styp := g.typ(info.elem_type)
g.write('${styp} _var_${left.pos.pos} = ')
}
if left.left_type.is_ptr() {
g.write('*')
}
needs_clone := info.elem_type == ast.string_type && g.is_autofree
if needs_clone {
g.write('/*2*/string_clone(')
}
g.expr(left)
if needs_clone {
g.write(')')
}
g.writeln(';')
} else if sym.kind == .map {
info := sym.info as ast.Map
skeytyp := g.typ(info.key_type)
styp := g.typ(info.value_type)
zero := g.type_default(info.value_type)
val_typ := g.table.sym(info.value_type)
if val_typ.kind == .function {
left_type := node.left_types[i]
left_sym := g.table.sym(left_type)
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.write(' = *(voidptr*)map_get(')
} else {
g.write('${styp} _var_${left.pos.pos} = *(${styp}*)map_get(')
}
if !left.left_type.is_ptr() {