Repository navigation
Expand file tree
/
Copy pathtypes.cpp
More file actions
5311 lines (4675 loc) · 149 KB
/
Copy pathtypes.cpp
File metadata and controls
5311 lines (4675 loc) · 149 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
struct Ast;
struct Scope;
struct Entity;
enum BasicKind {
Basic_Invalid,
Basic_llvm_bool,
Basic_bool,
Basic_b8,
Basic_b16,
Basic_b32,
Basic_b64,
Basic_i8,
Basic_u8,
Basic_i16,
Basic_u16,
Basic_i32,
Basic_u32,
Basic_i64,
Basic_u64,
Basic_i128,
Basic_u128,
Basic_rune,
Basic_f16,
Basic_f32,
Basic_f64,
Basic_complex32,
Basic_complex64,
Basic_complex128,
Basic_quaternion64,
Basic_quaternion128,
Basic_quaternion256,
Basic_int,
Basic_uint,
Basic_uintptr,
Basic_rawptr,
Basic_string, // [^]u8 + int
Basic_cstring, // [^]u8
Basic_string16, // [^]u16 + int
Basic_cstring16, // [^]u16 + int
Basic_any, // rawptr + ^Type_Info
Basic_typeid,
// Endian Specific Types
Basic_i16le,
Basic_u16le,
Basic_i32le,
Basic_u32le,
Basic_i64le,
Basic_u64le,
Basic_i128le,
Basic_u128le,
Basic_i16be,
Basic_u16be,
Basic_i32be,
Basic_u32be,
Basic_i64be,
Basic_u64be,
Basic_i128be,
Basic_u128be,
Basic_f16le,
Basic_f32le,
Basic_f64le,
Basic_f16be,
Basic_f32be,
Basic_f64be,
// Untyped types
Basic_UntypedBool,
Basic_UntypedInteger,
Basic_UntypedFloat,
Basic_UntypedComplex,
Basic_UntypedQuaternion,
Basic_UntypedString,
Basic_UntypedRune,
Basic_UntypedNil,
Basic_UntypedUninit,
Basic_COUNT,
Basic_byte = Basic_u8,
};
enum BasicFlag {
BasicFlag_Boolean = GB_BIT(0),
BasicFlag_Integer = GB_BIT(1),
BasicFlag_Unsigned = GB_BIT(2),
BasicFlag_Float = GB_BIT(3),
BasicFlag_Complex = GB_BIT(4),
BasicFlag_Quaternion = GB_BIT(5),
BasicFlag_Pointer = GB_BIT(6),
BasicFlag_String = GB_BIT(7),
BasicFlag_Rune = GB_BIT(8),
BasicFlag_Untyped = GB_BIT(9),
BasicFlag_LLVM = GB_BIT(11),
BasicFlag_EndianLittle = GB_BIT(13),
BasicFlag_EndianBig = GB_BIT(14),
BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float | BasicFlag_Complex | BasicFlag_Quaternion,
BasicFlag_Ordered = BasicFlag_Integer | BasicFlag_Float | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune,
BasicFlag_OrderedNumeric = BasicFlag_Integer | BasicFlag_Float | BasicFlag_Rune,
BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer | BasicFlag_Rune,
BasicFlag_SimpleCompare = BasicFlag_Boolean | BasicFlag_Integer | BasicFlag_Pointer | BasicFlag_Rune,
};
struct BasicType {
BasicKind kind;
u32 flags;
i64 size; // -1 if arch. dep.
String name;
};
enum StructSoaKind : u8 {
StructSoa_None = 0,
StructSoa_Fixed = 1,
StructSoa_Slice = 2,
StructSoa_Dynamic = 3,
};
struct TypeStruct {
Slice<Entity *> fields;
String * tags; // count == fields.count
i64 * offsets; // count == fields.count
Ast * node;
Scope * scope;
i64 custom_align;
i64 custom_min_field_align;
i64 custom_max_field_align;
Type * polymorphic_params; // Type_Tuple
Type * polymorphic_parent;
Wait_Signal polymorphic_wait_signal;
Type * soa_elem;
i32 soa_count;
StructSoaKind soa_kind;
Wait_Signal fields_wait_signal;
BlockingMutex soa_mutex;
BlockingMutex offset_mutex; // for settings offsets
bool is_polymorphic;
bool are_offsets_set : 1;
bool is_packed : 1;
bool is_raw_union : 1;
bool is_poly_specialized : 1;
std::atomic<bool> are_offsets_being_processed;
};
struct TypeUnion {
Slice<Type *> variants;
Ast * node;
Scope * scope;
i64 variant_block_size;
i64 custom_align;
Type * polymorphic_params; // Type_Tuple
Type * polymorphic_parent;
Wait_Signal polymorphic_wait_signal;
i16 tag_size;
bool is_polymorphic;
bool is_poly_specialized;
UnionTypeKind kind;
};
struct TypeProc {
Ast *node;
Scope * scope;
Type * params; // Type_Tuple
Type * results; // Type_Tuple
i32 result_count;
isize specialization_count;
ProcCallingConvention calling_convention;
i32 variadic_index;
String require_target_feature;
String enable_target_feature;
// TODO(bill): Make this a flag set rather than bools
bool variadic;
bool require_results;
bool c_vararg;
bool is_polymorphic;
bool is_poly_specialized;
bool has_named_results;
bool diverging; // no return
bool return_by_pointer;
bool optional_ok;
};
struct TypeNamed {
String name;
Type * base;
Entity *type_name; /* Entity_TypeName */
BlockingMutex gen_types_data_mutex;
GenTypesData *gen_types_data;
};
#define TYPE_KINDS \
TYPE_KIND(Basic, BasicType) \
TYPE_KIND(Named, TypeNamed) \
TYPE_KIND(Generic, struct { \
i64 id; \
String name; \
Type * specialized; \
Scope * scope; \
Entity *entity; \
}) \
TYPE_KIND(Pointer, struct { Type *elem; }) \
TYPE_KIND(MultiPointer, struct { Type *elem; }) \
TYPE_KIND(Array, struct { \
Type *elem; \
i64 count; \
Type *generic_count; \
}) \
TYPE_KIND(EnumeratedArray, struct { \
Type *elem; \
Type *index; \
ExactValue *min_value; \
ExactValue *max_value; \
i64 count; \
TokenKind op; \
bool is_sparse; \
}) \
TYPE_KIND(Slice, struct { Type *elem; }) \
TYPE_KIND(DynamicArray, struct { Type *elem; }) \
TYPE_KIND(Map, struct { \
Type *key; \
Type *value; \
Type *lookup_result_type; \
Type *debug_metadata_type; \
}) \
TYPE_KIND(Struct, TypeStruct) \
TYPE_KIND(Union, TypeUnion) \
TYPE_KIND(Enum, struct { \
Array<Entity *> fields; \
Ast *node; \
Scope * scope; \
Type * base_type; \
ExactValue *min_value; \
ExactValue *max_value; \
isize min_value_index; \
isize max_value_index; \
}) \
TYPE_KIND(Tuple, struct { \
Slice<Entity *> variables; /* Entity_Variable */ \
i64 * offsets; \
BlockingMutex mutex; /* for settings offsets */ \
std::atomic<bool> are_offsets_being_processed; \
bool are_offsets_set; \
bool is_packed; \
}) \
TYPE_KIND(Proc, TypeProc) \
TYPE_KIND(BitSet, struct { \
Type *elem; \
Type *underlying; \
i64 lower; \
i64 upper; \
Ast * node; \
}) \
TYPE_KIND(SimdVector, struct { \
i64 count; \
Type *elem; \
Type *generic_count; \
}) \
TYPE_KIND(Matrix, struct { \
Type *elem; \
i64 row_count; \
i64 column_count; \
Type *generic_row_count; \
Type *generic_column_count; \
i64 stride_in_bytes; \
bool is_row_major; \
}) \
TYPE_KIND(BitField, struct { \
Scope * scope; \
Type * backing_type; \
Slice<Entity *> fields; \
String * tags; /*count == fields.count*/ \
Slice<u8> bit_sizes; \
Slice<i64> bit_offsets; \
Ast * node; \
}) \
TYPE_KIND(SoaPointer, struct { Type *elem; })
enum TypeKind {
Type_Invalid,
#define TYPE_KIND(k, ...) GB_JOIN2(Type_, k),
TYPE_KINDS
#undef TYPE_KIND
Type_Count,
};
gb_global String const type_strings[] = {
{cast(u8 *)"Invalid", gb_size_of("Invalid")},
#define TYPE_KIND(k, ...) {cast(u8 *)#k, gb_size_of(#k)-1},
TYPE_KINDS
#undef TYPE_KIND
};
#define TYPE_KIND(k, ...) typedef __VA_ARGS__ GB_JOIN2(Type, k);
TYPE_KINDS
#undef TYPE_KIND
enum TypeFlag : u32 {
TypeFlag_Polymorphic = 1<<1,
TypeFlag_PolySpecialized = 1<<2,
TypeFlag_InProcessOfCheckingPolymorphic = 1<<3,
};
struct Type {
TypeKind kind;
union {
#define TYPE_KIND(k, ...) GB_JOIN2(Type, k) k;
TYPE_KINDS
#undef TYPE_KIND
};
// NOTE(bill): These need to be at the end to not affect the unionized data
std::atomic<i64> cached_size;
std::atomic<i64> cached_align;
std::atomic<u64> canonical_hash;
std::atomic<u32> flags; // TypeFlag
bool failure;
};
// IMPORTANT NOTE(bill): This must match the same as the in core.odin
enum Typeid_Kind : u8 {
Typeid_Invalid,
Typeid_Integer,
Typeid_Rune,
Typeid_Float,
Typeid_Complex,
Typeid_Quaternion,
Typeid_String,
Typeid_Boolean,
Typeid_Any,
Typeid_Type_Id,
Typeid_Pointer,
Typeid_Multi_Pointer,
Typeid_Procedure,
Typeid_Array,
Typeid_Enumerated_Array,
Typeid_Dynamic_Array,
Typeid_Slice,
Typeid_Tuple,
Typeid_Struct,
Typeid_Union,
Typeid_Enum,
Typeid_Map,
Typeid_Bit_Set,
Typeid_Simd_Vector,
Typeid_Matrix,
Typeid_SoaPointer,
Typeid_Bit_Field,
Typeid__COUNT
};
// IMPORTANT NOTE(bill): This must match the same as the in core.odin
enum TypeInfoFlag : u32 {
TypeInfoFlag_Comparable = 1<<0,
TypeInfoFlag_Simple_Compare = 1<<1,
};
enum : int {
MATRIX_ELEMENT_COUNT_MIN = 1,
MATRIX_ELEMENT_COUNT_MAX = 16,
MATRIX_ELEMENT_MAX_SIZE = MATRIX_ELEMENT_COUNT_MAX * (2 * 8), // complex128
SIMD_ELEMENT_COUNT_MIN = 1,
SIMD_ELEMENT_COUNT_MAX = 64,
};
gb_internal bool is_type_comparable(Type *t);
gb_internal bool is_type_simple_compare(Type *t);
gb_internal Type *type_deref(Type *t, bool allow_multi_pointer=false);
gb_internal Type *base_type(Type *t);
gb_internal Type *alloc_type_multi_pointer(Type *elem);
gb_internal u32 type_info_flags_of_type(Type *type) {
if (type == nullptr) {
return 0;
}
u32 flags = 0;
if (is_type_comparable(type)) {
flags |= TypeInfoFlag_Comparable;
}
if (is_type_simple_compare(type)) {
flags |= TypeInfoFlag_Comparable|TypeInfoFlag_Simple_Compare;
}
return flags;
}
// TODO(bill): Should I add extra information here specifying the kind of selection?
// e.g. field, constant, array field, type field, etc.
struct Selection {
Entity * entity;
Array<i32> index;
bool indirect; // Set if there was a pointer deref anywhere down the line
u8 swizzle_count; // maximum components = 4
u8 swizzle_indices; // 2 bits per component, representing which swizzle index
bool is_bit_field;
bool pseudo_field;
};
gb_global Selection const empty_selection = {0};
gb_internal Selection make_selection(Entity *entity, Array<i32> index, bool indirect) {
Selection s = {entity, index, indirect};
return s;
}
gb_internal void selection_add_index(Selection *s, isize index) {
if (s->index.data == nullptr) {
array_init(&s->index, permanent_allocator());
}
array_add(&s->index, cast(i32)index);
}
gb_internal Selection selection_combine(Selection const &lhs, Selection const &rhs) {
Selection new_sel = lhs;
new_sel.indirect = lhs.indirect || rhs.indirect;
new_sel.index = array_make<i32>(permanent_allocator(), lhs.index.count+rhs.index.count);
array_copy(&new_sel.index, lhs.index, 0);
array_copy(&new_sel.index, rhs.index, lhs.index.count);
return new_sel;
}
gb_internal Selection sub_selection(Selection const &sel, isize offset) {
Selection res = {};
res.index.data = sel.index.data + offset;
res.index.count = gb_max(sel.index.count - offset, 0);
res.index.capacity = res.index.count;
return res;
}
gb_internal Selection trim_selection(Selection const &sel) {
Selection res = {};
res.index.data = sel.index.data;
res.index.count = gb_max(sel.index.count - 1, 0);
res.index.capacity = res.index.count;
return res;
}
gb_global Type basic_types[] = {
{Type_Basic, {Basic_Invalid, 0, 0, STR_LIT("invalid type")}},
{Type_Basic, {Basic_llvm_bool, BasicFlag_Boolean | BasicFlag_LLVM, 1, STR_LIT("llvm bool")}},
{Type_Basic, {Basic_bool, BasicFlag_Boolean, 1, STR_LIT("bool")}},
{Type_Basic, {Basic_b8, BasicFlag_Boolean, 1, STR_LIT("b8")}},
{Type_Basic, {Basic_b16, BasicFlag_Boolean, 2, STR_LIT("b16")}},
{Type_Basic, {Basic_b32, BasicFlag_Boolean, 4, STR_LIT("b32")}},
{Type_Basic, {Basic_b64, BasicFlag_Boolean, 8, STR_LIT("b64")}},
{Type_Basic, {Basic_i8, BasicFlag_Integer, 1, STR_LIT("i8")}},
{Type_Basic, {Basic_u8, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("u8")}},
{Type_Basic, {Basic_i16, BasicFlag_Integer, 2, STR_LIT("i16")}},
{Type_Basic, {Basic_u16, BasicFlag_Integer | BasicFlag_Unsigned, 2, STR_LIT("u16")}},
{Type_Basic, {Basic_i32, BasicFlag_Integer, 4, STR_LIT("i32")}},
{Type_Basic, {Basic_u32, BasicFlag_Integer | BasicFlag_Unsigned, 4, STR_LIT("u32")}},
{Type_Basic, {Basic_i64, BasicFlag_Integer, 8, STR_LIT("i64")}},
{Type_Basic, {Basic_u64, BasicFlag_Integer | BasicFlag_Unsigned, 8, STR_LIT("u64")}},
{Type_Basic, {Basic_i128, BasicFlag_Integer, 16, STR_LIT("i128")}},
{Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, 16, STR_LIT("u128")}},
{Type_Basic, {Basic_rune, BasicFlag_Integer | BasicFlag_Rune, 4, STR_LIT("rune")}},
{Type_Basic, {Basic_f16, BasicFlag_Float, 2, STR_LIT("f16")}},
{Type_Basic, {Basic_f32, BasicFlag_Float, 4, STR_LIT("f32")}},
{Type_Basic, {Basic_f64, BasicFlag_Float, 8, STR_LIT("f64")}},
{Type_Basic, {Basic_complex32, BasicFlag_Complex, 4, STR_LIT("complex32")}},
{Type_Basic, {Basic_complex64, BasicFlag_Complex, 8, STR_LIT("complex64")}},
{Type_Basic, {Basic_complex128, BasicFlag_Complex, 16, STR_LIT("complex128")}},
{Type_Basic, {Basic_quaternion64, BasicFlag_Quaternion, 8, STR_LIT("quaternion64")}},
{Type_Basic, {Basic_quaternion128, BasicFlag_Quaternion, 16, STR_LIT("quaternion128")}},
{Type_Basic, {Basic_quaternion256, BasicFlag_Quaternion, 32, STR_LIT("quaternion256")}},
{Type_Basic, {Basic_int, BasicFlag_Integer, -1, STR_LIT("int")}},
{Type_Basic, {Basic_uint, BasicFlag_Integer | BasicFlag_Unsigned, -1, STR_LIT("uint")}},
{Type_Basic, {Basic_uintptr, BasicFlag_Integer | BasicFlag_Unsigned, -1, STR_LIT("uintptr")}},
{Type_Basic, {Basic_rawptr, BasicFlag_Pointer, -1, STR_LIT("rawptr")}},
{Type_Basic, {Basic_string, BasicFlag_String, -1, STR_LIT("string")}},
{Type_Basic, {Basic_cstring, BasicFlag_String, -1, STR_LIT("cstring")}},
{Type_Basic, {Basic_string16, BasicFlag_String, -1, STR_LIT("string16")}},
{Type_Basic, {Basic_cstring16, BasicFlag_String, -1, STR_LIT("cstring16")}},
{Type_Basic, {Basic_any, 0, 16, STR_LIT("any")}},
{Type_Basic, {Basic_typeid, 0, 8, STR_LIT("typeid")}},
// Endian
{Type_Basic, {Basic_i16le, BasicFlag_Integer | BasicFlag_EndianLittle, 2, STR_LIT("i16le")}},
{Type_Basic, {Basic_u16le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 2, STR_LIT("u16le")}},
{Type_Basic, {Basic_i32le, BasicFlag_Integer | BasicFlag_EndianLittle, 4, STR_LIT("i32le")}},
{Type_Basic, {Basic_u32le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 4, STR_LIT("u32le")}},
{Type_Basic, {Basic_i64le, BasicFlag_Integer | BasicFlag_EndianLittle, 8, STR_LIT("i64le")}},
{Type_Basic, {Basic_u64le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 8, STR_LIT("u64le")}},
{Type_Basic, {Basic_i128le, BasicFlag_Integer | BasicFlag_EndianLittle, 16, STR_LIT("i128le")}},
{Type_Basic, {Basic_u128le, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianLittle, 16, STR_LIT("u128le")}},
{Type_Basic, {Basic_i16be, BasicFlag_Integer | BasicFlag_EndianBig, 2, STR_LIT("i16be")}},
{Type_Basic, {Basic_u16be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 2, STR_LIT("u16be")}},
{Type_Basic, {Basic_i32be, BasicFlag_Integer | BasicFlag_EndianBig, 4, STR_LIT("i32be")}},
{Type_Basic, {Basic_u32be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 4, STR_LIT("u32be")}},
{Type_Basic, {Basic_i64be, BasicFlag_Integer | BasicFlag_EndianBig, 8, STR_LIT("i64be")}},
{Type_Basic, {Basic_u64be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 8, STR_LIT("u64be")}},
{Type_Basic, {Basic_i128be, BasicFlag_Integer | BasicFlag_EndianBig, 16, STR_LIT("i128be")}},
{Type_Basic, {Basic_u128be, BasicFlag_Integer | BasicFlag_Unsigned | BasicFlag_EndianBig, 16, STR_LIT("u128be")}},
{Type_Basic, {Basic_f16le, BasicFlag_Float | BasicFlag_EndianLittle, 2, STR_LIT("f16le")}},
{Type_Basic, {Basic_f32le, BasicFlag_Float | BasicFlag_EndianLittle, 4, STR_LIT("f32le")}},
{Type_Basic, {Basic_f64le, BasicFlag_Float | BasicFlag_EndianLittle, 8, STR_LIT("f64le")}},
{Type_Basic, {Basic_f16be, BasicFlag_Float | BasicFlag_EndianBig, 2, STR_LIT("f16be")}},
{Type_Basic, {Basic_f32be, BasicFlag_Float | BasicFlag_EndianBig, 4, STR_LIT("f32be")}},
{Type_Basic, {Basic_f64be, BasicFlag_Float | BasicFlag_EndianBig, 8, STR_LIT("f64be")}},
// Untyped types
{Type_Basic, {Basic_UntypedBool, BasicFlag_Boolean | BasicFlag_Untyped, 0, STR_LIT("untyped bool")}},
{Type_Basic, {Basic_UntypedInteger, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped integer")}},
{Type_Basic, {Basic_UntypedFloat, BasicFlag_Float | BasicFlag_Untyped, 0, STR_LIT("untyped float")}},
{Type_Basic, {Basic_UntypedComplex, BasicFlag_Complex | BasicFlag_Untyped, 0, STR_LIT("untyped complex")}},
{Type_Basic, {Basic_UntypedQuaternion, BasicFlag_Quaternion | BasicFlag_Untyped, 0, STR_LIT("untyped quaternion")}},
{Type_Basic, {Basic_UntypedString, BasicFlag_String | BasicFlag_Untyped, 0, STR_LIT("untyped string")}},
{Type_Basic, {Basic_UntypedRune, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped rune")}},
{Type_Basic, {Basic_UntypedNil, BasicFlag_Untyped, 0, STR_LIT("untyped nil")}},
{Type_Basic, {Basic_UntypedUninit, BasicFlag_Untyped, 0, STR_LIT("untyped uninitialized")}},
};
// gb_global Type basic_type_aliases[] = {
// // {Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, 1, STR_LIT("byte")}},
// // {Type_Basic, {Basic_rune, BasicFlag_Integer, 4, STR_LIT("rune")}},
// };
gb_global Type *t_invalid = &basic_types[Basic_Invalid];
gb_global Type *t_llvm_bool = &basic_types[Basic_llvm_bool];
gb_global Type *t_bool = &basic_types[Basic_bool];
gb_global Type *t_i8 = &basic_types[Basic_i8];
gb_global Type *t_u8 = &basic_types[Basic_u8];
gb_global Type *t_i16 = &basic_types[Basic_i16];
gb_global Type *t_u16 = &basic_types[Basic_u16];
gb_global Type *t_i32 = &basic_types[Basic_i32];
gb_global Type *t_u32 = &basic_types[Basic_u32];
gb_global Type *t_i64 = &basic_types[Basic_i64];
gb_global Type *t_u64 = &basic_types[Basic_u64];
gb_global Type *t_i128 = &basic_types[Basic_i128];
gb_global Type *t_u128 = &basic_types[Basic_u128];
gb_global Type *t_rune = &basic_types[Basic_rune];
gb_global Type *t_f16 = &basic_types[Basic_f16];
gb_global Type *t_f32 = &basic_types[Basic_f32];
gb_global Type *t_f64 = &basic_types[Basic_f64];
gb_global Type *t_f16be = &basic_types[Basic_f16be];
gb_global Type *t_f32be = &basic_types[Basic_f32be];
gb_global Type *t_f64be = &basic_types[Basic_f64be];
gb_global Type *t_f16le = &basic_types[Basic_f16le];
gb_global Type *t_f32le = &basic_types[Basic_f32le];
gb_global Type *t_f64le = &basic_types[Basic_f64le];
gb_global Type *t_complex32 = &basic_types[Basic_complex32];
gb_global Type *t_complex64 = &basic_types[Basic_complex64];
gb_global Type *t_complex128 = &basic_types[Basic_complex128];
gb_global Type *t_quaternion64 = &basic_types[Basic_quaternion64];
gb_global Type *t_quaternion128 = &basic_types[Basic_quaternion128];
gb_global Type *t_quaternion256 = &basic_types[Basic_quaternion256];
gb_global Type *t_int = &basic_types[Basic_int];
gb_global Type *t_uint = &basic_types[Basic_uint];
gb_global Type *t_uintptr = &basic_types[Basic_uintptr];
gb_global Type *t_rawptr = &basic_types[Basic_rawptr];
gb_global Type *t_string = &basic_types[Basic_string];
gb_global Type *t_cstring = &basic_types[Basic_cstring];
gb_global Type *t_string16 = &basic_types[Basic_string16];
gb_global Type *t_cstring16 = &basic_types[Basic_cstring16];
gb_global Type *t_any = &basic_types[Basic_any];
gb_global Type *t_typeid = &basic_types[Basic_typeid];
gb_global Type *t_i16le = &basic_types[Basic_i16le];
gb_global Type *t_u16le = &basic_types[Basic_u16le];
gb_global Type *t_i32le = &basic_types[Basic_i32le];
gb_global Type *t_u32le = &basic_types[Basic_u32le];
gb_global Type *t_i64le = &basic_types[Basic_i64le];
gb_global Type *t_u64le = &basic_types[Basic_u64le];
gb_global Type *t_i128le = &basic_types[Basic_i128le];
gb_global Type *t_u128le = &basic_types[Basic_u128le];
gb_global Type *t_i16be = &basic_types[Basic_i16be];
gb_global Type *t_u16be = &basic_types[Basic_u16be];
gb_global Type *t_i32be = &basic_types[Basic_i32be];
gb_global Type *t_u32be = &basic_types[Basic_u32be];
gb_global Type *t_i64be = &basic_types[Basic_i64be];
gb_global Type *t_u64be = &basic_types[Basic_u64be];
gb_global Type *t_i128be = &basic_types[Basic_i128be];
gb_global Type *t_u128be = &basic_types[Basic_u128be];
gb_global Type *t_untyped_bool = &basic_types[Basic_UntypedBool];
gb_global Type *t_untyped_integer = &basic_types[Basic_UntypedInteger];
gb_global Type *t_untyped_float = &basic_types[Basic_UntypedFloat];
gb_global Type *t_untyped_complex = &basic_types[Basic_UntypedComplex];
gb_global Type *t_untyped_quaternion = &basic_types[Basic_UntypedQuaternion];
gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString];
gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune];
gb_global Type *t_untyped_nil = &basic_types[Basic_UntypedNil];
gb_global Type *t_untyped_uninit = &basic_types[Basic_UntypedUninit];
gb_global Type *t_u8_ptr = nullptr;
gb_global Type *t_u8_multi_ptr = nullptr;
gb_global Type *t_u16_ptr = nullptr;
gb_global Type *t_u16_multi_ptr = nullptr;
gb_global Type *t_int_ptr = nullptr;
gb_global Type *t_i64_ptr = nullptr;
gb_global Type *t_f64_ptr = nullptr;
gb_global Type *t_u8_slice = nullptr;
gb_global Type *t_string_slice = nullptr;
// Type generated for the "preload" file
gb_global Type *t_type_info = nullptr;
gb_global Type *t_type_info_enum_value = nullptr;
gb_global Type *t_type_info_ptr = nullptr;
gb_global Type *t_type_info_enum_value_ptr = nullptr;
gb_global Type *t_type_info_string_encoding_kind = nullptr;
gb_global Type *t_type_info_named = nullptr;
gb_global Type *t_type_info_integer = nullptr;
gb_global Type *t_type_info_rune = nullptr;
gb_global Type *t_type_info_float = nullptr;
gb_global Type *t_type_info_complex = nullptr;
gb_global Type *t_type_info_quaternion = nullptr;
gb_global Type *t_type_info_any = nullptr;
gb_global Type *t_type_info_typeid = nullptr;
gb_global Type *t_type_info_string = nullptr;
gb_global Type *t_type_info_boolean = nullptr;
gb_global Type *t_type_info_pointer = nullptr;
gb_global Type *t_type_info_multi_pointer = nullptr;
gb_global Type *t_type_info_procedure = nullptr;
gb_global Type *t_type_info_array = nullptr;
gb_global Type *t_type_info_enumerated_array = nullptr;
gb_global Type *t_type_info_dynamic_array = nullptr;
gb_global Type *t_type_info_slice = nullptr;
gb_global Type *t_type_info_parameters = nullptr;
gb_global Type *t_type_info_struct = nullptr;
gb_global Type *t_type_info_union = nullptr;
gb_global Type *t_type_info_enum = nullptr;
gb_global Type *t_type_info_map = nullptr;
gb_global Type *t_type_info_bit_set = nullptr;
gb_global Type *t_type_info_simd_vector = nullptr;
gb_global Type *t_type_info_matrix = nullptr;
gb_global Type *t_type_info_soa_pointer
10BC8
= nullptr;
gb_global Type *t_type_info_bit_field = nullptr;
gb_global Type *t_type_info_named_ptr = nullptr;
gb_global Type *t_type_info_integer_ptr = nullptr;
gb_global Type *t_type_info_rune_ptr = nullptr;
gb_global Type *t_type_info_float_ptr = nullptr;
gb_global Type *t_type_info_complex_ptr = nullptr;
gb_global Type *t_type_info_quaternion_ptr = nullptr;
gb_global Type *t_type_info_any_ptr = nullptr;
gb_global Type *t_type_info_typeid_ptr = nullptr;
gb_global Type *t_type_info_string_ptr = nullptr;
gb_global Type *t_type_info_boolean_ptr = nullptr;
gb_global Type *t_type_info_pointer_ptr = nullptr;
gb_global Type *t_type_info_multi_pointer_ptr = nullptr;
gb_global Type *t_type_info_procedure_ptr = nullptr;
gb_global Type *t_type_info_array_ptr = nullptr;
gb_global Type *t_type_info_enumerated_array_ptr = nullptr;
gb_global Type *t_type_info_dynamic_array_ptr = nullptr;
gb_global Type *t_type_info_slice_ptr = nullptr;
gb_global Type *t_type_info_parameters_ptr = nullptr;
gb_global Type *t_type_info_struct_ptr = nullptr;
gb_global Type *t_type_info_union_ptr = nullptr;
gb_global Type *t_type_info_enum_ptr = nullptr;
gb_global Type *t_type_info_map_ptr = nullptr;
gb_global Type *t_type_info_bit_set_ptr = nullptr;
gb_global Type *t_type_info_simd_vector_ptr = nullptr;
gb_global Type *t_type_info_matrix_ptr = nullptr;
gb_global Type *t_type_info_soa_pointer_ptr = nullptr;
gb_global Type *t_type_info_bit_field_ptr = nullptr;
gb_global Type *t_allocator = nullptr;
gb_global Type *t_allocator_ptr = nullptr;
gb_global Type *t_context = nullptr;
gb_global Type *t_context_ptr = nullptr;
gb_global Type *t_allocator_error = nullptr;
gb_global Type *t_source_code_location = nullptr;
gb_global Type *t_source_code_location_ptr = nullptr;
gb_global Type *t_load_directory_file = nullptr;
gb_global Type *t_load_directory_file_ptr = nullptr;
gb_global Type *t_load_directory_file_slice = nullptr;
gb_global Type *t_map_info = nullptr;
gb_global Type *t_map_cell_info = nullptr;
gb_global Type *t_raw_map = nullptr;
gb_global Type *t_map_info_ptr = nullptr;
gb_global Type *t_map_cell_info_ptr = nullptr;
gb_global Type *t_raw_map_ptr = nullptr;
gb_global Type *t_equal_proc = nullptr;
gb_global Type *t_hasher_proc = nullptr;
gb_global Type *t_map_get_proc = nullptr;
gb_global Type *t_map_set_proc = nullptr;
gb_global Type *t_objc_object = nullptr;
gb_global Type *t_objc_selector = nullptr;
gb_global Type *t_objc_class = nullptr;
gb_global Type *t_objc_ivar = nullptr;
gb_global Type *t_objc_super = nullptr; // Struct used in lieu of the 'self' instance when calling objc_msgSendSuper.
gb_global Type *t_objc_super_ptr = nullptr;
gb_global Type *t_objc_id = nullptr;
gb_global Type *t_objc_SEL = nullptr;
gb_global Type *t_objc_Class = nullptr;
gb_global Type *t_objc_Ivar = nullptr;
gb_global Type *t_objc_instancetype = nullptr; // Special distinct variant of t_objc_id used mimic auto-typing of instancetype* in Objective-C
enum OdinAtomicMemoryOrder : i32 {
OdinAtomicMemoryOrder_relaxed = 0, // unordered
OdinAtomicMemoryOrder_consume = 1, // monotonic
OdinAtomicMemoryOrder_acquire = 2,
OdinAtomicMemoryOrder_release = 3,
OdinAtomicMemoryOrder_acq_rel = 4,
OdinAtomicMemoryOrder_seq_cst = 5,
OdinAtomicMemoryOrder_COUNT,
};
char const *OdinAtomicMemoryOrder_strings[OdinAtomicMemoryOrder_COUNT] = {
"Relaxed",
"Consume",
"Acquire",
"Release",
"Acq_Rel",
"Seq_Cst",
};
gb_global Type *t_atomic_memory_order = nullptr;
gb_global RecursiveMutex g_type_mutex;
struct TypePath;
gb_internal i64 type_size_of (Type *t);
gb_internal i64 type_align_of (Type *t);
gb_internal i64 type_offset_of (Type *t, i64 index, Type **field_type_=nullptr);
gb_internal gbString type_to_string (Type *type, bool shorthand=true);
gb_internal gbString type_to_string (Type *type, gbAllocator allocator, bool shorthand=true);
gb_internal i64 type_size_of_internal(Type *t, TypePath *path);
gb_internal i64 type_align_of_internal(Type *t, TypePath *path);
gb_internal Type * bit_set_to_int(Type *t);
gb_internal bool are_types_identical(Type *x, Type *y);
gb_internal bool is_type_pointer(Type *t);
gb_internal bool is_type_multi_pointer(Type *t);
gb_internal bool is_type_soa_pointer(Type *t);
gb_internal bool is_type_proc(Type *t);
gb_internal bool is_type_slice(Type *t);
gb_internal bool is_type_integer(Type *t);
gb_internal bool type_set_offsets(Type *t);
// IMPORTANT TODO(bill): SHould this TypePath code be removed since type cycle checking is handled much earlier on?
struct TypePath {
RecursiveMutex mutex;
Array<Entity *> path; // Entity_TypeName;
bool failure;
};
gb_internal void type_path_init(TypePath *tp) {
tp->path.allocator = heap_allocator();
}
gb_internal void type_path_free(TypePath *tp) {
mutex_lock(&tp->mutex);
array_free(&tp->path);
mutex_unlock(&tp->mutex);
}
gb_internal void type_path_print_illegal_cycle(TypePath *tp, isize start_index) {
GB_ASSERT(tp != nullptr);
GB_ASSERT(start_index < tp->path.count);
Entity *e = tp->path[start_index];
GB_ASSERT(e != nullptr);
error(e->token, "Illegal type declaration cycle of `%.*s`", LIT(e->token.string));
// NOTE(bill): Print cycle, if it's deep enough
for (isize j = start_index; j < tp->path.count; j++) {
Entity *e = tp->path[j];
error(e->token, "\t%.*s refers to", LIT(e->token.string));
}
// NOTE(bill): This will only print if the path count > 1
error(e->token, "\t%.*s", LIT(e->token.string));
tp->failure = true;
e->type->failure = true;
base_type(e->type)->failure = true;
}
gb_internal bool type_path_push(TypePath *tp, Type *t) {
GB_ASSERT(tp != nullptr);
if (t->kind != Type_Named) {
return false;
}
Entity *e = t->Named.type_name;
mutex_lock(&tp->mutex);
for (isize i = 0; i < tp->path.count; i++) {
Entity *p = tp->path[i];
if (p == e) {
type_path_print_illegal_cycle(tp, i);
}
}
array_add(&tp->path, e);
mutex_unlock(&tp->mutex);
return true;
}
gb_internal void type_path_pop(TypePath *tp) {
if (tp != nullptr) {
mutex_lock(&tp->mutex);
if (tp->path.count > 0) {
array_pop(&tp->path);
}
mutex_unlock(&tp->mutex);
}
}
#define FAILURE_SIZE 0
#define FAILURE_ALIGNMENT 0
gb_internal Type *base_type(Type *t) {
for (;;) {
if (t == nullptr) {
break;
}
if (t->kind != Type_Named) {
break;
}
if (t == t->Named.base) {
return t_invalid;
}
t = t->Named.base;
}
return t;
}
gb_internal Type *base_named_type(Type *t) {
if (t->kind != Type_Named) {
return t_invalid;
}
Type *prev_named = t;
t = t->Named.base;
for (;;) {
if (t == nullptr) {
break;
}
if (t->kind != Type_Named) {
break;
}
if (t == t->Named.base) {
return t_invalid;
}
prev_named = t;
t = t->Named.base;
}
return prev_named;
}
gb_internal Type *base_enum_type(Type *t) {
Type *bt = base_type(t);
if (bt != nullptr &&
bt->kind == Type_Enum) {
return bt->Enum.base_type;
}
return t;
}
gb_internal Type *core_type(Type *t) {
for (;;) {
if (t == nullptr) {
break;
}
switch (t->kind) {
case Type_Named:
if (t == t->Named.base) {
return t_invalid;
}
t = t->Named.base;
continue;
case Type_Enum:
t = t->Enum.base_type;
continue;
case Type_BitField:
t = t->BitField.backing_type;
continue;
}
break;
}
return t;
}
gb_internal void set_base_type(Type *t, Type *base) {
if (t && t->kind == Type_Named) {
t->Named.base = base;
}
}
gb_internal Type *alloc_type(TypeKind kind) {
// gbAllocator a = heap_allocator();
gbAllocator a = permanent_allocator();
Type *t = gb_alloc_item(a, Type);
gb_zero_item(t);
t->kind = kind;
t->cached_size = -1;
t->cached_align = -1;
return t;
}
gb_internal Type *alloc_type_generic(Scope *scope, i64 id, String name, Type *specialized) {
Type *t = alloc_type(Type_Generic);
t->Generic.id = id;
t->Generic.name = name;
t->Generic.specialized = specialized;
t->Generic.scope = scope;
return t;
}
gb_internal Type *alloc_type_pointer(Type *elem) {
Type *t = alloc_type(Type_Pointer);
t->Pointer.elem = elem;
return t;
}
gb_internal Type *alloc_type_multi_pointer(Type *elem) {
Type *t = alloc_type(Type_MultiPointer);
t->MultiPointer.elem = elem;
return t;
}
gb_internal Type *alloc_type_soa_pointer(Type *elem) {
Type *t = alloc_type(Type_SoaPointer);