/
table.v
2542 lines (2425 loc) · 69.4 KB
/
table.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-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
@[has_globals]
module ast
import v.cflag
import v.util
@[heap; minify]
pub struct Table {
mut:
parsing_type string // name of the type to enable recursive type parsing
pub mut:
type_symbols []&TypeSymbol
type_idxs map[string]int
fns map[string]Fn
iface_types map[string][]Type
dumps map[int]string // needed for efficiently generating all _v_dump_expr_TNAME() functions
imports []string // List of all imports
modules []string // Topologically sorted list of all modules registered by the application
global_scope &Scope = unsafe { nil }
cflags []cflag.CFlag
redefined_fns []string
fn_generic_types map[string][][]Type // for generic functions
interfaces map[int]InterfaceDecl
cmod_prefix string // needed for ast.type_to_str(Type) while vfmt; contains `os.`
is_fmt bool
used_fns map[string]bool // filled in by the checker, when pref.skip_unused = true;
used_consts map[string]bool // filled in by the checker, when pref.skip_unused = true;
used_globals map[string]bool // filled in by the checker, when pref.skip_unused = true;
used_vweb_types []Type // vweb context types, filled in by checker, when pref.skip_unused = true;
used_maps int // how many times maps were used, filled in by checker, when pref.skip_unused = true;
panic_handler FnPanicHandler = default_table_panic_handler
panic_userdata voidptr = unsafe { nil } // can be used to pass arbitrary data to panic_handler;
panic_npanics int
cur_fn &FnDecl = unsafe { nil } // previously stored in Checker.cur_fn and Gen.cur_fn
cur_concrete_types []Type // current concrete types, e.g. <int, string>
gostmts int // how many `go` statements there were in the parsed files.
// When table.gostmts > 0, __VTHREADS__ is defined, which can be checked with `$if threads {`
enum_decls map[string]EnumDecl
module_deprecated map[string]bool
module_attrs map[string][]Attr // module attributes
builtin_pub_fns map[string]bool
pointer_size int
// cache for type_to_str_using_aliases
cached_type_to_str map[u64]string
anon_struct_names map[string]int // anon struct name -> struct sym idx
// counter for anon struct, avoid name conflicts.
anon_struct_counter int
}
// used by vls to avoid leaks
// TODO remove manual memory management
@[unsafe]
pub fn (mut t Table) free() {
unsafe {
for s in t.type_symbols {
s.free()
}
t.type_symbols.free()
t.type_idxs.free()
t.fns.free()
t.dumps.free()
t.imports.free()
t.modules.free()
t.cflags.free()
t.redefined_fns.free()
t.fn_generic_types.free()
t.cmod_prefix.free()
t.used_fns.free()
t.used_consts.free()
t.used_globals.free()
t.used_vweb_types.free()
}
}
pub const invalid_type_idx = -1
pub const fn_type_escape_seq = [' ', '', '(', '_', ')', '']
pub const map_cname_escape_seq = ['[', '_T_', ', ', '_', ']', '']
pub type FnPanicHandler = fn (&Table, string)
fn default_table_panic_handler(t &Table, message string) {
panic(message)
}
pub fn (t &Table) panic(message string) {
mut mt := unsafe { &Table(t) }
mt.panic_npanics++
t.panic_handler(t, message)
}
pub fn new_table() &Table {
mut t := &Table{
global_scope: &Scope{
parent: unsafe { nil }
}
cur_fn: unsafe { nil }
}
t.register_builtin_type_symbols()
t.is_fmt = true
global_table = t
return t
}
__global global_table = &Table(unsafe { nil })
// used to compare fn's & for naming anon fn's
pub fn (t &Table) fn_type_signature(f &Fn) string {
mut sig := ''
for i, arg in f.params {
typ := arg.typ.set_nr_muls(0)
if arg.is_mut {
sig += 'mut_'
}
sig += t.sym(typ).cname.to_lower()
if i < f.params.len - 1 {
sig += '_'
}
}
if f.return_type != 0 && f.return_type != void_type {
sym := t.sym(f.return_type)
opt := if f.return_type.has_flag(.option) { 'option_' } else { '' }
res := if f.return_type.has_flag(.result) { 'result_' } else { '' }
sig += '__${opt}${res}${sym.cname}'
}
return sig
}
// fn_type_source_signature generates the signature of a function which looks like in the V source
pub fn (t &Table) fn_type_source_signature(f &Fn) string {
mut sig := '('
for i, arg in f.params {
if arg.is_mut {
sig += 'mut '
}
// Note: arg name is only added for fmt, else it would causes errors with generics
if t.is_fmt && arg.name.len > 0 {
sig += '${arg.name} '
}
arg_type_sym := t.sym(arg.typ)
sig += arg_type_sym.name
if i < f.params.len - 1 {
sig += ', '
}
}
sig += ')'
if f.return_type == ovoid_type {
sig += ' ?'
} else if f.return_type == rvoid_type {
sig += ' !'
} else if f.return_type != void_type && f.return_type != 0 {
return_type_sym := t.sym(f.return_type)
if f.return_type.has_flag(.option) {
sig += ' ?${return_type_sym.name}'
} else if f.return_type.has_flag(.result) {
sig += ' !${return_type_sym.name}'
} else {
sig += ' ${return_type_sym.name}'
}
}
return sig
}
pub fn (t &Table) is_same_method(f &Fn, func &Fn) string {
if f.return_type != func.return_type {
s := t.type_to_str(f.return_type)
return 'expected return type `${s}`'
}
if f.params.len != func.params.len {
return 'expected ${f.params.len} parameter(s), not ${func.params.len}'
}
// interface name() other mut name() : error
for i in 0 .. f.params.len {
// don't check receiver for `.typ`
has_unexpected_type := i > 0 && f.params[i].typ != func.params[i].typ
// temporary hack for JS ifaces
lsym := t.sym(f.params[i].typ)
rsym := t.sym(func.params[i].typ)
if lsym.language == .js && rsym.language == .js {
return ''
}
has_unexpected_mutability := !f.params[i].is_mut && func.params[i].is_mut
if has_unexpected_type || has_unexpected_mutability {
exps := t.type_to_str(f.params[i].typ)
gots := t.type_to_str(func.params[i].typ)
if has_unexpected_type {
return 'expected `${exps}`, not `${gots}` for parameter ${i}'
} else {
return 'expected `${exps}` which is immutable, not `mut ${gots}`'
}
}
}
return ''
}
pub fn (t &Table) find_fn(name string) ?Fn {
if f := t.fns[name] {
return f
}
return none
}
pub fn (t &Table) known_fn(name string) bool {
t.find_fn(name) or { return false }
return true
}
pub fn (mut t Table) register_fn(new_fn Fn) {
t.fns[new_fn.name] = new_fn
if new_fn.is_pub && new_fn.mod == 'builtin' {
t.builtin_pub_fns[new_fn.name] = true
}
}
pub fn (mut t Table) register_interface(idecl InterfaceDecl) {
t.interfaces[idecl.typ] = idecl
}
pub fn (mut t TypeSymbol) register_method(new_fn Fn) int {
// returns a method index, stored in the ast.FnDecl
// for faster lookup in the checker's fn_decl method
t.methods << new_fn
return t.methods.len - 1
}
pub fn (t &Table) register_aggregate_method(mut sym TypeSymbol, name string) !Fn {
if sym.kind != .aggregate {
t.panic('Unexpected type symbol: ${sym.kind}')
}
agg_info := sym.info as Aggregate
// an aggregate always has at least 2 types
mut found_once := false
mut new_fn := Fn{}
for typ in agg_info.types {
ts := t.sym(typ)
if type_method := ts.find_method(name) {
if !found_once {
found_once = true
new_fn = type_method
} else if !new_fn.method_equals(type_method) {
return error('method `${t.type_to_str(typ)}.${name}` signature is different')
}
} else {
return error('unknown method: `${t.type_to_str(typ)}.${name}`')
}
}
// register the method in the aggregate, so lookup is faster next time
sym.register_method(new_fn)
return new_fn
}
pub fn (t &Table) has_method(s &TypeSymbol, name string) bool {
t.find_method(s, name) or { return false }
return true
}
// find_method searches from current type up through each parent looking for method
pub fn (t &Table) find_method(s &TypeSymbol, name string) !Fn {
mut ts := unsafe { s }
for {
if method := ts.find_method(name) {
return method
}
if ts.kind == .aggregate {
if method := t.register_aggregate_method(mut ts, name) {
return method
} else {
return err
}
}
if ts.parent_idx == 0 {
break
}
ts = t.type_symbols[ts.parent_idx]
}
return error('unknown method `${name}`')
}
@[params]
pub struct GetEmbedsOptions {
preceding []Type
}
// get_embeds returns all nested embedded structs
// the hierarchy of embeds is returned as a list
pub fn (t &Table) get_embeds(sym &TypeSymbol, options GetEmbedsOptions) [][]Type {
mut embeds := [][]Type{}
unalias_sym := if sym.info is Alias { t.sym(sym.info.parent_type) } else { sym }
if unalias_sym.info is Struct {
for embed in unalias_sym.info.embeds {
embed_sym := t.sym(embed)
mut preceding := options.preceding.clone()
preceding << embed
embeds << t.get_embeds(embed_sym, preceding: preceding)
}
if unalias_sym.info.embeds.len == 0 && options.preceding.len > 0 {
embeds << options.preceding
}
}
return embeds
}
pub fn (t &Table) find_method_from_embeds(sym &TypeSymbol, method_name string) !(Fn, []Type) {
if sym.info is Struct {
mut found_methods := []Fn{}
mut embed_of_found_methods := []Type{}
for embed in sym.info.embeds {
embed_sym := t.sym(embed)
if m := embed_sym.find_method_with_generic_parent(method_name) {
found_methods << m
embed_of_found_methods << embed
} else {
method, types := t.find_method_from_embeds(embed_sym, method_name) or { continue }
found_methods << method
embed_of_found_methods << embed
embed_of_found_methods << types
}
}
if found_methods.len == 1 {
return found_methods[0], embed_of_found_methods
} else if found_methods.len > 1 {
return error('ambiguous method `${method_name}`')
}
} else if sym.info is Interface {
mut found_methods := []Fn{}
mut embed_of_found_methods := []Type{}
for embed in sym.info.embeds {
embed_sym := t.sym(embed)
if m := embed_sym.find_method_with_generic_parent(method_name) {
found_methods << m
embed_of_found_methods << embed
} else {
method, types := t.find_method_from_embeds(embed_sym, method_name) or { continue }
found_methods << method
embed_of_found_methods << embed
embed_of_found_methods << types
}
}
if found_methods.len == 1 {
return found_methods[0], embed_of_found_methods
} else if found_methods.len > 1 {
return error('ambiguous method `${method_name}`')
}
} else if sym.info is Aggregate {
for typ in sym.info.types {
agg_sym := t.sym(typ)
method, embed_types := t.find_method_from_embeds(agg_sym, method_name) or { continue }
if embed_types.len != 0 {
return method, embed_types
}
}
}
return error('')
}
// find_method_with_embeds searches for a given method, also looking through embedded fields
pub fn (t &Table) find_method_with_embeds(sym &TypeSymbol, method_name string) !Fn {
if func := t.find_method(sym, method_name) {
return func
} else {
// look for embedded field
func, _ := t.find_method_from_embeds(sym, method_name) or { return err }
return func
}
}
// find_enum_field_val finds the possible int value from the enum name and enum field
// (returns `none` if the value cannot be resolved at compile time)
pub fn (t &Table) find_enum_field_val(name string, field_ string) ?i64 {
mut val := i64(0)
enum_decl := t.enum_decls[name]
mut enum_vals := []i64{}
for field in enum_decl.fields {
if field.name == field_ {
if field.has_expr {
if field.expr is IntegerLiteral {
val = field.expr.val.i64()
break
}
return none
} else {
if enum_vals.len > 0 {
val = enum_vals.last() + 1
} else {
val = 0
}
break
}
} else {
if field.has_expr {
if field.expr is IntegerLiteral {
enum_vals << field.expr.val.i64()
} else {
return none
}
} else {
if enum_vals.len > 0 {
enum_vals << enum_vals.last() + 1
} else {
enum_vals << 0
}
}
}
}
return if enum_decl.is_flag { u64(1) << val } else { val }
}
pub fn (t &Table) get_enum_field_names(name string) []string {
enum_decl := t.enum_decls[name]
mut field_names := []string{}
for field in enum_decl.fields {
field_names << field.name
}
return field_names
}
pub fn (t &Table) get_enum_field_vals(name string) []i64 {
enum_decl := t.enum_decls[name]
mut enum_vals := []i64{}
mut last_val := i64(0)
for field in enum_decl.fields {
if field.has_expr {
if field.expr is IntegerLiteral {
enum_vals << field.expr.val.i64()
last_val = field.expr.val.i64()
}
} else {
if enum_vals.len > 0 {
enum_vals << last_val + 1
last_val++
} else {
enum_vals << 0
}
}
}
return enum_vals
}
pub fn (t &Table) get_embed_methods(sym &TypeSymbol) []Fn {
mut methods := []Fn{}
if sym.info is Struct {
for embed in sym.info.embeds {
embed_sym := t.sym(embed)
methods << embed_sym.methods
methods << t.get_embed_methods(embed_sym)
}
}
return methods
}
fn (t &Table) register_aggregate_field(mut sym TypeSymbol, name string) !StructField {
if sym.kind != .aggregate {
t.panic('Unexpected type symbol: ${sym.kind}')
}
mut agg_info := sym.info as Aggregate
// an aggregate always has at least 2 types
mut found_once := false
mut new_field := StructField{}
for typ in agg_info.types {
ts := t.sym(typ)
if type_field := t.find_field(ts, name) {
if !found_once {
found_once = true
new_field = type_field
} else if new_field.typ != type_field.typ {
return error('field `${t.type_to_str(typ)}.${name}` type is different')
}
new_field = StructField{
...new_field
is_mut: new_field.is_mut && type_field.is_mut
is_pub: new_field.is_pub && type_field.is_pub
}
} else {
return error('type `${t.type_to_str(typ)}` has no field or method `${name}`')
}
}
agg_info.fields << new_field
return new_field
}
pub fn (t &Table) struct_has_field(struct_ &TypeSymbol, name string) bool {
t.find_field(struct_, name) or { return false }
return true
}
// struct_fields returns all fields including fields from embeds
// use this instead symbol.info.fields to get all fields
pub fn (t &Table) struct_fields(sym &TypeSymbol) []StructField {
mut fields := []StructField{}
if sym.info is Struct {
fields << sym.info.fields
for embed in sym.info.embeds {
embed_sym := t.sym(embed)
fields << t.struct_fields(embed_sym)
}
}
return fields
}
// search from current type up through each parent looking for field
pub fn (t &Table) find_field(s &TypeSymbol, name string) !StructField {
mut ts := unsafe { s }
for {
match mut ts.info {
Struct {
if field := ts.info.find_field(name) {
return field
}
}
Aggregate {
if field := ts.info.find_field(name) {
return field
}
field := t.register_aggregate_field(mut ts, name) or { return err }
return field
}
Interface {
if field := ts.info.find_field(name) {
return field
}
}
SumType {
t.resolve_common_sumtype_fields(mut ts)
if field := ts.info.find_field(name) {
return field
}
// mut info := ts.info as SumType
// TODO a more detailed error so that it's easier to fix?
return error('field `${name}` does not exist or have the same type in all sumtype variants')
}
else {}
}
if ts.parent_idx == 0 {
break
}
ts = t.type_symbols[ts.parent_idx]
}
return error('')
}
// find_field_from_embeds tries to find a field in the nested embeds
pub fn (t &Table) find_field_from_embeds(sym &TypeSymbol, field_name string) !(StructField, []Type) {
if sym.info is Struct {
mut found_fields := []StructField{}
mut embeds_of_found_fields := []Type{}
for embed in sym.info.embeds {
embed_sym := t.sym(embed)
if field := t.find_field(embed_sym, field_name) {
found_fields << field
embeds_of_found_fields << embed
} else {
field, types := t.find_field_from_embeds(embed_sym, field_name) or { continue }
found_fields << field
embeds_of_found_fields << embed
embeds_of_found_fields << types
}
}
if found_fields.len == 1 {
return found_fields[0], embeds_of_found_fields
} else if found_fields.len > 1 {
return error('ambiguous field `${field_name}`')
}
} else if sym.info is Aggregate {
for typ in sym.info.types {
agg_sym := t.sym(typ)
field, embed_types := t.find_field_from_embeds(agg_sym, field_name) or { continue }
if embed_types.len > 0 {
return field, embed_types
}
}
} else if sym.info is Alias {
unalias_sym := t.sym(sym.info.parent_type)
return t.find_field_from_embeds(unalias_sym, field_name)
}
return error('')
}
// find_field_with_embeds searches for a given field, also looking through embedded fields
pub fn (t &Table) find_field_with_embeds(sym &TypeSymbol, field_name string) !StructField {
if field := t.find_field(sym, field_name) {
return field
} else {
// look for embedded field
first_err := err
field, _ := t.find_field_from_embeds(sym, field_name) or { return first_err }
return field
}
}
pub fn (t &Table) resolve_common_sumtype_fields(mut sym TypeSymbol) {
mut info := sym.info as SumType
if info.found_fields {
return
}
mut field_map := map[string]StructField{}
mut field_usages := map[string]int{}
for variant in info.variants {
mut v_sym := t.final_sym(variant)
fields := match mut v_sym.info {
Struct {
t.struct_fields(v_sym)
}
SumType {
t.resolve_common_sumtype_fields(mut v_sym)
v_sym.info.fields
}
else {
[]StructField{}
}
}
for field in fields {
if field.name !in field_map {
field_map[field.name] = field
field_usages[field.name]++
} else if field.equals(field_map[field.name]) {
field_usages[field.name]++
}
}
}
for field, nr_definitions in field_usages {
if nr_definitions == info.variants.len {
info.fields << field_map[field]
}
}
info.found_fields = true
sym.info = info
}
@[inline]
pub fn (t &Table) find_type_idx(name string) int {
return t.type_idxs[name]
}
@[inline]
pub fn (t &Table) find_sym(name string) ?&TypeSymbol {
idx := t.type_idxs[name]
if idx > 0 {
return t.type_symbols[idx]
}
return none
}
@[inline]
pub fn (t &Table) find_sym_and_type_idx(name string) (&TypeSymbol, int) {
idx := t.type_idxs[name]
if idx > 0 {
return t.type_symbols[idx], idx
}
return ast.invalid_type_symbol, idx
}
pub const invalid_type_symbol = &TypeSymbol{
idx: invalid_type_idx
parent_idx: invalid_type_idx
language: .v
mod: 'builtin'
kind: .placeholder
name: 'InvalidType'
cname: 'InvalidType'
}
@[inline]
pub fn (t &Table) sym_by_idx(idx int) &TypeSymbol {
return t.type_symbols[idx]
}
pub fn (t &Table) sym(typ Type) &TypeSymbol {
idx := typ.idx()
if idx > 0 {
return t.type_symbols[idx]
}
// this should never happen
t.panic('sym: invalid type (typ=${typ} idx=${idx}). Compiler bug. This should never happen. Please report the bug using `v bug file.v`.
')
return ast.invalid_type_symbol
}
// final_sym follows aliases until it gets to a "real" Type
@[inline]
pub fn (t &Table) final_sym(typ Type) &TypeSymbol {
mut idx := typ.idx()
if idx > 0 {
cur_sym := t.type_symbols[idx]
if cur_sym.info is Alias {
idx = cur_sym.info.parent_type.idx()
}
return t.type_symbols[idx]
}
// this should never happen
t.panic('final_sym: invalid type (typ=${typ} idx=${idx}). Compiler bug. This should never happen. Please report the bug using `v bug file.v`.')
return ast.invalid_type_symbol
}
@[inline]
pub fn (t &Table) get_type_name(typ Type) string {
return t.sym(typ).name
}
@[inline]
pub fn (t &Table) get_final_type_name(typ Type) string {
return t.final_sym(typ).name
}
@[inline]
pub fn (t &Table) unalias_num_type(typ Type) Type {
sym := t.sym(typ)
if sym.info is Alias {
if sym.info.parent_type <= char_type && sym.info.parent_type >= void_type {
return sym.info.parent_type
}
}
return typ
}
@[inline]
pub fn (t &Table) unaliased_type(typ Type) Type {
sym := t.sym(typ)
if sym.info is Alias {
return sym.info.parent_type
}
return typ
}
fn (mut t Table) rewrite_already_registered_symbol(typ TypeSymbol, existing_idx int) int {
existing_symbol := t.type_symbols[existing_idx]
$if trace_rewrite_already_registered_symbol ? {
eprintln('>> rewrite_already_registered_symbol sym: ${typ.name} | existing_idx: ${existing_idx} | existing_symbol: ${existing_symbol.name}')
}
if existing_symbol.kind == .placeholder {
// override placeholder
t.type_symbols[existing_idx] = &TypeSymbol{
...typ
methods: existing_symbol.methods
idx: existing_idx
}
return existing_idx
}
// Override the already registered builtin types with the actual
// v struct declarations in the vlib/builtin module sources:
if (existing_idx >= string_type_idx && existing_idx <= map_type_idx)
|| existing_idx == error_type_idx {
if existing_idx == string_type_idx {
// existing_type := t.type_symbols[existing_idx]
unsafe {
*existing_symbol = &TypeSymbol{
...typ
kind: existing_symbol.kind
idx: existing_idx
}
}
} else {
t.type_symbols[existing_idx] = &TypeSymbol{
...typ
idx: existing_idx
}
}
return existing_idx
}
return ast.invalid_type_idx
}
@[inline]
pub fn (mut t Table) register_sym(sym TypeSymbol) int {
mut idx := -2
$if trace_register_sym ? {
defer {
eprintln('>> register_sym: ${sym.name:-60} | idx: ${idx}')
}
}
mut existing_idx := t.type_idxs[sym.name]
if existing_idx > 0 {
idx = t.rewrite_already_registered_symbol(sym, existing_idx)
if idx != -2 {
return idx
}
}
if sym.mod == 'main' {
existing_idx = t.type_idxs[sym.name.trim_string_left('main.')]
if existing_idx > 0 {
idx = t.rewrite_already_registered_symbol(sym, existing_idx)
if idx != -2 {
return idx
}
}
}
idx = t.type_symbols.len
t.type_symbols << &TypeSymbol{
...sym
}
t.type_symbols[idx].idx = idx
t.type_idxs[sym.name] = idx
return idx
}
@[inline]
pub fn (mut t Table) register_enum_decl(enum_decl EnumDecl) {
t.enum_decls[enum_decl.name] = enum_decl
}
@[inline]
pub fn (mut t Table) register_anon_struct(name string, sym_idx int) {
t.anon_struct_names[name] = sym_idx
}
pub fn (t &Table) known_type(name string) bool {
return t.type_idxs[name] != 0 || t.parsing_type == name || name in ['i32', 'byte']
}
// start_parsing_type open the scope during the parsing of a type
// where the type name must include the module prefix
pub fn (mut t Table) start_parsing_type(type_name string) {
t.parsing_type = type_name
}
pub fn (mut t Table) reset_parsing_type() {
t.parsing_type = ''
}
pub fn (t &Table) known_type_idx(typ Type) bool {
if typ == 0 {
return false
}
sym := t.sym(typ)
match sym.kind {
.placeholder {
return sym.language != .v || sym.name.starts_with('C.')
}
.array {
return t.known_type_idx((sym.info as Array).elem_type)
}
.array_fixed {
return t.known_type_idx((sym.info as ArrayFixed).elem_type)
}
.map {
info := sym.info as Map
return t.known_type_idx(info.key_type) && t.known_type_idx(info.value_type)
}
else {}
}
return true
}
// array_source_name generates the original name for the v source.
// e. g. []int
@[inline]
pub fn (t &Table) array_name(elem_type Type) string {
elem_type_sym := t.sym(elem_type)
ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' }
opt := if elem_type.has_flag(.option) { '?' } else { '' }
res := if elem_type.has_flag(.result) { '!' } else { '' }
return '[]${opt}${res}${ptr}${elem_type_sym.name}'
}
@[inline]
pub fn (t &Table) array_cname(elem_type Type) string {
elem_type_sym := t.sym(elem_type)
suffix := if elem_type.is_ptr() { '_ptr'.repeat(elem_type.nr_muls()) } else { '' }
opt := if elem_type.has_flag(.option) { '_option_' } else { '' }
res := if elem_type.has_flag(.result) { '_result_' } else { '' }
if elem_type_sym.cname.contains('[') {
type_name := elem_type_sym.cname.replace_each(ast.map_cname_escape_seq)
return 'Array_${opt}${res}${type_name}${suffix}'
} else {
return 'Array_${opt}${res}${elem_type_sym.cname}${suffix}'
}
}
// array_fixed_source_name generates the original name for the v source.
// e. g. [16][8]int
@[inline]
pub fn (t &Table) array_fixed_name(elem_type Type, size int, size_expr Expr) string {
elem_type_sym := t.sym(elem_type)
ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' }
opt := if elem_type.has_flag(.option) { '?' } else { '' }
res := if elem_type.has_flag(.result) { '!' } else { '' }
size_str := if size_expr is EmptyExpr || size != 987654321 {
size.str()
} else {
size_expr.str()
}
return '[${size_str}]${opt}${res}${ptr}${elem_type_sym.name}'
}
@[inline]
pub fn (t &Table) array_fixed_cname(elem_type Type, size int) string {
elem_type_sym := t.sym(elem_type)
suffix := if elem_type.is_ptr() { '_ptr${elem_type.nr_muls()}' } else { '' }
opt := if elem_type.has_flag(.option) { '_option_' } else { '' }
res := if elem_type.has_flag(.result) { '_result_' } else { '' }
if elem_type_sym.cname.contains('[') {
type_name := elem_type_sym.cname.replace_each(ast.map_cname_escape_seq)
return 'Array_fixed_${opt}${res}${type_name}${suffix}_${size}'
} else {
return 'Array_fixed_${opt}${res}${elem_type_sym.cname}${suffix}_${size}'
}
}
@[inline]
pub fn (t &Table) chan_name(elem_type Type, is_mut bool) string {
elem_type_sym := t.sym(elem_type)
mut ptr := ''
if is_mut {
ptr = 'mut '
} else if elem_type.is_ptr() {
ptr = '&'
}
return 'chan ${ptr}${elem_type_sym.name}'
}
@[inline]
pub fn (t &Table) chan_cname(elem_type Type, is_mut bool) string {
elem_type_sym := t.sym(elem_type)
mut suffix := ''
if is_mut {
suffix = '_mut'
} else if elem_type.is_ptr() {
suffix = '_ptr'
}
type_name := if elem_type_sym.cname.contains('[') {
elem_type_sym.cname.replace_each(ast.map_cname_escape_seq)
} else {
elem_type_sym.cname
}
return 'chan_${type_name}' + suffix
}
@[inline]
pub fn (t &Table) promise_name(return_type Type) string {
if return_type.idx() == void_type_idx {
return 'Promise[JS.Any, JS.Any]'
}
return_type_sym := t.sym(return_type)
return 'Promise[${return_type_sym.name}, JS.Any]'
}
@[inline]
pub fn (t &Table) promise_cname(return_type Type) string {
if return_type == void_type {
return 'Promise_Any_Any'
}
return_type_sym := t.sym(return_type)
return 'Promise_${return_type_sym.name}_Any'
}
@[inline]
pub fn (t &Table) thread_name(return_type Type) string {
if return_type.idx() == void_type_idx {
if return_type.has_flag(.option) {
return 'thread ?'
} else if return_type.has_flag(.result) {
return 'thread !'
} else {
return 'thread'
}
}
return_type_sym := t.sym(return_type)
ptr := if return_type.is_ptr() { '&' } else { '' }
opt := if return_type.has_flag(.option) { '?' } else { '' }
res := if return_type.has_flag(.result) { '!' } else { '' }
return 'thread ${opt}${res}${ptr}${return_type_sym.name}'
}
@[inline]
pub fn (t &Table) thread_cname(return_type Type) string {
if return_type == void_type {
if return_type.has_flag(.option) {
return '__v_thread_Option_void'
} else if return_type.has_flag(.result) {
return '__v_thread_Result_void'
} else {
return '__v_thread'
}
}
return_type_sym := t.sym(return_type)
suffix := if return_type.is_ptr() { '_ptr' } else { '' }
opt := if return_type.has_flag(.option) { '_option_' } else { '' }
res := if return_type.has_flag(.result) { '_result_' } else { '' }
return '__v_thread_${opt}${res}${return_type_sym.cname}${suffix}'
}
// map_source_name generates the original name for the v source.
// e. g. map[string]int
@[inline]
pub fn (t &Table) map_name(key_type Type, value_type Type) string {
key_type_sym := t.sym(key_type)
value_type_sym := t.sym(value_type)
ptr := if value_type.is_ptr() { '&'.repeat(value_type.nr_muls()) } else { '' }
opt := if value_type.has_flag(.option) { '?' } else { '' }
res := if value_type.has_flag(.result) { '!' } else { '' }
return 'map[${key_type_sym.name}]${opt}${res}${ptr}${value_type_sym.name}'
}