/
types.v
1749 lines (1614 loc) · 41.1 KB
/
types.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.
//
// Type layout information (32 bits)
// flag (8 bits) | nr_muls (8 bits) | idx (16 bits)
// pack: (int(flag)<<24) | (nr_muls<<16) | u16(idx)
// unpack:
// flag: (int(type)>>24) & 0xff
// nr_muls: (int(type)>>16) & 0xff
// idx: u16(type) & 0xffff
module ast
import strings
import v.pref
import v.token
pub type Type = int
pub type TypeInfo = Aggregate
| Alias
| Array
| ArrayFixed
| Chan
| Enum
| FnType
| GenericInst
| Interface
| Map
| MultiReturn
| Struct
| SumType
| Thread
pub enum Language {
v
c
js
wasm
amd64 // aka x86_64
i386
arm64 // 64-bit arm
arm32 // 32-bit arm
rv64 // 64-bit risc-v
rv32 // 32-bit risc-v
wasm32
}
pub fn pref_arch_to_table_language(pref_arch pref.Arch) Language {
return match pref_arch {
.amd64 {
.amd64
}
.arm64 {
.arm64
}
.arm32 {
.arm32
}
.rv64 {
.rv64
}
.rv32 {
.rv32
}
.i386 {
.i386
}
.js_node, .js_browser, .js_freestanding {
.js
}
.wasm32 {
.wasm32
}
._auto, ._max {
.v
}
}
}
// Represents a type that only needs an identifier, e.g. int, array_int.
// A pointer type `&T` would have a TypeSymbol `T`.
// Note: For a Type, use:
// * Table.type_to_str(typ) not TypeSymbol.name.
// * Table.type_kind(typ) not TypeSymbol.kind.
// Each TypeSymbol is entered into `Table.types`.
// See also: Table.sym.
[minify]
pub struct TypeSymbol {
pub:
parent_idx int
pub mut:
info TypeInfo
kind Kind
name string // the internal & source name of the type, i.e. `[5]int`.
cname string // the name with no dots for use in the generated C code
rname string // the raw name
methods []Fn
generic_types []Type
mod string
is_pub bool
language Language
idx int
size int = -1
align int = -1
}
// max of 8
pub enum TypeFlag {
option
result
variadic
generic
shared_f
atomic_f
}
/*
To save precious TypeFlag bits the 4 possible ShareTypes are coded in the two
bits `shared` and `atomic_or_rw` (see sharetype_from_flags() below).
*/
pub enum ShareType {
mut_t
shared_t
atomic_t
}
pub fn (t ShareType) str() string {
match t {
.mut_t { return 'mut' }
.shared_t { return 'shared' }
.atomic_t { return 'atomic' }
}
}
pub struct MultiReturn {
pub mut:
types []Type
}
pub struct FnType {
pub mut:
is_anon bool
has_decl bool
func Fn
}
[minify]
pub struct Struct {
pub:
attrs []Attr
pub mut:
embeds []Type
fields []StructField
is_typedef bool // C. [typedef]
is_union bool
is_heap bool
is_minify bool
is_anon bool
is_generic bool
generic_types []Type
concrete_types []Type
parent_type Type
}
// instantiation of a generic struct
pub struct GenericInst {
pub mut:
parent_idx int // idx of the base generic struct
concrete_types []Type // concrete types, e.g. [int, string]
}
[minify]
pub struct Interface {
pub mut:
types []Type // all types that implement this interface
fields []StructField
methods []Fn
embeds []Type
// `I1 is I2` conversions
conversions map[int][]Type
// generic interface support
is_generic bool
generic_types []Type
concrete_types []Type
parent_type Type
}
pub struct Enum {
pub:
vals []string
is_flag bool
is_multi_allowed bool
uses_exprs bool
typ Type
attrs map[string][]Attr
}
[minify]
pub struct Alias {
pub:
parent_type Type
language Language
is_import bool
}
pub struct Aggregate {
mut:
fields []StructField // used for faster lookup inside the module
pub:
sum_type Type
types []Type
}
pub struct Array {
pub:
nr_dims int
pub mut:
elem_type Type
}
[minify]
pub struct ArrayFixed {
pub:
size int
size_expr Expr // used by fmt for e.g. ´[my_const]u8´
pub mut:
elem_type Type
is_fn_ret bool
}
pub struct Chan {
pub mut:
elem_type Type
is_mut bool
}
pub struct Thread {
pub mut:
return_type Type
}
pub struct Map {
pub mut:
key_type Type
value_type Type
}
[minify]
pub struct SumType {
pub mut:
fields []StructField
found_fields bool
is_anon bool
// generic sumtype support
is_generic bool
variants []Type
generic_types []Type
concrete_types []Type
parent_type Type
}
// <atomic.h> defines special typenames
pub fn (t Type) atomic_typename() string {
idx := t.idx()
match idx {
ast.u32_type_idx { return 'atomic_uint' }
ast.int_type_idx { return '_Atomic int' }
ast.i32_type_idx { return '_Atomic int' }
ast.u64_type_idx { return 'atomic_ullong' }
ast.i64_type_idx { return 'atomic_llong' }
else { return 'unknown_atomic' }
}
}
pub fn sharetype_from_flags(is_shared bool, is_atomic bool) ShareType {
return unsafe { ShareType(int(u32(is_atomic) << 1) | int(is_shared)) }
}
pub fn (t Type) share() ShareType {
return sharetype_from_flags(t.has_flag(.shared_f), t.has_flag(.atomic_f))
}
// return TypeSymbol idx for `t`
[inline]
pub fn (t Type) idx() int {
return u16(t) & 0xffff
}
[inline]
pub fn (t Type) is_void() bool {
return t == ast.void_type
}
[inline]
pub fn (t Type) is_full() bool {
return t != 0 && t != ast.void_type
}
// return nr_muls for `t`
[inline]
pub fn (t Type) nr_muls() int {
return (int(t) >> 16) & 0xff
}
// return true if `t` is a pointer (nr_muls>0)
[inline]
pub fn (t Type) is_ptr() bool {
// any normal pointer, i.e. &Type, &&Type etc;
// Note: voidptr, charptr and byteptr are NOT included!
return (int(t) >> 16) & 0xff > 0
}
[inline]
pub fn (typ Type) is_pointer() bool {
// builtin pointer types (voidptr, byteptr, charptr)
return typ.idx() in ast.pointer_type_idxs
}
[inline]
pub fn (typ Type) is_voidptr() bool {
return typ.idx() == ast.voidptr_type_idx
}
[inline]
pub fn (t Type) is_any_kind_of_pointer() bool {
return (int(t) >> 16) & 0xff > 0 || (u16(t) & 0xffff) in ast.pointer_type_idxs
}
// set nr_muls on `t` and return it
[inline]
pub fn (t Type) set_nr_muls(nr_muls int) Type {
if nr_muls < 0 || nr_muls > 255 {
panic('set_nr_muls: nr_muls must be between 0 & 255')
}
return int(t) & 0xff00ffff | int(u32(nr_muls) << 16)
}
// increments nr_muls on `t` and return it
[inline]
pub fn (t Type) ref() Type {
nr_muls := (int(t) >> 16) & 0xff
if nr_muls == 255 {
panic('ref: nr_muls is already at max of 255')
}
return int(t) & 0xff00ffff | int(u32(nr_muls + 1) << 16)
}
// decrement nr_muls on `t` and return it
[inline]
pub fn (t Type) deref() Type {
nr_muls := (int(t) >> 16) & 0xff
if nr_muls == 0 {
panic('deref: type `${t}` is not a pointer')
}
return int(t) & 0xff00ffff | int(u32(nr_muls - 1) << 16)
}
// set `flag` on `t` and return `t`
[inline]
pub fn (t Type) set_flag(flag TypeFlag) Type {
return int(t) | (1 << (int(flag) + 24))
}
// clear `flag` on `t` and return `t`
[inline]
pub fn (t Type) clear_flag(flag TypeFlag) Type {
return int(t) & ~(1 << (int(flag) + 24))
}
// clear all flags or multi flags
[inline]
pub fn (t Type) clear_flags(flags ...TypeFlag) Type {
if flags.len == 0 {
return int(t) & 0xffffff
} else {
mut typ := int(t)
for flag in flags {
typ = typ & ~(1 << (int(flag) + 24))
}
return typ
}
}
// return true if `flag` is set on `t`
[inline]
pub fn (t Type) has_flag(flag TypeFlag) bool {
return int(t) & (1 << (int(flag) + 24)) > 0
}
// debug returns a verbose representation of the information in ts, useful for tracing/debugging
pub fn (ts TypeSymbol) debug() []string {
mut res := []string{}
ts.dbg_common(mut res)
res << 'info: ${ts.info}'
res << 'methods (${ts.methods.len}): ' + ts.methods.map(it.str()).join(', ')
return res
}
// same as .debug(), but without the verbose .info and .methods fields
pub fn (ts TypeSymbol) dbg() []string {
mut res := []string{}
ts.dbg_common(mut res)
return res
}
fn (ts TypeSymbol) dbg_common(mut res []string) {
res << 'idx: 0x${ts.idx.hex()}'
res << 'parent_idx: 0x${ts.parent_idx.hex()}'
res << 'mod: ${ts.mod}'
res << 'name: ${ts.name}'
res << 'cname: ${ts.cname}'
res << 'kind: ${ts.kind}'
res << 'is_pub: ${ts.is_pub}'
res << 'language: ${ts.language}'
}
pub fn (t Type) str() string {
return 'ast.Type(0x${t.hex()} = ${u32(t)})'
}
pub fn (t &Table) type_str(typ Type) string {
return t.sym(typ).name
}
// debug returns a verbose representation of the information in the type `t`, useful for tracing/debugging
pub fn (t Type) debug() []string {
mut res := []string{}
res << 'idx: 0x${t.idx().hex():-8}'
res << 'type: 0x${t.hex():-8}'
res << 'nr_muls: ${t.nr_muls()}'
if t.has_flag(.option) {
res << 'option'
}
if t.has_flag(.result) {
res << 'result'
}
if t.has_flag(.variadic) {
res << 'variadic'
}
if t.has_flag(.generic) {
res << 'generic'
}
if t.has_flag(.shared_f) {
res << 'shared_f'
}
if t.has_flag(.atomic_f) {
res << 'atomic_f'
}
return res
}
// copy flags & nr_muls from `t_from` to `t` and return `t`
[inline]
pub fn (t Type) derive(t_from Type) Type {
return (0xffff0000 & t_from) | u16(t)
}
// copy flags from `t_from` to `t` and return `t`
[inline]
pub fn (t Type) derive_add_muls(t_from Type) Type {
return Type((0xff000000 & t_from) | u16(t)).set_nr_muls(t.nr_muls() + t_from.nr_muls())
}
// return new type with TypeSymbol idx set to `idx`
[inline]
pub fn new_type(idx int) Type {
if idx < 1 || idx > 65535 {
panic('new_type: idx must be between 1 & 65535')
}
return idx
}
// return new type with TypeSymbol idx set to `idx` & nr_muls set to `nr_muls`
[inline]
pub fn new_type_ptr(idx int, nr_muls int) Type {
if idx < 1 || idx > 65535 {
panic('new_type_ptr: idx must be between 1 & 65535')
}
if nr_muls < 0 || nr_muls > 255 {
panic('new_type_ptr: nr_muls must be between 0 & 255')
}
return (u32(nr_muls) << 16) | u16(idx)
}
[inline]
pub fn (typ Type) is_float() bool {
return !typ.is_ptr() && typ.idx() in ast.float_type_idxs
}
[inline]
pub fn (typ Type) is_int() bool {
return !typ.is_ptr() && typ.idx() in ast.integer_type_idxs
}
[inline]
pub fn (typ Type) is_int_valptr() bool {
return typ.is_ptr() && typ.idx() in ast.integer_type_idxs
}
[inline]
pub fn (typ Type) is_float_valptr() bool {
return typ.is_ptr() && typ.idx() in ast.float_type_idxs
}
[inline]
pub fn (typ Type) is_pure_int() bool {
return int(typ) in ast.integer_type_idxs
}
[inline]
pub fn (typ Type) is_pure_float() bool {
return int(typ) in ast.float_type_idxs
}
[inline]
pub fn (typ Type) is_signed() bool {
return typ.idx() in ast.signed_integer_type_idxs
}
[inline]
pub fn (typ Type) is_unsigned() bool {
return typ.idx() in ast.unsigned_integer_type_idxs
}
pub fn (typ Type) flip_signedness() Type {
return match typ {
ast.i8_type { ast.u8_type }
ast.i16_type { ast.u16_type }
ast.int_type { ast.u32_type }
ast.i32_type { ast.u32_type }
ast.isize_type { ast.usize_type }
ast.i64_type { ast.u64_type }
ast.u8_type { ast.i8_type }
ast.u16_type { ast.i16_type }
ast.u32_type { ast.int_type }
ast.usize_type { ast.isize_type }
ast.u64_type { ast.i64_type }
else { typ }
}
}
[inline]
pub fn (typ Type) is_int_literal() bool {
return int(typ) == ast.int_literal_type_idx
}
[inline]
pub fn (typ Type) is_number() bool {
return typ.clear_flags() in ast.number_type_idxs
}
[inline]
pub fn (typ Type) is_string() bool {
return typ.idx() == ast.string_type_idx
}
[inline]
pub fn (typ Type) is_bool() bool {
return typ.idx() == ast.bool_type_idx
}
pub const (
void_type_idx = 1
voidptr_type_idx = 2
byteptr_type_idx = 3
charptr_type_idx = 4
i8_type_idx = 5
i16_type_idx = 6
i32_type_idx = 7
int_type_idx = 8
i64_type_idx = 9
isize_type_idx = 10
u8_type_idx = 11
u16_type_idx = 12
u32_type_idx = 13
u64_type_idx = 14
usize_type_idx = 15
f32_type_idx = 16
f64_type_idx = 17
char_type_idx = 18
bool_type_idx = 19
none_type_idx = 20
string_type_idx = 21
rune_type_idx = 22
array_type_idx = 23
map_type_idx = 24
chan_type_idx = 25
any_type_idx = 26
float_literal_type_idx = 27
int_literal_type_idx = 28
thread_type_idx = 29
error_type_idx = 30
nil_type_idx = 31
)
// Note: builtin_type_names must be in the same order as the idx consts above
pub const builtin_type_names = ['void', 'voidptr', 'byteptr', 'charptr', 'i8', 'i16', 'int', 'i64',
'isize', 'u8', 'u16', 'u32', 'u64', 'usize', 'f32', 'f64', 'char', 'bool', 'none', 'string',
'rune', 'array', 'map', 'chan', 'any', 'float_literal', 'int_literal', 'thread', 'Error', 'nil',
'i32']
pub const builtin_type_names_matcher = token.new_keywords_matcher_from_array_trie(builtin_type_names)
pub const (
integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, u8_type_idx,
u16_type_idx, u32_type_idx, u64_type_idx, isize_type_idx, usize_type_idx,
int_literal_type_idx, rune_type_idx, i32_type_idx]
signed_integer_type_idxs = [char_type_idx, i8_type_idx, i16_type_idx, int_type_idx,
i64_type_idx, i32_type_idx, isize_type_idx]
unsigned_integer_type_idxs = [u8_type_idx, u16_type_idx, u32_type_idx, u64_type_idx,
usize_type_idx]
// C will promote any type smaller than int to int in an expression
int_promoted_type_idxs = [char_type_idx, i8_type_idx, i16_type_idx, u8_type_idx, u16_type_idx]
float_type_idxs = [f32_type_idx, f64_type_idx, float_literal_type_idx]
number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i32_type_idx, i64_type_idx,
u8_type_idx, char_type_idx, u16_type_idx, u32_type_idx, u64_type_idx, isize_type_idx,
usize_type_idx, f32_type_idx, f64_type_idx, int_literal_type_idx, float_literal_type_idx,
rune_type_idx]
pointer_type_idxs = [voidptr_type_idx, byteptr_type_idx, charptr_type_idx, nil_type_idx]
)
pub const (
void_type = new_type(void_type_idx)
ovoid_type = new_type(void_type_idx).set_flag(.option) // the return type of `fn ()?`
rvoid_type = new_type(void_type_idx).set_flag(.result) // the return type of `fn () !`
voidptr_type = new_type(voidptr_type_idx)
byteptr_type = new_type(byteptr_type_idx)
charptr_type = new_type(charptr_type_idx)
i8_type = new_type(i8_type_idx)
i16_type = new_type(i16_type_idx)
i32_type = new_type(i32_type_idx)
int_type = new_type(int_type_idx)
i64_type = new_type(i64_type_idx)
isize_type = new_type(isize_type_idx)
u8_type = new_type(u8_type_idx)
u16_type = new_type(u16_type_idx)
u32_type = new_type(u32_type_idx)
u64_type = new_type(u64_type_idx)
usize_type = new_type(usize_type_idx)
f32_type = new_type(f32_type_idx)
f64_type = new_type(f64_type_idx)
char_type = new_type(char_type_idx)
bool_type = new_type(bool_type_idx)
none_type = new_type(none_type_idx)
string_type = new_type(string_type_idx)
rune_type = new_type(rune_type_idx)
array_type = new_type(array_type_idx)
map_type = new_type(map_type_idx)
chan_type = new_type(chan_type_idx)
any_type = new_type(any_type_idx)
float_literal_type = new_type(float_literal_type_idx)
int_literal_type = new_type(int_literal_type_idx)
thread_type = new_type(thread_type_idx)
error_type = new_type(error_type_idx)
charptr_types = new_charptr_types()
byteptr_types = new_byteptr_types()
voidptr_types = new_voidptr_types()
cptr_types = merge_types(voidptr_types, byteptr_types, charptr_types)
nil_type = new_type(nil_type_idx)
)
fn new_charptr_types() []Type {
return [ast.charptr_type, new_type(ast.char_type_idx).set_nr_muls(1)]
}
fn new_byteptr_types() []Type {
return [ast.byteptr_type, new_type(ast.u8_type_idx).set_nr_muls(1)]
}
fn new_voidptr_types() []Type {
return [ast.voidptr_type, new_type(ast.voidptr_type_idx).set_nr_muls(1)]
}
pub fn merge_types(params ...[]Type) []Type {
mut res := []Type{cap: params.len}
for types in params {
for t in types {
if t !in res {
res << t
}
}
}
return res
}
pub fn mktyp(typ Type) Type {
return match typ {
ast.float_literal_type { ast.f64_type }
ast.int_literal_type { ast.int_type }
else { typ }
}
}
// returns TypeSymbol kind only if there are no type modifiers
pub fn (t &Table) type_kind(typ Type) Kind {
if typ.nr_muls() > 0 || typ.has_flag(.option) || typ.has_flag(.result) {
return Kind.placeholder
}
return t.sym(typ).kind
}
pub fn (t &Table) type_is_for_pointer_arithmetic(typ Type) bool {
if t.sym(typ).kind == .struct_ {
return false
} else {
return typ.is_any_kind_of_pointer() || typ.is_int_valptr()
}
}
pub enum Kind {
placeholder
void
voidptr
byteptr
charptr
i8
i16
i32
int
i64
isize
u8
u16
u32
u64
usize
f32
f64
char
rune
bool
none_
string
array
array_fixed
map
chan
any
struct_
generic_inst
multi_return
sum_type
alias
enum_
function
interface_
float_literal
int_literal
aggregate
thread
}
pub fn (t TypeSymbol) str() string {
return t.name
}
[noreturn]
fn (t &TypeSymbol) no_info_panic(fname string) {
panic('${fname}: no info for type: ${t.name}')
}
[inline]
pub fn (t &TypeSymbol) enum_info() Enum {
if t.info is Enum {
return t.info
}
if t.info is Alias {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is Enum {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
[inline]
pub fn (t &TypeSymbol) mr_info() MultiReturn {
if t.info is MultiReturn {
return t.info
}
if t.info is Alias {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is MultiReturn {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
[inline]
pub fn (t &TypeSymbol) array_info() Array {
if t.info is Array {
return t.info
}
if t.info is Alias {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is Array {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
[inline]
pub fn (t &TypeSymbol) array_fixed_info() ArrayFixed {
if t.info is ArrayFixed {
return t.info
}
if t.info is Alias {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is ArrayFixed {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
[inline]
pub fn (t &TypeSymbol) chan_info() Chan {
if t.info is Chan {
return t.info
}
if t.info is Alias {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is Chan {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
[inline]
pub fn (t &TypeSymbol) thread_info() Thread {
if t.info is Thread {
return t.info
}
if t.info is Alias {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is Thread {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
[inline]
pub fn (t &TypeSymbol) map_info() Map {
if t.info is Map {
return t.info
}
if t.info is Alias {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is Map {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
[inline]
pub fn (t &TypeSymbol) struct_info() Struct {
if t.info is Struct {
return t.info
}
if t.info is Alias {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is Struct {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
[inline]
pub fn (t &TypeSymbol) sumtype_info() SumType {
if t.info is SumType {
return t.info
}
if t.info is SumType {
fsym := global_table.final_sym(t.info.parent_type)
if fsym.info is SumType {
return fsym.info
}
}
t.no_info_panic(@METHOD)
}
pub fn (t &TypeSymbol) is_heap() bool {
if t.info is Struct {
return t.info.is_heap
} else {
return false
}
}
pub fn (t &ArrayFixed) is_compatible(t2 ArrayFixed) bool {
return t.size == t2.size && t.elem_type == t2.elem_type
}
pub fn (t &TypeSymbol) is_array_fixed() bool {
if t.info is ArrayFixed {
return true
} else if t.info is Alias {
return global_table.final_sym(t.info.parent_type).is_array_fixed()
} else {
return false
}
}
pub fn (t &TypeSymbol) is_array_fixed_ret() bool {
if t.info is ArrayFixed {
return t.info.is_fn_ret
} else if t.info is Alias {
return global_table.final_sym(t.info.parent_type).is_array_fixed_ret()
} else {
return false
}
}
pub fn (mut t Table) register_builtin_type_symbols() {
// reserve index 0 so nothing can go there
// save index check, 0 will mean not found
// THE ORDER MUST BE THE SAME AS xxx_type_idx CONSTS EARLIER IN THIS FILE
t.register_sym(kind: .placeholder, name: 'reserved_0')
t.register_sym(kind: .void, name: 'void', cname: 'void', mod: 'builtin') // 1
t.register_sym(kind: .voidptr, name: 'voidptr', cname: 'voidptr', mod: 'builtin') // 2
t.register_sym(kind: .byteptr, name: 'byteptr', cname: 'byteptr', mod: 'builtin') // 3
t.register_sym(kind: .charptr, name: 'charptr', cname: 'charptr', mod: 'builtin') // 4
t.register_sym(kind: .i8, name: 'i8', cname: 'i8', mod: 'builtin') // 5
t.register_sym(kind: .i16, name: 'i16', cname: 'i16', mod: 'builtin') // 6
t.register_sym(kind: .i32, name: 'i32', cname: 'i32', mod: 'builtin') // 7
t.register_sym(kind: .int, name: 'int', cname: int_type_name, mod: 'builtin') // 8
t.register_sym(kind: .i64, name: 'i64', cname: 'i64', mod: 'builtin') // 9
t.register_sym(kind: .isize, name: 'isize', cname: 'isize', mod: 'builtin') // 10
t.register_sym(kind: .u8, name: 'u8', cname: 'u8', mod: 'builtin') // 11
t.register_sym(kind: .u16, name: 'u16', cname: 'u16', mod: 'builtin') // 12
t.register_sym(kind: .u32, name: 'u32', cname: 'u32', mod: 'builtin') // 13
t.register_sym(kind: .u64, name: 'u64', cname: 'u64', mod: 'builtin') // 14
t.register_sym(kind: .usize, name: 'usize', cname: 'usize', mod: 'builtin') // 15
t.register_sym(kind: .f32, name: 'f32', cname: 'f32', mod: 'builtin') // 16
t.register_sym(kind: .f64, name: 'f64', cname: 'f64', mod: 'builtin') // 17
t.register_sym(kind: .char, name: 'char', cname: 'char', mod: 'builtin') // 18
t.register_sym(kind: .bool, name: 'bool', cname: 'bool', mod: 'builtin') // 19
t.register_sym(kind: .none_, name: 'none', cname: 'none', mod: 'builtin') // 20
t.register_sym(kind: .string, name: 'string', cname: 'string', mod: 'builtin') // 21
t.register_sym(kind: .rune, name: 'rune', cname: 'rune', mod: 'builtin') // 22
t.register_sym(kind: .array, name: 'array', cname: 'array', mod: 'builtin') // 23
t.register_sym(kind: .map, name: 'map', cname: 'map', mod: 'builtin') // 24
t.register_sym(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin') // 25
t.register_sym(kind: .any, name: 'any', cname: 'any', mod: 'builtin') // 26
t.register_sym(
kind: .float_literal
name: 'float literal'
cname: 'float_literal'
mod: 'builtin'
) // 27
t.register_sym(
kind: .int_literal
name: 'int literal'
cname: 'int_literal'
mod: 'builtin'
) // 28
t.register_sym(
kind: .thread
name: 'thread'
cname: '__v_thread'
mod: 'builtin'
info: Thread{
return_type: ast.void_type
}
) // 29
t.register_sym(kind: .interface_, name: 'IError', cname: 'IError', mod: 'builtin') // 30
t.register_sym(kind: .voidptr, name: 'nil', cname: 'voidptr', mod: 'builtin') // 31
}
[inline]
pub fn (t &TypeSymbol) is_pointer() bool {
return t.kind in [.byteptr, .charptr, .voidptr]
}
[inline]
pub fn (t &TypeSymbol) is_int() bool {
res := t.kind in [.i8, .i16, .int, .i64, .i32, .isize, .u8, .u16, .u32, .u64, .usize,
.int_literal, .rune]
if !res && t.kind == .alias {
return (t.info as Alias).parent_type.is_number()
}
return res
}
[inline]
pub fn (t &TypeSymbol) is_float() bool {
return t.kind in [.f32, .f64, .float_literal]
}
[inline]
pub fn (t &TypeSymbol) is_string() bool {
return t.kind == .string
}