-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathfn.v
More file actions
13890 lines (13442 loc) · 456 KB
/
Copy pathfn.v
File metadata and controls
13890 lines (13442 loc) · 456 KB
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
module transform
import v3.flat
import v3.types
const spread_index_expected_type_marker = '__v3_spread_index_expected_type'
// max_stringify_nesting_depth bounds how deeply the inline autostr lowering
// (structs, sum types) recurses through *distinct* aggregate types before it
// defers the remaining expansion to synthesized helpers. The per-type circular
// guards only stop a type repeating on the stack; without this total-depth
// bound a deeply nested distinct-type graph (e.g. v1's ast.Expr/ast.Stmt
// sumtypes referencing dozens of node structs) expands combinatorially at
// every `${x}` site, which blows up node generation (region overflow /
// effectively unbounded work).
// Overridable at runtime with V3_STR_CAP for experiments. Kept low because the
// expansion is combinatorial in this depth: v1's ast graph is unbounded past ~5.
const max_stringify_nesting_depth = 3
// deferred_str_expansion_threshold is the estimated inline-autostr node count
// above which a function is lowered serially (against the growable master arena)
// instead of inside a bounded parallel worker region. It is 0: any function that
// inline-expands an auto-str struct/sum is deferred, because that expansion is a
// poor fit for a cost-proportional region and hard to size precisely. Functions
// with only scalar / custom-str() interps estimate to 0 and stay parallel — this
// is every function during v3 self-host, whose parallel path is thus unchanged.
const deferred_str_expansion_threshold = 0
// unresolved_interp_expansion_estimate is charged for an interpolation part whose
// value type cannot be resolved at collection time, forcing the function onto the
// serial deferred path in case the transform expands it inline.
const unresolved_interp_expansion_estimate = 1000
// recursive_pointer_str_expansion_threshold bounds an indirect recursive pointer
// expansion before it is rendered as circular. Small recursive structs retain the
// normal auto-str depth, while large object graphs avoid generating every branch
// before the repeated type is reached.
const recursive_pointer_str_expansion_threshold = 512
// resolve_call_name resolves the function name from a .call node.
// child[0] is the function expression: .ident for plain calls, .selector for method calls.
fn (t &Transformer) resolve_call_name(node flat.Node) string {
if node.children_count == 0 {
return ''
}
fn_id := t.a.children[node.children_start]
if int(fn_id) < 0 {
return ''
}
fn_node := t.a.nodes[int(fn_id)]
match fn_node.kind {
.ident {
name := fn_node.value
if t.var_type(name).len > 0 {
return name
}
// Try qualified with current module
if t.cur_module.len > 0 && t.cur_module != 'main' && t.cur_module != 'builtin' {
qname := '${t.cur_module}.${name}'
if t.is_known_fn_name(qname) {
return qname
}
}
// Try unqualified name after current-module authority.
if t.is_known_fn_name(name) {
return name
}
return name
}
.selector {
if fn_node.children_count > 0 {
base_id := t.a.children[fn_node.children_start]
base := t.a.nodes[int(base_id)]
if base.kind == .ident {
full := '${base.value}.${fn_node.value}'
if t.is_known_fn_name(full) {
return full
}
}
method_name := t.resolve_receiver_method_name(base_id, fn_node.value)
if method_name.len > 0 {
return method_name
}
if base.kind == .ident {
return '${base.value}.${fn_node.value}'
}
}
return ''
}
else {
return ''
}
}
}
fn (t &Transformer) local_fn_decl_return_type(name string) ?string {
if name.len == 0 {
return none
}
qname := transform_qualified_fn_name(t.cur_module, name)
if ret := t.fn_ret_types[qname] {
return ret
}
if !isnil(t.tc) {
if ret := t.tc.fn_ret_types[qname] {
return t.semantic_type_name(ret)
}
}
return none
}
fn (t &Transformer) local_fn_value_return_type_from_type(typ string) ?string {
if typ.len == 0 {
return none
}
normalized := t.normalize_type_alias(typ)
return fn_type_return_type_text(normalized)
}
fn (t &Transformer) fn_value_call_return_type(node flat.Node) ?string {
if node.kind != .call || node.children_count == 0 {
return none
}
callee_id := t.a.child(&node, 0)
if int(callee_id) < 0 {
return none
}
callee := t.a.nodes[int(callee_id)]
if callee.kind == .ident {
local_type := t.var_type(callee.value)
if ret := t.local_fn_value_return_type_from_type(local_type) {
return ret
}
}
if callee.kind == .selector {
if raw_type := t.raw_selector_field_type(callee_id) {
if ret := fn_type_return_type_text(raw_type) {
return ret
}
}
}
if !isnil(t.tc) {
mut callee_type := t.tc.expr_type(callee_id) or { t.tc.resolve_type(callee_id) }
if fn_type := fn_type_from_type(callee_type) {
return t.semantic_type_name(fn_type.return_type)
}
}
callee_type_name := t.node_type(callee_id)
if ret := t.local_fn_value_return_type_from_type(callee_type_name) {
return ret
}
return none
}
fn fn_type_return_type_text(typ string) ?string {
clean := typ.trim_space()
if !(clean.starts_with('fn(') || clean.starts_with('fn (')) {
return none
}
open_idx := clean.index_u8(`(`)
if open_idx < 0 {
return none
}
mut depth := 0
for i in open_idx .. clean.len {
if clean[i] == `(` {
depth++
} else if clean[i] == `)` {
depth--
if depth == 0 {
ret := clean[i + 1..].trim_space()
if ret.len == 0 {
return 'void'
}
if ret in ['!', '?'] {
return '${ret}void'
}
return ret
}
}
}
return none
}
// is_known_fn_name reports whether is known fn name applies in transform.
fn (t &Transformer) is_known_fn_name(name string) bool {
if name in t.fn_ret_types {
return true
}
if !isnil(t.tc) {
return name in t.tc.fn_ret_types || name in t.tc.fn_param_types
}
return false
}
// resolve_receiver_method_name resolves resolve receiver method name information for transform.
fn (t &Transformer) resolve_receiver_method_name(base_id flat.NodeId, method string) string {
if method.len == 0 {
return ''
}
if smart_base_type := t.smartcast_receiver_type_name(base_id) {
if alias_method := t.resolve_alias_receiver_method(smart_base_type, method) {
return alias_method
}
if method_name := t.resolve_receiver_method_for_type(smart_base_type, method) {
return method_name
}
if embedded_method := t.resolve_embedded_receiver_method(smart_base_type, method) {
return embedded_method
}
}
mut base_type := t.lvalue_type(base_id)
if base_type.starts_with('&') {
base_type = base_type[1..]
}
if base_type.len == 0 {
return ''
}
mut raw_var_clean := ''
if raw_var_type := t.raw_var_type_for_expr(base_id) {
raw_clean := if raw_var_type.starts_with('&') {
raw_var_type[1..]
} else {
raw_var_type
}
raw_var_clean = raw_clean
if raw_clean.len > 0 && raw_clean != base_type {
if alias_method := t.resolve_alias_receiver_method(raw_clean, method) {
if t.receiver_method_matches_base_type(alias_method, base_id) {
return alias_method
}
}
if method_name := t.resolve_receiver_method_for_type(raw_clean, method) {
if t.receiver_method_matches_base_type(method_name, base_id) {
return method_name
}
}
}
}
if raw_const_type := t.raw_const_type_name_for_expr(base_id) {
raw_clean := if raw_const_type.starts_with('&') {
raw_const_type[1..]
} else {
raw_const_type
}
if raw_clean.len > 0 && raw_clean != base_type && raw_clean != raw_var_clean {
if alias_method := t.resolve_alias_receiver_method(raw_clean, method) {
if t.receiver_method_matches_base_type(alias_method, base_id) {
return alias_method
}
}
if method_name := t.resolve_receiver_method_for_type(raw_clean, method) {
if t.receiver_method_matches_base_type(method_name, base_id) {
return method_name
}
}
}
}
if alias_method := t.resolve_alias_receiver_method(base_type, method) {
if t.receiver_method_matches_base_type(alias_method, base_id) {
return alias_method
}
}
if method_name := t.resolve_receiver_method_for_type(base_type, method) {
if t.receiver_method_matches_base_type(method_name, base_id) {
return method_name
}
}
if embedded_method := t.resolve_embedded_receiver_method(base_type, method) {
// resolve_embedded_receiver_method already walked the embedding chain from
// base_type, so the method is reachable by promotion. Its receiver is the
// embedded type and legitimately differs from base_type (they can even share
// a short name under a name collision), so the receiver-match guard used for
// direct methods must not reject it here.
return embedded_method
}
return ''
}
fn (t &Transformer) smartcast_receiver_type_name(base_id flat.NodeId) ?string {
key := t.expr_key(base_id)
if key.len == 0 {
return none
}
sc := t.find_smartcast(key) or { return none }
mut target := t.smartcast_target_type(sc)
for target.starts_with('&') {
target = target[1..]
}
if target.len == 0 {
return none
}
return target
}
fn (t &Transformer) resolve_collection_receiver_method_name(base_id flat.NodeId, method string, clean_base_type string) string {
if method.len == 0 {
return ''
}
if raw_var_type := t.raw_var_type_for_expr(base_id) {
raw_clean := if raw_var_type.starts_with('&') { raw_var_type[1..] } else { raw_var_type }
if raw_clean.len > 0 && raw_clean != clean_base_type {
if method_name := t.resolve_receiver_method_for_type(raw_clean, method) {
return method_name
}
}
}
if raw_const_type := t.raw_const_type_name_for_expr(base_id) {
raw_clean := if raw_const_type.starts_with('&') {
raw_const_type[1..]
} else {
raw_const_type
}
if raw_clean.len > 0 && raw_clean != clean_base_type {
if method_name := t.resolve_receiver_method_for_type(raw_clean, method) {
return method_name
}
}
}
if method_name := t.resolve_receiver_method_for_type(clean_base_type, method) {
return method_name
}
return ''
}
// resolve_receiver_method_for_type resolves resolve_receiver_method_for_type logic in transform.
// Hot: called for every method call lowered during body transforms, with heavy
// repetition of (receiver type, method) pairs. The uncached resolution below
// scans struct tables and builds many candidate strings, so memoize per
// (module, type, method); the cache clears when the fn table grows (closure
// lifting / str-method synthesis can change what resolves).
fn (t &Transformer) resolve_receiver_method_for_type(receiver_type string, method string) ?string {
if !isnil(t.receiver_method_cache) {
mut cache := t.receiver_method_cache
if !same_transform_text(cache.module, t.cur_module) || cache.fn_count != t.fn_ret_types.len {
cache.module = t.cur_module
cache.fn_count = t.fn_ret_types.len
cache.entries.clear()
cache.misses.clear()
}
cache_key := '${receiver_type}\n${method}'
if cached := cache.entries[cache_key] {
return cached
}
if cache.misses[cache_key] {
return none
}
if resolved := t.resolve_receiver_method_for_type_uncached(receiver_type, method) {
cache.entries[cache_key] = resolved
return resolved
}
cache.misses[cache_key] = true
return none
}
return t.resolve_receiver_method_for_type_uncached(receiver_type, method)
}
fn (t &Transformer) resolve_receiver_method_for_type_uncached(receiver_type string, method string) ?string {
mut clean_type := receiver_type
if clean_type.starts_with('&') {
clean_type = clean_type[1..]
}
if clean_type.starts_with('map[') {
for candidate in t.map_receiver_method_candidates(clean_type, method) {
if t.is_known_fn_name(candidate) {
return candidate
}
}
} else {
if method_name := t.resolve_specialized_generic_receiver_method(clean_type, method) {
return method_name
}
if !isnil(t.tc) {
if method_name := t.tc.concrete_method_signature_key(clean_type, method) {
if t.is_known_fn_name(method_name) {
return method_name
}
}
}
direct := '${clean_type}.${method}'
if t.is_known_fn_name(direct) {
return direct
}
if declared := t.declared_receiver_method(clean_type, method) {
return declared
}
if clean_type.starts_with('main.') && !clean_type['main.'.len..].contains('.') {
main_receiver := clean_type['main.'.len..]
main_method := '${main_receiver}.${method}'
if t.is_known_fn_name(main_method) {
return main_method
}
// Test files retain their declared module even though their concrete
// types enter an imported generic specialization as `main.Type`.
// Resolve the unique module-qualified method for that concrete type.
mut matched := ''
suffix := '.${main_receiver}.${method}'
for candidate, _ in t.fn_ret_types {
if candidate.ends_with(suffix) {
if matched.len > 0 && matched != candidate {
matched = ''
break
}
matched = candidate
}
}
if !isnil(t.tc) {
for candidate, _ in t.tc.fn_ret_types {
if candidate.ends_with(suffix) {
if matched.len > 0 && matched != candidate {
matched = ''
break
}
matched = candidate
}
}
}
if matched.len > 0 {
return matched
}
}
// A bare (unqualified) receiver type reached through a selective import
// (`import cli { Command }`, then `cmd.add_flag()`): the method is registered
// under the declaring module's qualified name (`cli.Command.add_flag`). The
// selective-import table can be unavailable this late, so scan for the unique
// module-qualified struct that both shares the short name AND declares the
// method (disambiguating e.g. `cli.Command` from `os.Command`). Only do this
// when the bare name is NOT a locally-declared type: a real local `Command`
// that lacks the method is a genuine missing-method error, not an import.
if !clean_type.contains('.') && !clean_type.contains('[') && !isnil(t.tc)
&& !t.tc.is_locally_declared_bare_type(clean_type) {
mut matched := ''
for sname, _ in t.tc.structs {
if !sname.contains('.') || sname.contains('[')
|| short_name_view(sname) != clean_type {
continue
}
qmethod := '${sname}.${method}'
if t.is_known_fn_name(qmethod) {
if matched.len > 0 && matched != qmethod {
matched = ''
break
}
matched = qmethod
}
}
if matched.len > 0 {
return matched
}
}
for receiver in generic_receiver_flat_type_variants(clean_type) {
flat_method := '${receiver}.${method}'
if t.is_known_fn_name(flat_method) {
return flat_method
}
}
for receiver in flattened_generic_receiver_short_variants(clean_type) {
flat_method := '${receiver}.${method}'
if t.is_known_fn_name(flat_method) {
return flat_method
}
}
if t.is_interface_type_name(clean_type) && !isnil(t.tc)
&& method !in t.tc.interface_abstract_method_names(clean_type) {
if embedded_interface_method := t.tc.interface_method_signature_key(clean_type, method) {
if embedded_interface_method != direct
&& t.is_known_fn_name(embedded_interface_method) {
return embedded_interface_method
}
}
}
}
if clean_type.starts_with('[]') {
elem_type := clean_type[2..]
short_elem := if elem_type.contains('.') { elem_type.all_after_last('.') } else { elem_type }
short_array := '[]${short_elem}.${method}'
if t.is_known_fn_name(short_array) {
return short_array
}
if elem_type.contains('.') {
qualified_array := '${elem_type.all_before_last('.')}.[]${short_elem}.${method}'
if t.is_known_fn_name(qualified_array) {
return qualified_array
}
} else if transform_can_prefix_collection_receiver(t.cur_module) {
current_module_array := '${t.cur_module}.[]${short_elem}.${method}'
if t.is_known_fn_name(current_module_array) {
return current_module_array
}
}
} else if clean_type.contains('.') {
short_type := clean_type.all_after_last('.')
short_method := '${short_type}.${method}'
if t.is_known_fn_name(short_method) {
return short_method
}
qualified_short_method := '${clean_type.all_before_last('.')}.${short_type}.${method}'
if t.is_known_fn_name(qualified_short_method) {
return qualified_short_method
}
}
if method_name := t.unique_receiver_method_suffix_match(t.receiver_method_candidates(clean_type,
method))
{
return method_name
}
if !isnil(t.tc) {
if target := t.tc.type_aliases[clean_type] {
alias_method := '${target}.${method}'
if t.is_known_fn_name(alias_method) {
return alias_method
}
}
}
return none
}
fn (t &Transformer) declared_receiver_method(receiver string, method string) ?string {
if receiver.len == 0 || method.len == 0 {
return none
}
clean_receiver := if receiver.starts_with('main.') {
receiver['main.'.len..]
} else {
receiver
}
target := '${clean_receiver}.${method}'
target_count := t.declared_fn_name_counts[target]
lowered := c_name(target)
if lowered == target {
return if target_count == 1 { target } else { none }
}
lowered_count := t.declared_fn_name_counts[lowered]
if target_count + lowered_count != 1 {
return none
}
return if target_count == 1 { target } else { lowered }
}
fn (t &Transformer) unique_receiver_method_suffix_match(candidates []string) ?string {
mut found := ''
for candidate in candidates {
name := t.receiver_method_suffix_index[candidate] or { continue }
if name == receiver_method_suffix_ambiguous {
return none
}
if found.len > 0 && found != name {
return none
}
found = name
}
if found.len == 0 {
return none
}
return found
}
fn (t &Transformer) resolve_specialized_generic_receiver_method(receiver_type string, method string) ?string {
base, args, ok := generic_app_parts(receiver_type)
if !ok || args.len == 0 {
return none
}
short_args := generic_type_args_short(args)
suffix := generic_type_suffixes(args)
mut candidates := []string{}
if !base.contains('.') && t.cur_module.len > 0 && t.cur_module !in ['main', 'builtin'] {
candidates << '${t.cur_module}.${base}[${short_args}].${method}'
candidates << '${t.cur_module}.${base}_${suffix}.${method}'
candidates << c_name('${t.cur_module}.${base}_${suffix}.${method}')
}
candidates << '${base}[${short_args}].${method}'
candidates << '${base}_${suffix}.${method}'
candidates << c_name('${base}_${suffix}.${method}')
if base.contains('.') {
module_name := base.all_before_last('.')
short_base := base.all_after_last('.')
candidates << '${short_base}[${short_args}].${method}'
candidates << '${short_base}_${suffix}.${method}'
candidates << '${module_name}.${short_base}_${suffix}.${method}'
candidates << c_name('${module_name}.${short_base}_${suffix}.${method}')
}
for candidate in candidates {
if t.is_known_fn_name(candidate) {
return candidate
}
}
return none
}
// resolve_alias_receiver_method converts resolve alias receiver method data for transform.
fn (t &Transformer) resolve_alias_receiver_method(base_type string, method string) ?string {
if isnil(t.tc) || base_type.len == 0 || method.len == 0 {
return none
}
// Key the memo on the raw spelling so cache hits skip the trim/normalize
// allocations below; the result is a pure function of the spelling anyway.
cache_key := '${t.cur_module}\n${base_type}\n${method}'
if !isnil(t.alias_receiver_method_cache) {
mut cache := t.alias_receiver_method_cache
if cached := cache.entries[cache_key] {
return cached
}
if cache.misses[cache_key] {
return none
}
}
raw_base := base_type.trim_space().trim_left('&')
clean_base := t.normalize_type_alias(base_type)
mut exact_aliases := [raw_base]
if !raw_base.contains('.') && t.cur_module.len > 0 && t.cur_module !in ['main', 'builtin'] {
exact_aliases << '${t.cur_module}.${raw_base}'
}
if imported := t.resolve_imported_type_name(raw_base) {
exact_aliases << imported
}
for alias in exact_aliases {
alias_method := '${alias}.${method}'
if alias in t.tc.type_aliases && t.is_known_fn_name(alias_method) {
if !isnil(t.alias_receiver_method_cache) {
mut cache := t.alias_receiver_method_cache
cache.entries[cache_key] = alias_method
}
return alias_method
}
}
if alias_method := t.alias_methods['${clean_base}.${method}'] {
if !isnil(t.alias_receiver_method_cache) {
mut cache := t.alias_receiver_method_cache
cache.entries[cache_key] = alias_method
}
return alias_method
}
if !t.is_integer_type_name(clean_base) {
if !isnil(t.alias_receiver_method_cache) {
mut cache := t.alias_receiver_method_cache
cache.misses[cache_key] = true
}
return none
}
for name, params in t.tc.fn_param_types {
if !name.ends_with('.${method}') || params.len == 0 {
continue
}
receiver_name := name.all_before_last('.')
if receiver_name.len == 0 || receiver_name !in t.tc.type_aliases {
continue
}
param_name := t.semantic_type_name(params[0])
if t.alias_receiver_type_matches(clean_base, param_name) {
if !isnil(t.alias_receiver_method_cache) {
mut cache := t.alias_receiver_method_cache
cache.entries[cache_key] = name
}
return name
}
}
if !isnil(t.alias_receiver_method_cache) {
mut cache := t.alias_receiver_method_cache
cache.misses[cache_key] = true
}
return none
}
fn (t &Transformer) resolve_embedded_receiver_method(base_type string, method string) ?string {
if base_type.len == 0 || method.len == 0 {
return none
}
mut lookup_type := if base_type.starts_with('&') { base_type[1..] } else { base_type }
if lookup_type !in t.structs && lookup_type.contains('.') {
short_type := lookup_type.all_after_last('.')
if short_type in t.structs {
lookup_type = short_type
}
}
fields := t.embedded_fields[lookup_type] or { return none }
for field in fields {
field_type := if field.raw_typ.len > 0 { field.raw_typ } else { field.typ }
clean_field := if field_type.starts_with('&') { field_type[1..] } else { field_type }
if method_name := t.resolve_receiver_method_for_type(clean_field, method) {
return method_name
}
if method_name := t.resolve_embedded_receiver_method(clean_field, method) {
return method_name
}
}
return none
}
// alias_receiver_type_matches converts alias receiver type matches data for transform.
fn (t &Transformer) alias_receiver_type_matches(base_type string, alias_type string) bool {
if base_type.len == 0 || alias_type.len == 0 {
return false
}
clean_alias := if alias_type.starts_with('&') { alias_type[1..] } else { alias_type }
alias_target := t.normalize_type_alias(clean_alias)
if alias_target == base_type {
return true
}
if !isnil(t.tc) {
alias_c_type := t.tc.c_type(t.tc.parse_type(alias_target))
base_c_type := t.tc.c_type(t.tc.parse_type(base_type))
if alias_c_type == base_c_type {
return true
}
}
return t.is_integer_type_name(alias_target) && t.is_integer_type_name(base_type)
}
// is_integer_type_name reports whether is integer type name applies in transform.
fn (t &Transformer) is_integer_type_name(typ string) bool {
return typ in ['int', 'i8', 'i16', 'i32', 'i64', 'u8', 'byte', 'u16', 'u32', 'u64', 'rune',
'isize', 'usize']
}
// raw_var_type_for_expr supports raw var type for expr handling for Transformer.
fn (t &Transformer) raw_var_type_for_expr(id flat.NodeId) ?string {
if int(id) < 0 {
return none
}
node := t.a.nodes[int(id)]
if node.kind == .ident {
typ := t.raw_var_type(node.value)
if typ.len > 0 {
return typ
}
}
if node.kind == .cast_expr && node.value.len > 0 {
return node.value
}
if node.kind == .selector {
if raw_type := t.raw_selector_field_type(id) {
return raw_type
}
}
if node.typ.len > 0 {
return node.typ
}
return none
}
// raw_const_type_name_for_expr supports raw const type name for expr handling for Transformer.
fn (t &Transformer) raw_const_type_name_for_expr(id flat.NodeId) ?string {
if int(id) < 0 || isnil(t.tc) {
return none
}
node := t.a.nodes[int(id)]
if t.selector_const_base_is_value(node) {
return none
}
if node.kind == .ident && t.raw_var_type(node.value).len > 0 {
return none
}
name := t.expr_key(id)
if name.len == 0 {
return none
}
key := t.const_type_key_in_context(name, t.cur_module, t.cur_file) or { return none }
typ := t.tc.const_types[key] or { return none }
return t.semantic_type_name(typ)
}
// resolve_method_receiver_type determines the receiver type for method calls.
// For a call where child[0] is a .selector, resolves the type of the selector's base expression.
fn (t &Transformer) resolve_method_receiver_type(call_node flat.Node) string {
if call_node.children_count == 0 {
return ''
}
fn_id := t.a.children[call_node.children_start]
if int(fn_id) < 0 {
return ''
}
fn_node := t.a.nodes[int(fn_id)]
if fn_node.kind != .selector || fn_node.children_count == 0 {
return ''
}
base_id := t.a.children[fn_node.children_start]
return t.resolve_expr_type(base_id)
}
// normalize_generic_call_expr transforms normalize generic call expr data for transform.
fn (mut t Transformer) normalize_generic_call_expr(id flat.NodeId, node flat.Node) flat.NodeId {
if node.children_count == 0 {
return id
}
fn_id := t.a.child(&node, 0)
if int(fn_id) < 0 {
return id
}
fn_node := t.a.nodes[int(fn_id)]
if fn_node.kind != .index || fn_node.children_count < 2 || fn_node.value == 'range' {
return id
}
if t.index_callee_is_value_index(fn_node) {
return id
}
base_id := t.a.child(&fn_node, 0)
base := t.a.nodes[int(base_id)]
if base.kind !in [.ident, .selector] {
return id
}
type_arg := t.generic_call_type_args_name(fn_node)
if type_arg.len == 0 {
return id
}
mut children := []flat.NodeId{cap: int(node.children_count)}
children << base_id
for i in 1 .. node.children_count {
children << t.a.child(&node, i)
}
start := t.a.children.len
for child in children {
t.a.children << child
}
return t.a.add_node(flat.Node{
kind: .call
op: node.op
children_start: start
children_count: flat.child_count(children.len)
pos: node.pos
value: type_arg
typ: node.typ
})
}
// generic_call_type_arg_name supports generic call type arg name handling for Transformer.
fn (t &Transformer) generic_call_type_arg_name(id flat.NodeId) string {
if int(id) < 0 {
return ''
}
node := t.a.nodes[int(id)]
match node.kind {
.ident {
// Map type arguments are represented by a synthetic `Map_key_value`
// identifier, while `typ` retains the source-level `map[key]value`.
// Keep the latter so comptime type groups and generic substitution can
// still inspect both map components.
if node.typ.starts_with('map[') {
return node.typ
}
return node.value
}
.selector {
if node.children_count == 0 {
return node.value
}
base := t.generic_call_type_arg_name(t.a.child(&node, 0))
if base.len == 0 {
return node.value
}
return '${base}.${node.value}'
}
.index {
if node.children_count < 2 || node.value == 'range' {
return ''
}
base := t.generic_call_type_arg_name(t.a.child(&node, 0))
if base.len == 0 {
return ''
}
mut args := []string{}
for i in 1 .. node.children_count {
arg := t.generic_call_type_arg_name(t.a.child(&node, i))
if arg.len == 0 {
return ''
}
args << arg
}
return '${base}[${args.join(', ')}]'
}
.array_init {
if node.value.len > 0 {
if node.value.starts_with('[]') {
return '[]${node.value}'
}
if node.value.starts_with('[') {
return node.value
}
return '[]${node.value}'
}
return ''
}
.map_init {
return node.value
}
.struct_decl {
return node.value
}
.prefix {
if node.children_count == 0 {
return ''
}
child := t.generic_call_type_arg_name(t.a.child(&node, 0))
if child.len == 0 {
return ''
}
if node.op == .amp {
return '&${child}'
}
return child
}
else {
return ''
}
}
}
fn (t &Transformer) generic_call_type_args_name(index_node flat.Node) string {
if index_node.kind != .index || index_node.children_count < 2 || index_node.value == 'range' {
return ''
}
if t.index_callee_is_value_index(index_node) {
return ''
}
mut args := []string{}
for i in 1 .. index_node.children_count {
arg := t.generic_call_type_arg_name(t.a.child(&index_node, i))
if arg.len == 0 {
return ''
}
args << arg
}
return args.join(', ')
}
fn (t &Transformer) index_callee_is_value_index(index_node flat.Node) bool {
if index_node.kind != .index || index_node.children_count == 0 || index_node.value == 'range' {
return false
}
base_id := t.a.child(&index_node, 0)
if int(base_id) < 0 {
return false
}
base := t.a.nodes[int(base_id)]
if t.type_name_is_indexable(base.typ)
|| t.type_name_is_indexable(t.raw_checker_node_type(base_id)) {
return true
}
if base.kind == .ident && t.type_name_is_indexable(t.var_type(base.value)) {
return true
}
if base.kind == .ident || base.kind == .selector {
base_type := t.resolve_expr_type(base_id)
if t.type_name_is_indexable(base_type) {
return true
}
}
if !isnil(t.tc) {
if typ := t.tc.expr_type(base_id) {
if transform_type_is_indexable(typ) {
return true
}
}
if transform_type_is_indexable(t.tc.resolve_type(base_id)) {
return true
}
}
return false
}
fn (t &Transformer) type_name_is_indexable(name string) bool {
mut clean := t.normalize_type_alias(name)
if clean.len == 0 {
return false
}
if clean.starts_with('&') {
clean = t.normalize_type_alias(clean[1..])
}
return clean == 'string' || clean.starts_with('[]') || clean.starts_with('map[')
|| t.is_fixed_array_type(clean)
}
fn transform_type_is_indexable(typ types.Type) bool {
match typ {
types.Array, types.ArrayFixed, types.Map, types.String {
return true
}
types.Alias {
return transform_type_is_indexable(typ.base_type)
}
types.Pointer {
return transform_type_is_indexable(typ.base_type)
}
else {
return false
}
}
}
// transform_call_args transforms all children of a call expression.
// child[0] is the function expression, children[1..n] are arguments.
@[direct_array_access]
fn (mut t Transformer) transform_call_args(id flat.NodeId, node flat.Node) flat.NodeId {
if node.children_count == 0 {
return t.a.add_node(flat.Node{
kind: .call
op: node.op
pos: node.pos
value: node.value
typ: node.typ
})