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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
|
/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#ifndef ZIG_ALL_TYPES_HPP
#define ZIG_ALL_TYPES_HPP
#include "list.hpp"
#include "buffer.hpp"
#include "zig_llvm.hpp"
#include "hash_map.hpp"
#include "errmsg.hpp"
#include "bignum.hpp"
#include "target.hpp"
struct AstNode;
struct ImportTableEntry;
struct AsmToken;
struct FnTableEntry;
struct BlockContext;
struct TypeTableEntry;
struct VariableTableEntry;
struct ErrorTableEntry;
struct LabelTableEntry;
struct BuiltinFnEntry;
struct TypeStructField;
struct CodeGen;
struct ConstExprValue;
enum OutType {
OutTypeUnknown,
OutTypeExe,
OutTypeLib,
OutTypeObj,
};
struct ConstEnumValue {
uint64_t tag;
ConstExprValue *payload;
};
struct ConstStructValue {
ConstExprValue **fields;
};
struct ConstArrayValue {
ConstExprValue **fields;
};
struct ConstPtrValue {
ConstExprValue **ptr;
// len should almost always be 1. exceptions include C strings
uint64_t len;
};
struct ConstErrValue {
ErrorTableEntry *err;
ConstExprValue *payload;
};
struct ConstExprValue {
bool ok; // true if constant expression evalution worked
bool depends_on_compile_var;
bool undef;
union {
BigNum x_bignum;
bool x_bool;
FnTableEntry *x_fn;
TypeTableEntry *x_type;
ConstExprValue *x_maybe;
ConstErrValue x_err;
ConstEnumValue x_enum;
ConstStructValue x_struct;
ConstArrayValue x_array;
ConstPtrValue x_ptr;
ImportTableEntry *x_import;
} data;
};
enum ReturnKnowledge {
ReturnKnowledgeUnknown,
ReturnKnowledgeKnownError,
ReturnKnowledgeKnownNonError,
ReturnKnowledgeKnownNull,
ReturnKnowledgeKnownNonNull,
ReturnKnowledgeSkipDefers,
};
struct Expr {
TypeTableEntry *type_entry;
ReturnKnowledge return_knowledge;
VariableTableEntry *variable;
LLVMValueRef const_llvm_val;
ConstExprValue const_val;
bool has_global_const;
};
struct StructValExprCodeGen {
TypeTableEntry *type_entry;
LLVMValueRef ptr;
AstNode *source_node;
};
enum VisibMod {
VisibModPrivate,
VisibModPub,
VisibModExport,
};
enum TldResolution {
TldResolutionUnresolved,
TldResolutionInvalid,
TldResolutionOk,
};
struct TopLevelDecl {
// populated by parser
Buf *name;
ZigList<AstNode *> *directives;
VisibMod visib_mod;
// populated by semantic analyzer
ImportTableEntry *import;
// set this flag temporarily to detect infinite loops
bool dep_loop_flag;
TldResolution resolution;
AstNode *parent_decl;
};
struct TypeEnumField {
Buf *name;
TypeTableEntry *type_entry;
uint32_t value;
};
enum NodeType {
NodeTypeRoot,
NodeTypeFnProto,
NodeTypeFnDef,
NodeTypeFnDecl,
NodeTypeParamDecl,
NodeTypeBlock,
NodeTypeDirective,
NodeTypeReturnExpr,
NodeTypeDefer,
NodeTypeVariableDeclaration,
NodeTypeTypeDecl,
NodeTypeErrorValueDecl,
NodeTypeBinOpExpr,
NodeTypeUnwrapErrorExpr,
NodeTypeNumberLiteral,
NodeTypeStringLiteral,
NodeTypeCharLiteral,
NodeTypeSymbol,
NodeTypePrefixOpExpr,
NodeTypeFnCallExpr,
NodeTypeArrayAccessExpr,
NodeTypeSliceExpr,
NodeTypeFieldAccessExpr,
NodeTypeUse,
NodeTypeBoolLiteral,
NodeTypeNullLiteral,
NodeTypeUndefinedLiteral,
NodeTypeIfBoolExpr,
NodeTypeIfVarExpr,
NodeTypeWhileExpr,
NodeTypeForExpr,
NodeTypeSwitchExpr,
NodeTypeSwitchProng,
NodeTypeSwitchRange,
NodeTypeLabel,
NodeTypeGoto,
NodeTypeBreak,
NodeTypeContinue,
NodeTypeAsmExpr,
NodeTypeStructDecl,
NodeTypeStructField,
NodeTypeContainerInitExpr,
NodeTypeStructValueField,
NodeTypeArrayType,
NodeTypeErrorType,
NodeTypeTypeLiteral,
};
struct AstNodeRoot {
ZigList<AstNode *> top_level_decls;
};
struct AstNodeFnProto {
TopLevelDecl top_level_decl;
Buf name;
ZigList<AstNode *> generic_params;
ZigList<AstNode *> params;
AstNode *return_type;
bool generic_params_is_var_args;
bool is_var_args;
bool is_extern;
bool is_inline;
// populated by semantic analyzer:
// the function definition this fn proto is inside. can be null.
AstNode *fn_def_node;
FnTableEntry *fn_table_entry;
bool skip;
Expr resolved_expr;
TypeTableEntry *generic_fn_type;
};
struct AstNodeFnDef {
AstNode *fn_proto;
AstNode *body;
// populated by semantic analyzer
TypeTableEntry *implicit_return_type;
BlockContext *block_context;
};
struct AstNodeFnDecl {
AstNode *fn_proto;
};
struct AstNodeParamDecl {
Buf name;
AstNode *type;
bool is_noalias;
// populated by semantic analyzer
VariableTableEntry *variable;
};
struct AstNodeBlock {
ZigList<AstNode *> statements;
// populated by semantic analyzer
// this one is the scope that the block itself introduces
BlockContext *child_block;
// this is the innermost scope created by defers and var decls.
// you can follow its parents up to child_block. it will equal
// child_block if there are no defers or var decls in the block.
BlockContext *nested_block;
Expr resolved_expr;
};
enum ReturnKind {
ReturnKindUnconditional,
ReturnKindMaybe,
ReturnKindError,
};
struct AstNodeReturnExpr {
ReturnKind kind;
// might be null in case of return void;
AstNode *expr;
// populated by semantic analyzer:
Expr resolved_expr;
};
struct AstNodeDefer {
ReturnKind kind;
AstNode *expr;
// populated by semantic analyzer:
Expr resolved_expr;
int index_in_block;
LLVMBasicBlockRef basic_block;
BlockContext *child_block;
};
struct AstNodeVariableDeclaration {
TopLevelDecl top_level_decl;
Buf symbol;
bool is_const;
bool is_extern;
// one or both of type and expr will be non null
AstNode *type;
AstNode *expr;
// populated by semantic analyzer
Expr resolved_expr;
VariableTableEntry *variable;
};
struct AstNodeTypeDecl {
TopLevelDecl top_level_decl;
Buf symbol;
AstNode *child_type;
// populated by semantic analyzer
// if this is set, don't process the node; we've already done so
// and here is the type (with id TypeTableEntryIdTypeDecl)
TypeTableEntry *override_type;
TypeTableEntry *child_type_entry;
};
struct AstNodeErrorValueDecl {
TopLevelDecl top_level_decl;
Buf name;
// populated by semantic analyzer
ErrorTableEntry *err;
};
enum BinOpType {
BinOpTypeInvalid,
BinOpTypeAssign,
BinOpTypeAssignTimes,
BinOpTypeAssignDiv,
BinOpTypeAssignMod,
BinOpTypeAssignPlus,
BinOpTypeAssignMinus,
BinOpTypeAssignBitShiftLeft,
BinOpTypeAssignBitShiftRight,
BinOpTypeAssignBitAnd,
BinOpTypeAssignBitXor,
BinOpTypeAssignBitOr,
BinOpTypeAssignBoolAnd,
BinOpTypeAssignBoolOr,
BinOpTypeBoolOr,
BinOpTypeBoolAnd,
BinOpTypeCmpEq,
BinOpTypeCmpNotEq,
BinOpTypeCmpLessThan,
BinOpTypeCmpGreaterThan,
BinOpTypeCmpLessOrEq,
BinOpTypeCmpGreaterOrEq,
BinOpTypeBinOr,
BinOpTypeBinXor,
BinOpTypeBinAnd,
BinOpTypeBitShiftLeft,
BinOpTypeBitShiftRight,
BinOpTypeAdd,
BinOpTypeSub,
BinOpTypeMult,
BinOpTypeDiv,
BinOpTypeMod,
BinOpTypeUnwrapMaybe,
BinOpTypeStrCat,
};
struct AstNodeBinOpExpr {
AstNode *op1;
BinOpType bin_op;
AstNode *op2;
// populated by semantic analyzer:
// for when op is BinOpTypeAssign
VariableTableEntry *var_entry;
Expr resolved_expr;
};
struct AstNodeUnwrapErrorExpr {
AstNode *op1;
AstNode *symbol; // can be null
AstNode *op2;
// populated by semantic analyzer:
Expr resolved_expr;
VariableTableEntry *var;
};
enum CastOp {
CastOpNoCast, // signifies the function call expression is not a cast
CastOpNoop, // fn call expr is a cast, but does nothing
CastOpPtrToInt,
CastOpIntToPtr,
CastOpWidenOrShorten,
CastOpToUnknownSizeArray,
CastOpMaybeWrap,
CastOpErrorWrap,
CastOpPureErrorWrap,
CastOpPointerReinterpret,
CastOpErrToInt,
CastOpIntToFloat,
CastOpFloatToInt,
CastOpBoolToInt,
};
struct AstNodeFnCallExpr {
AstNode *fn_ref_expr;
ZigList<AstNode *> params;
bool is_builtin;
// populated by semantic analyzer:
BuiltinFnEntry *builtin_fn;
Expr resolved_expr;
FnTableEntry *fn_entry;
CastOp cast_op;
TypeTableEntry *enum_type;
// if cast_op is CastOpArrayToString, this will be a pointer to
// the string struct on the stack
LLVMValueRef tmp_ptr;
};
struct AstNodeArrayAccessExpr {
AstNode *array_ref_expr;
AstNode *subscript;
// populated by semantic analyzer:
Expr resolved_expr;
};
struct AstNodeSliceExpr {
AstNode *array_ref_expr;
AstNode *start;
AstNode *end;
bool is_const;
// populated by semantic analyzer:
Expr resolved_expr;
StructValExprCodeGen resolved_struct_val_expr;
};
struct AstNodeFieldAccessExpr {
AstNode *struct_expr;
Buf field_name;
// populated by semantic analyzer
TypeStructField *type_struct_field;
TypeEnumField *type_enum_field;
Expr resolved_expr;
StructValExprCodeGen resolved_struct_val_expr; // for enum values
bool is_fn_call;
TypeTableEntry *bare_struct_type;
bool is_member_fn;
};
struct AstNodeDirective {
Buf name;
AstNode *expr;
};
enum PrefixOp {
PrefixOpInvalid,
PrefixOpBoolNot,
PrefixOpBinNot,
PrefixOpNegation,
PrefixOpAddressOf,
PrefixOpConstAddressOf,
PrefixOpDereference,
PrefixOpMaybe,
PrefixOpError,
PrefixOpUnwrapError,
PrefixOpUnwrapMaybe,
};
struct AstNodePrefixOpExpr {
PrefixOp prefix_op;
AstNode *primary_expr;
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNodeUse {
AstNode *expr;
// populated by semantic analyzer
TopLevelDecl top_level_decl;
};
struct AstNodeIfBoolExpr {
AstNode *condition;
AstNode *then_block;
AstNode *else_node; // null, block node, or other if expr node
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNodeIfVarExpr {
AstNodeVariableDeclaration var_decl;
AstNode *then_block;
AstNode *else_node; // null, block node, or other if expr node
// populated by semantic analyzer
TypeTableEntry *type;
Expr resolved_expr;
};
struct AstNodeWhileExpr {
AstNode *condition;
AstNode *body;
// populated by semantic analyzer
bool condition_always_true;
bool contains_break;
bool contains_continue;
Expr resolved_expr;
};
struct AstNodeForExpr {
AstNode *array_expr;
AstNode *elem_node; // always a symbol
AstNode *index_node; // always a symbol, might be null
AstNode *body;
// populated by semantic analyzer
bool contains_break;
bool contains_continue;
Expr resolved_expr;
VariableTableEntry *elem_var;
VariableTableEntry *index_var;
};
struct AstNodeSwitchExpr {
AstNode *expr;
ZigList<AstNode *> prongs;
// populated by semantic analyzer
Expr resolved_expr;
int const_chosen_prong_index;
};
struct AstNodeSwitchProng {
ZigList<AstNode *> items;
AstNode *var_symbol;
AstNode *expr;
// populated by semantic analyzer
BlockContext *block_context;
VariableTableEntry *var;
bool var_is_target_expr;
};
struct AstNodeSwitchRange {
AstNode *start;
AstNode *end;
};
struct AstNodeLabel {
Buf name;
// populated by semantic analyzer
Expr resolved_expr;
LabelTableEntry *label_entry;
};
struct AstNodeGoto {
Buf name;
// populated by semantic analyzer
Expr resolved_expr;
LabelTableEntry *label_entry;
};
struct AsmOutput {
Buf asm_symbolic_name;
Buf constraint;
Buf variable_name;
AstNode *return_type; // null unless "=r" and return
// populated by semantic analyzer
VariableTableEntry *variable;
};
struct AsmInput {
Buf asm_symbolic_name;
Buf constraint;
AstNode *expr;
};
struct SrcPos {
int line;
int column;
};
struct AstNodeAsmExpr {
bool is_volatile;
Buf asm_template;
ZigList<SrcPos> offset_map;
ZigList<AsmToken> token_list;
ZigList<AsmOutput*> output_list;
ZigList<AsmInput*> input_list;
ZigList<Buf*> clobber_list;
// populated by semantic analyzer
int return_count;
Expr resolved_expr;
};
enum ContainerKind {
ContainerKindStruct,
ContainerKindEnum,
};
struct AstNodeStructDecl {
TopLevelDecl top_level_decl;
Buf name;
ContainerKind kind;
ZigList<AstNode *> fields;
ZigList<AstNode *> fns;
// populated by semantic analyzer
BlockContext *block_context;
TypeTableEntry *type_entry;
};
struct AstNodeStructField {
TopLevelDecl top_level_decl;
Buf name;
AstNode *type;
};
struct AstNodeStringLiteral {
Buf buf;
bool c;
// populated by semantic analyzer:
Expr resolved_expr;
};
struct AstNodeCharLiteral {
uint8_t value;
// populated by semantic analyzer:
Expr resolved_expr;
};
enum NumLit {
NumLitFloat,
NumLitUInt,
};
struct AstNodeNumberLiteral {
NumLit kind;
// overflow is true if when parsing the number, we discovered it would not
// fit without losing data in a uint64_t or double
bool overflow;
union {
uint64_t x_uint;
double x_float;
} data;
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNodeStructValueField {
Buf name;
AstNode *expr;
// populated by semantic analyzer
TypeStructField *type_struct_field;
};
enum ContainerInitKind {
ContainerInitKindStruct,
ContainerInitKindArray,
};
struct AstNodeContainerInitExpr {
AstNode *type;
ZigList<AstNode *> entries;
ContainerInitKind kind;
// populated by semantic analyzer
StructValExprCodeGen resolved_struct_val_expr;
Expr resolved_expr;
};
struct AstNodeNullLiteral {
// populated by semantic analyzer
StructValExprCodeGen resolved_struct_val_expr;
Expr resolved_expr;
};
struct AstNodeUndefinedLiteral {
// populated by semantic analyzer
StructValExprCodeGen resolved_struct_val_expr;
Expr resolved_expr;
};
struct AstNodeSymbolExpr {
Buf symbol;
// populated by semantic analyzer
Expr resolved_expr;
// set this to instead of analyzing the node, pretend it's a type entry and it's this one.
TypeTableEntry *override_type_entry;
TypeEnumField *enum_field;
};
struct AstNodeBoolLiteral {
bool value;
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNodeBreakExpr {
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNodeContinueExpr {
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNodeArrayType {
AstNode *size;
AstNode *child_type;
bool is_const;
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNodeErrorType {
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNodeTypeLiteral {
// populated by semantic analyzer
Expr resolved_expr;
};
struct AstNode {
enum NodeType type;
int line;
int column;
uint32_t create_index; // for determinism purposes
ImportTableEntry *owner;
AstNode **parent_field; // for AST rewriting
// the context in which this expression/node is evaluated.
// for blocks, this points to the containing scope, not the block's own scope for its children.
BlockContext *block_context;
union {
AstNodeRoot root;
AstNodeFnDef fn_def;
AstNodeFnDecl fn_decl;
AstNodeFnProto fn_proto;
AstNodeParamDecl param_decl;
AstNodeBlock block;
AstNodeReturnExpr return_expr;
AstNodeDefer defer;
AstNodeVariableDeclaration variable_declaration;
AstNodeTypeDecl type_decl;
AstNodeErrorValueDecl error_value_decl;
AstNodeBinOpExpr bin_op_expr;
AstNodeUnwrapErrorExpr unwrap_err_expr;
AstNodeDirective directive;
AstNodePrefixOpExpr prefix_op_expr;
AstNodeFnCallExpr fn_call_expr;
AstNodeArrayAccessExpr array_access_expr;
AstNodeSliceExpr slice_expr;
AstNodeUse use;
AstNodeIfBoolExpr if_bool_expr;
AstNodeIfVarExpr if_var_expr;
AstNodeWhileExpr while_expr;
AstNodeForExpr for_expr;
AstNodeSwitchExpr switch_expr;
AstNodeSwitchProng switch_prong;
AstNodeSwitchRange switch_range;
AstNodeLabel label;
AstNodeGoto goto_expr;
AstNodeAsmExpr asm_expr;
AstNodeFieldAccessExpr field_access_expr;
AstNodeStructDecl struct_decl;
AstNodeStructField struct_field;
AstNodeStringLiteral string_literal;
AstNodeCharLiteral char_literal;
AstNodeNumberLiteral number_literal;
AstNodeContainerInitExpr container_init_expr;
AstNodeStructValueField struct_val_field;
AstNodeNullLiteral null_literal;
AstNodeUndefinedLiteral undefined_literal;
AstNodeSymbolExpr symbol_expr;
AstNodeBoolLiteral bool_literal;
AstNodeBreakExpr break_expr;
AstNodeContinueExpr continue_expr;
AstNodeArrayType array_type;
AstNodeErrorType error_type;
AstNodeTypeLiteral type_literal;
} data;
};
enum AsmTokenId {
AsmTokenIdTemplate,
AsmTokenIdPercent,
AsmTokenIdVar,
};
struct AsmToken {
enum AsmTokenId id;
int start;
int end;
};
// this struct is allocated with allocate_nonzero
struct FnTypeParamInfo {
bool is_noalias;
TypeTableEntry *type;
};
struct GenericParamValue {
TypeTableEntry *type;
AstNode *node;
};
struct GenericFnTypeId {
AstNode *decl_node; // the generic fn or container decl node
GenericParamValue *generic_params;
int generic_param_count;
};
uint32_t generic_fn_type_id_hash(GenericFnTypeId *id);
bool generic_fn_type_id_eql(GenericFnTypeId *a, GenericFnTypeId *b);
static const int fn_type_id_prealloc_param_info_count = 4;
struct FnTypeId {
TypeTableEntry *return_type;
FnTypeParamInfo *param_info;
int param_count;
bool is_var_args;
bool is_naked;
bool is_cold;
bool is_extern;
FnTypeParamInfo prealloc_param_info[fn_type_id_prealloc_param_info_count];
};
uint32_t fn_type_id_hash(FnTypeId*);
bool fn_type_id_eql(FnTypeId *a, FnTypeId *b);
struct TypeTableEntryPointer {
TypeTableEntry *child_type;
bool is_const;
};
struct TypeTableEntryInt {
bool is_signed;
int bit_count;
};
struct TypeTableEntryFloat {
int bit_count;
};
struct TypeTableEntryArray {
TypeTableEntry *child_type;
uint64_t len;
};
struct TypeStructField {
Buf *name;
TypeTableEntry *type_entry;
int src_index;
int gen_index;
};
struct TypeTableEntryStruct {
AstNode *decl_node;
bool is_packed;
uint32_t src_field_count;
uint32_t gen_field_count;
TypeStructField *fields;
uint64_t size_bytes;
bool is_invalid; // true if any fields are invalid
bool is_unknown_size_array;
BlockContext *block_context;
// set this flag temporarily to detect infinite loops
bool embedded_in_current;
bool reported_infinite_err;
// whether we've finished resolving it
bool complete;
};
struct TypeTableEntryMaybe {
TypeTableEntry *child_type;
};
struct TypeTableEntryError {
TypeTableEntry *child_type;
};
struct TypeTableEntryEnum {
AstNode *decl_node;
uint32_t field_count;
uint32_t gen_field_count;
TypeEnumField *fields;
bool is_invalid; // true if any fields are invalid
TypeTableEntry *tag_type;
TypeTableEntry *union_type;
BlockContext *block_context;
// set this flag temporarily to detect infinite loops
bool embedded_in_current;
bool reported_infinite_err;
// whether we've finished resolving it
bool complete;
};
struct FnGenParamInfo {
int src_index;
int gen_index;
bool is_byval;
TypeTableEntry *type;
};
struct TypeTableEntryFn {
FnTypeId fn_type_id;
TypeTableEntry *gen_return_type;
int gen_param_count;
FnGenParamInfo *gen_param_info;
LLVMTypeRef raw_type_ref;
LLVMCallConv calling_convention;
};
struct TypeTableEntryGenericFn {
AstNode *decl_node;
};
struct TypeTableEntryTypeDecl {
TypeTableEntry *child_type;
TypeTableEntry *canonical_type;
};
enum TypeTableEntryId {
TypeTableEntryIdInvalid,
TypeTableEntryIdMetaType,
TypeTableEntryIdVoid,
TypeTableEntryIdBool,
TypeTableEntryIdUnreachable,
TypeTableEntryIdInt,
TypeTableEntryIdFloat,
TypeTableEntryIdPointer,
TypeTableEntryIdArray,
TypeTableEntryIdStruct,
TypeTableEntryIdNumLitFloat,
TypeTableEntryIdNumLitInt,
TypeTableEntryIdUndefLit,
TypeTableEntryIdMaybe,
TypeTableEntryIdErrorUnion,
TypeTableEntryIdPureError,
TypeTableEntryIdEnum,
TypeTableEntryIdFn,
TypeTableEntryIdTypeDecl,
TypeTableEntryIdNamespace,
TypeTableEntryIdGenericFn,
};
struct TypeTableEntry {
TypeTableEntryId id;
Buf name;
LLVMTypeRef type_ref;
LLVMZigDIType *di_type;
bool zero_bits;
bool deep_const;
union {
TypeTableEntryPointer pointer;
TypeTableEntryInt integral;
TypeTableEntryFloat floating;
TypeTableEntryArray array;
TypeTableEntryStruct structure;
TypeTableEntryMaybe maybe;
TypeTableEntryError error;
TypeTableEntryEnum enumeration;
TypeTableEntryFn fn;
TypeTableEntryTypeDecl type_decl;
TypeTableEntryGenericFn generic_fn;
} data;
// use these fields to make sure we don't duplicate type table entries for the same type
TypeTableEntry *pointer_parent[2];
TypeTableEntry *unknown_size_array_parent[2];
HashMap<uint64_t, TypeTableEntry *, uint64_hash, uint64_eq> arrays_by_size;
TypeTableEntry *maybe_parent;
TypeTableEntry *error_parent;
};
struct PackageTableEntry {
Buf root_src_dir;
Buf root_src_path; // relative to root_src_dir
// reminder: hash tables must be initialized before use
HashMap<Buf *, PackageTableEntry *, buf_hash, buf_eql_buf> package_table;
};
struct ImportTableEntry {
AstNode *root;
Buf *path; // relative to root_package->root_src_dir
PackageTableEntry *package;
LLVMZigDIFile *di_file;
Buf *source_code;
ZigList<int> *line_offsets;
BlockContext *block_context;
AstNode *c_import_node;
bool any_imports_failed;
ZigList<AstNode *> use_decls;
};
enum FnAnalState {
FnAnalStateReady,
FnAnalStateProbing,
FnAnalStateComplete,
FnAnalStateSkipped,
};
struct FnTableEntry {
LLVMValueRef fn_value;
AstNode *proto_node;
AstNode *fn_def_node;
ImportTableEntry *import_entry;
// Required to be a pre-order traversal of the AST. (parents must come before children)
ZigList<BlockContext *> all_block_contexts;
ZigList<LabelTableEntry *> all_labels;
Buf symbol_name;
TypeTableEntry *type_entry; // function type
bool is_inline;
bool internal_linkage;
bool is_extern;
bool is_test;
bool is_pure;
BlockContext *parent_block_context;
FnAnalState anal_state;
ZigList<AstNode *> cast_alloca_list;
ZigList<StructValExprCodeGen *> struct_val_expr_alloca_list;
ZigList<VariableTableEntry *> variable_list;
ZigList<AstNode *> goto_list;
};
struct EvalVar {
Buf *name;
ConstExprValue value;
};
struct EvalScope {
BlockContext *block_context;
ZigList<EvalVar> vars;
};
struct EvalFnRoot {
CodeGen *codegen;
FnTableEntry *fn;
AstNode *call_node;
int branch_quota;
int branches_used;
AstNode *exceeded_quota_node;
bool abort;
};
struct EvalFn {
EvalFnRoot *root;
FnTableEntry *fn;
ConstExprValue *return_expr;
ZigList<EvalScope*> scope_stack;
};
enum BuiltinFnId {
BuiltinFnIdInvalid,
BuiltinFnIdMemcpy,
BuiltinFnIdMemset,
BuiltinFnIdSizeof,
BuiltinFnIdAlignof,
BuiltinFnIdMaxValue,
BuiltinFnIdMinValue,
BuiltinFnIdMemberCount,
BuiltinFnIdTypeof,
BuiltinFnIdAddWithOverflow,
BuiltinFnIdSubWithOverflow,
BuiltinFnIdMulWithOverflow,
BuiltinFnIdCInclude,
BuiltinFnIdCDefine,
BuiltinFnIdCUndef,
BuiltinFnIdCompileVar,
BuiltinFnIdConstEval,
BuiltinFnIdCtz,
BuiltinFnIdClz,
BuiltinFnIdImport,
BuiltinFnIdCImport,
BuiltinFnIdErrName,
BuiltinFnIdBreakpoint,
};
struct BuiltinFnEntry {
BuiltinFnId id;
Buf name;
int param_count;
TypeTableEntry *return_type;
TypeTableEntry **param_types;
uint32_t ref_count;
LLVMValueRef fn_val;
};
struct CodeGen {
LLVMModuleRef module;
ZigList<ErrorMsg*> errors;
LLVMBuilderRef builder;
LLVMZigDIBuilder *dbuilder;
LLVMZigDICompileUnit *compile_unit;
ZigList<Buf *> link_libs; // non-libc link libs
// reminder: hash tables must be initialized before use
HashMap<Buf *, ImportTableEntry *, buf_hash, buf_eql_buf> import_table;
HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table;
HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> primitive_type_table;
HashMap<FnTypeId *, TypeTableEntry *, fn_type_id_hash, fn_type_id_eql> fn_type_table;
HashMap<Buf *, ErrorTableEntry *, buf_hash, buf_eql_buf> error_table;
HashMap<GenericFnTypeId *, AstNode *, generic_fn_type_id_hash, generic_fn_type_id_eql> generic_table;
ZigList<ImportTableEntry *> import_queue;
int import_queue_index;
ZigList<AstNode *> export_queue;
int export_queue_index;
ZigList<AstNode *> use_queue;
int use_queue_index;
uint32_t next_unresolved_index;
struct {
TypeTableEntry *entry_bool;
TypeTableEntry *entry_int[2][4]; // [signed,unsigned][8,16,32,64]
TypeTableEntry *entry_c_int[CIntTypeCount];
TypeTableEntry *entry_c_long_double;
TypeTableEntry *entry_c_void;
TypeTableEntry *entry_u8;
TypeTableEntry *entry_u16;
TypeTableEntry *entry_u32;
TypeTableEntry *entry_u64;
TypeTableEntry *entry_i8;
TypeTableEntry *entry_i16;
TypeTableEntry *entry_i32;
TypeTableEntry *entry_i64;
TypeTableEntry *entry_isize;
TypeTableEntry *entry_usize;
TypeTableEntry *entry_f32;
TypeTableEntry *entry_f64;
TypeTableEntry *entry_void;
TypeTableEntry *entry_unreachable;
TypeTableEntry *entry_type;
TypeTableEntry *entry_invalid;
TypeTableEntry *entry_namespace;
TypeTableEntry *entry_num_lit_int;
TypeTableEntry *entry_num_lit_float;
TypeTableEntry *entry_undef;
TypeTableEntry *entry_pure_error;
TypeTableEntry *entry_os_enum;
TypeTableEntry *entry_arch_enum;
TypeTableEntry *entry_environ_enum;
} builtin_types;
ZigTarget zig_target;
LLVMTargetDataRef target_data_ref;
unsigned pointer_size_bytes;
bool is_big_endian;
bool is_static;
bool strip_debug_symbols;
bool have_exported_main;
bool link_libc;
Buf *libc_lib_dir;
Buf *libc_static_lib_dir;
Buf *libc_include_dir;
Buf *dynamic_linker;
Buf *linker_path;
Buf triple_str;
bool is_release_build;
bool is_test_build;
uint32_t target_os_index;
uint32_t target_arch_index;
uint32_t target_environ_index;
LLVMTargetMachineRef target_machine;
LLVMZigDIFile *dummy_di_file;
bool is_native_target;
PackageTableEntry *root_package;
PackageTableEntry *std_package;
Buf *root_out_name;
bool windows_subsystem_windows;
bool windows_subsystem_console;
bool windows_linker_unicode;
Buf *darwin_linker_version;
Buf *mmacosx_version_min;
Buf *mios_version_min;
bool linker_rdynamic;
// The function definitions this module includes. There must be a corresponding
// fn_protos entry.
ZigList<FnTableEntry *> fn_defs;
// The function prototypes this module includes. In the case of external declarations,
// there will not be a corresponding fn_defs entry.
ZigList<FnTableEntry *> fn_protos;
ZigList<VariableTableEntry *> global_vars;
ZigList<AstNode *> global_const_list;
OutType out_type;
FnTableEntry *cur_fn;
FnTableEntry *main_fn;
LLVMValueRef cur_ret_ptr;
ZigList<LLVMBasicBlockRef> break_block_stack;
ZigList<LLVMBasicBlockRef> continue_block_stack;
bool c_stdint_used;
AstNode *root_export_decl;
int version_major;
int version_minor;
int version_patch;
bool verbose;
ErrColor err_color;
ImportTableEntry *root_import;
ImportTableEntry *bootstrap_import;
ImportTableEntry *test_runner_import;
LLVMValueRef memcpy_fn_val;
LLVMValueRef memset_fn_val;
LLVMValueRef trap_fn_val;
bool error_during_imports;
uint32_t next_node_index;
TypeTableEntry *err_tag_type;
LLVMValueRef int_overflow_fns[2][3][4]; // [0-signed,1-unsigned][0-add,1-sub,2-mul][0-8,1-16,2-32,3-64]
LLVMValueRef int_builtin_fns[2][4]; // [0-ctz,1-clz][0-8,1-16,2-32,3-64]
const char **clang_argv;
int clang_argv_len;
ZigList<const char *> lib_dirs;
uint32_t test_fn_count;
bool check_unused;
ZigList<AstNode *> error_decls;
bool generate_error_name_table;
LLVMValueRef err_name_table;
};
struct VariableTableEntry {
Buf name;
TypeTableEntry *type;
LLVMValueRef value_ref;
bool is_const;
bool is_ptr; // if true, value_ref is a pointer
// which node is the declaration of the variable
AstNode *decl_node;
// which node contains the ConstExprValue for this variable's value
AstNode *val_node;
LLVMZigDILocalVariable *di_loc_var;
int src_arg_index;
int gen_arg_index;
BlockContext *block_context;
};
struct ErrorTableEntry {
Buf name;
uint32_t value;
AstNode *decl_node;
};
struct LabelTableEntry {
AstNode *decl_node;
LLVMBasicBlockRef basic_block;
bool used;
bool entered_from_fallthrough;
};
struct BlockContext {
// One of: NodeTypeFnDef, NodeTypeBlock, NodeTypeRoot, NodeTypeDefer, NodeTypeVariableDeclaration
AstNode *node;
// any variables that are introduced by this scope
HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> decl_table;
HashMap<Buf *, VariableTableEntry *, buf_hash, buf_eql_buf> var_table;
HashMap<Buf *, LabelTableEntry *, buf_hash, buf_eql_buf> label_table;
// if the block is inside a function, this is the function it is in:
FnTableEntry *fn_entry;
// if the block has a parent, this is it
BlockContext *parent;
// if break or continue is valid in this context, this is the loop node that
// it would pertain to
AstNode *parent_loop_node;
LLVMZigDIScope *di_scope;
Buf *c_import_buf;
// if this is true, then this code will not be generated
bool codegen_excluded;
};
#endif
|