authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-01 23:25:09-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-04 03:40:40-05:00
log2f259b8176184f063f555aed34087cf7560124fb
treeb4e1cc89b32673c159c68a46ba1e0683ea208102
parentf6cbb73c7402fc100bbfb26c1a35c9f23b3f36ff

IR: re-organize where state goes to prepare for generics

* Rip out legacy code for generics * put scope in instruction instead of AST nodes * separate top level decl stuff from AST nodes - remove the assumption that there is a 1:1 correspondence between an output instruction and an AST node - This way we won't have to clone AST nodes for generics.

10 files changed, 1495 insertions(+), 1886 deletions(-)

src/all_types.hpp+88-202
......@@ -20,6 +20,8 @@ struct AstNode;
2020struct ImportTableEntry;
2121struct FnTableEntry;
2222struct Scope;
23struct ScopeBlock;
24struct ScopeFnDef;
2325struct TypeTableEntry;
2426struct VariableTableEntry;
2527struct ErrorTableEntry;
......@@ -33,6 +35,13 @@ struct IrInstructionCast;
3335struct IrBasicBlock;
3436struct ScopeDecls;
3537
38struct IrGotoItem {
39 AstNode *source_node;
40 IrBasicBlock *bb;
41 size_t instruction_index;
42 Scope *scope;
43};
44
3645struct IrExecutable {
3746 ZigList<IrBasicBlock *> basic_block_list;
3847 size_t mem_slot_count;
......@@ -41,8 +50,9 @@ struct IrExecutable {
4150 size_t backward_branch_quota;
4251 bool invalid;
4352 ZigList<LabelTableEntry *> all_labels;
44 ZigList<AstNode *> goto_list;
53 ZigList<IrGotoItem> goto_list;
4554 bool is_inline;
55 FnTableEntry *fn_entry;
4656};
4757
4858enum OutType {
......@@ -129,36 +139,62 @@ enum ReturnKnowledge {
129139 ReturnKnowledgeSkipDefers,
130140};
131141
132struct StructValExprCodeGen {
133 TypeTableEntry *type_entry;
134 LLVMValueRef ptr;
135 AstNode *source_node;
136};
137
138142enum VisibMod {
139143 VisibModPrivate,
140144 VisibModPub,
141145 VisibModExport,
142146};
143147
148enum TldId {
149 TldIdVar,
150 TldIdFn,
151 TldIdContainer,
152 TldIdTypeDef,
153};
154
144155enum TldResolution {
145156 TldResolutionUnresolved,
146157 TldResolutionInvalid,
147158 TldResolutionOk,
148159};
149160
150struct TopLevelDecl {
151 // populated by parser
161struct Tld {
162 TldId id;
152163 Buf *name;
153164 VisibMod visib_mod;
165 AstNode *source_node;
154166
155 // populated by semantic analyzer
156167 ImportTableEntry *import;
168 Scope *parent_scope;
157169 // set this flag temporarily to detect infinite loops
158170 bool dep_loop_flag;
159171 TldResolution resolution;
160 AstNode *parent_decl;
161 IrInstruction *value;
172 Tld *parent_tld;
173};
174
175struct TldVar {
176 Tld base;
177
178 VariableTableEntry *var;
179};
180
181struct TldFn {
182 Tld base;
183
184 FnTableEntry *fn_entry;
185};
186
187struct TldContainer {
188 Tld base;
189
190 ScopeDecls *decls_scope;
191 TypeTableEntry *type_entry;
192};
193
194struct TldTypeDef {
195 Tld base;
196
197 TypeTableEntry *type_entry;
162198};
163199
164200struct TypeEnumField {
......@@ -223,7 +259,7 @@ struct AstNodeRoot {
223259};
224260
225261struct AstNodeFnProto {
226 TopLevelDecl top_level_decl;
262 VisibMod visib_mod;
227263 Buf *name;
228264 ZigList<AstNode *> params;
229265 AstNode *return_type;
......@@ -232,28 +268,12 @@ struct AstNodeFnProto {
232268 bool is_inline;
233269 bool is_coldcc;
234270 bool is_nakedcc;
235
236 // populated by semantic analyzer:
237
238 // the function definition this fn proto is inside. can be null.
239271 AstNode *fn_def_node;
240 FnTableEntry *fn_table_entry;
241 bool skip;
242 // computed from params field
243 size_t inline_arg_count;
244 size_t inline_or_var_type_arg_count;
245 // if this is a generic function implementation, this points to the generic node
246 AstNode *generic_proto_node;
247272};
248273
249274struct AstNodeFnDef {
250275 AstNode *fn_proto;
251276 AstNode *body;
252
253 // populated by semantic analyzer
254 TypeTableEntry *implicit_return_type;
255 Scope *containing_scope;
256 Scope *child_scope;
257277};
258278
259279struct AstNodeFnDecl {
......@@ -265,21 +285,10 @@ struct AstNodeParamDecl {
265285 AstNode *type;
266286 bool is_noalias;
267287 bool is_inline;
268
269 // populated by semantic analyzer
270 VariableTableEntry *variable;
271288};
272289
273290struct AstNodeBlock {
274291 ZigList<AstNode *> statements;
275
276 // populated by semantic analyzer
277 // this one is the scope that the block itself introduces
278 Scope *child_block;
279 // this is the innermost scope created by defers and var decls.
280 // you can follow its parents up to child_block. it will equal
281 // child_block if there are no defers or var decls in the block.
282 Scope *nested_block;
283292};
284293
285294enum ReturnKind {
......@@ -298,14 +307,13 @@ struct AstNodeDefer {
298307 ReturnKind kind;
299308 AstNode *expr;
300309
301 // populated by semantic analyzer:
302 size_t index_in_block;
303 LLVMBasicBlockRef basic_block;
304 Scope *child_block;
310 // temporary data used in IR generation
311 // TODO populate during gen_defer
312 Scope *child_scope;
305313};
306314
307315struct AstNodeVariableDeclaration {
308 TopLevelDecl top_level_decl;
316 VisibMod visib_mod;
309317 Buf *symbol;
310318 bool is_const;
311319 bool is_inline;
......@@ -313,28 +321,19 @@ struct AstNodeVariableDeclaration {
313321 // one or both of type and expr will be non null
314322 AstNode *type;
315323 AstNode *expr;
316
317 // populated by semantic analyzer
318 VariableTableEntry *variable;
319324};
320325
321326struct AstNodeTypeDecl {
322 TopLevelDecl top_level_decl;
327 VisibMod visib_mod;
323328 Buf *symbol;
324329 AstNode *child_type;
325
326 // populated by semantic analyzer
327 // if this is set, don't process the node; we've already done so
328 // and here is the type (with id TypeTableEntryIdTypeDecl)
329 TypeTableEntry *override_type;
330 TypeTableEntry *child_type_entry;
331330};
332331
333332struct AstNodeErrorValueDecl {
334 TopLevelDecl top_level_decl;
333 // always invalid if it's not VisibModPrivate but can be parsed that way
334 VisibMod visib_mod;
335335 Buf *name;
336336
337 // populated by semantic analyzer
338337 ErrorTableEntry *err;
339338};
340339
......@@ -388,19 +387,12 @@ struct AstNodeBinOpExpr {
388387 AstNode *op1;
389388 BinOpType bin_op;
390389 AstNode *op2;
391
392 // populated by semantic analyzer:
393 // for when op is BinOpTypeAssign
394 VariableTableEntry *var_entry;
395390};
396391
397392struct AstNodeUnwrapErrorExpr {
398393 AstNode *op1;
399394 AstNode *symbol; // can be null
400395 AstNode *op2;
401
402 // populated by semantic analyzer:
403 VariableTableEntry *var;
404396};
405397
406398enum CastOp {
......@@ -429,21 +421,11 @@ struct AstNodeFnCallExpr {
429421 AstNode *fn_ref_expr;
430422 ZigList<AstNode *> params;
431423 bool is_builtin;
432
433 // populated by semantic analyzer:
434 BuiltinFnEntry *builtin_fn;
435 FnTableEntry *fn_entry;
436 CastOp cast_op;
437 // if cast_op is CastOpArrayToString, this will be a pointer to
438 // the string struct on the stack
439 LLVMValueRef tmp_ptr;
440424};
441425
442426struct AstNodeArrayAccessExpr {
443427 AstNode *array_ref_expr;
444428 AstNode *subscript;
445
446 // populated by semantic analyzer:
447429};
448430
449431struct AstNodeSliceExpr {
......@@ -451,22 +433,11 @@ struct AstNodeSliceExpr {
451433 AstNode *start;
452434 AstNode *end;
453435 bool is_const;
454
455 // populated by semantic analyzer:
456 StructValExprCodeGen resolved_struct_val_expr;
457436};
458437
459438struct AstNodeFieldAccessExpr {
460439 AstNode *struct_expr;
461440 Buf *field_name;
462
463 // populated by semantic analyzer
464 TypeStructField *type_struct_field;
465 TypeEnumField *type_enum_field;
466 StructValExprCodeGen resolved_struct_val_expr; // for enum values
467 TypeTableEntry *bare_container_type;
468 bool is_member_fn;
469 AstNode *container_init_expr_node;
470441};
471442
472443enum PrefixOp {
......@@ -487,24 +458,21 @@ enum PrefixOp {
487458struct AstNodePrefixOpExpr {
488459 PrefixOp prefix_op;
489460 AstNode *primary_expr;
490
491 // populated by semantic analyzer
492461};
493462
494463struct AstNodeUse {
464 VisibMod visib_mod;
495465 AstNode *expr;
496466
497 // populated by semantic analyzer
498 TopLevelDecl top_level_decl;
467 TldResolution resolution;
468 IrInstruction *value;
499469};
500470
501471struct AstNodeIfBoolExpr {
502472 AstNode *condition;
503473 AstNode *then_block;
504474 AstNode *else_node; // null, block node, or other if expr node
505 bool is_inline; // TODO
506
507 // populated by semantic analyzer
475 bool is_inline; // TODO parse inline if
508476};
509477
510478struct AstNodeIfVarExpr {
......@@ -512,10 +480,7 @@ struct AstNodeIfVarExpr {
512480 AstNode *then_block;
513481 AstNode *else_node; // null, block node, or other if expr node
514482 bool var_is_ptr;
515 bool is_inline; // TODO
516
517 // populated by semantic analyzer
518 TypeTableEntry *type;
483 bool is_inline; // TODO parse inline ?if?
519484};
520485
521486struct AstNodeWhileExpr {
......@@ -523,11 +488,6 @@ struct AstNodeWhileExpr {
523488 AstNode *continue_expr;
524489 AstNode *body;
525490 bool is_inline;
526
527 // populated by semantic analyzer
528 bool condition_always_true;
529 bool contains_break;
530 bool contains_continue;
531491};
532492
533493struct AstNodeForExpr {
......@@ -537,20 +497,12 @@ struct AstNodeForExpr {
537497 AstNode *body;
538498 bool elem_is_ptr;
539499 bool is_inline;
540
541 // populated by semantic analyzer
542 bool contains_break;
543 bool contains_continue;
544 VariableTableEntry *elem_var;
545 VariableTableEntry *index_var;
546500};
547501
548502struct AstNodeSwitchExpr {
549503 AstNode *expr;
550504 ZigList<AstNode *> prongs;
551505 bool is_inline;
552
553 // populated by semantic analyzer
554506};
555507
556508struct AstNodeSwitchProng {
......@@ -573,10 +525,6 @@ struct AstNodeLabel {
573525struct AstNodeGoto {
574526 Buf *name;
575527 bool is_inline;
576
577 // populated by semantic analyzer
578 IrBasicBlock *bb;
579 size_t instruction_index;
580528};
581529
582530struct AsmOutput {
......@@ -584,9 +532,6 @@ struct AsmOutput {
584532 Buf *constraint;
585533 Buf *variable_name;
586534 AstNode *return_type; // null unless "=r" and return
587
588 // populated by semantic analyzer
589 VariableTableEntry *variable;
590535};
591536
592537struct AsmInput {
......@@ -619,8 +564,6 @@ struct AstNodeAsmExpr {
619564 ZigList<AsmOutput*> output_list;
620565 ZigList<AsmInput*> input_list;
621566 ZigList<Buf*> clobber_list;
622
623 // populated by semantic analyzer
624567};
625568
626569enum ContainerKind {
......@@ -630,23 +573,17 @@ enum ContainerKind {
630573};
631574
632575struct AstNodeStructDecl {
633 TopLevelDecl top_level_decl;
576 VisibMod visib_mod;
634577 Buf *name;
635578 ContainerKind kind;
636579 ZigList<AstNode *> generic_params;
637580 bool generic_params_is_var_args; // always an error but it can happen from parsing
638581 ZigList<AstNode *> fields;
639582 ZigList<AstNode *> decls;
640
641 // populated by semantic analyzer
642 ScopeDecls *decls_scope;
643 TypeTableEntry *type_entry;
644 TypeTableEntry *generic_fn_type;
645 bool skip;
646583};
647584
648585struct AstNodeStructField {
649 TopLevelDecl top_level_decl;
586 VisibMod visib_mod;
650587 Buf *name;
651588 AstNode *type;
652589};
......@@ -654,14 +591,10 @@ struct AstNodeStructField {
654591struct AstNodeStringLiteral {
655592 Buf *buf;
656593 bool c;
657
658 // populated by semantic analyzer:
659594};
660595
661596struct AstNodeCharLiteral {
662597 uint8_t value;
663
664 // populated by semantic analyzer:
665598};
666599
667600struct AstNodeNumberLiteral {
......@@ -670,16 +603,11 @@ struct AstNodeNumberLiteral {
670603 // overflow is true if when parsing the number, we discovered it would not
671604 // fit without losing data in a uint64_t or double
672605 bool overflow;
673
674 // populated by semantic analyzer
675606};
676607
677608struct AstNodeStructValueField {
678609 Buf *name;
679610 AstNode *expr;
680
681 // populated by semantic analyzer
682 TypeStructField *type_struct_field;
683611};
684612
685613enum ContainerInitKind {
......@@ -691,68 +619,47 @@ struct AstNodeContainerInitExpr {
691619 AstNode *type;
692620 ZigList<AstNode *> entries;
693621 ContainerInitKind kind;
694
695 // populated by semantic analyzer
696 StructValExprCodeGen resolved_struct_val_expr;
697 TypeTableEntry *enum_type;
698622};
699623
700624struct AstNodeNullLiteral {
701 // populated by semantic analyzer
702625};
703626
704627struct AstNodeUndefinedLiteral {
705 // populated by semantic analyzer
706628};
707629
708630struct AstNodeZeroesLiteral {
709 // populated by semantic analyzer
710631};
711632
712633struct AstNodeThisLiteral {
713 // populated by semantic analyzer
714634};
715635
716636struct AstNodeSymbolExpr {
717637 Buf *symbol;
718
719 // populated by semantic analyzer
720 TypeEnumField *enum_field;
721 uint32_t err_value;
722638};
723639
724640struct AstNodeBoolLiteral {
725641 bool value;
726
727 // populated by semantic analyzer
728642};
729643
730644struct AstNodeBreakExpr {
731 // populated by semantic analyzer
732645};
733646
734647struct AstNodeContinueExpr {
735 // populated by semantic analyzer
736648};
737649
738650struct AstNodeArrayType {
739651 AstNode *size;
740652 AstNode *child_type;
741653 bool is_const;
742
743 // populated by semantic analyzer
744654};
745655
746656struct AstNodeErrorType {
747 // populated by semantic analyzer
748657};
749658
750659struct AstNodeTypeLiteral {
751 // populated by semantic analyzer
752660};
753661
754662struct AstNodeVarLiteral {
755 // populated by semantic analyzer
756663};
757664
758665struct AstNode {
......@@ -761,9 +668,6 @@ struct AstNode {
761668 size_t column;
762669 uint32_t create_index; // for determinism purposes
763670 ImportTableEntry *owner;
764 // the context in which this expression/node is evaluated.
765 // for blocks, this points to the containing scope, not the block's own scope for its children.
766 Scope *scope;
767671 union {
768672 AstNodeRoot root;
769673 AstNodeFnDef fn_def;
......@@ -822,26 +726,12 @@ struct FnTypeParamInfo {
822726 TypeTableEntry *type;
823727};
824728
825struct GenericParamValue {
826 TypeTableEntry *type;
827 AstNode *node;
828 size_t impl_index;
829};
830
831struct GenericFnTypeId {
832 AstNode *decl_node; // the generic fn or container decl node
833 GenericParamValue *generic_params;
834 size_t generic_param_count;
835};
836
837uint32_t generic_fn_type_id_hash(GenericFnTypeId *id);
838bool generic_fn_type_id_eql(GenericFnTypeId *a, GenericFnTypeId *b);
839
840729
841730struct FnTypeId {
842731 TypeTableEntry *return_type;
843732 FnTypeParamInfo *param_info;
844733 size_t param_count;
734 size_t next_param_index;
845735 bool is_var_args;
846736 bool is_naked;
847737 bool is_cold;
......@@ -945,6 +835,7 @@ struct FnGenParamInfo {
945835
946836struct TypeTableEntryFn {
947837 FnTypeId fn_type_id;
838 bool is_generic;
948839 TypeTableEntry *gen_return_type;
949840 size_t gen_param_count;
950841 FnGenParamInfo *gen_param_info;
......@@ -955,10 +846,6 @@ struct TypeTableEntryFn {
955846 TypeTableEntry *bound_fn_parent;
956847};
957848
958struct TypeTableEntryGenericFn {
959 AstNode *decl_node;
960};
961
962849struct TypeTableEntryBoundFn {
963850 TypeTableEntry *fn_type;
964851};
......@@ -993,7 +880,6 @@ enum TypeTableEntryId {
993880 TypeTableEntryIdTypeDecl,
994881 TypeTableEntryIdNamespace,
995882 TypeTableEntryIdBlock,
996 TypeTableEntryIdGenericFn,
997883 TypeTableEntryIdBoundFn,
998884};
999885
......@@ -1018,7 +904,6 @@ struct TypeTableEntry {
1018904 TypeTableEntryUnion unionation;
1019905 TypeTableEntryFn fn;
1020906 TypeTableEntryTypeDecl type_decl;
1021 TypeTableEntryGenericFn generic_fn;
1022907 TypeTableEntryBoundFn bound_fn;
1023908 } data;
1024909
......@@ -1056,10 +941,9 @@ enum FnAnalState {
1056941 FnAnalStateReady,
1057942 FnAnalStateProbing,
1058943 FnAnalStateComplete,
1059 FnAnalStateSkipped,
944 FnAnalStateInvalid,
1060945};
1061946
1062
1063947enum FnInline {
1064948 FnInlineAuto,
1065949 FnInlineAlways,
......@@ -1067,14 +951,18 @@ enum FnInline {
1067951};
1068952
1069953struct FnTableEntry {
1070 LLVMValueRef fn_value;
954 LLVMValueRef llvm_value;
1071955 AstNode *proto_node;
1072956 AstNode *fn_def_node;
957 ScopeFnDef *fndef_scope; // parent should be the top level decls or container decls
958 Scope *child_scope; // parent is scope for last parameter
959 ScopeBlock *def_scope; // parent is child_scope
1073960 ImportTableEntry *import_entry;
1074961 Buf symbol_name;
1075962 TypeTableEntry *type_entry; // function type
963 TypeTableEntry *implicit_return_type;
1076964 bool internal_linkage;
1077 bool is_extern;
965 bool disable_export;
1078966 bool is_test;
1079967 FnInline fn_inline;
1080968 FnAnalState anal_state;
......@@ -1161,11 +1049,10 @@ struct CodeGen {
11611049 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> primitive_type_table;
11621050 HashMap<FnTypeId *, TypeTableEntry *, fn_type_id_hash, fn_type_id_eql> fn_type_table;
11631051 HashMap<Buf *, ErrorTableEntry *, buf_hash, buf_eql_buf> error_table;
1164 HashMap<GenericFnTypeId *, AstNode *, generic_fn_type_id_hash, generic_fn_type_id_eql> generic_table;
11651052
11661053 ZigList<ImportTableEntry *> import_queue;
11671054 size_t import_queue_index;
1168 ZigList<AstNode *> resolve_queue;
1055 ZigList<Tld *> resolve_queue;
11691056 size_t resolve_queue_index;
11701057 ZigList<AstNode *> use_queue;
11711058 size_t use_queue_index;
......@@ -1249,6 +1136,7 @@ struct CodeGen {
12491136 // The function definitions this module includes. There must be a corresponding
12501137 // fn_protos entry.
12511138 ZigList<FnTableEntry *> fn_defs;
1139 size_t fn_defs_index;
12521140 // The function prototypes this module includes. In the case of external declarations,
12531141 // there will not be a corresponding fn_defs entry.
12541142 ZigList<FnTableEntry *> fn_protos;
......@@ -1258,6 +1146,7 @@ struct CodeGen {
12581146 FnTableEntry *cur_fn;
12591147 FnTableEntry *main_fn;
12601148 LLVMValueRef cur_ret_ptr;
1149 LLVMValueRef cur_fn_val;
12611150 ZigList<LLVMBasicBlockRef> break_block_stack;
12621151 ZigList<LLVMBasicBlockRef> continue_block_stack;
12631152 bool c_want_stdint;
......@@ -1295,6 +1184,7 @@ struct CodeGen {
12951184 IrInstruction *invalid_instruction;
12961185};
12971186
1187// TODO after merging IR branch, we can probably delete some of these fields
12981188struct VariableTableEntry {
12991189 Buf name;
13001190 TypeTableEntry *type;
......@@ -1304,8 +1194,6 @@ struct VariableTableEntry {
13041194 bool is_inline;
13051195 // which node is the declaration of the variable
13061196 AstNode *decl_node;
1307 // which node contains the ConstExprValue for this variable's value
1308 AstNode *val_node;
13091197 ZigLLVMDILocalVariable *di_loc_var;
13101198 size_t src_arg_index;
13111199 size_t gen_arg_index;
......@@ -1313,10 +1201,10 @@ struct VariableTableEntry {
13131201 Scope *child_scope;
13141202 LLVMValueRef param_value_ref;
13151203 bool force_depends_on_compile_var;
1316 ImportTableEntry *import;
13171204 bool shadowable;
13181205 size_t mem_slot_index;
13191206 size_t ref_count;
1207 ConstExprValue *value;
13201208};
13211209
13221210struct ErrorTableEntry {
......@@ -1339,9 +1227,6 @@ struct Scope {
13391227 Scope *parent;
13401228
13411229 ZigLLVMDIScope *di_scope;
1342
1343 bool safety_off;
1344 AstNode *safety_set_node;
13451230};
13461231
13471232// This scope comes from global declarations or from
......@@ -1350,15 +1235,12 @@ struct Scope {
13501235struct ScopeDecls {
13511236 Scope base;
13521237
1353 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> decl_table;
1354};
1355
1356// This scope comes from a container declaration such as a struct,
1357// enum, or union.
1358struct ScopeContainer {
1359 Scope base;
1360
1361 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> decl_table;
1238 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> decl_table;
1239 bool safety_off;
1240 AstNode *safety_set_node;
1241 ImportTableEntry *import;
1242 // If this is a scope from a container, this is the type entry, otherwise null
1243 TypeTableEntry *container_type;
13621244};
13631245
13641246// This scope comes from a block expression in user code.
......@@ -1367,6 +1249,8 @@ struct ScopeBlock {
13671249 Scope base;
13681250
13691251 HashMap<Buf *, LabelTableEntry *, buf_hash, buf_eql_buf> label_table;
1252 bool safety_off;
1253 AstNode *safety_set_node;
13701254};
13711255
13721256// This scope is created from every defer expression.
......@@ -1401,7 +1285,7 @@ struct ScopeLoop {
14011285
14021286// This scope is created for a function definition.
14031287// NodeTypeFnDef
1404struct ScopeFnBody {
1288struct ScopeFnDef {
14051289 Scope base;
14061290
14071291 FnTableEntry *fn_entry;
......@@ -1479,6 +1363,7 @@ enum IrInstructionId {
14791363
14801364struct IrInstruction {
14811365 IrInstructionId id;
1366 Scope *scope;
14821367 AstNode *source_node;
14831368 ConstExprValue static_value;
14841369 TypeTableEntry *type_entry;
......@@ -1798,6 +1683,7 @@ struct IrInstructionAsm {
17981683 // Most information on inline assembly comes from the source node.
17991684 IrInstruction **input_list;
18001685 IrInstruction **output_types;
1686 VariableTableEntry **output_vars;
18011687 size_t return_count;
18021688 bool has_side_effects;
18031689};
src/analyze.cpp+496-806
......@@ -19,8 +19,8 @@
1919
2020static const size_t default_backward_branch_quota = 1000;
2121
22static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *enum_type);
23static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *struct_type);
22static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type);
23static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type);
2424
2525AstNode *first_executing_node(AstNode *node) {
2626 switch (node->type) {
......@@ -130,9 +130,82 @@ ScopeDecls *get_container_scope(TypeTableEntry *type_entry) {
130130 return *get_container_scope_ptr(type_entry);
131131}
132132
133void init_scope(Scope *dest, AstNode *node, Scope *parent) {
134 dest->node = node;
135 dest->parent = parent;
136}
137
138static ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, TypeTableEntry *container_type, ImportTableEntry *import) {
139 assert(node->type == NodeTypeRoot || node->type == NodeTypeContainerDecl);
140 ScopeDecls *scope = allocate<ScopeDecls>(1);
141 init_scope(&scope->base, node, parent);
142 scope->decl_table.init(4);
143 scope->container_type = container_type;
144 scope->import = import;
145 return scope;
146}
147
148Scope *create_block_scope(AstNode *node, Scope *parent) {
149 assert(node->type == NodeTypeBlock);
150 ScopeBlock *scope = allocate<ScopeBlock>(1);
151 init_scope(&scope->base, node, parent);
152 scope->label_table.init(1);
153 return &scope->base;
154}
155
156Scope *create_defer_scope(AstNode *node, Scope *parent) {
157 assert(node->type == NodeTypeDefer);
158 ScopeDefer *scope = allocate<ScopeDefer>(1);
159 init_scope(&scope->base, node, parent);
160 return &scope->base;
161}
162
163Scope *create_var_scope(AstNode *node, Scope *parent, VariableTableEntry *var) {
164 assert(node->type == NodeTypeVariableDeclaration || node->type == NodeTypeParamDecl);
165 ScopeVarDecl *scope = allocate<ScopeVarDecl>(1);
166 init_scope(&scope->base, node, parent);
167 scope->var = var;
168 return &scope->base;
169}
170
171Scope *create_cimport_scope(AstNode *node, Scope *parent) {
172 assert(node->type == NodeTypeFnCallExpr);
173 ScopeCImport *scope = allocate<ScopeCImport>(1);
174 init_scope(&scope->base, node, parent);
175 buf_resize(&scope->c_import_buf, 0);
176 return &scope->base;
177}
178
179Scope *create_loop_scope(AstNode *node, Scope *parent) {
180 assert(node->type == NodeTypeWhileExpr || node->type == NodeTypeForExpr);
181 ScopeLoop *scope = allocate<ScopeLoop>(1);
182 init_scope(&scope->base, node, parent);
183 return &scope->base;
184}
185
186ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry) {
187 assert(node->type == NodeTypeFnDef);
188 ScopeFnDef *scope = allocate<ScopeFnDef>(1);
189 init_scope(&scope->base, node, parent);
190 scope->fn_entry = fn_entry;
191 return scope;
192}
193
194ImportTableEntry *get_scope_import(Scope *scope) {
195 while (scope) {
196 if (scope->node->type == NodeTypeRoot || scope->node->type == NodeTypeContainerDecl) {
197 ScopeDecls *decls_scope = (ScopeDecls *)scope;
198 assert(decls_scope->import);
199 return decls_scope->import;
200 }
201 scope = scope->parent;
202 }
203 zig_unreachable();
204}
205
133206static TypeTableEntry *new_container_type_entry(TypeTableEntryId id, AstNode *source_node, Scope *parent_scope) {
134207 TypeTableEntry *entry = new_type_table_entry(id);
135 *get_container_scope_ptr(entry) = create_decls_scope(source_node, parent_scope);
208 *get_container_scope_ptr(entry) = create_decls_scope(source_node, parent_scope, entry, get_scope_import(parent_scope));
136209 return entry;
137210}
138211
......@@ -179,7 +252,6 @@ bool type_is_complete(TypeTableEntry *type_entry) {
179252 case TypeTableEntryIdTypeDecl:
180253 case TypeTableEntryIdNamespace:
181254 case TypeTableEntryIdBlock:
182 case TypeTableEntryIdGenericFn:
183255 case TypeTableEntryIdBoundFn:
184256 return true;
185257 }
......@@ -202,14 +274,6 @@ TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {
202274 return get_int_type(g, false, bits_needed_for_unsigned(x));
203275}
204276
205static TypeTableEntry *get_generic_fn_type(CodeGen *g, AstNode *decl_node) {
206 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdGenericFn);
207 buf_init_from_str(&entry->name, "(generic function)");
208 entry->zero_bits = true;
209 entry->data.generic_fn.decl_node = decl_node;
210 return entry;
211}
212
213277TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const) {
214278 assert(child_type->id != TypeTableEntryIdInvalid);
215279 TypeTableEntry **parent_pointer = &child_type->pointer_parent[(is_const ? 1 : 0)];
......@@ -643,8 +707,7 @@ TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry) {
643707 return bound_fn_type;
644708}
645709
646// accepts ownership of fn_type_id memory
647TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id, bool gen_debug_info) {
710TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
648711 auto table_entry = g->fn_type_table.maybe_get(fn_type_id);
649712 if (table_entry) {
650713 return table_entry->value;
......@@ -685,67 +748,65 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id, bool gen_debug_inf
685748 buf_appendf(&fn_type->name, " -> %s", buf_ptr(&fn_type_id->return_type->name));
686749 }
687750
688 if (gen_debug_info) {
689 // next, loop over the parameters again and compute debug information
690 // and codegen information
691 bool first_arg_return = !fn_type_id->is_extern && handle_is_ptr(fn_type_id->return_type);
692 // +1 for maybe making the first argument the return value
693 LLVMTypeRef *gen_param_types = allocate<LLVMTypeRef>(1 + fn_type_id->param_count);
694 // +1 because 0 is the return type and +1 for maybe making first arg ret val
695 ZigLLVMDIType **param_di_types = allocate<ZigLLVMDIType*>(2 + fn_type_id->param_count);
696 param_di_types[0] = fn_type_id->return_type->di_type;
697 size_t gen_param_index = 0;
698 TypeTableEntry *gen_return_type;
699 if (!type_has_bits(fn_type_id->return_type)) {
700 gen_return_type = g->builtin_types.entry_void;
701 } else if (first_arg_return) {
702 TypeTableEntry *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
751 // next, loop over the parameters again and compute debug information
752 // and codegen information
753 bool first_arg_return = !fn_type_id->is_extern && handle_is_ptr(fn_type_id->return_type);
754 // +1 for maybe making the first argument the return value
755 LLVMTypeRef *gen_param_types = allocate<LLVMTypeRef>(1 + fn_type_id->param_count);
756 // +1 because 0 is the return type and +1 for maybe making first arg ret val
757 ZigLLVMDIType **param_di_types = allocate<ZigLLVMDIType*>(2 + fn_type_id->param_count);
758 param_di_types[0] = fn_type_id->return_type->di_type;
759 size_t gen_param_index = 0;
760 TypeTableEntry *gen_return_type;
761 if (!type_has_bits(fn_type_id->return_type)) {
762 gen_return_type = g->builtin_types.entry_void;
763 } else if (first_arg_return) {
764 TypeTableEntry *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
765 gen_param_types[gen_param_index] = gen_type->type_ref;
766 gen_param_index += 1;
767 // after the gen_param_index += 1 because 0 is the return type
768 param_di_types[gen_param_index] = gen_type->di_type;
769 gen_return_type = g->builtin_types.entry_void;
770 } else {
771 gen_return_type = fn_type_id->return_type;
772 }
773 fn_type->data.fn.gen_return_type = gen_return_type;
774
775 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
776 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
777 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
778 TypeTableEntry *type_entry = src_param_info->type;
779 FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i];
780
781 gen_param_info->src_index = i;
782 gen_param_info->gen_index = SIZE_MAX;
783
784 assert(type_is_complete(type_entry));
785 if (type_has_bits(type_entry)) {
786 TypeTableEntry *gen_type;
787 if (handle_is_ptr(type_entry)) {
788 gen_type = get_pointer_to_type(g, type_entry, true);
789 gen_param_info->is_byval = true;
790 } else {
791 gen_type = type_entry;
792 }
703793 gen_param_types[gen_param_index] = gen_type->type_ref;
794 gen_param_info->gen_index = gen_param_index;
795 gen_param_info->type = gen_type;
796
704797 gen_param_index += 1;
798
705799 // after the gen_param_index += 1 because 0 is the return type
706800 param_di_types[gen_param_index] = gen_type->di_type;
707 gen_return_type = g->builtin_types.entry_void;
708 } else {
709 gen_return_type = fn_type_id->return_type;
710801 }
711 fn_type->data.fn.gen_return_type = gen_return_type;
712
713 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
714 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
715 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
716 TypeTableEntry *type_entry = src_param_info->type;
717 FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i];
718
719 gen_param_info->src_index = i;
720 gen_param_info->gen_index = SIZE_MAX;
721
722 assert(type_is_complete(type_entry));
723 if (type_has_bits(type_entry)) {
724 TypeTableEntry *gen_type;
725 if (handle_is_ptr(type_entry)) {
726 gen_type = get_pointer_to_type(g, type_entry, true);
727 gen_param_info->is_byval = true;
728 } else {
729 gen_type = type_entry;
730 }
731 gen_param_types[gen_param_index] = gen_type->type_ref;
732 gen_param_info->gen_index = gen_param_index;
733 gen_param_info->type = gen_type;
802 }
734803
735 gen_param_index += 1;
804 fn_type->data.fn.gen_param_count = gen_param_index;
736805
737 // after the gen_param_index += 1 because 0 is the return type
738 param_di_types[gen_param_index] = gen_type->di_type;
739 }
740 }
741
742 fn_type->data.fn.gen_param_count = gen_param_index;
743
744 fn_type->data.fn.raw_type_ref = LLVMFunctionType(gen_return_type->type_ref,
745 gen_param_types, gen_param_index, fn_type_id->is_var_args);
746 fn_type->type_ref = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
747 fn_type->di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types, gen_param_index + 1, 0);
748 }
806 fn_type->data.fn.raw_type_ref = LLVMFunctionType(gen_return_type->type_ref,
807 gen_param_types, gen_param_index, fn_type_id->is_var_args);
808 fn_type->type_ref = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
809 fn_type->di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types, gen_param_index + 1, 0);
749810
750811 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);
751812
......@@ -767,6 +828,7 @@ static TypeTableEntryId container_to_type(ContainerKind kind) {
767828TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import, Scope *scope,
768829 ContainerKind kind, AstNode *decl_node, const char *name)
769830{
831
770832 TypeTableEntryId type_id = container_to_type(kind);
771833 TypeTableEntry *entry = new_container_type_entry(type_id, decl_node, scope);
772834
......@@ -782,7 +844,7 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import,
782844 break;
783845 }
784846
785 unsigned line = decl_node ? decl_node->line : 0;
847 unsigned line = decl_node->line;
786848
787849 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
788850 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
......@@ -794,6 +856,14 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import,
794856 return entry;
795857}
796858
859static TypeTableEntry *get_partial_container_type_tld(CodeGen *g, Scope *scope, TldContainer *tld_container) {
860 ImportTableEntry *import = tld_container->base.import;
861 AstNode *container_node = tld_container->base.source_node;
862 assert(container_node->type == NodeTypeContainerDecl);
863 ContainerKind kind = container_node->data.struct_decl.kind;
864 return get_partial_container_type(g, import, scope, kind, container_node, buf_ptr(tld_container->base.name));
865}
866
797867
798868TypeTableEntry *get_underlying_type(TypeTableEntry *type_entry) {
799869 if (type_entry->id == TypeTableEntryIdTypeDecl) {
......@@ -842,9 +912,7 @@ static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *nod
842912 return result;
843913}
844914
845static TypeTableEntry *analyze_type_expr(CodeGen *g, ImportTableEntry *import, Scope *scope,
846 AstNode *node)
847{
915static TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
848916 IrInstruction *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type);
849917 if (result->type_entry->id == TypeTableEntryIdInvalid)
850918 return g->builtin_types.entry_invalid;
......@@ -853,71 +921,77 @@ static TypeTableEntry *analyze_type_expr(CodeGen *g, ImportTableEntry *import, S
853921 return result->static_value.data.x_type;
854922}
855923
856static bool fn_wants_full_static_eval(FnTableEntry *fn_table_entry) {
857 assert(fn_table_entry);
858 return false;
924static TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
925 TypeTableEntry *fn_type = new_type_table_entry(TypeTableEntryIdFn);
926 fn_type->data.fn.fn_type_id = *fn_type_id;
927 fn_type->data.fn.is_generic = true;
928 return fn_type;
859929}
860930
861// fn_table_entry is populated if and only if there is a function definition for this prototype
862static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *import, Scope *scope,
863 TypeTableEntry *expected_type, AstNode *node, bool is_naked, bool is_cold, FnTableEntry *fn_table_entry)
864{
865 assert(node->type == NodeTypeFnProto);
866 AstNodeFnProto *fn_proto = &node->data.fn_proto;
867
868 if (fn_proto->skip) {
869 return g->builtin_types.entry_invalid;
870 }
931static TypeTableEntry *analyze_fn_type(CodeGen *g, TldFn *tld_fn) {
932 AstNode *proto_node = tld_fn->base.source_node;
933 assert(proto_node->type == NodeTypeFnProto);
934 AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
871935
872936 FnTypeId fn_type_id = {0};
873 fn_type_id.is_extern = fn_proto->is_extern || (fn_proto->top_level_decl.visib_mod == VisibModExport);
874 fn_type_id.is_naked = is_naked;
875 fn_type_id.is_cold = is_cold;
937 fn_type_id.is_extern = fn_proto->is_extern || (fn_proto->visib_mod == VisibModExport);
938 fn_type_id.is_naked = fn_proto->is_nakedcc;
939 fn_type_id.is_cold = fn_proto->is_coldcc;
876940 fn_type_id.param_count = fn_proto->params.length;
877941 fn_type_id.param_info = allocate_nonzero<FnTypeParamInfo>(fn_type_id.param_count);
878
942 fn_type_id.next_param_index = 0;
879943 fn_type_id.is_var_args = fn_proto->is_var_args;
880944
881 for (size_t i = 0; i < fn_type_id.param_count; i += 1) {
882 AstNode *child = fn_proto->params.at(i);
883 assert(child->type == NodeTypeParamDecl);
945 FnTableEntry *fn_entry = tld_fn->fn_entry;
946 Scope *child_scope = fn_entry->fndef_scope ? &fn_entry->fndef_scope->base : tld_fn->base.parent_scope;
884947
885 TypeTableEntry *type_entry;
886 if (fn_proto->skip) {
887 type_entry = g->builtin_types.entry_invalid;
888 } else {
889 type_entry = analyze_type_expr(g, import, scope, child->data.param_decl.type);
948 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
949 AstNode *param_node = fn_proto->params.at(fn_type_id.next_param_index);
950 assert(param_node->type == NodeTypeParamDecl);
951
952 bool param_is_inline = param_node->data.param_decl.is_inline;
953
954 if (param_is_inline) {
955 if (fn_type_id.is_extern) {
956 add_node_error(g, param_node,
957 buf_sprintf("inline parameter not allowed in extern function"));
958 return g->builtin_types.entry_invalid;
959 }
960 return get_generic_fn_type(g, &fn_type_id);
961 }
962
963 if (fn_entry && buf_len(param_node->data.param_decl.name) == 0) {
964 add_node_error(g, param_node, buf_sprintf("missing parameter name"));
890965 }
891966
967 TypeTableEntry *type_entry = analyze_type_expr(g, child_scope, param_node->data.param_decl.type);
968
892969 switch (type_entry->id) {
893970 case TypeTableEntryIdInvalid:
894 fn_proto->skip = true;
895 break;
896 case TypeTableEntryIdNumLitFloat:
897 case TypeTableEntryIdNumLitInt:
971 return g->builtin_types.entry_invalid;
972 case TypeTableEntryIdUnreachable:
898973 case TypeTableEntryIdUndefLit:
899974 case TypeTableEntryIdNullLit:
900 case TypeTableEntryIdUnreachable:
975 add_node_error(g, param_node->data.param_decl.type,
976 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));
977 return g->builtin_types.entry_invalid;
978 case TypeTableEntryIdVar:
979 if (fn_type_id.is_extern) {
980 add_node_error(g, param_node->data.param_decl.type,
981 buf_sprintf("parameter of type 'var' not allowed in extern function"));
982 return g->builtin_types.entry_invalid;
983 }
984 return get_generic_fn_type(g, &fn_type_id);
985 case TypeTableEntryIdNumLitFloat:
986 case TypeTableEntryIdNumLitInt:
901987 case TypeTableEntryIdNamespace:
902988 case TypeTableEntryIdBlock:
903 case TypeTableEntryIdGenericFn:
904989 case TypeTableEntryIdBoundFn:
905 if (!fn_proto->skip) {
906 fn_proto->skip = true;
907 add_node_error(g, child->data.param_decl.type,
908 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));
909 }
910 break;
911990 case TypeTableEntryIdMetaType:
912 if (!child->data.param_decl.is_inline) {
913 if (!fn_proto->skip) {
914 fn_proto->skip = true;
915 add_node_error(g, child->data.param_decl.type,
916 buf_sprintf("parameter of type '%s' must be declared inline",
917 buf_ptr(&type_entry->name)));
918 }
919 }
920 break;
991 add_node_error(g, param_node->data.param_decl.type,
992 buf_sprintf("parameter of type '%s' must be declared inline",
993 buf_ptr(&type_entry->name)));
994 return g->builtin_types.entry_invalid;
921995 case TypeTableEntryIdVoid:
922996 case TypeTableEntryIdBool:
923997 case TypeTableEntryIdInt:
......@@ -933,44 +1007,38 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
9331007 case TypeTableEntryIdFn:
9341008 case TypeTableEntryIdTypeDecl:
9351009 break;
936 case TypeTableEntryIdVar:
937 // var types are treated as generic functions; if we get to this code we should
938 // already be an instantiated function.
939 zig_unreachable();
9401010 }
941 if (type_entry->id == TypeTableEntryIdInvalid) {
942 fn_proto->skip = true;
943 }
944 FnTypeParamInfo *param_info = &fn_type_id.param_info[i];
1011 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
9451012 param_info->type = type_entry;
946 param_info->is_noalias = child->data.param_decl.is_noalias;
1013 param_info->is_noalias = param_node->data.param_decl.is_noalias;
9471014 }
9481015
949 if (fn_proto->skip) {
950 fn_type_id.return_type = g->builtin_types.entry_invalid;
951 } else {
952 fn_type_id.return_type = analyze_type_expr(g, import, scope, fn_proto->return_type);
953 }
1016 fn_type_id.return_type = analyze_type_expr(g, child_scope, fn_proto->return_type);
1017
9541018 switch (fn_type_id.return_type->id) {
9551019 case TypeTableEntryIdInvalid:
956 fn_proto->skip = true;
957 break;
958 case TypeTableEntryIdNumLitFloat:
959 case TypeTableEntryIdNumLitInt:
1020 return g->builtin_types.entry_invalid;
1021
9601022 case TypeTableEntryIdUndefLit:
9611023 case TypeTableEntryIdNullLit:
1024 add_node_error(g, fn_proto->return_type,
1025 buf_sprintf("return type '%s' not allowed", buf_ptr(&fn_type_id.return_type->name)));
1026 return g->builtin_types.entry_invalid;
1027
1028 case TypeTableEntryIdNumLitFloat:
1029 case TypeTableEntryIdNumLitInt:
9621030 case TypeTableEntryIdNamespace:
9631031 case TypeTableEntryIdBlock:
964 case TypeTableEntryIdGenericFn:
9651032 case TypeTableEntryIdBoundFn:
9661033 case TypeTableEntryIdVar:
967 if (!fn_proto->skip) {
968 fn_proto->skip = true;
1034 case TypeTableEntryIdMetaType:
1035 if (fn_type_id.is_extern) {
9691036 add_node_error(g, fn_proto->return_type,
970 buf_sprintf("return type '%s' not allowed", buf_ptr(&fn_type_id.return_type->name)));
1037 buf_sprintf("return type '%s' not allowed in extern function",
1038 buf_ptr(&fn_type_id.return_type->name)));
1039 return g->builtin_types.entry_invalid;
9711040 }
972 break;
973 case TypeTableEntryIdMetaType:
1041 return get_generic_fn_type(g, &fn_type_id);
9741042 case TypeTableEntryIdUnreachable:
9751043 case TypeTableEntryIdVoid:
9761044 case TypeTableEntryIdBool:
......@@ -989,114 +1057,10 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
9891057 break;
9901058 }
9911059
992
993 if (fn_proto->skip) {
994 return g->builtin_types.entry_invalid;
995 }
996
997 if (fn_table_entry && fn_type_id.return_type->id == TypeTableEntryIdMetaType) {
998 ErrorMsg *err_msg = nullptr;
999 for (size_t i = 0; i < fn_proto->params.length; i += 1) {
1000 AstNode *param_decl_node = fn_proto->params.at(i);
1001 assert(param_decl_node->type == NodeTypeParamDecl);
1002 if (!param_decl_node->data.param_decl.is_inline) {
1003 if (!err_msg) {
1004 err_msg = add_node_error(g, fn_proto->return_type,
1005 buf_sprintf("function with return type '%s' must declare all parameters inline",
1006 buf_ptr(&fn_type_id.return_type->name)));
1007 }
1008 add_error_note(g, err_msg, param_decl_node,
1009 buf_sprintf("non-inline parameter here"));
1010 }
1011 }
1012 if (err_msg) {
1013 fn_proto->skip = true;
1014 return g->builtin_types.entry_invalid;
1015 }
1016 }
1017
1018 bool gen_debug_info = fn_table_entry && !fn_wants_full_static_eval(fn_table_entry);
1019 return get_fn_type(g, &fn_type_id, gen_debug_info);
1020}
1021
1022static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_table_entry,
1023 ImportTableEntry *import, Scope *containing_scope)
1024{
1025 assert(node->type == NodeTypeFnProto);
1026 AstNodeFnProto *fn_proto = &node->data.fn_proto;
1027
1028 if (fn_proto->skip) {
1029 return;
1030 }
1031
1032 bool is_internal = (fn_proto->top_level_decl.visib_mod != VisibModExport);
1033 bool is_c_compat = !is_internal || fn_proto->is_extern;
1034 fn_table_entry->internal_linkage = !is_c_compat;
1035
1036
1037
1038 TypeTableEntry *fn_type = analyze_fn_proto_type(g, import, containing_scope, nullptr, node,
1039 fn_proto->is_nakedcc, fn_proto->is_coldcc, fn_table_entry);
1040
1041 fn_table_entry->type_entry = fn_type;
1042
1043 if (fn_type->id == TypeTableEntryIdInvalid) {
1044 fn_proto->skip = true;
1045 return;
1046 }
1047
1048 if (fn_proto->is_inline) {
1049 fn_table_entry->fn_inline = FnInlineAlways;
1050 }
1051
1052
1053 Buf *symbol_name;
1054 if (is_c_compat) {
1055 symbol_name = &fn_table_entry->symbol_name;
1056 } else {
1057 symbol_name = buf_sprintf("_%s", buf_ptr(&fn_table_entry->symbol_name));
1058 }
1059
1060 if (fn_table_entry->fn_def_node) {
1061 fn_table_entry->fn_def_node->data.fn_def.containing_scope = create_fndef_scope(
1062 fn_table_entry->fn_def_node, containing_scope, fn_table_entry);
1063 }
1064
1065 if (!fn_wants_full_static_eval(fn_table_entry)) {
1066 fn_table_entry->fn_value = LLVMAddFunction(g->module, buf_ptr(symbol_name), fn_type->data.fn.raw_type_ref);
1067
1068 switch (fn_table_entry->fn_inline) {
1069 case FnInlineAlways:
1070 LLVMAddFunctionAttr(fn_table_entry->fn_value, LLVMAlwaysInlineAttribute);
1071 break;
1072 case FnInlineNever:
1073 LLVMAddFunctionAttr(fn_table_entry->fn_value, LLVMNoInlineAttribute);
1074 break;
1075 case FnInlineAuto:
1076 break;
1077 }
1078 if (fn_type->data.fn.fn_type_id.is_naked) {
1079 LLVMAddFunctionAttr(fn_table_entry->fn_value, LLVMNakedAttribute);
1080 }
1081
1082 LLVMSetLinkage(fn_table_entry->fn_value, fn_table_entry->internal_linkage ?
1083 LLVMInternalLinkage : LLVMExternalLinkage);
1084
1085 if (fn_type->data.fn.fn_type_id.return_type->id == TypeTableEntryIdUnreachable) {
1086 LLVMAddFunctionAttr(fn_table_entry->fn_value, LLVMNoReturnAttribute);
1087 }
1088 LLVMSetFunctionCallConv(fn_table_entry->fn_value, fn_type->data.fn.calling_convention);
1089 if (!fn_table_entry->is_extern) {
1090 LLVMAddFunctionAttr(fn_table_entry->fn_value, LLVMNoUnwindAttribute);
1091 }
1092 if (!g->is_release_build && fn_table_entry->fn_inline != FnInlineAlways) {
1093 ZigLLVMAddFunctionAttr(fn_table_entry->fn_value, "no-frame-pointer-elim", "true");
1094 ZigLLVMAddFunctionAttr(fn_table_entry->fn_value, "no-frame-pointer-elim-non-leaf", nullptr);
1095 }
1096 }
1060 return get_fn_type(g, &fn_type_id);
10971061}
10981062
1099static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *enum_type) {
1063static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
11001064 // if you change this logic you likely must also change similar logic in parseh.cpp
11011065 assert(enum_type->id == TypeTableEntryIdEnum);
11021066
......@@ -1133,6 +1097,7 @@ static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEnt
11331097 uint64_t biggest_union_member_size_in_bits = 0;
11341098
11351099 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
1100 ImportTableEntry *import = get_scope_import(scope);
11361101
11371102 // set temporary flag
11381103 enum_type->data.enumeration.embedded_in_current = true;
......@@ -1142,17 +1107,16 @@ static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEnt
11421107 AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
11431108 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i];
11441109 type_enum_field->name = field_node->data.struct_field.name;
1145 TypeTableEntry *field_type = analyze_type_expr(g, import, scope,
1146 field_node->data.struct_field.type);
1110 TypeTableEntry *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
11471111 type_enum_field->type_entry = field_type;
11481112 type_enum_field->value = i;
11491113
11501114 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(type_enum_field->name), i);
11511115
11521116 if (field_type->id == TypeTableEntryIdStruct) {
1153 resolve_struct_type(g, import, field_type);
1117 resolve_struct_type(g, field_type);
11541118 } else if (field_type->id == TypeTableEntryIdEnum) {
1155 resolve_enum_type(g, import, field_type);
1119 resolve_enum_type(g, field_type);
11561120 } else if (field_type->id == TypeTableEntryIdInvalid) {
11571121 enum_type->data.enumeration.is_invalid = true;
11581122 continue;
......@@ -1281,7 +1245,7 @@ static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEnt
12811245 }
12821246}
12831247
1284static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *struct_type) {
1248static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
12851249 // if you change the logic of this function likely you must make a similar change in
12861250 // parseh.cpp
12871251 assert(struct_type->id == TypeTableEntryIdStruct);
......@@ -1319,22 +1283,23 @@ static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableE
13191283 struct_type->data.structure.embedded_in_current = true;
13201284
13211285 Scope *scope = &struct_type->data.structure.decls_scope->base;
1286 ImportTableEntry *import = get_scope_import(scope);
13221287
13231288 size_t gen_field_index = 0;
13241289 for (size_t i = 0; i < field_count; i += 1) {
13251290 AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
13261291 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
13271292 type_struct_field->name = field_node->data.struct_field.name;
1328 TypeTableEntry *field_type = analyze_type_expr(g, import, scope,
1293 TypeTableEntry *field_type = analyze_type_expr(g, scope,
13291294 field_node->data.struct_field.type);
13301295 type_struct_field->type_entry = field_type;
13311296 type_struct_field->src_index = i;
13321297 type_struct_field->gen_index = SIZE_MAX;
13331298
13341299 if (field_type->id == TypeTableEntryIdStruct) {
1335 resolve_struct_type(g, import, field_type);
1300 resolve_struct_type(g, field_type);
13361301 } else if (field_type->id == TypeTableEntryIdEnum) {
1337 resolve_enum_type(g, import, field_type);
1302 resolve_enum_type(g, field_type);
13381303 } else if (field_type->id == TypeTableEntryIdInvalid) {
13391304 struct_type->data.structure.is_invalid = true;
13401305 continue;
......@@ -1408,16 +1373,13 @@ static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableE
14081373 struct_type->zero_bits = (debug_size_in_bits == 0);
14091374}
14101375
1411static void resolve_union_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *enum_type) {
1376static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
14121377 zig_panic("TODO");
14131378}
14141379
1415static void get_fully_qualified_decl_name(Buf *buf, AstNode *decl_node, uint8_t sep) {
1416 TopLevelDecl *tld = get_as_top_level_decl(decl_node);
1417 AstNode *parent_decl = tld->parent_decl;
1418
1419 if (parent_decl) {
1420 get_fully_qualified_decl_name(buf, parent_decl, sep);
1380static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, uint8_t sep) {
1381 if (tld->parent_tld) {
1382 get_fully_qualified_decl_name(buf, tld->parent_tld, sep);
14211383 buf_append_char(buf, sep);
14221384 buf_append_buf(buf, tld->name);
14231385 } else {
......@@ -1425,183 +1387,109 @@ static void get_fully_qualified_decl_name(Buf *buf, AstNode *decl_node, uint8_t
14251387 }
14261388}
14271389
1428static void preview_generic_fn_proto(CodeGen *g, ImportTableEntry *import, AstNode *node) {
1429 assert(node->type == NodeTypeContainerDecl);
1430
1431 if (node->data.struct_decl.generic_params_is_var_args) {
1432 add_node_error(g, node, buf_sprintf("generic parameters cannot be var args"));
1433 node->data.struct_decl.skip = true;
1434 node->data.struct_decl.generic_fn_type = g->builtin_types.entry_invalid;
1435 return;
1436 }
1437
1438 node->data.struct_decl.generic_fn_type = get_generic_fn_type(g, node);
1439}
1440
1441static bool get_is_generic_fn(AstNode *proto_node) {
1390static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
1391 ImportTableEntry *import = tld_fn->base.import;
1392 AstNode *proto_node = tld_fn->base.source_node;
14421393 assert(proto_node->type == NodeTypeFnProto);
1443 return proto_node->data.fn_proto.inline_or_var_type_arg_count > 0;
1444}
1445
1446static void preview_fn_proto_instance(CodeGen *g, ImportTableEntry *import, AstNode *proto_node,
1447 Scope *containing_scope)
1448{
1449 assert(proto_node->type == NodeTypeFnProto);
1450
1451 if (proto_node->data.fn_proto.skip) {
1452 return;
1453 }
1394 AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
14541395
1455 bool is_generic_instance = proto_node->data.fn_proto.generic_proto_node;
1456 bool is_generic_fn = get_is_generic_fn(proto_node);
1457 assert(!is_generic_instance || !is_generic_fn);
1396 AstNode *fn_def_node = fn_proto->fn_def_node;
14581397
1459 AstNode *parent_decl = proto_node->data.fn_proto.top_level_decl.parent_decl;
1460 Buf *proto_name = proto_node->data.fn_proto.name;
1461
1462 AstNode *fn_def_node = proto_node->data.fn_proto.fn_def_node;
1463 bool is_extern = proto_node->data.fn_proto.is_extern;
1464
1465 assert(!is_extern || !is_generic_instance);
1466
1467 if (fn_def_node && proto_node->data.fn_proto.is_var_args) {
1398 if (fn_def_node && fn_proto->is_var_args) {
14681399 add_node_error(g, proto_node,
14691400 buf_sprintf("variadic arguments only allowed in extern function declarations"));
1401 tld_fn->base.resolution = TldResolutionInvalid;
1402 return;
14701403 }
14711404
14721405 FnTableEntry *fn_table_entry = allocate<FnTableEntry>(1);
14731406 fn_table_entry->analyzed_executable.backward_branch_quota = default_backward_branch_quota;
1407 fn_table_entry->ir_executable.fn_entry = fn_table_entry;
1408 fn_table_entry->analyzed_executable.fn_entry = fn_table_entry;
14741409 fn_table_entry->import_entry = import;
14751410 fn_table_entry->proto_node = proto_node;
14761411 fn_table_entry->fn_def_node = fn_def_node;
1477 fn_table_entry->is_extern = is_extern;
1412 fn_table_entry->fn_inline = fn_proto->is_inline ? FnInlineAlways : FnInlineAuto;
1413 fn_table_entry->internal_linkage = (fn_proto->visib_mod != VisibModExport);
14781414
1479 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, proto_node, '_');
1415 get_fully_qualified_decl_name(&fn_table_entry->symbol_name, &tld_fn->base, '_');
14801416
1481 proto_node->data.fn_proto.fn_table_entry = fn_table_entry;
1417 tld_fn->fn_entry = fn_table_entry;
14821418
1483 if (is_generic_fn) {
1484 fn_table_entry->type_entry = get_generic_fn_type(g, proto_node);
1419 if (fn_table_entry->fn_def_node) {
1420 fn_table_entry->fndef_scope = create_fndef_scope(
1421 fn_table_entry->fn_def_node, tld_fn->base.parent_scope, fn_table_entry);
1422 }
14851423
1486 if (is_extern || proto_node->data.fn_proto.top_level_decl.visib_mod == VisibModExport) {
1487 for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) {
1488 AstNode *param_decl_node = proto_node->data.fn_proto.params.at(i);
1489 if (param_decl_node->data.param_decl.is_inline) {
1490 proto_node->data.fn_proto.skip = true;
1491 add_node_error(g, param_decl_node,
1492 buf_sprintf("inline parameter not allowed in extern function"));
1493 }
1494 }
1495 }
1424 fn_table_entry->type_entry = analyze_fn_type(g, tld_fn);
14961425
1426 if (fn_table_entry->type_entry->id == TypeTableEntryIdInvalid) {
1427 tld_fn->base.resolution = TldResolutionInvalid;
1428 return;
1429 }
14971430
1498 } else {
1499 resolve_function_proto(g, proto_node, fn_table_entry, import, containing_scope);
1431 if (!fn_table_entry->type_entry->data.fn.is_generic) {
1432 g->fn_protos.append(fn_table_entry);
15001433
1501 if (!fn_wants_full_static_eval(fn_table_entry)) {
1502 g->fn_protos.append(fn_table_entry);
1434 if (fn_def_node)
1435 g->fn_defs.append(fn_table_entry);
15031436
1504 if (fn_def_node) {
1505 g->fn_defs.append(fn_table_entry);
1506 }
1437 Tld *parent_tld = tld_fn->base.parent_tld;
1438 bool is_main_fn = !parent_tld && (import == g->root_import) &&
1439 buf_eql_str(&fn_table_entry->symbol_name, "main");
1440 if (is_main_fn)
1441 g->main_fn = fn_table_entry;
15071442
1508 bool is_main_fn = !is_generic_instance &&
1509 !parent_decl && (import == g->root_import) &&
1510 !proto_node->data.fn_proto.skip &&
1511 buf_eql_str(proto_name, "main");
1512 if (is_main_fn) {
1513 g->main_fn = fn_table_entry;
1514 }
1515
1516 if (is_main_fn && !g->link_libc) {
1517 TypeTableEntry *err_void = get_error_type(g, g->builtin_types.entry_void);
1518 TypeTableEntry *actual_return_type = fn_table_entry->type_entry->data.fn.fn_type_id.return_type;
1519 if (actual_return_type != err_void) {
1520 AstNode *return_type_node = fn_table_entry->proto_node->data.fn_proto.return_type;
1521 add_node_error(g, return_type_node,
1522 buf_sprintf("expected return type of main to be '%%void', instead is '%s'",
1523 buf_ptr(&actual_return_type->name)));
1524 }
1443 if (is_main_fn && !g->link_libc) {
1444 TypeTableEntry *err_void = get_error_type(g, g->builtin_types.entry_void);
1445 TypeTableEntry *actual_return_type = fn_table_entry->type_entry->data.fn.fn_type_id.return_type;
1446 if (actual_return_type != err_void) {
1447 add_node_error(g, fn_proto->return_type,
1448 buf_sprintf("expected return type of main to be '%%void', instead is '%s'",
1449 buf_ptr(&actual_return_type->name)));
15251450 }
15261451 }
15271452 }
15281453}
15291454
1530static void add_top_level_decl(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope,
1531 AstNode *node, Buf *name)
1532{
1533 assert(import);
1534
1535 TopLevelDecl *tld = get_as_top_level_decl(node);
1536 tld->import = import;
1537 tld->name = name;
1538
1455static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
15391456 bool want_to_resolve = (g->check_unused || g->is_test_build || tld->visib_mod == VisibModExport);
1540 bool is_generic_container = (node->type == NodeTypeContainerDecl &&
1541 node->data.struct_decl.generic_params.length > 0);
1542 if (want_to_resolve && !is_generic_container) {
1543 g->resolve_queue.append(node);
1544 }
1457 if (want_to_resolve)
1458 g->resolve_queue.append(tld);
15451459
1546 node->scope = &decls_scope->base;
1547
1548 auto entry = decls_scope->decl_table.put_unique(name, node);
1460 auto entry = decls_scope->decl_table.put_unique(tld->name, tld);
15491461 if (entry) {
1550 AstNode *other_decl_node = entry->value;
1551 ErrorMsg *msg = add_node_error(g, node, buf_sprintf("redefinition of '%s'", buf_ptr(name)));
1552 add_error_note(g, msg, other_decl_node, buf_sprintf("previous definition is here"));
1462 Tld *other_tld = entry->value;
1463 ErrorMsg *msg = add_node_error(g, tld->source_node, buf_sprintf("redefinition of '%s'", buf_ptr(tld->name)));
1464 add_error_note(g, msg, other_tld->source_node, buf_sprintf("previous definition is here"));
1465 return;
15531466 }
15541467}
15551468
1556static void scan_struct_decl(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node) {
1557 assert(node->type == NodeTypeContainerDecl);
1469static void scan_struct_decl(CodeGen *g, ScopeDecls *decls_scope, TldContainer *tld_container) {
1470 assert(!tld_container->type_entry);
15581471
1559 if (node->data.struct_decl.type_entry) {
1560 // already scanned; we can ignore. This can happen from importing from an .h file.
1561 return;
1562 }
1472 AstNode *container_node = tld_container->base.source_node;
1473 assert(container_node->type == NodeTypeContainerDecl);
15631474
1564 Buf *name = node->data.struct_decl.name;
1565 TypeTableEntry *container_type = get_partial_container_type(g, import, &decls_scope->base,
1566 node->data.struct_decl.kind, node, buf_ptr(name));
1567 node->data.struct_decl.type_entry = container_type;
1475 TypeTableEntry *container_type = get_partial_container_type_tld(g, &decls_scope->base, tld_container);
1476 tld_container->type_entry = container_type;
15681477
15691478 // handle the member function definitions independently
1570 for (size_t i = 0; i < node->data.struct_decl.decls.length; i += 1) {
1571 AstNode *child_node = node->data.struct_decl.decls.at(i);
1572 get_as_top_level_decl(child_node)->parent_decl = node;
1479 for (size_t i = 0; i < container_node->data.struct_decl.decls.length; i += 1) {
1480 AstNode *child_node = container_node->data.struct_decl.decls.at(i);
15731481 ScopeDecls *child_scope = get_container_scope(container_type);
1574 scan_decls(g, import, child_scope, child_node);
1575 }
1576}
1577
1578static void count_inline_and_var_args(AstNode *proto_node) {
1579 assert(proto_node->type == NodeTypeFnProto);
1580
1581 size_t *inline_arg_count = &proto_node->data.fn_proto.inline_arg_count;
1582 size_t *inline_or_var_type_arg_count = &proto_node->data.fn_proto.inline_or_var_type_arg_count;
1583
1584 *inline_arg_count = 0;
1585 *inline_or_var_type_arg_count = 0;
1586
1587 // TODO run these nodes through the type analysis system rather than looking for
1588 // specialized ast nodes. this would get fooled by `{var}` instead of `var` which
1589 // is supposed to be equivalent
1590 for (size_t i = 0; i < proto_node->data.fn_proto.params.length; i += 1) {
1591 AstNode *param_node = proto_node->data.fn_proto.params.at(i);
1592 assert(param_node->type == NodeTypeParamDecl);
1593 if (param_node->data.param_decl.is_inline) {
1594 *inline_arg_count += 1;
1595 *inline_or_var_type_arg_count += 1;
1596 } else if (param_node->data.param_decl.type->type == NodeTypeVarLiteral) {
1597 *inline_or_var_type_arg_count += 1;
1598 }
1482 scan_decls(g, tld_container->base.import, child_scope, child_node, &tld_container->base);
15991483 }
16001484}
16011485
16021486static void preview_error_value_decl(CodeGen *g, AstNode *node) {
16031487 assert(node->type == NodeTypeErrorValueDecl);
16041488
1489 if (node->data.error_value_decl.visib_mod != VisibModPrivate) {
1490 add_node_error(g, node, buf_sprintf("error values require no visibility modifier"));
1491 }
1492
16051493 ErrorTableEntry *err = allocate<ErrorTableEntry>(1);
16061494
16071495 err->decl_node = node;
......@@ -1620,40 +1508,61 @@ static void preview_error_value_decl(CodeGen *g, AstNode *node) {
16201508 }
16211509
16221510 node->data.error_value_decl.err = err;
1623 node->data.error_value_decl.top_level_decl.resolution = TldResolutionOk;
16241511}
16251512
1626void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node) {
1513void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node,
1514 Scope *parent_scope, Tld *parent_tld)
1515{
1516 tld->id = id;
1517 tld->name = name;
1518 tld->visib_mod = visib_mod;
1519 tld->source_node = source_node;
1520 tld->import = source_node->owner;
1521 tld->parent_scope = parent_scope;
1522 tld->parent_tld = parent_tld;
1523}
1524
1525void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node, Tld *parent_tld) {
16271526 switch (node->type) {
16281527 case NodeTypeRoot:
16291528 for (size_t i = 0; i < import->root->data.root.top_level_decls.length; i += 1) {
16301529 AstNode *child = import->root->data.root.top_level_decls.at(i);
1631 scan_decls(g, import, decls_scope, child);
1530 scan_decls(g, import, decls_scope, child, parent_tld);
16321531 }
16331532 break;
16341533 case NodeTypeContainerDecl:
16351534 {
16361535 Buf *name = node->data.struct_decl.name;
1637 add_top_level_decl(g, import, decls_scope, node, name);
1536 VisibMod visib_mod = node->data.struct_decl.visib_mod;
1537 TldContainer *tld_container = allocate<TldContainer>(1);
1538 init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, &decls_scope->base, parent_tld);
1539 add_top_level_decl(g, decls_scope, &tld_container->base);
16381540 if (node->data.struct_decl.generic_params.length == 0) {
1639 scan_struct_decl(g, import, decls_scope, node);
1541 scan_struct_decl(g, decls_scope, tld_container);
1542 } else {
1543 zig_panic("TODO all structs anonymous?");
16401544 }
16411545 }
16421546 break;
16431547 case NodeTypeFnDef:
1644 node->data.fn_def.fn_proto->data.fn_proto.fn_def_node = node;
1645 scan_decls(g, import, decls_scope, node->data.fn_def.fn_proto);
1548 scan_decls(g, import, decls_scope, node->data.fn_def.fn_proto, parent_tld);
16461549 break;
16471550 case NodeTypeVariableDeclaration:
16481551 {
16491552 Buf *name = node->data.variable_declaration.symbol;
1650 add_top_level_decl(g, import, decls_scope, node, name);
1553 VisibMod visib_mod = node->data.variable_declaration.visib_mod;
1554 TldVar *tld_var = allocate<TldVar>(1);
1555 init_tld(&tld_var->base, TldIdVar, name, visib_mod, node, &decls_scope->base, parent_tld);
1556 add_top_level_decl(g, decls_scope, &tld_var->base);
16511557 break;
16521558 }
16531559 case NodeTypeTypeDecl:
16541560 {
16551561 Buf *name = node->data.type_decl.symbol;
1656 add_top_level_decl(g, import, decls_scope, node, name);
1562 VisibMod visib_mod = node->data.type_decl.visib_mod;
1563 TldTypeDef *tld_typedef = allocate<TldTypeDef>(1);
1564 init_tld(&tld_typedef->base, TldIdTypeDef, name, visib_mod, node, &decls_scope->base, parent_tld);
1565 add_top_level_decl(g, decls_scope, &tld_typedef->base);
16571566 break;
16581567 }
16591568 case NodeTypeFnProto:
......@@ -1661,22 +1570,20 @@ void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, A
16611570 // if the name is missing, we immediately announce an error
16621571 Buf *fn_name = node->data.fn_proto.name;
16631572 if (buf_len(fn_name) == 0) {
1664 node->data.fn_proto.skip = true;
16651573 add_node_error(g, node, buf_sprintf("missing function name"));
16661574 break;
16671575 }
1668 count_inline_and_var_args(node);
16691576
1670 add_top_level_decl(g, import, decls_scope, node, fn_name);
1577 VisibMod visib_mod = node->data.fn_proto.visib_mod;
1578 TldFn *tld_fn = allocate<TldFn>(1);
1579 init_tld(&tld_fn->base, TldIdFn, fn_name, visib_mod, node, &decls_scope->base, parent_tld);
1580 add_top_level_decl(g, decls_scope, &tld_fn->base);
16711581 break;
16721582 }
16731583 case NodeTypeUse:
16741584 {
1675 TopLevelDecl *tld = get_as_top_level_decl(node);
1676 tld->import = import;
1677 node->scope = &decls_scope->base;
16781585 g->use_queue.append(node);
1679 tld->import->use_decls.append(node);
1586 import->use_decls.append(node);
16801587 break;
16811588 }
16821589 case NodeTypeErrorValueDecl:
......@@ -1727,30 +1634,22 @@ void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, A
17271634 }
17281635}
17291636
1730static void resolve_struct_instance(CodeGen *g, ImportTableEntry *import, AstNode *node) {
1731 TypeTableEntry *type_entry = node->data.struct_decl.type_entry;
1637static void resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
1638 TypeTableEntry *type_entry = tld_container->type_entry;
17321639 assert(type_entry);
17331640
1734 // struct/enum member fns will get resolved independently
1735
1736 switch (node->data.struct_decl.kind) {
1737 case ContainerKindStruct:
1738 resolve_struct_type(g, import, type_entry);
1739 break;
1641 switch (type_entry->id) {
1642 case TypeTableEntryIdStruct:
1643 resolve_struct_type(g, tld_container->type_entry);
1644 return;
17401645 case ContainerKindEnum:
1741 resolve_enum_type(g, import, type_entry);
1742 break;
1646 resolve_enum_type(g, tld_container->type_entry);
1647 return;
17431648 case ContainerKindUnion:
1744 resolve_union_type(g, import, type_entry);
1745 break;
1746 }
1747}
1748
1749static void resolve_struct_decl(CodeGen *g, ImportTableEntry *import, AstNode *node) {
1750 if (node->data.struct_decl.generic_params.length > 0) {
1751 return preview_generic_fn_proto(g, import, node);
1752 } else {
1753 return resolve_struct_instance(g, import, node);
1649 resolve_union_type(g, tld_container->type_entry);
1650 return;
1651 default:
1652 zig_unreachable();
17541653 }
17551654}
17561655
......@@ -1786,7 +1685,6 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
17861685 case TypeTableEntryIdEnum:
17871686 case TypeTableEntryIdUnion:
17881687 case TypeTableEntryIdFn:
1789 case TypeTableEntryIdGenericFn:
17901688 case TypeTableEntryIdBoundFn:
17911689 return type_entry;
17921690 }
......@@ -1795,15 +1693,16 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
17951693
17961694// Set name to nullptr to make the variable anonymous (not visible to programmer).
17971695// TODO merge with definition of add_local_var in ir.cpp
1798static VariableTableEntry *add_local_var(CodeGen *g, AstNode *source_node, ImportTableEntry *import,
1799 Scope *parent_scope, Buf *name, TypeTableEntry *type_entry, bool is_const, AstNode *val_node)
1696VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
1697 TypeTableEntry *type_entry, bool is_const, ConstExprValue *init_value)
18001698{
18011699 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);
18021700 variable_entry->type = type_entry;
18031701 variable_entry->parent_scope = parent_scope;
1804 variable_entry->import = import;
18051702 variable_entry->shadowable = false;
18061703 variable_entry->mem_slot_index = SIZE_MAX;
1704 variable_entry->value = init_value;
1705 variable_entry->src_arg_index = SIZE_MAX;
18071706
18081707 assert(name);
18091708
......@@ -1824,11 +1723,11 @@ static VariableTableEntry *add_local_var(CodeGen *g, AstNode *source_node, Impor
18241723 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
18251724 variable_entry->type = g->builtin_types.entry_invalid;
18261725 } else {
1827 AstNode *decl_node = find_decl(parent_scope, name);
1828 if (decl_node && decl_node->type != NodeTypeVariableDeclaration) {
1726 Tld *tld = find_decl(parent_scope, name);
1727 if (tld && tld->id != TldIdVar) {
18291728 ErrorMsg *msg = add_node_error(g, source_node,
18301729 buf_sprintf("redefinition of '%s'", buf_ptr(name)));
1831 add_error_note(g, msg, decl_node, buf_sprintf("previous definition is here"));
1730 add_error_note(g, msg, tld->source_node, buf_sprintf("previous definition is here"));
18321731 variable_entry->type = g->builtin_types.entry_invalid;
18331732 }
18341733 }
......@@ -1847,78 +1746,84 @@ static VariableTableEntry *add_local_var(CodeGen *g, AstNode *source_node, Impor
18471746 variable_entry->src_is_const = is_const;
18481747 variable_entry->gen_is_const = is_const;
18491748 variable_entry->decl_node = source_node;
1850 variable_entry->val_node = val_node;
18511749 variable_entry->child_scope = child_scope;
18521750
18531751
18541752 return variable_entry;
18551753}
18561754
1857static void resolve_var_decl(CodeGen *g, ImportTableEntry *import, AstNode *node) {
1858 assert(node->type == NodeTypeVariableDeclaration);
1755static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
1756 AstNodeVariableDeclaration *var_decl = &tld_var->base.source_node->data.variable_declaration;
18591757
1860 AstNodeVariableDeclaration *var_decl = &node->data.variable_declaration;
1861 Scope *scope = node->scope;
18621758 bool is_const = var_decl->is_const;
1863 bool is_export = (var_decl->top_level_decl.visib_mod == VisibModExport);
1759 bool is_export = (tld_var->base.visib_mod == VisibModExport);
18641760 bool is_extern = var_decl->is_extern;
18651761
18661762 TypeTableEntry *explicit_type = nullptr;
18671763 if (var_decl->type) {
1868 TypeTableEntry *proposed_type = analyze_type_expr(g, import, scope, var_decl->type);
1764 TypeTableEntry *proposed_type = analyze_type_expr(g, tld_var->base.parent_scope, var_decl->type);
18691765 explicit_type = validate_var_type(g, var_decl->type, proposed_type);
18701766 }
18711767
1768 AstNode *source_node = tld_var->base.source_node;
1769
1770 IrInstruction *init_value = nullptr;
1771
18721772 TypeTableEntry *implicit_type = nullptr;
18731773 if (explicit_type && explicit_type->id == TypeTableEntryIdInvalid) {
18741774 implicit_type = explicit_type;
18751775 } else if (var_decl->expr) {
1876 IrInstruction *result = analyze_const_value(g, scope, var_decl->expr, explicit_type);
1877 assert(result);
1878 implicit_type = result->type_entry;
1776 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type);
1777 assert(init_value);
1778 implicit_type = init_value->type_entry;
18791779
18801780 if (implicit_type->id == TypeTableEntryIdUnreachable) {
1881 add_node_error(g, node, buf_sprintf("variable initialization is unreachable"));
1781 add_node_error(g, source_node, buf_sprintf("variable initialization is unreachable"));
18821782 implicit_type = g->builtin_types.entry_invalid;
18831783 } else if ((!is_const || is_export) &&
18841784 (implicit_type->id == TypeTableEntryIdNumLitFloat ||
18851785 implicit_type->id == TypeTableEntryIdNumLitInt))
18861786 {
1887 add_node_error(g, node, buf_sprintf("unable to infer variable type"));
1787 add_node_error(g, source_node, buf_sprintf("unable to infer variable type"));
18881788 implicit_type = g->builtin_types.entry_invalid;
18891789 } else if (implicit_type->id == TypeTableEntryIdNullLit) {
1890 add_node_error(g, node, buf_sprintf("unable to infer variable type"));
1790 add_node_error(g, source_node, buf_sprintf("unable to infer variable type"));
18911791 implicit_type = g->builtin_types.entry_invalid;
18921792 } else if (implicit_type->id == TypeTableEntryIdMetaType && !is_const) {
1893 add_node_error(g, node, buf_sprintf("variable of type 'type' must be constant"));
1793 add_node_error(g, source_node, buf_sprintf("variable of type 'type' must be constant"));
18941794 implicit_type = g->builtin_types.entry_invalid;
18951795 }
1896 if (implicit_type->id != TypeTableEntryIdInvalid) {
1897 assert(result->static_value.special != ConstValSpecialRuntime);
1898 var_decl->top_level_decl.value = result;
1899 }
1796 assert(implicit_type->id == TypeTableEntryIdInvalid || init_value->static_value.special != ConstValSpecialRuntime);
19001797 } else if (!is_extern) {
1901 add_node_error(g, node, buf_sprintf("variables must be initialized"));
1798 add_node_error(g, source_node, buf_sprintf("variables must be initialized"));
19021799 implicit_type = g->builtin_types.entry_invalid;
19031800 }
19041801
19051802 TypeTableEntry *type = explicit_type ? explicit_type : implicit_type;
19061803 assert(type != nullptr); // should have been caught by the parser
19071804
1908 VariableTableEntry *var = add_local_var(g, node, import, scope,
1909 var_decl->symbol, type, is_const, var_decl->expr);
1805 tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope,
1806 var_decl->symbol, type, is_const, &init_value->static_value);
1807
1808 g->global_vars.append(tld_var->var);
1809}
19101810
1911 var_decl->variable = var;
1811static void resolve_decl_typedef(CodeGen *g, TldTypeDef *tld_typedef) {
1812 AstNode *typedef_node = tld_typedef->base.source_node;
1813 assert(typedef_node->type == NodeTypeTypeDecl);
1814 AstNode *type_node = typedef_node->data.type_decl.child_type;
1815 Buf *decl_name = typedef_node->data.type_decl.symbol;
19121816
1913 g->global_vars.append(var);
1817 TypeTableEntry *child_type = analyze_type_expr(g, tld_typedef->base.parent_scope, type_node);
1818 tld_typedef->type_entry = (child_type->id == TypeTableEntryIdInvalid) ?
1819 child_type : get_typedecl_type(g, buf_ptr(decl_name), child_type);
19141820}
19151821
1916void resolve_top_level_decl(CodeGen *g, AstNode *node, bool pointer_only) {
1917 TopLevelDecl *tld = get_as_top_level_decl(node);
1918 if (tld->resolution != TldResolutionUnresolved) {
1822void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {
1823 if (tld->resolution != TldResolutionUnresolved)
19191824 return;
1920 }
1921 if (pointer_only && node->type == NodeTypeContainerDecl) {
1825 if (pointer_only && tld->id == TldIdContainer) {
1826 g->resolve_queue.append(tld);
19221827 return;
19231828 }
19241829
......@@ -1926,90 +1831,38 @@ void resolve_top_level_decl(CodeGen *g, AstNode *node, bool pointer_only) {
19261831 assert(import);
19271832
19281833 if (tld->dep_loop_flag) {
1929 add_node_error(g, node, buf_sprintf("'%s' depends on itself", buf_ptr(tld->name)));
1834 add_node_error(g, tld->source_node, buf_sprintf("'%s' depends on itself", buf_ptr(tld->name)));
19301835 tld->resolution = TldResolutionInvalid;
19311836 return;
19321837 } else {
19331838 tld->dep_loop_flag = true;
19341839 }
19351840
1936 switch (node->type) {
1937 case NodeTypeFnProto:
1938 preview_fn_proto_instance(g, import, node, node->scope);
1939 break;
1940 case NodeTypeContainerDecl:
1941 resolve_struct_decl(g, import, node);
1942 break;
1943 case NodeTypeVariableDeclaration:
1944 resolve_var_decl(g, import, node);
1945 break;
1946 case NodeTypeTypeDecl:
1841 switch (tld->id) {
1842 case TldIdVar:
19471843 {
1948 AstNode *type_node = node->data.type_decl.child_type;
1949 Buf *decl_name = node->data.type_decl.symbol;
1950
1951 TypeTableEntry *entry;
1952 if (node->data.type_decl.override_type) {
1953 entry = node->data.type_decl.override_type;
1954 } else {
1955 TypeTableEntry *child_type = analyze_type_expr(g, import, &import->decls_scope->base, type_node);
1956 if (child_type->id == TypeTableEntryIdInvalid) {
1957 entry = child_type;
1958 } else {
1959 entry = get_typedecl_type(g, buf_ptr(decl_name), child_type);
1960 }
1961 }
1962 node->data.type_decl.child_type_entry = entry;
1844 TldVar *tld_var = (TldVar *)tld;
1845 resolve_decl_var(g, tld_var);
1846 break;
1847 }
1848 case TldIdFn:
1849 {
1850 TldFn *tld_fn = (TldFn *)tld;
1851 resolve_decl_fn(g, tld_fn);
1852 break;
1853 }
1854 case TldIdContainer:
1855 {
1856 TldContainer *tld_container = (TldContainer *)tld;
1857 resolve_decl_container(g, tld_container);
1858 break;
1859 }
1860 case TldIdTypeDef:
1861 {
1862 TldTypeDef *tld_typedef = (TldTypeDef *)tld;
1863 resolve_decl_typedef(g, tld_typedef);
19631864 break;
19641865 }
1965 case NodeTypeErrorValueDecl:
1966 break;
1967 case NodeTypeUse:
1968 zig_panic("TODO resolve_top_level_decl NodeTypeUse");
1969 break;
1970 case NodeTypeFnDef:
1971 case NodeTypeParamDecl:
1972 case NodeTypeFnDecl:
1973 case NodeTypeReturnExpr:
1974 case NodeTypeDefer:
1975 case NodeTypeRoot:
1976 case NodeTypeBlock:
1977 case NodeTypeBinOpExpr:
1978 case NodeTypeUnwrapErrorExpr:
1979 case NodeTypeFnCallExpr:
1980 case NodeTypeArrayAccessExpr:
1981 case NodeTypeSliceExpr:
1982 case NodeTypeNumberLiteral:
1983 case NodeTypeStringLiteral:
1984 case NodeTypeCharLiteral:
1985 case NodeTypeBoolLiteral:
1986 case NodeTypeNullLiteral:
1987 case NodeTypeUndefinedLiteral:
1988 case NodeTypeZeroesLiteral:
1989 case NodeTypeThisLiteral:
1990 case NodeTypeSymbol:
1991 case NodeTypePrefixOpExpr:
1992 case NodeTypeIfBoolExpr:
1993 case NodeTypeIfVarExpr:
1994 case NodeTypeWhileExpr:
1995 case NodeTypeForExpr:
1996 case NodeTypeSwitchExpr:
1997 case NodeTypeSwitchProng:
1998 case NodeTypeSwitchRange:
1999 case NodeTypeLabel:
2000 case NodeTypeGoto:
2001 case NodeTypeBreak:
2002 case NodeTypeContinue:
2003 case NodeTypeAsmExpr:
2004 case NodeTypeFieldAccessExpr:
2005 case NodeTypeStructField:
2006 case NodeTypeStructValueField:
2007 case NodeTypeContainerInitExpr:
2008 case NodeTypeArrayType:
2009 case NodeTypeErrorType:
2010 case NodeTypeTypeLiteral:
2011 case NodeTypeVarLiteral:
2012 zig_unreachable();
20131866 }
20141867
20151868 tld->resolution = TldResolutionOk;
......@@ -2028,7 +1881,6 @@ static bool type_has_codegen_value(TypeTableEntry *type_entry) {
20281881 case TypeTableEntryIdNullLit:
20291882 case TypeTableEntryIdNamespace:
20301883 case TypeTableEntryIdBlock:
2031 case TypeTableEntryIdGenericFn:
20321884 case TypeTableEntryIdBoundFn:
20331885 return false;
20341886
......@@ -2142,68 +1994,7 @@ bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *
21421994 return false;
21431995}
21441996
2145ScopeDecls *create_decls_scope(AstNode *node, Scope *parent) {
2146 assert(node->type == NodeTypeRoot || node->type == NodeTypeContainerDecl);
2147 ScopeDecls *scope = allocate<ScopeDecls>(1);
2148 scope->base.node = node;
2149 scope->base.parent = parent;
2150 scope->decl_table.init(4);
2151 return scope;
2152}
2153
2154Scope *create_block_scope(AstNode *node, Scope *parent) {
2155 assert(node->type == NodeTypeBlock);
2156 ScopeBlock *scope = allocate<ScopeBlock>(1);
2157 scope->base.node = node;
2158 scope->base.parent = parent;
2159 scope->label_table.init(1);
2160 return &scope->base;
2161}
2162
2163Scope *create_defer_scope(AstNode *node, Scope *parent) {
2164 assert(node->type == NodeTypeDefer);
2165 ScopeDefer *scope = allocate<ScopeDefer>(1);
2166 scope->base.node = node;
2167 scope->base.parent = parent;
2168 return &scope->base;
2169}
2170
2171Scope *create_var_scope(AstNode *node, Scope *parent, VariableTableEntry *var) {
2172 assert(node->type == NodeTypeVariableDeclaration || node->type == NodeTypeParamDecl);
2173 ScopeVarDecl *scope = allocate<ScopeVarDecl>(1);
2174 scope->base.node = node;
2175 scope->base.parent = parent;
2176 scope->var = var;
2177 return &scope->base;
2178}
2179
2180Scope *create_cimport_scope(AstNode *node, Scope *parent) {
2181 assert(node->type == NodeTypeFnCallExpr);
2182 ScopeCImport *scope = allocate<ScopeCImport>(1);
2183 scope->base.node = node;
2184 scope->base.parent = parent;
2185 buf_resize(&scope->c_import_buf, 0);
2186 return &scope->base;
2187}
2188
2189Scope *create_loop_scope(AstNode *node, Scope *parent) {
2190 assert(node->type == NodeTypeWhileExpr || node->type == NodeTypeForExpr);
2191 ScopeLoop *scope = allocate<ScopeLoop>(1);
2192 scope->base.node = node;
2193 scope->base.parent = parent;
2194 return &scope->base;
2195}
2196
2197Scope *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry) {
2198 assert(node->type == NodeTypeFnDef);
2199 ScopeFnBody *scope = allocate<ScopeFnBody>(1);
2200 scope->base.node = node;
2201 scope->base.parent = parent;
2202 scope->fn_entry = fn_entry;
2203 return &scope->base;
2204}
2205
2206AstNode *find_decl(Scope *scope, Buf *name) {
1997Tld *find_decl(Scope *scope, Buf *name) {
22071998 while (scope) {
22081999 if (scope->node->type == NodeTypeRoot ||
22092000 scope->node->type == NodeTypeContainerDecl)
......@@ -2232,11 +2023,11 @@ VariableTableEntry *find_variable(CodeGen *g, Scope *scope, Buf *name) {
22322023 ScopeDecls *decls_scope = (ScopeDecls *)scope;
22332024 auto entry = decls_scope->decl_table.maybe_get(name);
22342025 if (entry) {
2235 AstNode *decl_node = entry->value;
2236 if (decl_node->type == NodeTypeVariableDeclaration) {
2237 VariableTableEntry *var = decl_node->data.variable_declaration.variable;
2238 if (var)
2239 return var;
2026 Tld *tld = entry->value;
2027 if (tld->id == TldIdVar) {
2028 TldVar *tld_var = (TldVar *)tld;
2029 if (tld_var->var)
2030 return tld_var->var;
22402031 }
22412032 }
22422033 }
......@@ -2246,6 +2037,17 @@ VariableTableEntry *find_variable(CodeGen *g, Scope *scope, Buf *name) {
22462037 return nullptr;
22472038}
22482039
2040FnTableEntry *scope_fn_entry(Scope *scope) {
2041 while (scope) {
2042 if (scope->node->type == NodeTypeFnDef) {
2043 ScopeFnDef *fn_scope = (ScopeFnDef *)scope;
2044 return fn_scope->fn_entry;
2045 }
2046 scope = scope->parent;
2047 }
2048 return nullptr;
2049}
2050
22492051TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name) {
22502052 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
22512053 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i];
......@@ -2296,7 +2098,6 @@ static bool is_container(TypeTableEntry *type_entry) {
22962098 case TypeTableEntryIdTypeDecl:
22972099 case TypeTableEntryIdNamespace:
22982100 case TypeTableEntryIdBlock:
2299 case TypeTableEntryIdGenericFn:
23002101 case TypeTableEntryIdBoundFn:
23012102 return false;
23022103 }
......@@ -2317,13 +2118,13 @@ TypeTableEntry *container_ref_type(TypeTableEntry *type_entry) {
23172118void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
23182119 switch (type_entry->id) {
23192120 case TypeTableEntryIdStruct:
2320 resolve_struct_type(g, type_entry->data.structure.decl_node->owner, type_entry);
2121 resolve_struct_type(g, type_entry);
23212122 break;
23222123 case TypeTableEntryIdEnum:
2323 resolve_enum_type(g, type_entry->data.enumeration.decl_node->owner, type_entry);
2124 resolve_enum_type(g, type_entry);
23242125 break;
23252126 case TypeTableEntryIdUnion:
2326 resolve_union_type(g, type_entry->data.unionation.decl_node->owner, type_entry);
2127 resolve_union_type(g, type_entry);
23272128 break;
23282129 case TypeTableEntryIdPointer:
23292130 case TypeTableEntryIdMetaType:
......@@ -2344,7 +2145,6 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
23442145 case TypeTableEntryIdTypeDecl:
23452146 case TypeTableEntryIdNamespace:
23462147 case TypeTableEntryIdBlock:
2347 case TypeTableEntryIdGenericFn:
23482148 case TypeTableEntryIdBoundFn:
23492149 case TypeTableEntryIdInvalid:
23502150 case TypeTableEntryIdVar:
......@@ -2362,54 +2162,41 @@ bool type_is_codegen_pointer(TypeTableEntry *type) {
23622162 return false;
23632163}
23642164
2365
2366
23672165static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
2368 ImportTableEntry *import = fn_table_entry->import_entry;
2369 AstNode *node = fn_table_entry->fn_def_node;
2370 assert(node->type == NodeTypeFnDef);
2371
2372 AstNode *fn_proto_node = node->data.fn_def.fn_proto;
2373 assert(fn_proto_node->type == NodeTypeFnProto);
2374
2375 if (fn_proto_node->data.fn_proto.skip) {
2376 // we detected an error with this function definition which prevents us
2377 // from further analyzing it.
2378 fn_table_entry->anal_state = FnAnalStateSkipped;
2166 assert(fn_table_entry->anal_state != FnAnalStateProbing);
2167 if (fn_table_entry->anal_state != FnAnalStateReady)
23792168 return;
2380 }
2169
23812170 fn_table_entry->anal_state = FnAnalStateProbing;
23822171
2383 Scope *child_scope = node->data.fn_def.containing_scope;
2172 AstNodeFnProto *fn_proto = &fn_table_entry->proto_node->data.fn_proto;
23842173
2174 Scope *child_scope = &fn_table_entry->fndef_scope->base;
2175 assert(child_scope);
2176
2177 // define local variables for parameters
23852178 TypeTableEntry *fn_type = fn_table_entry->type_entry;
2179 assert(!fn_type->data.fn.is_generic);
23862180 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
2387 AstNodeFnProto *fn_proto = &fn_proto_node->data.fn_proto;
2388 for (size_t i = 0; i < fn_proto->params.length; i += 1) {
2181 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
23892182 AstNode *param_decl_node = fn_proto->params.at(i);
2390 assert(param_decl_node->type == NodeTypeParamDecl);
2391
2392 // define local variables for parameters
23932183 AstNodeParamDecl *param_decl = &param_decl_node->data.param_decl;
2394 TypeTableEntry *type = fn_type_id->param_info[i].type;
2184 FnTypeParamInfo *param_info = &fn_type_id->param_info[i];
23952185
2396 if (param_decl->is_noalias && !type_is_codegen_pointer(type)) {
2186 TypeTableEntry *param_type = param_info->type;
2187 bool is_noalias = param_info->is_noalias;
2188
2189 if (is_noalias && !type_is_codegen_pointer(param_type)) {
23972190 add_node_error(g, param_decl_node, buf_sprintf("noalias on non-pointer parameter"));
23982191 }
23992192
2400 if (fn_type->data.fn.fn_type_id.is_extern && handle_is_ptr(type)) {
2193 if (fn_type_id->is_extern && handle_is_ptr(param_type)) {
24012194 add_node_error(g, param_decl_node,
24022195 buf_sprintf("byvalue types not yet supported on extern function parameters"));
24032196 }
24042197
2405 if (buf_len(param_decl->name) == 0) {
2406 add_node_error(g, param_decl_node, buf_sprintf("missing parameter name"));
2407 }
2408
2409 VariableTableEntry *var = add_local_var(g, param_decl_node, import, child_scope, param_decl->name,
2410 type, true, nullptr);
2198 VariableTableEntry *var = add_variable(g, param_decl_node, child_scope, param_decl->name, param_type, true, nullptr);
24112199 var->src_arg_index = i;
2412 param_decl_node->data.param_decl.variable = var;
24132200 child_scope = var->child_scope;
24142201 fn_table_entry->variable_list.append(var);
24152202
......@@ -2418,19 +2205,18 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
24182205 }
24192206 }
24202207
2421 node->data.fn_def.child_scope = child_scope;
2208 fn_table_entry->child_scope = child_scope;
24222209
2423 TypeTableEntry *expected_type = fn_type->data.fn.fn_type_id.return_type;
2210 TypeTableEntry *expected_type = fn_type_id->return_type;
24242211
2425 if (fn_type->data.fn.fn_type_id.is_extern && handle_is_ptr(expected_type)) {
2426 add_node_error(g, fn_proto_node->data.fn_proto.return_type,
2212 if (fn_type_id->is_extern && handle_is_ptr(expected_type)) {
2213 add_node_error(g, fn_proto->return_type,
24272214 buf_sprintf("byvalue types not yet supported on extern function return values"));
24282215 }
24292216
24302217 ir_gen_fn(g, fn_table_entry);
24312218 if (fn_table_entry->ir_executable.invalid) {
2432 fn_proto_node->data.fn_proto.skip = true;
2433 fn_table_entry->anal_state = FnAnalStateSkipped;
2219 fn_table_entry->anal_state = FnAnalStateInvalid;
24342220 return;
24352221 }
24362222 if (g->verbose) {
......@@ -2443,9 +2229,16 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
24432229
24442230 TypeTableEntry *block_return_type = ir_analyze(g, &fn_table_entry->ir_executable,
24452231 &fn_table_entry->analyzed_executable, expected_type, fn_proto->return_type);
2446 node->data.fn_def.implicit_return_type = block_return_type;
2232 fn_table_entry->implicit_return_type = block_return_type;
24472233
2448 if (block_return_type->id != TypeTableEntryIdInvalid && g->verbose) {
2234 if (block_return_type->id == TypeTableEntryIdInvalid ||
2235 fn_table_entry->analyzed_executable.invalid)
2236 {
2237 fn_table_entry->anal_state = FnAnalStateInvalid;
2238 return;
2239 }
2240
2241 if (g->verbose) {
24492242 fprintf(stderr, "{ // (analyzed)\n");
24502243 ir_print(stderr, &fn_table_entry->analyzed_executable, 4);
24512244 fprintf(stderr, "}\n");
......@@ -2454,16 +2247,14 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
24542247 fn_table_entry->anal_state = FnAnalStateComplete;
24552248}
24562249
2457static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *dst_use_node) {
2458 TopLevelDecl *tld = get_as_top_level_decl(dst_use_node);
2459
2460 IrInstruction *use_target_value = tld->value;
2250static void add_symbols_from_import(CodeGen *g, AstNode *dst_use_node) {
2251 IrInstruction *use_target_value = dst_use_node->data.use.value;
24612252 if (use_target_value->type_entry->id == TypeTableEntryIdInvalid) {
2462 tld->import->any_imports_failed = true;
2253 dst_use_node->owner->any_imports_failed = true;
24632254 return;
24642255 }
24652256
2466 tld->resolution = TldResolutionOk;
2257 dst_use_node->data.use.resolution = TldResolutionOk;
24672258
24682259 ConstExprValue *const_val = &use_target_value->static_value;
24692260 assert(const_val->special != ConstValSpecialRuntime);
......@@ -2472,59 +2263,60 @@ static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *
24722263 assert(target_import);
24732264
24742265 if (target_import->any_imports_failed) {
2475 tld->import->any_imports_failed = true;
2266 dst_use_node->owner->any_imports_failed = true;
24762267 }
24772268
2478 for (size_t i = 0; i < target_import->root->data.root.top_level_decls.length; i += 1) {
2479 AstNode *decl_node = target_import->root->data.root.top_level_decls.at(i);
2480 if (decl_node->type == NodeTypeFnDef) {
2481 decl_node = decl_node->data.fn_def.fn_proto;
2482 }
2483 TopLevelDecl *target_tld = get_as_top_level_decl(decl_node);
2484 if (!target_tld->name) {
2269 auto it = target_import->decls_scope->decl_table.entry_iterator();
2270 for (;;) {
2271 auto *entry = it.next();
2272 if (!entry)
2273 break;
2274
2275 Tld *target_tld = entry->value;
2276 if (target_tld->import != target_import ||
2277 target_tld->visib_mod == VisibModPrivate)
2278 {
24852279 continue;
24862280 }
2487 if (target_tld->visib_mod != VisibModPrivate) {
2488 auto existing_entry = tld->import->decls_scope->decl_table.put_unique(target_tld->name, decl_node);
2489 if (existing_entry) {
2490 AstNode *existing_decl = existing_entry->value;
2491 if (existing_decl != decl_node) {
2492 ErrorMsg *msg = add_node_error(g, dst_use_node,
2493 buf_sprintf("import of '%s' overrides existing definition",
2494 buf_ptr(target_tld->name)));
2495 add_error_note(g, msg, existing_decl, buf_sprintf("previous definition here"));
2496 add_error_note(g, msg, decl_node, buf_sprintf("imported definition here"));
2497 }
2281
2282 auto existing_entry = dst_use_node->owner->decls_scope->decl_table.put_unique(target_tld->name, target_tld);
2283 if (existing_entry) {
2284 Tld *existing_decl = existing_entry->value;
2285 if (existing_decl != target_tld) {
2286 ErrorMsg *msg = add_node_error(g, dst_use_node,
2287 buf_sprintf("import of '%s' overrides existing definition",
2288 buf_ptr(target_tld->name)));
2289 add_error_note(g, msg, existing_decl->source_node, buf_sprintf("previous definition here"));
2290 add_error_note(g, msg, target_tld->source_node, buf_sprintf("imported definition here"));
24982291 }
24992292 }
25002293 }
25012294
25022295 for (size_t i = 0; i < target_import->use_decls.length; i += 1) {
25032296 AstNode *use_decl_node = target_import->use_decls.at(i);
2504 TopLevelDecl *target_tld = get_as_top_level_decl(use_decl_node);
2505 if (target_tld->visib_mod != VisibModPrivate) {
2506 add_symbols_from_import(g, use_decl_node, dst_use_node);
2507 }
2297 if (use_decl_node->data.use.visib_mod != VisibModPrivate)
2298 add_symbols_from_import(g, dst_use_node);
25082299 }
2509
25102300}
25112301
25122302void resolve_use_decl(CodeGen *g, AstNode *node) {
25132303 assert(node->type == NodeTypeUse);
2514 if (get_as_top_level_decl(node)->resolution != TldResolutionUnresolved) {
2304
2305 if (node->data.use.resolution != TldResolutionUnresolved)
25152306 return;
2516 }
2517 add_symbols_from_import(g, node, node);
2307 add_symbols_from_import(g, node);
25182308}
25192309
25202310void preview_use_decl(CodeGen *g, AstNode *node) {
25212311 assert(node->type == NodeTypeUse);
2522 TopLevelDecl *tld = get_as_top_level_decl(node);
25232312
2524 IrInstruction *result = analyze_const_value(g, &tld->import->decls_scope->base, node->data.use.expr,
2525 g->builtin_types.entry_namespace);
2313 IrInstruction *result = analyze_const_value(g, &node->owner->decls_scope->base,
2314 node->data.use.expr, g->builtin_types.entry_namespace);
2315
25262316 if (result->type_entry->id == TypeTableEntryIdInvalid)
2527 tld->import->any_imports_failed = true;
2317 node->owner->any_imports_failed = true;
2318
2319 node->data.use.value = result;
25282320}
25292321
25302322ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package,
......@@ -2580,7 +2372,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package,
25802372 g->import_table.put(abs_full_path, import_entry);
25812373 g->import_queue.append(import_entry);
25822374
2583 import_entry->decls_scope = create_decls_scope(import_entry->root, nullptr);
2375 import_entry->decls_scope = create_decls_scope(import_entry->root, nullptr, nullptr, import_entry);
25842376
25852377
25862378 assert(import_entry->root->type == NodeTypeRoot);
......@@ -2592,7 +2384,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package,
25922384 assert(proto_node->type == NodeTypeFnProto);
25932385 Buf *proto_name = proto_node->data.fn_proto.name;
25942386
2595 bool is_private = (proto_node->data.fn_proto.top_level_decl.visib_mod == VisibModPrivate);
2387 bool is_private = (proto_node->data.fn_proto.visib_mod == VisibModPrivate);
25962388
25972389 if (buf_eql_str(proto_name, "main") && !is_private) {
25982390 g->have_exported_main = true;
......@@ -2607,7 +2399,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package,
26072399void semantic_analyze(CodeGen *g) {
26082400 for (; g->import_queue_index < g->import_queue.length; g->import_queue_index += 1) {
26092401 ImportTableEntry *import = g->import_queue.at(g->import_queue_index);
2610 scan_decls(g, import, import->decls_scope, import->root);
2402 scan_decls(g, import, import->decls_scope, import->root, nullptr);
26112403 }
26122404
26132405 for (; g->use_queue_index < g->use_queue.length; g->use_queue_index += 1) {
......@@ -2620,82 +2412,22 @@ void semantic_analyze(CodeGen *g) {
26202412 resolve_use_decl(g, use_decl_node);
26212413 }
26222414
2623 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
2624 AstNode *decl_node = g->resolve_queue.at(g->resolve_queue_index);
2625 bool pointer_only = false;
2626 resolve_top_level_decl(g, decl_node, pointer_only);
2627 }
2415 while (g->resolve_queue_index < g->resolve_queue.length ||
2416 g->fn_defs_index < g->fn_defs.length)
2417 {
2418 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
2419 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
2420 bool pointer_only = false;
2421 resolve_top_level_decl(g, tld, pointer_only);
2422 }
26282423
2629 for (size_t i = 0; i < g->fn_defs.length; i += 1) {
2630 FnTableEntry *fn_entry = g->fn_defs.at(i);
2631 if (fn_entry->anal_state == FnAnalStateReady) {
2424 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
2425 FnTableEntry *fn_entry = g->fn_defs.at(g->fn_defs_index);
26322426 analyze_fn_body(g, fn_entry);
26332427 }
26342428 }
26352429}
26362430
2637TopLevelDecl *get_as_top_level_decl(AstNode *node) {
2638 switch (node->type) {
2639 case NodeTypeVariableDeclaration:
2640 return &node->data.variable_declaration.top_level_decl;
2641 case NodeTypeFnProto:
2642 return &node->data.fn_proto.top_level_decl;
2643 case NodeTypeFnDef:
2644 return &node->data.fn_def.fn_proto->data.fn_proto.top_level_decl;
2645 case NodeTypeContainerDecl:
2646 return &node->data.struct_decl.top_level_decl;
2647 case NodeTypeErrorValueDecl:
2648 return &node->data.error_value_decl.top_level_decl;
2649 case NodeTypeUse:
2650 return &node->data.use.top_level_decl;
2651 case NodeTypeTypeDecl:
2652 return &node->data.type_decl.top_level_decl;
2653 case NodeTypeNumberLiteral:
2654 case NodeTypeReturnExpr:
2655 case NodeTypeDefer:
2656 case NodeTypeBinOpExpr:
2657 case NodeTypeUnwrapErrorExpr:
2658 case NodeTypePrefixOpExpr:
2659 case NodeTypeFnCallExpr:
2660 case NodeTypeArrayAccessExpr:
2661 case NodeTypeSliceExpr:
2662 case NodeTypeFieldAccessExpr:
2663 case NodeTypeIfBoolExpr:
2664 case NodeTypeIfVarExpr:
2665 case NodeTypeWhileExpr:
2666 case NodeTypeForExpr:
2667 case NodeTypeSwitchExpr:
2668 case NodeTypeSwitchProng:
2669 case NodeTypeSwitchRange:
2670 case NodeTypeAsmExpr:
2671 case NodeTypeContainerInitExpr:
2672 case NodeTypeRoot:
2673 case NodeTypeFnDecl:
2674 case NodeTypeParamDecl:
2675 case NodeTypeBlock:
2676 case NodeTypeStringLiteral:
2677 case NodeTypeCharLiteral:
2678 case NodeTypeSymbol:
2679 case NodeTypeBoolLiteral:
2680 case NodeTypeNullLiteral:
2681 case NodeTypeUndefinedLiteral:
2682 case NodeTypeZeroesLiteral:
2683 case NodeTypeThisLiteral:
2684 case NodeTypeLabel:
2685 case NodeTypeGoto:
2686 case NodeTypeBreak:
2687 case NodeTypeContinue:
2688 case NodeTypeStructField:
2689 case NodeTypeStructValueField:
2690 case NodeTypeArrayType:
2691 case NodeTypeErrorType:
2692 case NodeTypeTypeLiteral:
2693 case NodeTypeVarLiteral:
2694 zig_unreachable();
2695 }
2696 zig_unreachable();
2697}
2698
26992431bool is_node_void_expr(AstNode *node) {
27002432 if (node->type == NodeTypeContainerInitExpr &&
27012433 node->data.container_init_expr.kind == ContainerInitKindArray)
......@@ -2749,7 +2481,6 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
27492481 case TypeTableEntryIdNullLit:
27502482 case TypeTableEntryIdNamespace:
27512483 case TypeTableEntryIdBlock:
2752 case TypeTableEntryIdGenericFn:
27532484 case TypeTableEntryIdBoundFn:
27542485 case TypeTableEntryIdVar:
27552486 zig_unreachable();
......@@ -2902,7 +2633,6 @@ static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val)
29022633 return hash_ptr(const_val->data.x_import);
29032634 case TypeTableEntryIdBlock:
29042635 return hash_ptr(const_val->data.x_block);
2905 case TypeTableEntryIdGenericFn:
29062636 case TypeTableEntryIdBoundFn:
29072637 case TypeTableEntryIdInvalid:
29082638 case TypeTableEntryIdUnreachable:
......@@ -2912,45 +2642,6 @@ static uint32_t hash_const_val(TypeTableEntry *type, ConstExprValue *const_val)
29122642 zig_unreachable();
29132643}
29142644
2915uint32_t generic_fn_type_id_hash(GenericFnTypeId *id) {
2916 zig_panic("TODO generic_fn_type_id_hash");
2917 //uint32_t result = 0;
2918 //result += hash_ptr(id->decl_node);
2919 //for (size_t i = 0; i < id->generic_param_count; i += 1) {
2920 // GenericParamValue *generic_param = &id->generic_params[i];
2921 // if (generic_param->node) {
2922 // ConstExprValue *const_val = &get_resolved_expr(generic_param->node)->instruction->static_value;
2923 // assert(const_val->special != ConstValSpecialRuntime);
2924 // result += hash_const_val(generic_param->type, const_val);
2925 // }
2926 // result += hash_ptr(generic_param->type);
2927 //}
2928 //return result;
2929}
2930
2931bool generic_fn_type_id_eql(GenericFnTypeId *a, GenericFnTypeId *b) {
2932 zig_panic("TODO generic_fn_type_id_eql");
2933 //if (a->decl_node != b->decl_node) return false;
2934 //assert(a->generic_param_count == b->generic_param_count);
2935 //for (size_t i = 0; i < a->generic_param_count; i += 1) {
2936 // GenericParamValue *a_val = &a->generic_params[i];
2937 // GenericParamValue *b_val = &b->generic_params[i];
2938 // if (a_val->type != b_val->type) return false;
2939 // if (a_val->node && b_val->node) {
2940 // ConstExprValue *a_const_val = &get_resolved_expr(a_val->node)->instruction->static_value;
2941 // ConstExprValue *b_const_val = &get_resolved_expr(b_val->node)->instruction->static_value;
2942 // assert(a_const_val->special != ConstValSpecialRuntime);
2943 // assert(b_const_val->special != ConstValSpecialRuntime);
2944 // if (!const_values_equal(a_const_val, b_const_val, a_val->type)) {
2945 // return false;
2946 // }
2947 // } else {
2948 // assert(!a_val->node && !b_val->node);
2949 // }
2950 //}
2951 //return true;
2952}
2953
29542645bool type_has_bits(TypeTableEntry *type_entry) {
29552646 assert(type_entry);
29562647 assert(type_entry->id != TypeTableEntryIdInvalid);
......@@ -2981,7 +2672,6 @@ static TypeTableEntry *type_of_first_thing_in_memory(TypeTableEntry *type_entry)
29812672 case TypeTableEntryIdVoid:
29822673 case TypeTableEntryIdNamespace:
29832674 case TypeTableEntryIdBlock:
2984 case TypeTableEntryIdGenericFn:
29852675 case TypeTableEntryIdBoundFn:
29862676 case TypeTableEntryIdVar:
29872677 zig_unreachable();
src/analyze.hpp+12-8
......@@ -22,11 +22,11 @@ TypeTableEntry *get_int_type(CodeGen *g, bool is_signed, size_t size_in_bits);
2222TypeTableEntry **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type);
2323TypeTableEntry *get_c_int_type(CodeGen *g, CIntType c_int_type);
2424TypeTableEntry *get_typedecl_type(CodeGen *g, const char *name, TypeTableEntry *child_type);
25TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id, bool gen_debug_info);
25TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id);
2626TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type);
2727TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size);
2828TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
29TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import, Scope *context,
29TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import, Scope *scope,
3030 ContainerKind kind, AstNode *decl_node, const char *name);
3131TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
3232TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type);
......@@ -49,9 +49,8 @@ AstNode *first_executing_node(AstNode *node);
4949// TODO move these over, these used to be static
5050bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *actual_type);
5151VariableTableEntry *find_variable(CodeGen *g, Scope *orig_context, Buf *name);
52AstNode *find_decl(Scope *context, Buf *name);
53void resolve_top_level_decl(CodeGen *g, AstNode *node, bool pointer_only);
54TopLevelDecl *get_as_top_level_decl(AstNode *node);
52Tld *find_decl(Scope *scope, Buf *name);
53void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only);
5554bool type_is_codegen_pointer(TypeTableEntry *type);
5655TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry);
5756TypeTableEntry *container_ref_type(TypeTableEntry *type_entry);
......@@ -61,16 +60,21 @@ TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name);
6160ScopeDecls *get_container_scope(TypeTableEntry *type_entry);
6261TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name);
6362bool is_container_ref(TypeTableEntry *type_entry);
64void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node);
63void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node, Tld *parent_tld);
6564void preview_use_decl(CodeGen *g, AstNode *node);
6665void resolve_use_decl(CodeGen *g, AstNode *node);
66FnTableEntry *scope_fn_entry(Scope *scope);
67ImportTableEntry *get_scope_import(Scope *scope);
68void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node,
69 Scope *parent_scope, Tld *parent_tld);
70VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
71 TypeTableEntry *type_entry, bool is_const, ConstExprValue *init_value);
6772
68ScopeDecls *create_decls_scope(AstNode *node, Scope *parent);
6973Scope *create_block_scope(AstNode *node, Scope *parent);
7074Scope *create_defer_scope(AstNode *node, Scope *parent);
7175Scope *create_var_scope(AstNode *node, Scope *parent, VariableTableEntry *var);
7276Scope *create_cimport_scope(AstNode *node, Scope *parent);
7377Scope *create_loop_scope(AstNode *node, Scope *parent);
74Scope *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry);
78ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry);
7579
7680#endif
src/ast_render.cpp+4-4
......@@ -377,7 +377,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
377377 break;
378378 case NodeTypeFnProto:
379379 {
380 const char *pub_str = visib_mod_string(node->data.fn_proto.top_level_decl.visib_mod);
380 const char *pub_str = visib_mod_string(node->data.fn_proto.visib_mod);
381381 const char *extern_str = extern_string(node->data.fn_proto.is_extern);
382382 const char *inline_str = inline_string(node->data.fn_proto.is_inline);
383383 fprintf(ar->f, "%s%s%sfn ", pub_str, inline_str, extern_str);
......@@ -460,7 +460,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
460460 }
461461 case NodeTypeVariableDeclaration:
462462 {
463 const char *pub_str = visib_mod_string(node->data.variable_declaration.top_level_decl.visib_mod);
463 const char *pub_str = visib_mod_string(node->data.variable_declaration.visib_mod);
464464 const char *extern_str = extern_string(node->data.variable_declaration.is_extern);
465465 const char *const_or_var = const_or_var_string(node->data.variable_declaration.is_const);
466466 fprintf(ar->f, "%s%s%s ", pub_str, extern_str, const_or_var);
......@@ -478,7 +478,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
478478 }
479479 case NodeTypeTypeDecl:
480480 {
481 const char *pub_str = visib_mod_string(node->data.type_decl.top_level_decl.visib_mod);
481 const char *pub_str = visib_mod_string(node->data.type_decl.visib_mod);
482482 const char *var_name = buf_ptr(node->data.type_decl.symbol);
483483 fprintf(ar->f, "%stype %s = ", pub_str, var_name);
484484 render_node_grouped(ar, node->data.type_decl.child_type);
......@@ -575,7 +575,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
575575 case NodeTypeContainerDecl:
576576 {
577577 const char *struct_name = buf_ptr(node->data.struct_decl.name);
578 const char *pub_str = visib_mod_string(node->data.struct_decl.top_level_decl.visib_mod);
578 const char *pub_str = visib_mod_string(node->data.struct_decl.visib_mod);
579579 const char *container_str = container_string(node->data.struct_decl.kind);
580580 fprintf(ar->f, "%s%s %s {\n", pub_str, container_str, struct_name);
581581 ar->indent += ar->indent_size;
src/codegen.cpp+168-116
......@@ -60,7 +60,6 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) {
6060 g->primitive_type_table.init(32);
6161 g->fn_type_table.init(32);
6262 g->error_table.init(16);
63 g->generic_table.init(16);
6463 g->is_release_build = false;
6564 g->is_test_build = false;
6665 g->want_h_file = true;
......@@ -227,13 +226,59 @@ void codegen_set_rdynamic(CodeGen *g, bool rdynamic) {
227226static void render_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val);
228227static void render_const_val_global(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val);
229228
229static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) {
230 if (fn_table_entry->llvm_value)
231 return fn_table_entry->llvm_value;
232
233 Buf *symbol_name;
234 if (!fn_table_entry->internal_linkage) {
235 symbol_name = &fn_table_entry->symbol_name;
236 } else {
237 symbol_name = buf_sprintf("_%s", buf_ptr(&fn_table_entry->symbol_name));
238 }
239
240 TypeTableEntry *fn_type = fn_table_entry->type_entry;
241 fn_table_entry->llvm_value = LLVMAddFunction(g->module, buf_ptr(symbol_name), fn_type->data.fn.raw_type_ref);
242
243 switch (fn_table_entry->fn_inline) {
244 case FnInlineAlways:
245 LLVMAddFunctionAttr(fn_table_entry->llvm_value, LLVMAlwaysInlineAttribute);
246 break;
247 case FnInlineNever:
248 LLVMAddFunctionAttr(fn_table_entry->llvm_value, LLVMNoInlineAttribute);
249 break;
250 case FnInlineAuto:
251 break;
252 }
253 if (fn_type->data.fn.fn_type_id.is_naked) {
254 LLVMAddFunctionAttr(fn_table_entry->llvm_value, LLVMNakedAttribute);
255 }
256
257 LLVMSetLinkage(fn_table_entry->llvm_value, fn_table_entry->internal_linkage ?
258 LLVMInternalLinkage : LLVMExternalLinkage);
259
260 if (fn_type->data.fn.fn_type_id.return_type->id == TypeTableEntryIdUnreachable) {
261 LLVMAddFunctionAttr(fn_table_entry->llvm_value, LLVMNoReturnAttribute);
262 }
263 LLVMSetFunctionCallConv(fn_table_entry->llvm_value, fn_type->data.fn.calling_convention);
264 if (!fn_type->data.fn.fn_type_id.is_extern) {
265 LLVMAddFunctionAttr(fn_table_entry->llvm_value, LLVMNoUnwindAttribute);
266 }
267 if (!g->is_release_build && fn_table_entry->fn_inline != FnInlineAlways) {
268 ZigLLVMAddFunctionAttr(fn_table_entry->llvm_value, "no-frame-pointer-elim", "true");
269 ZigLLVMAddFunctionAttr(fn_table_entry->llvm_value, "no-frame-pointer-elim-non-leaf", nullptr);
270 }
271
272 return fn_table_entry->llvm_value;
273}
274
230275static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
231276 if (scope->di_scope)
232277 return scope->di_scope;
233278
234279 if (scope->node->type == NodeTypeFnDef) {
235280 assert(scope->parent);
236 ScopeFnBody *fn_scope = (ScopeFnBody *)scope;
281 ScopeFnDef *fn_scope = (ScopeFnDef *)scope;
237282 FnTableEntry *fn_table_entry = fn_scope->fn_entry;
238283 unsigned line_number = fn_table_entry->proto_node->line + 1;
239284 unsigned scope_line = line_number;
......@@ -247,11 +292,13 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
247292 is_definition, scope_line, flags, is_optimized, nullptr);
248293
249294 scope->di_scope = ZigLLVMSubprogramToScope(subprogram);
250 ZigLLVMFnSetSubprogram(fn_table_entry->fn_value, subprogram);
251 } else if (scope->node->type == NodeTypeRoot ||
252 scope->node->type == NodeTypeContainerDecl)
253 {
295 ZigLLVMFnSetSubprogram(fn_llvm_value(g, fn_table_entry), subprogram);
296 } else if (scope->node->type == NodeTypeRoot) {
254297 scope->di_scope = ZigLLVMFileToScope(scope->node->owner->di_file);
298 } else if (scope->node->type == NodeTypeContainerDecl) {
299 ScopeDecls *decls_scope = (ScopeDecls *)scope;
300 assert(decls_scope->container_type);
301 scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type);
255302 } else {
256303 assert(scope->parent);
257304 ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder,
......@@ -265,11 +312,6 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
265312 return scope->di_scope;
266313}
267314
268static void set_debug_source_node(CodeGen *g, AstNode *node) {
269 assert(node->scope);
270 ZigLLVMSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1, get_di_scope(g, node->scope));
271}
272
273315static void clear_debug_source_node(CodeGen *g) {
274316 ZigLLVMClearCurrentDebugLocation(g->builder);
275317}
......@@ -350,24 +392,25 @@ static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, TypeTableEntr
350392 }
351393}
352394
353static bool want_debug_safety_recursive(CodeGen *g, Scope *context) {
354 if (context->safety_set_node || !context->parent) {
355 return !context->safety_off;
356 }
357 context->safety_off = !want_debug_safety_recursive(g, context->parent);
358 context->safety_set_node = context->parent->safety_set_node;
359 return !context->safety_off;
360}
361
362static bool want_debug_safety(CodeGen *g, AstNode *node) {
363 if (g->is_release_build) {
395static bool ir_want_debug_safety(CodeGen *g, IrInstruction *instruction) {
396 if (g->is_release_build)
364397 return false;
365 }
366 return want_debug_safety_recursive(g, node->scope);
367}
368398
369static bool ir_want_debug_safety(CodeGen *g, IrInstruction *instruction) {
370 return want_debug_safety(g, instruction->source_node);
399 // TODO memoize
400 Scope *scope = instruction->scope;
401 while (scope) {
402 if (scope->node->type == NodeTypeBlock) {
403 ScopeBlock *block_scope = (ScopeBlock *)scope;
404 if (block_scope->safety_set_node)
405 return !block_scope->safety_off;
406 } else if (scope->node->type == NodeTypeRoot || scope->node->type == NodeTypeContainerDecl) {
407 ScopeDecls *decls_scope = (ScopeDecls *)scope;
408 if (decls_scope->safety_set_node)
409 return !decls_scope->safety_off;
410 }
411 scope = scope->parent;
412 }
413 return true;
371414}
372415
373416static void gen_debug_safety_crash(CodeGen *g) {
......@@ -388,10 +431,10 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,
388431 upper_value = nullptr;
389432 }
390433
391 LLVMBasicBlockRef bounds_check_fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "BoundsCheckFail");
392 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "BoundsCheckOk");
434 LLVMBasicBlockRef bounds_check_fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "BoundsCheckFail");
435 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "BoundsCheckOk");
393436 LLVMBasicBlockRef lower_ok_block = upper_value ?
394 LLVMAppendBasicBlock(g->cur_fn->fn_value, "FirstBoundsCheckOk") : ok_block;
437 LLVMAppendBasicBlock(g->cur_fn_val, "FirstBoundsCheckOk") : ok_block;
395438
396439 LLVMValueRef lower_ok_val = LLVMBuildICmp(g->builder, lower_pred, target_val, lower_value, "");
397440 LLVMBuildCondBr(g->builder, lower_ok_val, lower_ok_block, bounds_check_fail_block);
......@@ -408,7 +451,7 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,
408451 LLVMPositionBuilderAtEnd(g->builder, ok_block);
409452}
410453
411static LLVMValueRef gen_widen_or_shorten(CodeGen *g, AstNode *source_node, TypeTableEntry *actual_type_non_canon,
454static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_debug_safety, TypeTableEntry *actual_type_non_canon,
412455 TypeTableEntry *wanted_type_non_canon, LLVMValueRef expr_val)
413456{
414457 TypeTableEntry *actual_type = get_underlying_type(actual_type_non_canon);
......@@ -430,13 +473,13 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, AstNode *source_node, TypeT
430473
431474 if (actual_bits >= wanted_bits && actual_type->id == TypeTableEntryIdInt &&
432475 !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&
433 want_debug_safety(g, source_node))
476 want_debug_safety)
434477 {
435478 LLVMValueRef zero = LLVMConstNull(actual_type->type_ref);
436479 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");
437480
438 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "SignCastOk");
439 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "SignCastFail");
481 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SignCastOk");
482 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SignCastFail");
440483 LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block);
441484
442485 LLVMPositionBuilderAtEnd(g->builder, fail_block);
......@@ -464,7 +507,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, AstNode *source_node, TypeT
464507 return LLVMBuildFPTrunc(g->builder, expr_val, wanted_type->type_ref, "");
465508 } else if (actual_type->id == TypeTableEntryIdInt) {
466509 LLVMValueRef trunc_val = LLVMBuildTrunc(g->builder, expr_val, wanted_type->type_ref, "");
467 if (!want_debug_safety(g, source_node)) {
510 if (!want_debug_safety) {
468511 return trunc_val;
469512 }
470513 LLVMValueRef orig_val;
......@@ -474,8 +517,8 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, AstNode *source_node, TypeT
474517 orig_val = LLVMBuildZExt(g->builder, trunc_val, actual_type->type_ref, "");
475518 }
476519 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, orig_val, "");
477 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "CastShortenOk");
478 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "CastShortenFail");
520 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
521 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenFail");
479522 LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block);
480523
481524 LLVMPositionBuilderAtEnd(g->builder, fail_block);
......@@ -502,8 +545,8 @@ static LLVMValueRef gen_overflow_op(CodeGen *g, TypeTableEntry *type_entry, AddS
502545 LLVMValueRef result_struct = LLVMBuildCall(g->builder, fn_val, params, 2, "");
503546 LLVMValueRef result = LLVMBuildExtractValue(g->builder, result_struct, 0, "");
504547 LLVMValueRef overflow_bit = LLVMBuildExtractValue(g->builder, result_struct, 1, "");
505 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "OverflowFail");
506 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "OverflowOk");
548 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "OverflowFail");
549 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "OverflowOk");
507550 LLVMBuildCondBr(g->builder, overflow_bit, fail_block, ok_block);
508551
509552 LLVMPositionBuilderAtEnd(g->builder, fail_block);
......@@ -645,8 +688,8 @@ static LLVMValueRef gen_overflow_shl_op(CodeGen *g, TypeTableEntry *type_entry,
645688 }
646689 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, val1, orig_val, "");
647690
648 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "OverflowOk");
649 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "OverflowFail");
691 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "OverflowOk");
692 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "OverflowFail");
650693 LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block);
651694
652695 LLVMPositionBuilderAtEnd(g->builder, fail_block);
......@@ -656,11 +699,11 @@ static LLVMValueRef gen_overflow_shl_op(CodeGen *g, TypeTableEntry *type_entry,
656699 return result;
657700}
658701
659static LLVMValueRef gen_div(CodeGen *g, AstNode *source_node, LLVMValueRef val1, LLVMValueRef val2,
702static LLVMValueRef gen_div(CodeGen *g, bool want_debug_safety, LLVMValueRef val1, LLVMValueRef val2,
660703 TypeTableEntry *type_entry, bool exact)
661704{
662705
663 if (want_debug_safety(g, source_node)) {
706 if (want_debug_safety) {
664707 LLVMValueRef zero = LLVMConstNull(type_entry->type_ref);
665708 LLVMValueRef is_zero_bit;
666709 if (type_entry->id == TypeTableEntryIdInt) {
......@@ -670,8 +713,8 @@ static LLVMValueRef gen_div(CodeGen *g, AstNode *source_node, LLVMValueRef val1,
670713 } else {
671714 zig_unreachable();
672715 }
673 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "DivZeroOk");
674 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "DivZeroFail");
716 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivZeroOk");
717 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivZeroFail");
675718 LLVMBuildCondBr(g->builder, is_zero_bit, fail_block, ok_block);
676719
677720 LLVMPositionBuilderAtEnd(g->builder, fail_block);
......@@ -688,7 +731,7 @@ static LLVMValueRef gen_div(CodeGen *g, AstNode *source_node, LLVMValueRef val1,
688731 assert(type_entry->id == TypeTableEntryIdInt);
689732
690733 if (exact) {
691 if (want_debug_safety(g, source_node)) {
734 if (want_debug_safety) {
692735 LLVMValueRef remainder_val;
693736 if (type_entry->data.integral.is_signed) {
694737 remainder_val = LLVMBuildSRem(g->builder, val1, val2, "");
......@@ -698,8 +741,8 @@ static LLVMValueRef gen_div(CodeGen *g, AstNode *source_node, LLVMValueRef val1,
698741 LLVMValueRef zero = LLVMConstNull(type_entry->type_ref);
699742 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");
700743
701 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "DivExactOk");
702 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "DivExactFail");
744 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivExactOk");
745 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivExactFail");
703746 LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block);
704747
705748 LLVMPositionBuilderAtEnd(g->builder, fail_block);
......@@ -727,7 +770,6 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
727770 IrBinOp op_id = bin_op_instruction->op_id;
728771 IrInstruction *op1 = bin_op_instruction->op1;
729772 IrInstruction *op2 = bin_op_instruction->op2;
730 AstNode *source_node = bin_op_instruction->base.source_node;
731773
732774 assert(op1->type_entry == op2->type_entry);
733775
......@@ -801,7 +843,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
801843 bool is_wrapping = (op_id == IrBinOpBitShiftLeftWrap);
802844 if (is_wrapping) {
803845 return LLVMBuildShl(g->builder, op1_value, op2_value, "");
804 } else if (want_debug_safety(g, source_node)) {
846 } else if (ir_want_debug_safety(g, &bin_op_instruction->base)) {
805847 return gen_overflow_shl_op(g, op1->type_entry, op1_value, op2_value);
806848 } else if (op1->type_entry->data.integral.is_signed) {
807849 return ZigLLVMBuildNSWShl(g->builder, op1_value, op2_value, "");
......@@ -824,7 +866,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
824866 bool is_wrapping = (op_id == IrBinOpSubWrap);
825867 if (is_wrapping) {
826868 return LLVMBuildSub(g->builder, op1_value, op2_value, "");
827 } else if (want_debug_safety(g, source_node)) {
869 } else if (ir_want_debug_safety(g, &bin_op_instruction->base)) {
828870 return gen_overflow_op(g, op1->type_entry, AddSubMulSub, op1_value, op2_value);
829871 } else if (op1->type_entry->data.integral.is_signed) {
830872 return LLVMBuildNSWSub(g->builder, op1_value, op2_value, "");
......@@ -842,7 +884,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
842884 bool is_wrapping = (op_id == IrBinOpMultWrap);
843885 if (is_wrapping) {
844886 return LLVMBuildMul(g->builder, op1_value, op2_value, "");
845 } else if (want_debug_safety(g, source_node)) {
887 } else if (ir_want_debug_safety(g, &bin_op_instruction->base)) {
846888 return gen_overflow_op(g, op1->type_entry, AddSubMulMul, op1_value, op2_value);
847889 } else if (op1->type_entry->data.integral.is_signed) {
848890 return LLVMBuildNSWMul(g->builder, op1_value, op2_value, "");
......@@ -853,7 +895,8 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
853895 zig_unreachable();
854896 }
855897 case IrBinOpDiv:
856 return gen_div(g, source_node, op1_value, op2_value, op1->type_entry, false);
898 return gen_div(g, ir_want_debug_safety(g, &bin_op_instruction->base),
899 op1_value, op2_value, op1->type_entry, false);
857900 case IrBinOpMod:
858901 if (op1->type_entry->id == TypeTableEntryIdFloat) {
859902 return LLVMBuildFRem(g->builder, op1_value, op2_value, "");
......@@ -885,8 +928,8 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
885928 case CastOpErrToInt:
886929 assert(actual_type->id == TypeTableEntryIdErrorUnion);
887930 if (!type_has_bits(actual_type->data.error.child_type)) {
888 return gen_widen_or_shorten(g, cast_instruction->base.source_node,
889 g->err_tag_type, wanted_type, expr_val);
931 return gen_widen_or_shorten(g, ir_want_debug_safety(g, &cast_instruction->base),
932 g->err_tag_type, wanted_type, expr_val);
890933 } else {
891934 zig_panic("TODO");
892935 }
......@@ -954,7 +997,8 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
954997 case CastOpPointerReinterpret:
955998 return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, "");
956999 case CastOpWidenOrShorten:
957 return gen_widen_or_shorten(g, cast_instruction->base.source_node, actual_type, wanted_type, expr_val);
1000 return gen_widen_or_shorten(g, ir_want_debug_safety(g, &cast_instruction->base),
1001 actual_type, wanted_type, expr_val);
9581002 case CastOpToUnknownSizeArray:
9591003 {
9601004 assert(cast_instruction->tmp_ptr);
......@@ -1021,8 +1065,8 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
10211065 LLVMValueRef remainder_val = LLVMBuildURem(g->builder, src_len, dest_size_val, "");
10221066 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);
10231067 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");
1024 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "SliceWidenOk");
1025 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "SliceWidenFail");
1068 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenOk");
1069 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenFail");
10261070 LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block);
10271071
10281072 LLVMPositionBuilderAtEnd(g->builder, fail_block);
......@@ -1087,10 +1131,10 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
10871131 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
10881132
10891133 case CastOpIntToEnum:
1090 return gen_widen_or_shorten(g, cast_instruction->base.source_node,
1134 return gen_widen_or_shorten(g, ir_want_debug_safety(g, &cast_instruction->base),
10911135 actual_type, wanted_type->data.enumeration.tag_type, expr_val);
10921136 case CastOpEnumToInt:
1093 return gen_widen_or_shorten(g, cast_instruction->base.source_node,
1137 return gen_widen_or_shorten(g, ir_want_debug_safety(g, &cast_instruction->base),
10941138 actual_type->data.enumeration.tag_type, wanted_type, expr_val);
10951139 }
10961140 zig_unreachable();
......@@ -1197,8 +1241,8 @@ static LLVMValueRef ir_render_un_op(CodeGen *g, IrExecutable *executable, IrInst
11971241 }
11981242 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);
11991243 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");
1200 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapErrError");
1201 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapErrOk");
1244 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrError");
1245 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrOk");
12021246 LLVMBuildCondBr(g->builder, cond_val, ok_block, err_block);
12031247
12041248 LLVMPositionBuilderAtEnd(g->builder, err_block);
......@@ -1231,8 +1275,8 @@ static LLVMValueRef ir_render_un_op(CodeGen *g, IrExecutable *executable, IrInst
12311275 cond_val = LLVMBuildLoad(g->builder, maybe_null_ptr, "");
12321276 }
12331277
1234 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapMaybeOk");
1235 LLVMBasicBlockRef null_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapMaybeNull");
1278 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapMaybeOk");
1279 LLVMBasicBlockRef null_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapMaybeNull");
12361280 LLVMBuildCondBr(g->builder, cond_val, ok_block, null_block);
12371281
12381282 LLVMPositionBuilderAtEnd(g->builder, null_block);
......@@ -1408,7 +1452,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
14081452 LLVMValueRef fn_val;
14091453 TypeTableEntry *fn_type;
14101454 if (instruction->fn_entry) {
1411 fn_val = instruction->fn_entry->fn_value;
1455 fn_val = fn_llvm_value(g, instruction->fn_entry);
14121456 fn_type = instruction->fn_entry->type_entry;
14131457 } else {
14141458 assert(instruction->fn_ref);
......@@ -1563,7 +1607,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
15631607 }
15641608
15651609 if (!is_return) {
1566 VariableTableEntry *variable = asm_output->variable;
1610 VariableTableEntry *variable = instruction->output_vars[i];
15671611 assert(variable);
15681612 param_types[param_index] = LLVMTypeOf(variable->value_ref);
15691613 param_values[param_index] = variable->value_ref;
......@@ -1638,8 +1682,8 @@ static LLVMValueRef ir_render_unwrap_maybe(CodeGen *g, IrExecutable *executable,
16381682 LLVMValueRef maybe_ptr = ir_llvm_value(g, instruction->value);
16391683 if (ir_want_debug_safety(g, &instruction->base) && instruction->safety_check_on) {
16401684 LLVMValueRef nonnull_bit = gen_null_bit(g, ptr_type, maybe_ptr);
1641 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapMaybeOk");
1642 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "UnwrapMaybeFail");
1685 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapMaybeOk");
1686 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapMaybeFail");
16431687 LLVMBuildCondBr(g->builder, nonnull_bit, ok_block, fail_block);
16441688
16451689 LLVMPositionBuilderAtEnd(g->builder, fail_block);
......@@ -1730,8 +1774,18 @@ static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutable *executable, IrInstru
17301774 }
17311775}
17321776
1777static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
1778 AstNode *source_node = instruction->source_node;
1779 Scope *scope = instruction->scope;
1780
1781 assert(source_node);
1782 assert(scope);
1783
1784 ZigLLVMSetCurrentDebugLocation(g->builder, source_node->line + 1, source_node->column + 1, get_di_scope(g, scope));
1785}
1786
17331787static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, IrInstruction *instruction) {
1734 set_debug_source_node(g, instruction->source_node);
1788 set_debug_location(g, instruction);
17351789
17361790 switch (instruction->id) {
17371791 case IrInstructionIdInvalid:
......@@ -1961,7 +2015,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
19612015 }
19622016 }
19632017 case TypeTableEntryIdFn:
1964 return const_val->data.x_fn->fn_value;
2018 return fn_llvm_value(g, const_val->data.x_fn);
19652019 case TypeTableEntryIdPointer:
19662020 {
19672021 TypeTableEntry *child_type = type_entry->data.pointer.child_type;
......@@ -2021,7 +2075,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
20212075 case TypeTableEntryIdNullLit:
20222076 case TypeTableEntryIdNamespace:
20232077 case TypeTableEntryIdBlock:
2024 case TypeTableEntryIdGenericFn:
20252078 case TypeTableEntryIdBoundFn:
20262079 case TypeTableEntryIdVar:
20272080 zig_unreachable();
......@@ -2107,7 +2160,7 @@ static LLVMValueRef gen_test_fn_val(CodeGen *g, FnTableEntry *fn_entry) {
21072160 LLVMValueRef name_val = LLVMConstStruct(name_fields, 2, false);
21082161 LLVMValueRef fields[] = {
21092162 name_val,
2110 fn_entry->fn_value,
2163 fn_llvm_value(g, fn_entry),
21112164 };
21122165 return LLVMConstStruct(fields, 2, false);
21132166}
......@@ -2157,7 +2210,7 @@ static void build_all_basic_blocks(CodeGen *g, FnTableEntry *fn) {
21572210 assert(executable->basic_block_list.length > 0);
21582211 for (size_t block_i = 0; block_i < executable->basic_block_list.length; block_i += 1) {
21592212 IrBasicBlock *bb = executable->basic_block_list.at(block_i);
2160 bb->llvm_block = LLVMAppendBasicBlock(fn->fn_value, bb->name_hint);
2213 bb->llvm_block = LLVMAppendBasicBlock(fn_llvm_value(g, fn), bb->name_hint);
21612214 }
21622215 IrBasicBlock *entry_bb = executable->basic_block_list.at(0);
21632216 LLVMPositionBuilderAtEnd(g->builder, entry_bb->llvm_block);
......@@ -2167,11 +2220,14 @@ static void gen_global_var(CodeGen *g, VariableTableEntry *var, LLVMValueRef ini
21672220 TypeTableEntry *type_entry)
21682221{
21692222 assert(var->gen_is_const);
2170 assert(var->import);
21712223 assert(type_entry);
2224
2225 ImportTableEntry *import = get_scope_import(var->parent_scope);
2226 assert(import);
2227
21722228 bool is_local_to_unit = true;
21732229 ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name),
2174 buf_ptr(&var->name), var->import->di_file, var->decl_node->line + 1,
2230 buf_ptr(&var->name), import->di_file, var->decl_node->line + 1,
21752231 type_entry->di_type, is_local_to_unit, init_val);
21762232}
21772233
......@@ -2187,7 +2243,7 @@ static void do_code_gen(CodeGen *g) {
21872243
21882244 if (var->type->id == TypeTableEntryIdNumLitFloat) {
21892245 // Generate debug info for it but that's it.
2190 ConstExprValue *const_val = &var->decl_node->data.variable_declaration.top_level_decl.value->static_value;
2246 ConstExprValue *const_val = var->value;
21912247 assert(const_val->special != ConstValSpecialRuntime);
21922248 TypeTableEntry *var_type = g->builtin_types.entry_f64;
21932249 LLVMValueRef init_val = LLVMConstReal(var_type->type_ref, const_val->data.x_bignum.data.x_float);
......@@ -2197,7 +2253,7 @@ static void do_code_gen(CodeGen *g) {
21972253
21982254 if (var->type->id == TypeTableEntryIdNumLitInt) {
21992255 // Generate debug info for it but that's it.
2200 ConstExprValue *const_val = &var->decl_node->data.variable_declaration.top_level_decl.value->static_value;
2256 ConstExprValue *const_val = var->value;
22012257 assert(const_val->special != ConstValSpecialRuntime);
22022258 TypeTableEntry *var_type = const_val->data.x_bignum.is_negative ?
22032259 g->builtin_types.entry_isize : g->builtin_types.entry_usize;
......@@ -2222,13 +2278,12 @@ static void do_code_gen(CodeGen *g) {
22222278
22232279 LLVMSetLinkage(global_value, LLVMExternalLinkage);
22242280 } else {
2225 IrInstruction *instruction = var->decl_node->data.variable_declaration.top_level_decl.value;
2226 render_const_val(g, instruction->type_entry, &instruction->static_value);
2227 render_const_val_global(g, instruction->type_entry, &instruction->static_value);
2228 global_value = instruction->static_value.llvm_global;
2281 render_const_val(g, var->type, var->value);
2282 render_const_val_global(g, var->type, var->value);
2283 global_value = var->value->llvm_global;
22292284 // TODO debug info for function pointers
22302285 if (var->gen_is_const && var->type->id != TypeTableEntryIdFn) {
2231 gen_global_var(g, var, instruction->static_value.llvm_value, var->type);
2286 gen_global_var(g, var, var->value->llvm_value, var->type);
22322287 }
22332288 }
22342289
......@@ -2246,12 +2301,8 @@ static void do_code_gen(CodeGen *g) {
22462301 // Generate function prototypes
22472302 for (size_t fn_proto_i = 0; fn_proto_i < g->fn_protos.length; fn_proto_i += 1) {
22482303 FnTableEntry *fn_table_entry = g->fn_protos.at(fn_proto_i);
2249 if (should_skip_fn_codegen(g, fn_table_entry)) {
2250 // huge time saver
2251 LLVMDeleteFunction(fn_table_entry->fn_value);
2252 fn_table_entry->fn_value = nullptr;
2304 if (should_skip_fn_codegen(g, fn_table_entry))
22532305 continue;
2254 }
22552306
22562307 AstNode *proto_node = fn_table_entry->proto_node;
22572308 assert(proto_node->type == NodeTypeFnProto);
......@@ -2259,16 +2310,18 @@ static void do_code_gen(CodeGen *g) {
22592310
22602311 TypeTableEntry *fn_type = fn_table_entry->type_entry;
22612312
2313 LLVMValueRef fn_val = fn_llvm_value(g, fn_table_entry);
2314
22622315 if (!type_has_bits(fn_type->data.fn.fn_type_id.return_type)) {
22632316 // nothing to do
22642317 } else if (fn_type->data.fn.fn_type_id.return_type->id == TypeTableEntryIdPointer) {
2265 ZigLLVMAddNonNullAttr(fn_table_entry->fn_value, 0);
2318 ZigLLVMAddNonNullAttr(fn_val, 0);
22662319 } else if (handle_is_ptr(fn_type->data.fn.fn_type_id.return_type) &&
22672320 !fn_type->data.fn.fn_type_id.is_extern)
22682321 {
2269 LLVMValueRef first_arg = LLVMGetParam(fn_table_entry->fn_value, 0);
2322 LLVMValueRef first_arg = LLVMGetParam(fn_val, 0);
22702323 LLVMAddAttribute(first_arg, LLVMStructRetAttribute);
2271 ZigLLVMAddNonNullAttr(fn_table_entry->fn_value, 1);
2324 ZigLLVMAddNonNullAttr(fn_val, 1);
22722325 }
22732326
22742327
......@@ -2286,7 +2339,7 @@ static void do_code_gen(CodeGen *g) {
22862339 }
22872340
22882341 TypeTableEntry *param_type = info->type;
2289 LLVMValueRef argument_val = LLVMGetParam(fn_table_entry->fn_value, gen_index);
2342 LLVMValueRef argument_val = LLVMGetParam(fn_val, gen_index);
22902343 bool param_is_noalias = param_node->data.param_decl.is_noalias;
22912344 if (param_is_noalias) {
22922345 LLVMAddAttribute(argument_val, LLVMNoAliasAttribute);
......@@ -2295,7 +2348,7 @@ static void do_code_gen(CodeGen *g) {
22952348 LLVMAddAttribute(argument_val, LLVMReadOnlyAttribute);
22962349 }
22972350 if (param_type->id == TypeTableEntryIdPointer) {
2298 ZigLLVMAddNonNullAttr(fn_table_entry->fn_value, gen_index + 1);
2351 ZigLLVMAddNonNullAttr(fn_val, gen_index + 1);
22992352 }
23002353 if (is_byval) {
23012354 // TODO
......@@ -2345,14 +2398,13 @@ static void do_code_gen(CodeGen *g) {
23452398 // Generate function definitions.
23462399 for (size_t fn_i = 0; fn_i < g->fn_defs.length; fn_i += 1) {
23472400 FnTableEntry *fn_table_entry = g->fn_defs.at(fn_i);
2348 if (should_skip_fn_codegen(g, fn_table_entry)) {
2349 // huge time saver
2401 if (should_skip_fn_codegen(g, fn_table_entry))
23502402 continue;
2351 }
23522403
23532404 ImportTableEntry *import = fn_table_entry->import_entry;
2354 LLVMValueRef fn = fn_table_entry->fn_value;
2405 LLVMValueRef fn = fn_llvm_value(g, fn_table_entry);
23552406 g->cur_fn = fn_table_entry;
2407 g->cur_fn_val = fn;
23562408 if (handle_is_ptr(fn_table_entry->type_entry->data.fn.fn_type_id.return_type)) {
23572409 g->cur_ret_ptr = LLVMGetParam(fn, 0);
23582410 } else {
......@@ -2401,7 +2453,18 @@ static void do_code_gen(CodeGen *g) {
24012453 if (var->is_inline)
24022454 continue;
24032455
2404 if (var->parent_scope->node->type == NodeTypeFnDef) {
2456 if (var->src_arg_index == SIZE_MAX) {
2457 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
2458
2459
2460 unsigned align_bytes = ZigLLVMGetPrefTypeAlignment(g->target_data_ref, var->type->type_ref);
2461 LLVMSetAlignment(var->value_ref, align_bytes);
2462
2463 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
2464 buf_ptr(&var->name), import->di_file, var->decl_node->line + 1,
2465 var->type->di_type, !g->strip_debug_symbols, 0);
2466
2467 } else {
24052468 assert(var->gen_arg_index != SIZE_MAX);
24062469 TypeTableEntry *gen_type;
24072470 if (handle_is_ptr(var->type)) {
......@@ -2417,31 +2480,21 @@ static void do_code_gen(CodeGen *g) {
24172480 buf_ptr(&var->name), import->di_file, var->decl_node->line + 1,
24182481 gen_type->di_type, !g->strip_debug_symbols, 0, var->gen_arg_index + 1);
24192482
2420 } else {
2421 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
2422
2423
2424 unsigned align_bytes = ZigLLVMGetPrefTypeAlignment(g->target_data_ref, var->type->type_ref);
2425 LLVMSetAlignment(var->value_ref, align_bytes);
2426
2427 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
2428 buf_ptr(&var->name), import->di_file, var->decl_node->line + 1,
2429 var->type->di_type, !g->strip_debug_symbols, 0);
24302483 }
24312484 }
24322485
24332486 // create debug variable declarations for parameters
2487 // rely on the first variables in the variable_list being parameters.
2488 size_t next_var_i = 0;
24342489 for (size_t param_i = 0; param_i < fn_proto->params.length; param_i += 1) {
2435 AstNode *param_decl = fn_proto->params.at(param_i);
2436 assert(param_decl->type == NodeTypeParamDecl);
2437
24382490 FnGenParamInfo *info = &fn_table_entry->type_entry->data.fn.gen_param_info[param_i];
2439
2440 if (info->gen_index == SIZE_MAX) {
2491 if (info->gen_index == SIZE_MAX)
24412492 continue;
2442 }
24432493
2444 VariableTableEntry *variable = param_decl->data.param_decl.variable;
2494 VariableTableEntry *variable = fn_table_entry->variable_list.at(next_var_i);
2495 assert(variable->src_arg_index != SIZE_MAX);
2496 next_var_i += 1;
2497
24452498 assert(variable);
24462499 assert(variable->value_ref);
24472500
......@@ -3308,7 +3361,6 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
33083361 zig_panic("TODO");
33093362 case TypeTableEntryIdInvalid:
33103363 case TypeTableEntryIdMetaType:
3311 case TypeTableEntryIdGenericFn:
33123364 case TypeTableEntryIdBoundFn:
33133365 case TypeTableEntryIdNamespace:
33143366 case TypeTableEntryIdBlock:
......@@ -3343,7 +3395,7 @@ void codegen_generate_h_file(CodeGen *g) {
33433395 assert(proto_node->type == NodeTypeFnProto);
33443396 AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
33453397
3346 if (fn_proto->top_level_decl.visib_mod != VisibModExport)
3398 if (fn_proto->visib_mod != VisibModExport)
33473399 continue;
33483400
33493401 FnTypeId *fn_type_id = &fn_table_entry->type_entry->data.fn.fn_type_id;
src/eval.cpp-1
......@@ -55,7 +55,6 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b, TypeTableEntry *ty
5555 zig_panic("TODO");
5656 case TypeTableEntryIdBlock:
5757 zig_panic("TODO");
58 case TypeTableEntryIdGenericFn:
5958 case TypeTableEntryIdBoundFn:
6059 case TypeTableEntryIdInvalid:
6160 case TypeTableEntryIdUnreachable:
src/ir.cpp+686-668
......@@ -1,3 +1,10 @@
1/*
2 * Copyright (c) 2016 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
18#include "analyze.hpp"
29#include "error.hpp"
310#include "eval.hpp"
......@@ -71,6 +78,10 @@ static size_t exec_next_mem_slot(IrExecutable *exec) {
7178 return result;
7279}
7380
81static FnTableEntry *exec_fn_entry(IrExecutable *exec) {
82 return exec->fn_entry;
83}
84
7485static void ir_link_new_instruction(IrInstruction *new_instruction, IrInstruction *old_instruction) {
7586 new_instruction->other = old_instruction;
7687 old_instruction->other = new_instruction;
......@@ -289,26 +300,27 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionStructInit *) {
289300}
290301
291302template<typename T>
292static T *ir_create_instruction(IrExecutable *exec, AstNode *source_node) {
303static T *ir_create_instruction(IrExecutable *exec, Scope *scope, AstNode *source_node) {
293304 T *special_instruction = allocate<T>(1);
294305 special_instruction->base.id = ir_instruction_id(special_instruction);
306 special_instruction->base.scope = scope;
295307 special_instruction->base.source_node = source_node;
296308 special_instruction->base.debug_id = exec_next_debug_id(exec);
297309 return special_instruction;
298310}
299311
300312template<typename T>
301static T *ir_build_instruction(IrBuilder *irb, AstNode *source_node) {
313static T *ir_build_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
302314 assert(source_node);
303 T *special_instruction = ir_create_instruction<T>(irb->exec, source_node);
315 T *special_instruction = ir_create_instruction<T>(irb->exec, scope, source_node);
304316 ir_instruction_append(irb->current_basic_block, &special_instruction->base);
305317 return special_instruction;
306318}
307319
308static IrInstruction *ir_build_cast(IrBuilder *irb, AstNode *source_node, TypeTableEntry *dest_type,
320static IrInstruction *ir_build_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, TypeTableEntry *dest_type,
309321 IrInstruction *value, CastOp cast_op)
310322{
311 IrInstructionCast *cast_instruction = ir_build_instruction<IrInstructionCast>(irb, source_node);
323 IrInstructionCast *cast_instruction = ir_build_instruction<IrInstructionCast>(irb, scope, source_node);
312324 cast_instruction->dest_type = dest_type;
313325 cast_instruction->value = value;
314326 cast_instruction->cast_op = cast_op;
......@@ -318,10 +330,10 @@ static IrInstruction *ir_build_cast(IrBuilder *irb, AstNode *source_node, TypeTa
318330 return &cast_instruction->base;
319331}
320332
321static IrInstruction *ir_build_cond_br(IrBuilder *irb, AstNode *source_node, IrInstruction *condition,
333static IrInstruction *ir_build_cond_br(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *condition,
322334 IrBasicBlock *then_block, IrBasicBlock *else_block, bool is_inline)
323335{
324 IrInstructionCondBr *cond_br_instruction = ir_build_instruction<IrInstructionCondBr>(irb, source_node);
336 IrInstructionCondBr *cond_br_instruction = ir_build_instruction<IrInstructionCondBr>(irb, scope, source_node);
325337 cond_br_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;
326338 cond_br_instruction->base.static_value.special = ConstValSpecialStatic;
327339 cond_br_instruction->condition = condition;
......@@ -339,14 +351,14 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, AstNode *source_node, IrI
339351static IrInstruction *ir_build_cond_br_from(IrBuilder *irb, IrInstruction *old_instruction,
340352 IrInstruction *condition, IrBasicBlock *then_block, IrBasicBlock *else_block, bool is_inline)
341353{
342 IrInstruction *new_instruction = ir_build_cond_br(irb, old_instruction->source_node,
354 IrInstruction *new_instruction = ir_build_cond_br(irb, old_instruction->scope, old_instruction->source_node,
343355 condition, then_block, else_block, is_inline);
344356 ir_link_new_instruction(new_instruction, old_instruction);
345357 return new_instruction;
346358}
347359
348static IrInstruction *ir_build_return(IrBuilder *irb, AstNode *source_node, IrInstruction *return_value) {
349 IrInstructionReturn *return_instruction = ir_build_instruction<IrInstructionReturn>(irb, source_node);
360static IrInstruction *ir_build_return(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *return_value) {
361 IrInstructionReturn *return_instruction = ir_build_instruction<IrInstructionReturn>(irb, scope, source_node);
350362 return_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;
351363 return_instruction->base.static_value.special = ConstValSpecialStatic;
352364 return_instruction->value = return_value;
......@@ -359,38 +371,38 @@ static IrInstruction *ir_build_return(IrBuilder *irb, AstNode *source_node, IrIn
359371static IrInstruction *ir_build_return_from(IrBuilder *irb, IrInstruction *old_instruction,
360372 IrInstruction *return_value)
361373{
362 IrInstruction *new_instruction = ir_build_return(irb, old_instruction->source_node, return_value);
374 IrInstruction *new_instruction = ir_build_return(irb, old_instruction->scope, old_instruction->source_node, return_value);
363375 ir_link_new_instruction(new_instruction, old_instruction);
364376 return new_instruction;
365377}
366378
367static IrInstruction *ir_create_const(IrBuilder *irb, AstNode *source_node,
379static IrInstruction *ir_create_const(IrBuilder *irb, Scope *scope, AstNode *source_node,
368380 TypeTableEntry *type_entry, bool depends_on_compile_var)
369381{
370382 assert(type_entry);
371 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, source_node);
383 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);
372384 const_instruction->base.type_entry = type_entry;
373385 const_instruction->base.static_value.special = ConstValSpecialStatic;
374386 const_instruction->base.static_value.depends_on_compile_var = depends_on_compile_var;
375387 return &const_instruction->base;
376388}
377389
378static IrInstruction *ir_build_const_void(IrBuilder *irb, AstNode *source_node) {
379 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
390static IrInstruction *ir_build_const_void(IrBuilder *irb, Scope *scope, AstNode *source_node) {
391 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
380392 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_void;
381393 const_instruction->base.static_value.special = ConstValSpecialStatic;
382394 return &const_instruction->base;
383395}
384396
385static IrInstruction *ir_build_const_undefined(IrBuilder *irb, AstNode *source_node) {
386 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
397static IrInstruction *ir_build_const_undefined(IrBuilder *irb, Scope *scope, AstNode *source_node) {
398 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
387399 const_instruction->base.static_value.special = ConstValSpecialUndef;
388400 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_undef;
389401 return &const_instruction->base;
390402}
391403
392static IrInstruction *ir_build_const_bignum(IrBuilder *irb, AstNode *source_node, BigNum *bignum) {
393 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
404static IrInstruction *ir_build_const_bignum(IrBuilder *irb, Scope *scope, AstNode *source_node, BigNum *bignum) {
405 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
394406 const_instruction->base.type_entry = (bignum->kind == BigNumKindInt) ?
395407 irb->codegen->builtin_types.entry_num_lit_int : irb->codegen->builtin_types.entry_num_lit_float;
396408 const_instruction->base.static_value.special = ConstValSpecialStatic;
......@@ -398,79 +410,77 @@ static IrInstruction *ir_build_const_bignum(IrBuilder *irb, AstNode *source_node
398410 return &const_instruction->base;
399411}
400412
401static IrInstruction *ir_build_const_null(IrBuilder *irb, AstNode *source_node) {
402 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
413static IrInstruction *ir_build_const_null(IrBuilder *irb, Scope *scope, AstNode *source_node) {
414 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
403415 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_null;
404416 const_instruction->base.static_value.special = ConstValSpecialStatic;
405417 return &const_instruction->base;
406418}
407419
408static IrInstruction *ir_build_const_usize(IrBuilder *irb, AstNode *source_node, uint64_t value) {
409 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
420static IrInstruction *ir_build_const_usize(IrBuilder *irb, Scope *scope, AstNode *source_node, uint64_t value) {
421 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
410422 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_usize;
411423 const_instruction->base.static_value.special = ConstValSpecialStatic;
412424 bignum_init_unsigned(&const_instruction->base.static_value.data.x_bignum, value);
413425 return &const_instruction->base;
414426}
415427
416static IrInstruction *ir_create_const_type(IrBuilder *irb, AstNode *source_node, TypeTableEntry *type_entry) {
417 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, source_node);
428static IrInstruction *ir_create_const_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
429 TypeTableEntry *type_entry)
430{
431 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);
418432 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_type;
419433 const_instruction->base.static_value.special = ConstValSpecialStatic;
420434 const_instruction->base.static_value.data.x_type = type_entry;
421435 return &const_instruction->base;
422436}
423437
424static IrInstruction *ir_build_const_type(IrBuilder *irb, AstNode *source_node, TypeTableEntry *type_entry) {
425 IrInstruction *instruction = ir_create_const_type(irb, source_node, type_entry);
438static IrInstruction *ir_build_const_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
439 TypeTableEntry *type_entry)
440{
441 IrInstruction *instruction = ir_create_const_type(irb, scope, source_node, type_entry);
426442 ir_instruction_append(irb->current_basic_block, instruction);
427443 return instruction;
428444}
429445
430static IrInstruction *ir_build_const_fn(IrBuilder *irb, AstNode *source_node, FnTableEntry *fn_entry) {
431 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
446static IrInstruction *ir_build_const_fn(IrBuilder *irb, Scope *scope, AstNode *source_node, FnTableEntry *fn_entry) {
447 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
432448 const_instruction->base.type_entry = fn_entry->type_entry;
433449 const_instruction->base.static_value.special = ConstValSpecialStatic;
434450 const_instruction->base.static_value.data.x_fn = fn_entry;
435451 return &const_instruction->base;
436452}
437453
438static IrInstruction *ir_build_const_generic_fn(IrBuilder *irb, AstNode *source_node, TypeTableEntry *fn_type) {
439 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
440 const_instruction->base.type_entry = fn_type;
441 const_instruction->base.static_value.special = ConstValSpecialStatic;
442 const_instruction->base.static_value.data.x_type = fn_type;
443 return &const_instruction->base;
444}
445
446static IrInstruction *ir_build_const_import(IrBuilder *irb, AstNode *source_node, ImportTableEntry *import) {
447 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
454static IrInstruction *ir_build_const_import(IrBuilder *irb, Scope *scope, AstNode *source_node, ImportTableEntry *import) {
455 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
448456 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_namespace;
449457 const_instruction->base.static_value.special = ConstValSpecialStatic;
450458 const_instruction->base.static_value.data.x_import = import;
451459 return &const_instruction->base;
452460}
453461
454static IrInstruction *ir_build_const_scope(IrBuilder *irb, AstNode *source_node, Scope *scope) {
455 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
462static IrInstruction *ir_build_const_scope(IrBuilder *irb, Scope *parent_scope, AstNode *source_node,
463 Scope *target_scope)
464{
465 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, parent_scope, source_node);
456466 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_block;
457467 const_instruction->base.static_value.special = ConstValSpecialStatic;
458 const_instruction->base.static_value.data.x_block = scope;
468 const_instruction->base.static_value.data.x_block = target_scope;
459469 return &const_instruction->base;
460470}
461471
462static IrInstruction *ir_build_const_bool(IrBuilder *irb, AstNode *source_node, bool value) {
463 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
472static IrInstruction *ir_build_const_bool(IrBuilder *irb, Scope *scope, AstNode *source_node, bool value) {
473 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
464474 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_bool;
465475 const_instruction->base.static_value.special = ConstValSpecialStatic;
466476 const_instruction->base.static_value.data.x_bool = value;
467477 return &const_instruction->base;
468478}
469479
470static IrInstruction *ir_build_const_bound_fn(IrBuilder *irb, AstNode *source_node,
480static IrInstruction *ir_build_const_bound_fn(IrBuilder *irb, Scope *scope, AstNode *source_node,
471481 FnTableEntry *fn_entry, IrInstruction *first_arg, bool depends_on_compile_var)
472482{
473 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
483 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
474484 const_instruction->base.type_entry = get_bound_fn_type(irb->codegen, fn_entry);
475485 const_instruction->base.static_value.special = ConstValSpecialStatic;
476486 const_instruction->base.static_value.depends_on_compile_var = depends_on_compile_var;
......@@ -479,8 +489,8 @@ static IrInstruction *ir_build_const_bound_fn(IrBuilder *irb, AstNode *source_no
479489 return &const_instruction->base;
480490}
481491
482static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, AstNode *source_node, Buf *str) {
483 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
492static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
493 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
484494 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;
485495 TypeTableEntry *type_entry = get_array_type(irb->codegen, u8_type, buf_len(str));
486496 const_instruction->base.type_entry = type_entry;
......@@ -498,7 +508,7 @@ static IrInstruction *ir_build_const_str_lit(IrBuilder *irb, AstNode *source_nod
498508 return &const_instruction->base;
499509}
500510
501static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, AstNode *source_node, Buf *str) {
511static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
502512 // first we build the underlying array
503513 size_t len_with_null = buf_len(str) + 1;
504514 ConstExprValue *array_val = allocate<ConstExprValue>(1);
......@@ -515,7 +525,7 @@ static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, AstNode *source_n
515525 bignum_init_unsigned(&null_char->data.x_bignum, 0);
516526
517527 // then make the pointer point to it
518 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
528 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
519529 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;
520530 TypeTableEntry *type_entry = get_pointer_to_type(irb->codegen, u8_type, true);
521531 const_instruction->base.type_entry = type_entry;
......@@ -528,10 +538,10 @@ static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, AstNode *source_n
528538 return &const_instruction->base;
529539}
530540
531static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id,
541static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *source_node, IrBinOp op_id,
532542 IrInstruction *op1, IrInstruction *op2)
533543{
534 IrInstructionBinOp *bin_op_instruction = ir_build_instruction<IrInstructionBinOp>(irb, source_node);
544 IrInstructionBinOp *bin_op_instruction = ir_build_instruction<IrInstructionBinOp>(irb, scope, source_node);
535545 bin_op_instruction->op_id = op_id;
536546 bin_op_instruction->op1 = op1;
537547 bin_op_instruction->op2 = op2;
......@@ -545,13 +555,14 @@ static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBi
545555static IrInstruction *ir_build_bin_op_from(IrBuilder *irb, IrInstruction *old_instruction, IrBinOp op_id,
546556 IrInstruction *op1, IrInstruction *op2)
547557{
548 IrInstruction *new_instruction = ir_build_bin_op(irb, old_instruction->source_node, op_id, op1, op2);
558 IrInstruction *new_instruction = ir_build_bin_op(irb, old_instruction->scope,
559 old_instruction->source_node, op_id, op1, op2);
549560 ir_link_new_instruction(new_instruction, old_instruction);
550561 return new_instruction;
551562}
552563
553static IrInstruction *ir_build_var_ptr(IrBuilder *irb, AstNode *source_node, VariableTableEntry *var) {
554 IrInstructionVarPtr *instruction = ir_build_instruction<IrInstructionVarPtr>(irb, source_node);
564static IrInstruction *ir_build_var_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, VariableTableEntry *var) {
565 IrInstructionVarPtr *instruction = ir_build_instruction<IrInstructionVarPtr>(irb, scope, source_node);
555566 instruction->var = var;
556567
557568 ir_ref_var(var);
......@@ -560,16 +571,16 @@ static IrInstruction *ir_build_var_ptr(IrBuilder *irb, AstNode *source_node, Var
560571}
561572
562573static IrInstruction *ir_build_var_ptr_from(IrBuilder *irb, IrInstruction *old_instruction, VariableTableEntry *var) {
563 IrInstruction *new_instruction = ir_build_var_ptr(irb, old_instruction->source_node, var);
574 IrInstruction *new_instruction = ir_build_var_ptr(irb, old_instruction->scope, old_instruction->source_node, var);
564575 ir_link_new_instruction(new_instruction, old_instruction);
565576 return new_instruction;
566577
567578}
568579
569static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, AstNode *source_node, IrInstruction *array_ptr,
580static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *array_ptr,
570581 IrInstruction *elem_index, bool safety_check_on)
571582{
572 IrInstructionElemPtr *instruction = ir_build_instruction<IrInstructionElemPtr>(irb, source_node);
583 IrInstructionElemPtr *instruction = ir_build_instruction<IrInstructionElemPtr>(irb, scope, source_node);
573584 instruction->array_ptr = array_ptr;
574585 instruction->elem_index = elem_index;
575586 instruction->safety_check_on = safety_check_on;
......@@ -583,16 +594,16 @@ static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, AstNode *source_node, Ir
583594static IrInstruction *ir_build_elem_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,
584595 IrInstruction *array_ptr, IrInstruction *elem_index, bool safety_check_on)
585596{
586 IrInstruction *new_instruction = ir_build_elem_ptr(irb, old_instruction->source_node, array_ptr, elem_index,
587 safety_check_on);
597 IrInstruction *new_instruction = ir_build_elem_ptr(irb, old_instruction->scope,
598 old_instruction->source_node, array_ptr, elem_index, safety_check_on);
588599 ir_link_new_instruction(new_instruction, old_instruction);
589600 return new_instruction;
590601}
591602
592static IrInstruction *ir_build_field_ptr(IrBuilder *irb, AstNode *source_node,
603static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
593604 IrInstruction *container_ptr, Buf *field_name)
594605{
595 IrInstructionFieldPtr *instruction = ir_build_instruction<IrInstructionFieldPtr>(irb, source_node);
606 IrInstructionFieldPtr *instruction = ir_build_instruction<IrInstructionFieldPtr>(irb, scope, source_node);
596607 instruction->container_ptr = container_ptr;
597608 instruction->field_name = field_name;
598609
......@@ -601,10 +612,10 @@ static IrInstruction *ir_build_field_ptr(IrBuilder *irb, AstNode *source_node,
601612 return &instruction->base;
602613}
603614
604static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, AstNode *source_node,
615static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
605616 IrInstruction *struct_ptr, TypeStructField *field)
606617{
607 IrInstructionStructFieldPtr *instruction = ir_build_instruction<IrInstructionStructFieldPtr>(irb, source_node);
618 IrInstructionStructFieldPtr *instruction = ir_build_instruction<IrInstructionStructFieldPtr>(irb, scope, source_node);
608619 instruction->struct_ptr = struct_ptr;
609620 instruction->field = field;
610621
......@@ -616,16 +627,16 @@ static IrInstruction *ir_build_struct_field_ptr(IrBuilder *irb, AstNode *source_
616627static IrInstruction *ir_build_struct_field_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,
617628 IrInstruction *struct_ptr, TypeStructField *type_struct_field)
618629{
619 IrInstruction *new_instruction = ir_build_struct_field_ptr(irb, old_instruction->source_node,
620 struct_ptr, type_struct_field);
630 IrInstruction *new_instruction = ir_build_struct_field_ptr(irb, old_instruction->scope,
631 old_instruction->source_node, struct_ptr, type_struct_field);
621632 ir_link_new_instruction(new_instruction, old_instruction);
622633 return new_instruction;
623634}
624635
625static IrInstruction *ir_build_enum_field_ptr(IrBuilder *irb, AstNode *source_node,
636static IrInstruction *ir_build_enum_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
626637 IrInstruction *enum_ptr, TypeEnumField *field)
627638{
628 IrInstructionEnumFieldPtr *instruction = ir_build_instruction<IrInstructionEnumFieldPtr>(irb, source_node);
639 IrInstructionEnumFieldPtr *instruction = ir_build_instruction<IrInstructionEnumFieldPtr>(irb, scope, source_node);
629640 instruction->enum_ptr = enum_ptr;
630641 instruction->field = field;
631642
......@@ -637,16 +648,16 @@ static IrInstruction *ir_build_enum_field_ptr(IrBuilder *irb, AstNode *source_no
637648static IrInstruction *ir_build_enum_field_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,
638649 IrInstruction *enum_ptr, TypeEnumField *type_enum_field)
639650{
640 IrInstruction *new_instruction = ir_build_enum_field_ptr(irb, old_instruction->source_node,
641 enum_ptr, type_enum_field);
651 IrInstruction *new_instruction = ir_build_enum_field_ptr(irb, old_instruction->scope,
652 old_instruction->source_node, enum_ptr, type_enum_field);
642653 ir_link_new_instruction(new_instruction, old_instruction);
643654 return new_instruction;
644655}
645656
646static IrInstruction *ir_build_call(IrBuilder *irb, AstNode *source_node,
657static IrInstruction *ir_build_call(IrBuilder *irb, Scope *scope, AstNode *source_node,
647658 FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args)
648659{
649 IrInstructionCall *call_instruction = ir_build_instruction<IrInstructionCall>(irb, source_node);
660 IrInstructionCall *call_instruction = ir_build_instruction<IrInstructionCall>(irb, scope, source_node);
650661 call_instruction->fn_entry = fn_entry;
651662 call_instruction->fn_ref = fn_ref;
652663 call_instruction->arg_count = arg_count;
......@@ -663,18 +674,19 @@ static IrInstruction *ir_build_call(IrBuilder *irb, AstNode *source_node,
663674static IrInstruction *ir_build_call_from(IrBuilder *irb, IrInstruction *old_instruction,
664675 FnTableEntry *fn_entry, IrInstruction *fn_ref, size_t arg_count, IrInstruction **args)
665676{
666 IrInstruction *new_instruction = ir_build_call(irb, old_instruction->source_node, fn_entry, fn_ref, arg_count, args);
677 IrInstruction *new_instruction = ir_build_call(irb, old_instruction->scope,
678 old_instruction->source_node, fn_entry, fn_ref, arg_count, args);
667679 ir_link_new_instruction(new_instruction, old_instruction);
668680 return new_instruction;
669681}
670682
671static IrInstruction *ir_build_phi(IrBuilder *irb, AstNode *source_node,
683static IrInstruction *ir_build_phi(IrBuilder *irb, Scope *scope, AstNode *source_node,
672684 size_t incoming_count, IrBasicBlock **incoming_blocks, IrInstruction **incoming_values)
673685{
674686 assert(incoming_count != 0);
675687 assert(incoming_count != SIZE_MAX);
676688
677 IrInstructionPhi *phi_instruction = ir_build_instruction<IrInstructionPhi>(irb, source_node);
689 IrInstructionPhi *phi_instruction = ir_build_instruction<IrInstructionPhi>(irb, scope, source_node);
678690 phi_instruction->incoming_count = incoming_count;
679691 phi_instruction->incoming_blocks = incoming_blocks;
680692 phi_instruction->incoming_values = incoming_values;
......@@ -690,14 +702,16 @@ static IrInstruction *ir_build_phi(IrBuilder *irb, AstNode *source_node,
690702static IrInstruction *ir_build_phi_from(IrBuilder *irb, IrInstruction *old_instruction,
691703 size_t incoming_count, IrBasicBlock **incoming_blocks, IrInstruction **incoming_values)
692704{
693 IrInstruction *new_instruction = ir_build_phi(irb, old_instruction->source_node,
705 IrInstruction *new_instruction = ir_build_phi(irb, old_instruction->scope, old_instruction->source_node,
694706 incoming_count, incoming_blocks, incoming_values);
695707 ir_link_new_instruction(new_instruction, old_instruction);
696708 return new_instruction;
697709}
698710
699static IrInstruction *ir_create_br(IrBuilder *irb, AstNode *source_node, IrBasicBlock *dest_block, bool is_inline) {
700 IrInstructionBr *br_instruction = ir_create_instruction<IrInstructionBr>(irb->exec, source_node);
711static IrInstruction *ir_create_br(IrBuilder *irb, Scope *scope, AstNode *source_node,
712 IrBasicBlock *dest_block, bool is_inline)
713{
714 IrInstructionBr *br_instruction = ir_create_instruction<IrInstructionBr>(irb->exec, scope, source_node);
701715 br_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;
702716 br_instruction->base.static_value.special = ConstValSpecialStatic;
703717 br_instruction->dest_block = dest_block;
......@@ -708,20 +722,23 @@ static IrInstruction *ir_create_br(IrBuilder *irb, AstNode *source_node, IrBasic
708722 return &br_instruction->base;
709723}
710724
711static IrInstruction *ir_build_br(IrBuilder *irb, AstNode *source_node, IrBasicBlock *dest_block, bool is_inline) {
712 IrInstruction *instruction = ir_create_br(irb, source_node, dest_block, is_inline);
725static IrInstruction *ir_build_br(IrBuilder *irb, Scope *scope, AstNode *source_node,
726 IrBasicBlock *dest_block, bool is_inline)
727{
728 IrInstruction *instruction = ir_create_br(irb, scope, source_node, dest_block, is_inline);
713729 ir_instruction_append(irb->current_basic_block, instruction);
714730 return instruction;
715731}
716732
717733static IrInstruction *ir_build_br_from(IrBuilder *irb, IrInstruction *old_instruction, IrBasicBlock *dest_block) {
718 IrInstruction *new_instruction = ir_build_br(irb, old_instruction->source_node, dest_block, false);
734 IrInstruction *new_instruction = ir_build_br(irb, old_instruction->scope,
735 old_instruction->source_node, dest_block, false);
719736 ir_link_new_instruction(new_instruction, old_instruction);
720737 return new_instruction;
721738}
722739
723static IrInstruction *ir_build_un_op(IrBuilder *irb, AstNode *source_node, IrUnOp op_id, IrInstruction *value) {
724 IrInstructionUnOp *br_instruction = ir_build_instruction<IrInstructionUnOp>(irb, source_node);
740static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *source_node, IrUnOp op_id, IrInstruction *value) {
741 IrInstructionUnOp *br_instruction = ir_build_instruction<IrInstructionUnOp>(irb, scope, source_node);
725742 br_instruction->op_id = op_id;
726743 br_instruction->value = value;
727744
......@@ -733,16 +750,17 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, AstNode *source_node, IrUnO
733750static IrInstruction *ir_build_un_op_from(IrBuilder *irb, IrInstruction *old_instruction,
734751 IrUnOp op_id, IrInstruction *value)
735752{
736 IrInstruction *new_instruction = ir_build_un_op(irb, old_instruction->source_node, op_id, value);
753 IrInstruction *new_instruction = ir_build_un_op(irb, old_instruction->scope,
754 old_instruction->source_node, op_id, value);
737755 ir_link_new_instruction(new_instruction, old_instruction);
738756 return new_instruction;
739757}
740758
741static IrInstruction *ir_build_container_init_list(IrBuilder *irb, AstNode *source_node,
759static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node,
742760 IrInstruction *container_type, size_t item_count, IrInstruction **items)
743761{
744762 IrInstructionContainerInitList *container_init_list_instruction =
745 ir_build_instruction<IrInstructionContainerInitList>(irb, source_node);
763 ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node);
746764 container_init_list_instruction->container_type = container_type;
747765 container_init_list_instruction->item_count = item_count;
748766 container_init_list_instruction->items = items;
......@@ -758,17 +776,17 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, AstNode *sour
758776static IrInstruction *ir_build_container_init_list_from(IrBuilder *irb, IrInstruction *old_instruction,
759777 IrInstruction *container_type, size_t item_count, IrInstruction **items)
760778{
761 IrInstruction *new_instruction = ir_build_container_init_list(irb, old_instruction->source_node,
762 container_type, item_count, items);
779 IrInstruction *new_instruction = ir_build_container_init_list(irb, old_instruction->scope,
780 old_instruction->source_node, container_type, item_count, items);
763781 ir_link_new_instruction(new_instruction, old_instruction);
764782 return new_instruction;
765783}
766784
767static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, AstNode *source_node,
785static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scope, AstNode *source_node,
768786 IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields)
769787{
770788 IrInstructionContainerInitFields *container_init_fields_instruction =
771 ir_build_instruction<IrInstructionContainerInitFields>(irb, source_node);
789 ir_build_instruction<IrInstructionContainerInitFields>(irb, scope, source_node);
772790 container_init_fields_instruction->container_type = container_type;
773791 container_init_fields_instruction->field_count = field_count;
774792 container_init_fields_instruction->fields = fields;
......@@ -781,10 +799,10 @@ static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, AstNode *so
781799 return &container_init_fields_instruction->base;
782800}
783801
784static IrInstruction *ir_build_struct_init(IrBuilder *irb, AstNode *source_node,
802static IrInstruction *ir_build_struct_init(IrBuilder *irb, Scope *scope, AstNode *source_node,
785803 TypeTableEntry *struct_type, size_t field_count, IrInstructionStructInitField *fields)
786804{
787 IrInstructionStructInit *struct_init_instruction = ir_build_instruction<IrInstructionStructInit>(irb, source_node);
805 IrInstructionStructInit *struct_init_instruction = ir_build_instruction<IrInstructionStructInit>(irb, scope, source_node);
788806 struct_init_instruction->struct_type = struct_type;
789807 struct_init_instruction->field_count = field_count;
790808 struct_init_instruction->fields = fields;
......@@ -798,30 +816,30 @@ static IrInstruction *ir_build_struct_init(IrBuilder *irb, AstNode *source_node,
798816static IrInstruction *ir_build_struct_init_from(IrBuilder *irb, IrInstruction *old_instruction,
799817 TypeTableEntry *struct_type, size_t field_count, IrInstructionStructInitField *fields)
800818{
801 IrInstruction *new_instruction = ir_build_struct_init(irb, old_instruction->source_node,
802 struct_type, field_count, fields);
819 IrInstruction *new_instruction = ir_build_struct_init(irb, old_instruction->scope,
820 old_instruction->source_node, struct_type, field_count, fields);
803821 ir_link_new_instruction(new_instruction, old_instruction);
804822 return new_instruction;
805823}
806824
807static IrInstruction *ir_build_unreachable(IrBuilder *irb, AstNode *source_node) {
825static IrInstruction *ir_build_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node) {
808826 IrInstructionUnreachable *unreachable_instruction =
809 ir_build_instruction<IrInstructionUnreachable>(irb, source_node);
827 ir_build_instruction<IrInstructionUnreachable>(irb, scope, source_node);
810828 unreachable_instruction->base.static_value.special = ConstValSpecialStatic;
811829 unreachable_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;
812830 return &unreachable_instruction->base;
813831}
814832
815833static IrInstruction *ir_build_unreachable_from(IrBuilder *irb, IrInstruction *old_instruction) {
816 IrInstruction *new_instruction = ir_build_unreachable(irb, old_instruction->source_node);
834 IrInstruction *new_instruction = ir_build_unreachable(irb, old_instruction->scope, old_instruction->source_node);
817835 ir_link_new_instruction(new_instruction, old_instruction);
818836 return new_instruction;
819837}
820838
821static IrInstruction *ir_build_store_ptr(IrBuilder *irb, AstNode *source_node,
839static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
822840 IrInstruction *ptr, IrInstruction *value)
823841{
824 IrInstructionStorePtr *instruction = ir_build_instruction<IrInstructionStorePtr>(irb, source_node);
842 IrInstructionStorePtr *instruction = ir_build_instruction<IrInstructionStorePtr>(irb, scope, source_node);
825843 instruction->base.static_value.special = ConstValSpecialStatic;
826844 instruction->base.type_entry = irb->codegen->builtin_types.entry_void;
827845 instruction->ptr = ptr;
......@@ -836,15 +854,16 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, AstNode *source_node,
836854static IrInstruction *ir_build_store_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,
837855 IrInstruction *ptr, IrInstruction *value)
838856{
839 IrInstruction *new_instruction = ir_build_store_ptr(irb, old_instruction->source_node, ptr, value);
857 IrInstruction *new_instruction = ir_build_store_ptr(irb, old_instruction->scope,
858 old_instruction->source_node, ptr, value);
840859 ir_link_new_instruction(new_instruction, old_instruction);
841860 return new_instruction;
842861}
843862
844static IrInstruction *ir_build_var_decl(IrBuilder *irb, AstNode *source_node,
863static IrInstruction *ir_build_var_decl(IrBuilder *irb, Scope *scope, AstNode *source_node,
845864 VariableTableEntry *var, IrInstruction *var_type, IrInstruction *init_value)
846865{
847 IrInstructionDeclVar *decl_var_instruction = ir_build_instruction<IrInstructionDeclVar>(irb, source_node);
866 IrInstructionDeclVar *decl_var_instruction = ir_build_instruction<IrInstructionDeclVar>(irb, scope, source_node);
848867 decl_var_instruction->base.static_value.special = ConstValSpecialStatic;
849868 decl_var_instruction->base.type_entry = irb->codegen->builtin_types.entry_void;
850869 decl_var_instruction->var = var;
......@@ -860,13 +879,14 @@ static IrInstruction *ir_build_var_decl(IrBuilder *irb, AstNode *source_node,
860879static IrInstruction *ir_build_var_decl_from(IrBuilder *irb, IrInstruction *old_instruction,
861880 VariableTableEntry *var, IrInstruction *var_type, IrInstruction *init_value)
862881{
863 IrInstruction *new_instruction = ir_build_var_decl(irb, old_instruction->source_node, var, var_type, init_value);
882 IrInstruction *new_instruction = ir_build_var_decl(irb, old_instruction->scope,
883 old_instruction->source_node, var, var_type, init_value);
864884 ir_link_new_instruction(new_instruction, old_instruction);
865885 return new_instruction;
866886}
867887
868static IrInstruction *ir_build_load_ptr(IrBuilder *irb, AstNode *source_node, IrInstruction *ptr) {
869 IrInstructionLoadPtr *instruction = ir_build_instruction<IrInstructionLoadPtr>(irb, source_node);
888static IrInstruction *ir_build_load_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *ptr) {
889 IrInstructionLoadPtr *instruction = ir_build_instruction<IrInstructionLoadPtr>(irb, scope, source_node);
870890 instruction->ptr = ptr;
871891
872892 ir_ref_instruction(ptr);
......@@ -875,13 +895,14 @@ static IrInstruction *ir_build_load_ptr(IrBuilder *irb, AstNode *source_node, Ir
875895}
876896
877897static IrInstruction *ir_build_load_ptr_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *ptr) {
878 IrInstruction *new_instruction = ir_build_load_ptr(irb, old_instruction->source_node, ptr);
898 IrInstruction *new_instruction = ir_build_load_ptr(irb, old_instruction->scope,
899 old_instruction->source_node, ptr);
879900 ir_link_new_instruction(new_instruction, old_instruction);
880901 return new_instruction;
881902}
882903
883static IrInstruction *ir_build_typeof(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
884 IrInstructionTypeOf *instruction = ir_build_instruction<IrInstructionTypeOf>(irb, source_node);
904static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
905 IrInstructionTypeOf *instruction = ir_build_instruction<IrInstructionTypeOf>(irb, scope, source_node);
885906 instruction->value = value;
886907
887908 ir_ref_instruction(value);
......@@ -889,8 +910,8 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, AstNode *source_node, IrIn
889910 return &instruction->base;
890911}
891912
892static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
893 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, source_node);
913static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
914 IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(irb, scope, source_node);
894915 instruction->value = value;
895916
896917 ir_ref_instruction(value);
......@@ -898,8 +919,8 @@ static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, AstNode *source_node,
898919 return &instruction->base;
899920}
900921
901static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
902 IrInstructionPtrTypeChild *instruction = ir_build_instruction<IrInstructionPtrTypeChild>(irb, source_node);
922static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
923 IrInstructionPtrTypeChild *instruction = ir_build_instruction<IrInstructionPtrTypeChild>(irb, scope, source_node);
903924 instruction->value = value;
904925
905926 ir_ref_instruction(value);
......@@ -907,10 +928,10 @@ static IrInstruction *ir_build_ptr_type_child(IrBuilder *irb, AstNode *source_no
907928 return &instruction->base;
908929}
909930
910static IrInstruction *ir_build_set_fn_test(IrBuilder *irb, AstNode *source_node, IrInstruction *fn_value,
931static IrInstruction *ir_build_set_fn_test(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *fn_value,
911932 IrInstruction *is_test)
912933{
913 IrInstructionSetFnTest *instruction = ir_build_instruction<IrInstructionSetFnTest>(irb, source_node);
934 IrInstructionSetFnTest *instruction = ir_build_instruction<IrInstructionSetFnTest>(irb, scope, source_node);
914935 instruction->fn_value = fn_value;
915936 instruction->is_test = is_test;
916937
......@@ -920,10 +941,10 @@ static IrInstruction *ir_build_set_fn_test(IrBuilder *irb, AstNode *source_node,
920941 return &instruction->base;
921942}
922943
923static IrInstruction *ir_build_set_fn_visible(IrBuilder *irb, AstNode *source_node, IrInstruction *fn_value,
944static IrInstruction *ir_build_set_fn_visible(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *fn_value,
924945 IrInstruction *is_visible)
925946{
926 IrInstructionSetFnVisible *instruction = ir_build_instruction<IrInstructionSetFnVisible>(irb, source_node);
947 IrInstructionSetFnVisible *instruction = ir_build_instruction<IrInstructionSetFnVisible>(irb, scope, source_node);
927948 instruction->fn_value = fn_value;
928949 instruction->is_visible = is_visible;
929950
......@@ -933,10 +954,10 @@ static IrInstruction *ir_build_set_fn_visible(IrBuilder *irb, AstNode *source_no
933954 return &instruction->base;
934955}
935956
936static IrInstruction *ir_build_set_debug_safety(IrBuilder *irb, AstNode *source_node,
957static IrInstruction *ir_build_set_debug_safety(IrBuilder *irb, Scope *scope, AstNode *source_node,
937958 IrInstruction *scope_value, IrInstruction *debug_safety_on)
938959{
939 IrInstructionSetDebugSafety *instruction = ir_build_instruction<IrInstructionSetDebugSafety>(irb, source_node);
960 IrInstructionSetDebugSafety *instruction = ir_build_instruction<IrInstructionSetDebugSafety>(irb, scope, source_node);
940961 instruction->scope_value = scope_value;
941962 instruction->debug_safety_on = debug_safety_on;
942963
......@@ -946,10 +967,10 @@ static IrInstruction *ir_build_set_debug_safety(IrBuilder *irb, AstNode *source_
946967 return &instruction->base;
947968}
948969
949static IrInstruction *ir_build_array_type(IrBuilder *irb, AstNode *source_node, IrInstruction *size,
970static IrInstruction *ir_build_array_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *size,
950971 IrInstruction *child_type)
951972{
952 IrInstructionArrayType *instruction = ir_build_instruction<IrInstructionArrayType>(irb, source_node);
973 IrInstructionArrayType *instruction = ir_build_instruction<IrInstructionArrayType>(irb, scope, source_node);
953974 instruction->size = size;
954975 instruction->child_type = child_type;
955976
......@@ -959,10 +980,10 @@ static IrInstruction *ir_build_array_type(IrBuilder *irb, AstNode *source_node,
959980 return &instruction->base;
960981}
961982
962static IrInstruction *ir_build_slice_type(IrBuilder *irb, AstNode *source_node, bool is_const,
983static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode *source_node, bool is_const,
963984 IrInstruction *child_type)
964985{
965 IrInstructionSliceType *instruction = ir_build_instruction<IrInstructionSliceType>(irb, source_node);
986 IrInstructionSliceType *instruction = ir_build_instruction<IrInstructionSliceType>(irb, scope, source_node);
966987 instruction->is_const = is_const;
967988 instruction->child_type = child_type;
968989
......@@ -971,12 +992,13 @@ static IrInstruction *ir_build_slice_type(IrBuilder *irb, AstNode *source_node,
971992 return &instruction->base;
972993}
973994
974static IrInstruction *ir_build_asm(IrBuilder *irb, AstNode *source_node, IrInstruction **input_list,
975 IrInstruction **output_types, size_t return_count, bool has_side_effects)
995static IrInstruction *ir_build_asm(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction **input_list,
996 IrInstruction **output_types, VariableTableEntry **output_vars, size_t return_count, bool has_side_effects)
976997{
977 IrInstructionAsm *instruction = ir_build_instruction<IrInstructionAsm>(irb, source_node);
998 IrInstructionAsm *instruction = ir_build_instruction<IrInstructionAsm>(irb, scope, source_node);
978999 instruction->input_list = input_list;
9791000 instruction->output_types = output_types;
1001 instruction->output_vars = output_vars;
9801002 instruction->return_count = return_count;
9811003 instruction->has_side_effects = has_side_effects;
9821004
......@@ -995,16 +1017,16 @@ static IrInstruction *ir_build_asm(IrBuilder *irb, AstNode *source_node, IrInstr
9951017}
9961018
9971019static IrInstruction *ir_build_asm_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction **input_list,
998 IrInstruction **output_types, size_t return_count, bool has_side_effects)
1020 IrInstruction **output_types, VariableTableEntry **output_vars, size_t return_count, bool has_side_effects)
9991021{
1000 IrInstruction *new_instruction = ir_build_asm(irb, old_instruction->source_node, input_list, output_types,
1001 return_count, has_side_effects);
1022 IrInstruction *new_instruction = ir_build_asm(irb, old_instruction->scope,
1023 old_instruction->source_node, input_list, output_types, output_vars, return_count, has_side_effects);
10021024 ir_link_new_instruction(new_instruction, old_instruction);
10031025 return new_instruction;
10041026}
10051027
1006static IrInstruction *ir_build_compile_var(IrBuilder *irb, AstNode *source_node, IrInstruction *name) {
1007 IrInstructionCompileVar *instruction = ir_build_instruction<IrInstructionCompileVar>(irb, source_node);
1028static IrInstruction *ir_build_compile_var(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *name) {
1029 IrInstructionCompileVar *instruction = ir_build_instruction<IrInstructionCompileVar>(irb, scope, source_node);
10081030 instruction->name = name;
10091031
10101032 ir_ref_instruction(name);
......@@ -1012,8 +1034,8 @@ static IrInstruction *ir_build_compile_var(IrBuilder *irb, AstNode *source_node,
10121034 return &instruction->base;
10131035}
10141036
1015static IrInstruction *ir_build_size_of(IrBuilder *irb, AstNode *source_node, IrInstruction *type_value) {
1016 IrInstructionSizeOf *instruction = ir_build_instruction<IrInstructionSizeOf>(irb, source_node);
1037static IrInstruction *ir_build_size_of(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type_value) {
1038 IrInstructionSizeOf *instruction = ir_build_instruction<IrInstructionSizeOf>(irb, scope, source_node);
10171039 instruction->type_value = type_value;
10181040
10191041 ir_ref_instruction(type_value);
......@@ -1021,8 +1043,8 @@ static IrInstruction *ir_build_size_of(IrBuilder *irb, AstNode *source_node, IrI
10211043 return &instruction->base;
10221044}
10231045
1024static IrInstruction *ir_build_test_null(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
1025 IrInstructionTestNull *instruction = ir_build_instruction<IrInstructionTestNull>(irb, source_node);
1046static IrInstruction *ir_build_test_null(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1047 IrInstructionTestNull *instruction = ir_build_instruction<IrInstructionTestNull>(irb, scope, source_node);
10261048 instruction->value = value;
10271049
10281050 ir_ref_instruction(value);
......@@ -1033,15 +1055,16 @@ static IrInstruction *ir_build_test_null(IrBuilder *irb, AstNode *source_node, I
10331055static IrInstruction *ir_build_test_null_from(IrBuilder *irb, IrInstruction *old_instruction,
10341056 IrInstruction *value)
10351057{
1036 IrInstruction *new_instruction = ir_build_test_null(irb, old_instruction->source_node, value);
1058 IrInstruction *new_instruction = ir_build_test_null(irb, old_instruction->scope,
1059 old_instruction->source_node, value);
10371060 ir_link_new_instruction(new_instruction, old_instruction);
10381061 return new_instruction;
10391062}
10401063
1041static IrInstruction *ir_build_unwrap_maybe(IrBuilder *irb, AstNode *source_node, IrInstruction *value,
1064static IrInstruction *ir_build_unwrap_maybe(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value,
10421065 bool safety_check_on)
10431066{
1044 IrInstructionUnwrapMaybe *instruction = ir_build_instruction<IrInstructionUnwrapMaybe>(irb, source_node);
1067 IrInstructionUnwrapMaybe *instruction = ir_build_instruction<IrInstructionUnwrapMaybe>(irb, scope, source_node);
10451068 instruction->value = value;
10461069 instruction->safety_check_on = safety_check_on;
10471070
......@@ -1053,14 +1076,14 @@ static IrInstruction *ir_build_unwrap_maybe(IrBuilder *irb, AstNode *source_node
10531076static IrInstruction *ir_build_unwrap_maybe_from(IrBuilder *irb, IrInstruction *old_instruction,
10541077 IrInstruction *value, bool safety_check_on)
10551078{
1056 IrInstruction *new_instruction = ir_build_unwrap_maybe(irb, old_instruction->source_node,
1079 IrInstruction *new_instruction = ir_build_unwrap_maybe(irb, old_instruction->scope, old_instruction->source_node,
10571080 value, safety_check_on);
10581081 ir_link_new_instruction(new_instruction, old_instruction);
10591082 return new_instruction;
10601083}
10611084
1062static IrInstruction *ir_build_clz(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
1063 IrInstructionClz *instruction = ir_build_instruction<IrInstructionClz>(irb, source_node);
1085static IrInstruction *ir_build_clz(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1086 IrInstructionClz *instruction = ir_build_instruction<IrInstructionClz>(irb, scope, source_node);
10641087 instruction->value = value;
10651088
10661089 ir_ref_instruction(value);
......@@ -1069,13 +1092,13 @@ static IrInstruction *ir_build_clz(IrBuilder *irb, AstNode *source_node, IrInstr
10691092}
10701093
10711094static IrInstruction *ir_build_clz_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *value) {
1072 IrInstruction *new_instruction = ir_build_clz(irb, old_instruction->source_node, value);
1095 IrInstruction *new_instruction = ir_build_clz(irb, old_instruction->scope, old_instruction->source_node, value);
10731096 ir_link_new_instruction(new_instruction, old_instruction);
10741097 return new_instruction;
10751098}
10761099
1077static IrInstruction *ir_build_ctz(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
1078 IrInstructionCtz *instruction = ir_build_instruction<IrInstructionCtz>(irb, source_node);
1100static IrInstruction *ir_build_ctz(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1101 IrInstructionCtz *instruction = ir_build_instruction<IrInstructionCtz>(irb, scope, source_node);
10791102 instruction->value = value;
10801103
10811104 ir_ref_instruction(value);
......@@ -1084,15 +1107,15 @@ static IrInstruction *ir_build_ctz(IrBuilder *irb, AstNode *source_node, IrInstr
10841107}
10851108
10861109static IrInstruction *ir_build_ctz_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *value) {
1087 IrInstruction *new_instruction = ir_build_ctz(irb, old_instruction->source_node, value);
1110 IrInstruction *new_instruction = ir_build_ctz(irb, old_instruction->scope, old_instruction->source_node, value);
10881111 ir_link_new_instruction(new_instruction, old_instruction);
10891112 return new_instruction;
10901113}
10911114
1092static IrInstruction *ir_build_switch_br(IrBuilder *irb, AstNode *source_node, IrInstruction *target_value,
1115static IrInstruction *ir_build_switch_br(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *target_value,
10931116 IrBasicBlock *else_block, size_t case_count, IrInstructionSwitchBrCase *cases, bool is_inline)
10941117{
1095 IrInstructionSwitchBr *instruction = ir_build_instruction<IrInstructionSwitchBr>(irb, source_node);
1118 IrInstructionSwitchBr *instruction = ir_build_instruction<IrInstructionSwitchBr>(irb, scope, source_node);
10961119 instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;
10971120 instruction->base.static_value.special = ConstValSpecialStatic;
10981121 instruction->target_value = target_value;
......@@ -1116,16 +1139,16 @@ static IrInstruction *ir_build_switch_br_from(IrBuilder *irb, IrInstruction *old
11161139 IrInstruction *target_value, IrBasicBlock *else_block, size_t case_count,
11171140 IrInstructionSwitchBrCase *cases, bool is_inline)
11181141{
1119 IrInstruction *new_instruction = ir_build_switch_br(irb, old_instruction->source_node,
1142 IrInstruction *new_instruction = ir_build_switch_br(irb, old_instruction->scope, old_instruction->source_node,
11201143 target_value, else_block, case_count, cases, is_inline);
11211144 ir_link_new_instruction(new_instruction, old_instruction);
11221145 return new_instruction;
11231146}
11241147
1125static IrInstruction *ir_build_switch_target(IrBuilder *irb, AstNode *source_node,
1148static IrInstruction *ir_build_switch_target(IrBuilder *irb, Scope *scope, AstNode *source_node,
11261149 IrInstruction *target_value_ptr)
11271150{
1128 IrInstructionSwitchTarget *instruction = ir_build_instruction<IrInstructionSwitchTarget>(irb, source_node);
1151 IrInstructionSwitchTarget *instruction = ir_build_instruction<IrInstructionSwitchTarget>(irb, scope, source_node);
11291152 instruction->target_value_ptr = target_value_ptr;
11301153
11311154 ir_ref_instruction(target_value_ptr);
......@@ -1133,10 +1156,10 @@ static IrInstruction *ir_build_switch_target(IrBuilder *irb, AstNode *source_nod
11331156 return &instruction->base;
11341157}
11351158
1136static IrInstruction *ir_build_switch_var(IrBuilder *irb, AstNode *source_node,
1159static IrInstruction *ir_build_switch_var(IrBuilder *irb, Scope *scope, AstNode *source_node,
11371160 IrInstruction *target_value_ptr, IrInstruction *prong_value)
11381161{
1139 IrInstructionSwitchVar *instruction = ir_build_instruction<IrInstructionSwitchVar>(irb, source_node);
1162 IrInstructionSwitchVar *instruction = ir_build_instruction<IrInstructionSwitchVar>(irb, scope, source_node);
11401163 instruction->target_value_ptr = target_value_ptr;
11411164 instruction->prong_value = prong_value;
11421165
......@@ -1146,8 +1169,8 @@ static IrInstruction *ir_build_switch_var(IrBuilder *irb, AstNode *source_node,
11461169 return &instruction->base;
11471170}
11481171
1149static IrInstruction *ir_build_enum_tag(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
1150 IrInstructionEnumTag *instruction = ir_build_instruction<IrInstructionEnumTag>(irb, source_node);
1172static IrInstruction *ir_build_enum_tag(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1173 IrInstructionEnumTag *instruction = ir_build_instruction<IrInstructionEnumTag>(irb, scope, source_node);
11511174 instruction->value = value;
11521175
11531176 ir_ref_instruction(value);
......@@ -1156,13 +1179,14 @@ static IrInstruction *ir_build_enum_tag(IrBuilder *irb, AstNode *source_node, Ir
11561179}
11571180
11581181static IrInstruction *ir_build_enum_tag_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *value) {
1159 IrInstruction *new_instruction = ir_build_enum_tag(irb, old_instruction->source_node, value);
1182 IrInstruction *new_instruction = ir_build_enum_tag(irb, old_instruction->scope,
1183 old_instruction->source_node, value);
11601184 ir_link_new_instruction(new_instruction, old_instruction);
11611185 return new_instruction;
11621186}
11631187
1164static IrInstruction *ir_build_static_eval(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
1165 IrInstructionStaticEval *instruction = ir_build_instruction<IrInstructionStaticEval>(irb, source_node);
1188static IrInstruction *ir_build_static_eval(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1189 IrInstructionStaticEval *instruction = ir_build_instruction<IrInstructionStaticEval>(irb, scope, source_node);
11661190 instruction->value = value;
11671191
11681192 ir_ref_instruction(value);
......@@ -1170,8 +1194,8 @@ static IrInstruction *ir_build_static_eval(IrBuilder *irb, AstNode *source_node,
11701194 return &instruction->base;
11711195}
11721196
1173static IrInstruction *ir_build_import(IrBuilder *irb, AstNode *source_node, IrInstruction *name) {
1174 IrInstructionImport *instruction = ir_build_instruction<IrInstructionImport>(irb, source_node);
1197static IrInstruction *ir_build_import(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *name) {
1198 IrInstructionImport *instruction = ir_build_instruction<IrInstructionImport>(irb, scope, source_node);
11751199 instruction->name = name;
11761200
11771201 ir_ref_instruction(name);
......@@ -1179,8 +1203,8 @@ static IrInstruction *ir_build_import(IrBuilder *irb, AstNode *source_node, IrIn
11791203 return &instruction->base;
11801204}
11811205
1182static IrInstruction *ir_build_array_len(IrBuilder *irb, AstNode *source_node, IrInstruction *array_value) {
1183 IrInstructionArrayLen *instruction = ir_build_instruction<IrInstructionArrayLen>(irb, source_node);
1206static IrInstruction *ir_build_array_len(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *array_value) {
1207 IrInstructionArrayLen *instruction = ir_build_instruction<IrInstructionArrayLen>(irb, scope, source_node);
11841208 instruction->array_value = array_value;
11851209
11861210 ir_ref_instruction(array_value);
......@@ -1191,13 +1215,14 @@ static IrInstruction *ir_build_array_len(IrBuilder *irb, AstNode *source_node, I
11911215static IrInstruction *ir_build_array_len_from(IrBuilder *irb, IrInstruction *old_instruction,
11921216 IrInstruction *array_value)
11931217{
1194 IrInstruction *new_instruction = ir_build_array_len(irb, old_instruction->source_node, array_value);
1218 IrInstruction *new_instruction = ir_build_array_len(irb, old_instruction->scope,
1219 old_instruction->source_node, array_value);
11951220 ir_link_new_instruction(new_instruction, old_instruction);
11961221 return new_instruction;
11971222}
11981223
1199static IrInstruction *ir_build_ref(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
1200 IrInstructionRef *instruction = ir_build_instruction<IrInstructionRef>(irb, source_node);
1224static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1225 IrInstructionRef *instruction = ir_build_instruction<IrInstructionRef>(irb, scope, source_node);
12011226 instruction->value = value;
12021227
12031228 ir_ref_instruction(value);
......@@ -1206,12 +1231,12 @@ static IrInstruction *ir_build_ref(IrBuilder *irb, AstNode *source_node, IrInstr
12061231}
12071232
12081233static IrInstruction *ir_build_ref_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *value) {
1209 IrInstruction *new_instruction = ir_build_ref(irb, old_instruction->source_node, value);
1234 IrInstruction *new_instruction = ir_build_ref(irb, old_instruction->scope, old_instruction->source_node, value);
12101235 ir_link_new_instruction(new_instruction, old_instruction);
12111236 return new_instruction;
12121237}
12131238
1214static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
1239static void ir_gen_defers_for_block(IrBuilder *irb, Scope *parent_scope, Scope *inner_scope, Scope *outer_scope,
12151240 bool gen_error_defers, bool gen_maybe_defers)
12161241{
12171242 while (inner_scope != outer_scope) {
......@@ -1221,29 +1246,16 @@ static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *o
12211246 (gen_maybe_defers && inner_scope->node->data.defer.kind == ReturnKindMaybe)))
12221247 {
12231248 AstNode *defer_expr_node = inner_scope->node->data.defer.expr;
1224 ir_gen_node(irb, defer_expr_node, defer_expr_node->scope);
1249 ir_gen_node(irb, defer_expr_node, parent_scope);
12251250 }
12261251 inner_scope = inner_scope->parent;
12271252 }
12281253}
12291254
1230static FnTableEntry *scope_fn_entry(Scope *scope) {
1231 while (scope) {
1232 if (scope->node->type == NodeTypeFnDef) {
1233 ScopeFnBody *fn_scope = (ScopeFnBody *)scope;
1234 return fn_scope->fn_entry;
1235 }
1236 scope = scope->parent;
1237 }
1238 return nullptr;
1239}
1240
1241static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *node) {
1255static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node) {
12421256 assert(node->type == NodeTypeReturnExpr);
12431257
1244 Scope *scope = node->scope;
1245
1246 if (!scope_fn_entry(scope)) {
1258 if (!exec_fn_entry(irb->exec)) {
12471259 add_node_error(irb->codegen, node, buf_sprintf("return expression outside function definition"));
12481260 return irb->codegen->invalid_instruction;
12491261 }
......@@ -1254,12 +1266,12 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *node) {
12541266 {
12551267 IrInstruction *return_value;
12561268 if (expr_node) {
1257 return_value = ir_gen_node(irb, expr_node, node->scope);
1269 return_value = ir_gen_node(irb, expr_node, scope);
12581270 } else {
1259 return_value = ir_build_const_void(irb, node);
1271 return_value = ir_build_const_void(irb, scope, node);
12601272 }
12611273
1262 return ir_build_return(irb, node, return_value);
1274 return ir_build_return(irb, scope, node, return_value);
12631275 }
12641276 case ReturnKindError:
12651277 zig_panic("TODO gen IR for %%return");
......@@ -1275,15 +1287,15 @@ static void ir_set_cursor_at_end(IrBuilder *irb, IrBasicBlock *basic_block) {
12751287 irb->current_basic_block = basic_block;
12761288}
12771289
1278static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, Scope *parent_scope,
1290static VariableTableEntry *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_scope,
12791291 Buf *name, bool src_is_const, bool gen_is_const, bool is_shadowable, bool is_inline)
12801292{
12811293 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);
12821294 variable_entry->parent_scope = parent_scope;
1283 variable_entry->import = node->owner;
12841295 variable_entry->shadowable = is_shadowable;
12851296 variable_entry->mem_slot_index = SIZE_MAX;
12861297 variable_entry->is_inline = is_inline;
1298 variable_entry->src_arg_index = SIZE_MAX;
12871299
12881300 if (name) {
12891301 buf_init_from_buf(&variable_entry->name, name);
......@@ -1302,11 +1314,11 @@ static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, Scope
13021314 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
13031315 variable_entry->type = codegen->builtin_types.entry_invalid;
13041316 } else {
1305 AstNode *decl_node = find_decl(parent_scope, name);
1306 if (decl_node && decl_node->type != NodeTypeVariableDeclaration) {
1317 Tld *tld = find_decl(parent_scope, name);
1318 if (tld && tld->id != TldIdVar) {
13071319 ErrorMsg *msg = add_node_error(codegen, node,
13081320 buf_sprintf("redefinition of '%s'", buf_ptr(name)));
1309 add_error_note(codegen, msg, decl_node, buf_sprintf("previous definition is here"));
1321 add_error_note(codegen, msg, tld->source_node, buf_sprintf("previous definition is here"));
13101322 variable_entry->type = codegen->builtin_types.entry_invalid;
13111323 }
13121324 }
......@@ -1333,7 +1345,7 @@ static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, Scope
13331345static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *scope, Buf *name,
13341346 bool src_is_const, bool gen_is_const, bool is_shadowable, bool is_inline)
13351347{
1336 VariableTableEntry *var = add_local_var(irb->codegen, node, scope, name,
1348 VariableTableEntry *var = create_local_var(irb->codegen, node, scope, name,
13371349 src_is_const, gen_is_const, is_shadowable, is_inline);
13381350 if (is_inline || gen_is_const)
13391351 var->mem_slot_index = exec_next_mem_slot(irb->exec);
......@@ -1341,10 +1353,9 @@ static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *s
13411353 return var;
13421354}
13431355
1344static IrInstruction *ir_gen_block(IrBuilder *irb, AstNode *block_node) {
1356static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode *block_node) {
13451357 assert(block_node->type == NodeTypeBlock);
13461358
1347 Scope *parent_scope = block_node->scope;
13481359 Scope *outer_block_scope = create_block_scope(block_node, parent_scope);
13491360 Scope *child_scope = outer_block_scope;
13501361
......@@ -1353,56 +1364,57 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, AstNode *block_node) {
13531364 AstNode *statement_node = block_node->data.block.statements.at(i);
13541365 return_value = ir_gen_node(irb, statement_node, child_scope);
13551366 if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {
1356 // defer starts a new block context
1357 child_scope = statement_node->data.defer.child_block;
1367 // defer starts a new scope
1368 child_scope = statement_node->data.defer.child_scope;
13581369 assert(child_scope);
13591370 } else if (return_value->id == IrInstructionIdDeclVar) {
1371 // variable declarations start a new scope
13601372 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)return_value;
13611373 child_scope = decl_var_instruction->var->child_scope;
13621374 }
13631375 }
13641376
13651377 if (!return_value)
1366 return_value = ir_build_const_void(irb, block_node);
1378 return_value = ir_build_const_void(irb, child_scope, block_node);
13671379
1368 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
1380 ir_gen_defers_for_block(irb, parent_scope, child_scope, outer_block_scope, false, false);
13691381
13701382 return return_value;
13711383}
13721384
1373static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, AstNode *node, IrBinOp op_id) {
1374 IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, node->scope);
1375 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, node->scope);
1376 return ir_build_bin_op(irb, node, op_id, op1, op2);
1385static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {
1386 IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope);
1387 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
1388 return ir_build_bin_op(irb, scope, node, op_id, op1, op2);
13771389}
13781390
1379static IrInstruction *ir_gen_assign(IrBuilder *irb, AstNode *node) {
1380 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, node->scope, LValPurposeAssign);
1391static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node) {
1392 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPurposeAssign);
13811393 if (lvalue == irb->codegen->invalid_instruction)
13821394 return lvalue;
13831395
1384 IrInstruction *rvalue = ir_gen_node(irb, node->data.bin_op_expr.op2, node->scope);
1396 IrInstruction *rvalue = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
13851397 if (rvalue == irb->codegen->invalid_instruction)
13861398 return rvalue;
13871399
1388 ir_build_store_ptr(irb, node, lvalue, rvalue);
1389 return ir_build_const_void(irb, node);
1400 ir_build_store_ptr(irb, scope, node, lvalue, rvalue);
1401 return ir_build_const_void(irb, scope, node);
13901402}
13911403
1392static IrInstruction *ir_gen_assign_op(IrBuilder *irb, AstNode *node, IrBinOp op_id) {
1393 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, node->scope, LValPurposeAssign);
1404static IrInstruction *ir_gen_assign_op(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {
1405 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPurposeAssign);
13941406 if (lvalue == irb->codegen->invalid_instruction)
13951407 return lvalue;
1396 IrInstruction *op1 = ir_build_load_ptr(irb, node->data.bin_op_expr.op1, lvalue);
1397 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, node->scope);
1408 IrInstruction *op1 = ir_build_load_ptr(irb, scope, node->data.bin_op_expr.op1, lvalue);
1409 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
13981410 if (op2 == irb->codegen->invalid_instruction)
13991411 return op2;
1400 IrInstruction *result = ir_build_bin_op(irb, node, op_id, op1, op2);
1401 ir_build_store_ptr(irb, node, lvalue, result);
1402 return ir_build_const_void(irb, node);
1412 IrInstruction *result = ir_build_bin_op(irb, scope, node, op_id, op1, op2);
1413 ir_build_store_ptr(irb, scope, node, lvalue, result);
1414 return ir_build_const_void(irb, scope, node);
14031415}
14041416
1405static IrInstruction *ir_gen_bin_op(IrBuilder *irb, AstNode *node) {
1417static IrInstruction *ir_gen_bin_op(IrBuilder *irb, Scope *scope, AstNode *node) {
14061418 assert(node->type == NodeTypeBinOpExpr);
14071419
14081420 BinOpType bin_op_type = node->data.bin_op_expr.bin_op;
......@@ -1410,95 +1422,95 @@ static IrInstruction *ir_gen_bin_op(IrBuilder *irb, AstNode *node) {
14101422 case BinOpTypeInvalid:
14111423 zig_unreachable();
14121424 case BinOpTypeAssign:
1413 return ir_gen_assign(irb, node);
1425 return ir_gen_assign(irb, scope, node);
14141426 case BinOpTypeAssignTimes:
1415 return ir_gen_assign_op(irb, node, IrBinOpMult);
1427 return ir_gen_assign_op(irb, scope, node, IrBinOpMult);
14161428 case BinOpTypeAssignTimesWrap:
1417 return ir_gen_assign_op(irb, node, IrBinOpMultWrap);
1429 return ir_gen_assign_op(irb, scope, node, IrBinOpMultWrap);
14181430 case BinOpTypeAssignDiv:
1419 return ir_gen_assign_op(irb, node, IrBinOpDiv);
1431 return ir_gen_assign_op(irb, scope, node, IrBinOpDiv);
14201432 case BinOpTypeAssignMod:
1421 return ir_gen_assign_op(irb, node, IrBinOpMod);
1433 return ir_gen_assign_op(irb, scope, node, IrBinOpMod);
14221434 case BinOpTypeAssignPlus:
1423 return ir_gen_assign_op(irb, node, IrBinOpAdd);
1435 return ir_gen_assign_op(irb, scope, node, IrBinOpAdd);
14241436 case BinOpTypeAssignPlusWrap:
1425 return ir_gen_assign_op(irb, node, IrBinOpAddWrap);
1437 return ir_gen_assign_op(irb, scope, node, IrBinOpAddWrap);
14261438 case BinOpTypeAssignMinus:
1427 return ir_gen_assign_op(irb, node, IrBinOpSub);
1439 return ir_gen_assign_op(irb, scope, node, IrBinOpSub);
14281440 case BinOpTypeAssignMinusWrap:
1429 return ir_gen_assign_op(irb, node, IrBinOpSubWrap);
1441 return ir_gen_assign_op(irb, scope, node, IrBinOpSubWrap);
14301442 case BinOpTypeAssignBitShiftLeft:
1431 return ir_gen_assign_op(irb, node, IrBinOpBitShiftLeft);
1443 return ir_gen_assign_op(irb, scope, node, IrBinOpBitShiftLeft);
14321444 case BinOpTypeAssignBitShiftLeftWrap:
1433 return ir_gen_assign_op(irb, node, IrBinOpBitShiftLeftWrap);
1445 return ir_gen_assign_op(irb, scope, node, IrBinOpBitShiftLeftWrap);
14341446 case BinOpTypeAssignBitShiftRight:
1435 return ir_gen_assign_op(irb, node, IrBinOpBitShiftRight);
1447 return ir_gen_assign_op(irb, scope, node, IrBinOpBitShiftRight);
14361448 case BinOpTypeAssignBitAnd:
1437 return ir_gen_assign_op(irb, node, IrBinOpBinAnd);
1449 return ir_gen_assign_op(irb, scope, node, IrBinOpBinAnd);
14381450 case BinOpTypeAssignBitXor:
1439 return ir_gen_assign_op(irb, node, IrBinOpBinXor);
1451 return ir_gen_assign_op(irb, scope, node, IrBinOpBinXor);
14401452 case BinOpTypeAssignBitOr:
1441 return ir_gen_assign_op(irb, node, IrBinOpBinOr);
1453 return ir_gen_assign_op(irb, scope, node, IrBinOpBinOr);
14421454 case BinOpTypeAssignBoolAnd:
1443 return ir_gen_assign_op(irb, node, IrBinOpBoolAnd);
1455 return ir_gen_assign_op(irb, scope, node, IrBinOpBoolAnd);
14441456 case BinOpTypeAssignBoolOr:
1445 return ir_gen_assign_op(irb, node, IrBinOpBoolOr);
1457 return ir_gen_assign_op(irb, scope, node, IrBinOpBoolOr);
14461458 case BinOpTypeBoolOr:
14471459 case BinOpTypeBoolAnd:
14481460 // note: this is not a direct mapping to IrBinOpBoolOr/And
14491461 // because of the control flow
14501462 zig_panic("TODO gen IR for bool or/and");
14511463 case BinOpTypeCmpEq:
1452 return ir_gen_bin_op_id(irb, node, IrBinOpCmpEq);
1464 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpEq);
14531465 case BinOpTypeCmpNotEq:
1454 return ir_gen_bin_op_id(irb, node, IrBinOpCmpNotEq);
1466 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpNotEq);
14551467 case BinOpTypeCmpLessThan:
1456 return ir_gen_bin_op_id(irb, node, IrBinOpCmpLessThan);
1468 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpLessThan);
14571469 case BinOpTypeCmpGreaterThan:
1458 return ir_gen_bin_op_id(irb, node, IrBinOpCmpGreaterThan);
1470 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpGreaterThan);
14591471 case BinOpTypeCmpLessOrEq:
1460 return ir_gen_bin_op_id(irb, node, IrBinOpCmpLessOrEq);
1472 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpLessOrEq);
14611473 case BinOpTypeCmpGreaterOrEq:
1462 return ir_gen_bin_op_id(irb, node, IrBinOpCmpGreaterOrEq);
1474 return ir_gen_bin_op_id(irb, scope, node, IrBinOpCmpGreaterOrEq);
14631475 case BinOpTypeBinOr:
1464 return ir_gen_bin_op_id(irb, node, IrBinOpBinOr);
1476 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBinOr);
14651477 case BinOpTypeBinXor:
1466 return ir_gen_bin_op_id(irb, node, IrBinOpBinXor);
1478 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBinXor);
14671479 case BinOpTypeBinAnd:
1468 return ir_gen_bin_op_id(irb, node, IrBinOpBinAnd);
1480 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBinAnd);
14691481 case BinOpTypeBitShiftLeft:
1470 return ir_gen_bin_op_id(irb, node, IrBinOpBitShiftLeft);
1482 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBitShiftLeft);
14711483 case BinOpTypeBitShiftLeftWrap:
1472 return ir_gen_bin_op_id(irb, node, IrBinOpBitShiftLeftWrap);
1484 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBitShiftLeftWrap);
14731485 case BinOpTypeBitShiftRight:
1474 return ir_gen_bin_op_id(irb, node, IrBinOpBitShiftRight);
1486 return ir_gen_bin_op_id(irb, scope, node, IrBinOpBitShiftRight);
14751487 case BinOpTypeAdd:
1476 return ir_gen_bin_op_id(irb, node, IrBinOpAdd);
1488 return ir_gen_bin_op_id(irb, scope, node, IrBinOpAdd);
14771489 case BinOpTypeAddWrap:
1478 return ir_gen_bin_op_id(irb, node, IrBinOpAddWrap);
1490 return ir_gen_bin_op_id(irb, scope, node, IrBinOpAddWrap);
14791491 case BinOpTypeSub:
1480 return ir_gen_bin_op_id(irb, node, IrBinOpSub);
1492 return ir_gen_bin_op_id(irb, scope, node, IrBinOpSub);
14811493 case BinOpTypeSubWrap:
1482 return ir_gen_bin_op_id(irb, node, IrBinOpSubWrap);
1494 return ir_gen_bin_op_id(irb, scope, node, IrBinOpSubWrap);
14831495 case BinOpTypeMult:
1484 return ir_gen_bin_op_id(irb, node, IrBinOpMult);
1496 return ir_gen_bin_op_id(irb, scope, node, IrBinOpMult);
14851497 case BinOpTypeMultWrap:
1486 return ir_gen_bin_op_id(irb, node, IrBinOpMultWrap);
1498 return ir_gen_bin_op_id(irb, scope, node, IrBinOpMultWrap);
14871499 case BinOpTypeDiv:
1488 return ir_gen_bin_op_id(irb, node, IrBinOpDiv);
1500 return ir_gen_bin_op_id(irb, scope, node, IrBinOpDiv);
14891501 case BinOpTypeMod:
1490 return ir_gen_bin_op_id(irb, node, IrBinOpMod);
1502 return ir_gen_bin_op_id(irb, scope, node, IrBinOpMod);
14911503 case BinOpTypeArrayCat:
1492 return ir_gen_bin_op_id(irb, node, IrBinOpArrayCat);
1504 return ir_gen_bin_op_id(irb, scope, node, IrBinOpArrayCat);
14931505 case BinOpTypeArrayMult:
1494 return ir_gen_bin_op_id(irb, node, IrBinOpArrayMult);
1506 return ir_gen_bin_op_id(irb, scope, node, IrBinOpArrayMult);
14951507 case BinOpTypeUnwrapMaybe:
14961508 zig_panic("TODO gen IR for unwrap maybe binary operation");
14971509 }
14981510 zig_unreachable();
14991511}
15001512
1501static IrInstruction *ir_gen_num_lit(IrBuilder *irb, AstNode *node) {
1513static IrInstruction *ir_gen_num_lit(IrBuilder *irb, Scope *scope, AstNode *node) {
15021514 assert(node->type == NodeTypeNumberLiteral);
15031515
15041516 if (node->data.number_literal.overflow) {
......@@ -1506,95 +1518,95 @@ static IrInstruction *ir_gen_num_lit(IrBuilder *irb, AstNode *node) {
15061518 return irb->codegen->invalid_instruction;
15071519 }
15081520
1509 return ir_build_const_bignum(irb, node, node->data.number_literal.bignum);
1521 return ir_build_const_bignum(irb, scope, node, node->data.number_literal.bignum);
15101522}
15111523
1512static IrInstruction *ir_gen_null_literal(IrBuilder *irb, AstNode *node) {
1524static IrInstruction *ir_gen_null_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
15131525 assert(node->type == NodeTypeNullLiteral);
15141526
1515 return ir_build_const_null(irb, node);
1527 return ir_build_const_null(irb, scope, node);
15161528}
15171529
1518static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, AstNode *decl_node,
1530static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, Tld *tld,
15191531 LValPurpose lval, Scope *scope)
15201532{
1521 resolve_top_level_decl(irb->codegen, decl_node, lval != LValPurposeNone);
1522 TopLevelDecl *tld = get_as_top_level_decl(decl_node);
1533 resolve_top_level_decl(irb->codegen, tld, lval != LValPurposeNone);
15231534 if (tld->resolution == TldResolutionInvalid)
15241535 return irb->codegen->invalid_instruction;
15251536
1526 if (decl_node->type == NodeTypeVariableDeclaration) {
1527 VariableTableEntry *var = decl_node->data.variable_declaration.variable;
1528 IrInstruction *var_ptr = ir_build_var_ptr(irb, source_node, var);
1529 if (lval != LValPurposeNone)
1530 return var_ptr;
1531 else
1532 return ir_build_load_ptr(irb, source_node, var_ptr);
1533 } else if (decl_node->type == NodeTypeFnProto) {
1534 FnTableEntry *fn_entry = decl_node->data.fn_proto.fn_table_entry;
1535 assert(fn_entry->type_entry);
1536 IrInstruction *ref_instruction;
1537 if (fn_entry->type_entry->id == TypeTableEntryIdGenericFn) {
1538 ref_instruction = ir_build_const_generic_fn(irb, source_node, fn_entry->type_entry);
1539 } else {
1540 ref_instruction = ir_build_const_fn(irb, source_node, fn_entry);
1537 switch (tld->id) {
1538 case TldIdVar:
1539 {
1540 TldVar *tld_var = (TldVar *)tld;
1541 VariableTableEntry *var = tld_var->var;
1542 IrInstruction *var_ptr = ir_build_var_ptr(irb, scope, source_node, var);
1543 if (lval != LValPurposeNone)
1544 return var_ptr;
1545 else
1546 return ir_build_load_ptr(irb, scope, source_node, var_ptr);
15411547 }
1542 if (lval != LValPurposeNone)
1543 return ir_build_ref(irb, source_node, ref_instruction);
1544 else
1545 return ref_instruction;
1546 } else if (decl_node->type == NodeTypeContainerDecl) {
1547 IrInstruction *ref_instruction;
1548 if (decl_node->data.struct_decl.generic_params.length > 0) {
1549 TypeTableEntry *type_entry = decl_node->data.struct_decl.generic_fn_type;
1550 assert(type_entry);
1551 ref_instruction = ir_build_const_generic_fn(irb, source_node, type_entry);
1552 } else {
1553 ref_instruction = ir_build_const_type(irb, source_node, decl_node->data.struct_decl.type_entry);
1548 case TldIdFn:
1549 {
1550 TldFn *tld_fn = (TldFn *)tld;
1551 FnTableEntry *fn_entry = tld_fn->fn_entry;
1552 assert(fn_entry->type_entry);
1553 IrInstruction *ref_instruction = ir_build_const_fn(irb, scope, source_node, fn_entry);
1554 if (lval != LValPurposeNone)
1555 return ir_build_ref(irb, scope, source_node, ref_instruction);
1556 else
1557 return ref_instruction;
1558 }
1559 case TldIdContainer:
1560 {
1561 TldContainer *tld_container = (TldContainer *)tld;
1562
1563 IrInstruction *ref_instruction = ir_build_const_type(irb, scope, source_node, tld_container->type_entry);
1564 if (lval != LValPurposeNone)
1565 return ir_build_ref(irb, scope, source_node, ref_instruction);
1566 else
1567 return ref_instruction;
1568 }
1569 case TldIdTypeDef:
1570 {
1571 TldTypeDef *tld_typedef = (TldTypeDef *)tld;
1572 TypeTableEntry *typedef_type = tld_typedef->type_entry;
1573 IrInstruction *ref_instruction = ir_build_const_type(irb, scope, source_node, typedef_type);
1574 if (lval != LValPurposeNone)
1575 return ir_build_ref(irb, scope, source_node, ref_instruction);
1576 else
1577 return ref_instruction;
15541578 }
1555 if (lval != LValPurposeNone)
1556 return ir_build_ref(irb, source_node, ref_instruction);
1557 else
1558 return ref_instruction;
1559 } else if (decl_node->type == NodeTypeTypeDecl) {
1560 TypeTableEntry *child_type = decl_node->data.type_decl.child_type_entry;
1561 IrInstruction *ref_instruction = ir_build_const_type(irb, source_node, child_type);
1562 if (lval != LValPurposeNone)
1563 return ir_build_ref(irb, source_node, ref_instruction);
1564 else
1565 return ref_instruction;
1566 } else {
1567 zig_unreachable();
15681579 }
1580 zig_unreachable();
15691581}
15701582
1571static IrInstruction *ir_gen_symbol(IrBuilder *irb, AstNode *node, LValPurpose lval) {
1583static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LValPurpose lval) {
15721584 assert(node->type == NodeTypeSymbol);
15731585
15741586 Buf *variable_name = node->data.symbol_expr.symbol;
15751587
15761588 auto primitive_table_entry = irb->codegen->primitive_type_table.maybe_get(variable_name);
15771589 if (primitive_table_entry) {
1578 IrInstruction *value = ir_build_const_type(irb, node, primitive_table_entry->value);
1590 IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_table_entry->value);
15791591 if (lval == LValPurposeAddressOf) {
1580 return ir_build_un_op(irb, node, IrUnOpAddressOf, value);
1592 return ir_build_un_op(irb, scope, node, IrUnOpAddressOf, value);
15811593 } else {
15821594 return value;
15831595 }
15841596 }
15851597
1586 VariableTableEntry *var = find_variable(irb->codegen, node->scope, variable_name);
1598 VariableTableEntry *var = find_variable(irb->codegen, scope, variable_name);
15871599 if (var) {
1588 IrInstruction *var_ptr = ir_build_var_ptr(irb, node, var);
1600 IrInstruction *var_ptr = ir_build_var_ptr(irb, scope, node, var);
15891601 if (lval != LValPurposeNone)
15901602 return var_ptr;
15911603 else
1592 return ir_build_load_ptr(irb, node, var_ptr);
1604 return ir_build_load_ptr(irb, scope, node, var_ptr);
15931605 }
15941606
1595 AstNode *decl_node = find_decl(node->scope, variable_name);
1596 if (decl_node)
1597 return ir_gen_decl_ref(irb, node, decl_node, lval, node->scope);
1607 Tld *tld = find_decl(scope, variable_name);
1608 if (tld)
1609 return ir_gen_decl_ref(irb, node, tld, lval, scope);
15981610
15991611 if (node->owner->any_imports_failed) {
16001612 // skip the error message since we had a failing import in this file
......@@ -1606,47 +1618,47 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, AstNode *node, LValPurpose l
16061618 return irb->codegen->invalid_instruction;
16071619}
16081620
1609static IrInstruction *ir_gen_array_access(IrBuilder *irb, AstNode *node, LValPurpose lval) {
1621static IrInstruction *ir_gen_array_access(IrBuilder *irb, Scope *scope, AstNode *node, LValPurpose lval) {
16101622 assert(node->type == NodeTypeArrayAccessExpr);
16111623
16121624 AstNode *array_ref_node = node->data.array_access_expr.array_ref_expr;
1613 IrInstruction *array_ref_instruction = ir_gen_node_extra(irb, array_ref_node, node->scope,
1625 IrInstruction *array_ref_instruction = ir_gen_node_extra(irb, array_ref_node, scope,
16141626 LValPurposeAddressOf);
16151627 if (array_ref_instruction == irb->codegen->invalid_instruction)
16161628 return array_ref_instruction;
16171629
16181630 AstNode *subscript_node = node->data.array_access_expr.subscript;
1619 IrInstruction *subscript_instruction = ir_gen_node(irb, subscript_node, node->scope);
1631 IrInstruction *subscript_instruction = ir_gen_node(irb, subscript_node, scope);
16201632 if (subscript_instruction == irb->codegen->invalid_instruction)
16211633 return subscript_instruction;
16221634
1623 IrInstruction *ptr_instruction = ir_build_elem_ptr(irb, node, array_ref_instruction,
1635 IrInstruction *ptr_instruction = ir_build_elem_ptr(irb, scope, node, array_ref_instruction,
16241636 subscript_instruction, true);
16251637 if (lval != LValPurposeNone)
16261638 return ptr_instruction;
16271639
1628 return ir_build_load_ptr(irb, node, ptr_instruction);
1640 return ir_build_load_ptr(irb, scope, node, ptr_instruction);
16291641}
16301642
1631static IrInstruction *ir_gen_field_access(IrBuilder *irb, AstNode *node, LValPurpose lval) {
1643static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode *node, LValPurpose lval) {
16321644 assert(node->type == NodeTypeFieldAccessExpr);
16331645
16341646 AstNode *container_ref_node = node->data.field_access_expr.struct_expr;
16351647 Buf *field_name = node->data.field_access_expr.field_name;
16361648
1637 IrInstruction *container_ref_instruction = ir_gen_node_extra(irb, container_ref_node, node->scope,
1649 IrInstruction *container_ref_instruction = ir_gen_node_extra(irb, container_ref_node, scope,
16381650 LValPurposeAddressOf);
16391651 if (container_ref_instruction == irb->codegen->invalid_instruction)
16401652 return container_ref_instruction;
16411653
1642 IrInstruction *ptr_instruction = ir_build_field_ptr(irb, node, container_ref_instruction, field_name);
1654 IrInstruction *ptr_instruction = ir_build_field_ptr(irb, scope, node, container_ref_instruction, field_name);
16431655 if (lval != LValPurposeNone)
16441656 return ptr_instruction;
16451657
1646 return ir_build_load_ptr(irb, node, ptr_instruction);
1658 return ir_build_load_ptr(irb, scope, node, ptr_instruction);
16471659}
16481660
1649static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, AstNode *node) {
1661static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNode *node) {
16501662 assert(node->type == NodeTypeFnCallExpr);
16511663
16521664 AstNode *fn_ref_expr = node->data.fn_call_expr.fn_ref_expr;
......@@ -1675,115 +1687,115 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, AstNode *node) {
16751687 case BuiltinFnIdInvalid:
16761688 zig_unreachable();
16771689 case BuiltinFnIdUnreachable:
1678 return ir_build_unreachable(irb, node);
1690 return ir_build_unreachable(irb, scope, node);
16791691 case BuiltinFnIdTypeof:
16801692 {
16811693 AstNode *arg_node = node->data.fn_call_expr.params.at(0);
1682 IrInstruction *arg = ir_gen_node(irb, arg_node, node->scope);
1694 IrInstruction *arg = ir_gen_node(irb, arg_node, scope);
16831695 if (arg == irb->codegen->invalid_instruction)
16841696 return arg;
1685 return ir_build_typeof(irb, node, arg);
1697 return ir_build_typeof(irb, scope, node, arg);
16861698 }
16871699 case BuiltinFnIdSetFnTest:
16881700 {
16891701 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1690 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1702 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
16911703 if (arg0_value == irb->codegen->invalid_instruction)
16921704 return arg0_value;
16931705
16941706 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
1695 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, node->scope);
1707 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
16961708 if (arg1_value == irb->codegen->invalid_instruction)
16971709 return arg1_value;
16981710
1699 return ir_build_set_fn_test(irb, node, arg0_value, arg1_value);
1711 return ir_build_set_fn_test(irb, scope, node, arg0_value, arg1_value);
17001712 }
17011713 case BuiltinFnIdSetFnVisible:
17021714 {
17031715 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1704 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1716 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
17051717 if (arg0_value == irb->codegen->invalid_instruction)
17061718 return arg0_value;
17071719
17081720 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
1709 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, node->scope);
1721 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
17101722 if (arg1_value == irb->codegen->invalid_instruction)
17111723 return arg1_value;
17121724
1713 return ir_build_set_fn_visible(irb, node, arg0_value, arg1_value);
1725 return ir_build_set_fn_visible(irb, scope, node, arg0_value, arg1_value);
17141726 }
17151727 case BuiltinFnIdSetDebugSafety:
17161728 {
17171729 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1718 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1730 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
17191731 if (arg0_value == irb->codegen->invalid_instruction)
17201732 return arg0_value;
17211733
17221734 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
1723 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, node->scope);
1735 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
17241736 if (arg1_value == irb->codegen->invalid_instruction)
17251737 return arg1_value;
17261738
1727 return ir_build_set_debug_safety(irb, node, arg0_value, arg1_value);
1739 return ir_build_set_debug_safety(irb, scope, node, arg0_value, arg1_value);
17281740 }
17291741 case BuiltinFnIdCompileVar:
17301742 {
17311743 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1732 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1744 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
17331745 if (arg0_value == irb->codegen->invalid_instruction)
17341746 return arg0_value;
17351747
1736 return ir_build_compile_var(irb, node, arg0_value);
1748 return ir_build_compile_var(irb, scope, node, arg0_value);
17371749 }
17381750 case BuiltinFnIdSizeof:
17391751 {
17401752 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1741 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1753 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
17421754 if (arg0_value == irb->codegen->invalid_instruction)
17431755 return arg0_value;
17441756
1745 return ir_build_size_of(irb, node, arg0_value);
1757 return ir_build_size_of(irb, scope, node, arg0_value);
17461758 }
17471759 case BuiltinFnIdCtz:
17481760 {
17491761 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1750 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1762 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
17511763 if (arg0_value == irb->codegen->invalid_instruction)
17521764 return arg0_value;
17531765
1754 return ir_build_ctz(irb, node, arg0_value);
1766 return ir_build_ctz(irb, scope, node, arg0_value);
17551767 }
17561768 case BuiltinFnIdClz:
17571769 {
17581770 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1759 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1771 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
17601772 if (arg0_value == irb->codegen->invalid_instruction)
17611773 return arg0_value;
17621774
1763 return ir_build_clz(irb, node, arg0_value);
1775 return ir_build_clz(irb, scope, node, arg0_value);
17641776 }
17651777 case BuiltinFnIdStaticEval:
17661778 {
17671779 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1768 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1780 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
17691781 if (arg0_value == irb->codegen->invalid_instruction)
17701782 return arg0_value;
17711783
1772 return ir_build_static_eval(irb, node, arg0_value);
1784 return ir_build_static_eval(irb, scope, node, arg0_value);
17731785 }
17741786 case BuiltinFnIdImport:
17751787 {
17761788 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1777 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->scope);
1789 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
17781790 if (arg0_value == irb->codegen->invalid_instruction)
17791791 return arg0_value;
17801792
1781 if (scope_fn_entry(node->scope)) {
1793 if (exec_fn_entry(irb->exec)) {
17821794 add_node_error(irb->codegen, node, buf_sprintf("import valid only at top level scope"));
17831795 return irb->codegen->invalid_instruction;
17841796 }
17851797
1786 return ir_build_import(irb, node, arg0_value);
1798 return ir_build_import(irb, scope, node, arg0_value);
17871799 }
17881800 case BuiltinFnIdMemcpy:
17891801 case BuiltinFnIdMemset:
......@@ -1816,14 +1828,14 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, AstNode *node) {
18161828 zig_unreachable();
18171829}
18181830
1819static IrInstruction *ir_gen_fn_call(IrBuilder *irb, AstNode *node) {
1831static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node) {
18201832 assert(node->type == NodeTypeFnCallExpr);
18211833
18221834 if (node->data.fn_call_expr.is_builtin)
1823 return ir_gen_builtin_fn_call(irb, node);
1835 return ir_gen_builtin_fn_call(irb, scope, node);
18241836
18251837 AstNode *fn_ref_node = node->data.fn_call_expr.fn_ref_expr;
1826 IrInstruction *fn_ref = ir_gen_node(irb, fn_ref_node, node->scope);
1838 IrInstruction *fn_ref = ir_gen_node(irb, fn_ref_node, scope);
18271839 if (fn_ref == irb->codegen->invalid_instruction)
18281840 return fn_ref;
18291841
......@@ -1831,16 +1843,16 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, AstNode *node) {
18311843 IrInstruction **args = allocate<IrInstruction*>(arg_count);
18321844 for (size_t i = 0; i < arg_count; i += 1) {
18331845 AstNode *arg_node = node->data.fn_call_expr.params.at(i);
1834 args[i] = ir_gen_node(irb, arg_node, node->scope);
1846 args[i] = ir_gen_node(irb, arg_node, scope);
18351847 }
18361848
1837 return ir_build_call(irb, node, nullptr, fn_ref, arg_count, args);
1849 return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args);
18381850}
18391851
1840static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, AstNode *node) {
1852static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
18411853 assert(node->type == NodeTypeIfBoolExpr);
18421854
1843 IrInstruction *condition = ir_gen_node(irb, node->data.if_bool_expr.condition, node->scope);
1855 IrInstruction *condition = ir_gen_node(irb, node->data.if_bool_expr.condition, scope);
18441856 if (condition == irb->codegen->invalid_instruction)
18451857 return condition;
18461858
......@@ -1852,26 +1864,26 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, AstNode *node) {
18521864 IrBasicBlock *endif_block = ir_build_basic_block(irb, "EndIf");
18531865
18541866 bool is_inline = ir_should_inline(irb) || node->data.if_bool_expr.is_inline;
1855 ir_build_cond_br(irb, condition->source_node, condition, then_block, else_block, is_inline);
1867 ir_build_cond_br(irb, scope, condition->source_node, condition, then_block, else_block, is_inline);
18561868
18571869 ir_set_cursor_at_end(irb, then_block);
1858 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, node->scope);
1870 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, scope);
18591871 if (then_expr_result == irb->codegen->invalid_instruction)
18601872 return then_expr_result;
18611873 IrBasicBlock *after_then_block = irb->current_basic_block;
1862 ir_build_br(irb, node, endif_block, is_inline);
1874 ir_build_br(irb, scope, node, endif_block, is_inline);
18631875
18641876 ir_set_cursor_at_end(irb, else_block);
18651877 IrInstruction *else_expr_result;
18661878 if (else_node) {
1867 else_expr_result = ir_gen_node(irb, else_node, node->scope);
1879 else_expr_result = ir_gen_node(irb, else_node, scope);
18681880 if (else_expr_result == irb->codegen->invalid_instruction)
18691881 return else_expr_result;
18701882 } else {
1871 else_expr_result = ir_build_const_void(irb, node);
1883 else_expr_result = ir_build_const_void(irb, scope, node);
18721884 }
18731885 IrBasicBlock *after_else_block = irb->current_basic_block;
1874 ir_build_br(irb, node, endif_block, is_inline);
1886 ir_build_br(irb, scope, node, endif_block, is_inline);
18751887
18761888 ir_set_cursor_at_end(irb, endif_block);
18771889 IrInstruction **incoming_values = allocate<IrInstruction *>(2);
......@@ -1881,14 +1893,14 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, AstNode *node) {
18811893 incoming_blocks[0] = after_then_block;
18821894 incoming_blocks[1] = after_else_block;
18831895
1884 return ir_build_phi(irb, node, 2, incoming_blocks, incoming_values);
1896 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
18851897}
18861898
1887static IrInstruction *ir_gen_prefix_op_id_lval(IrBuilder *irb, AstNode *node, IrUnOp op_id, LValPurpose lval) {
1899static IrInstruction *ir_gen_prefix_op_id_lval(IrBuilder *irb, Scope *scope, AstNode *node, IrUnOp op_id, LValPurpose lval) {
18881900 assert(node->type == NodeTypePrefixOpExpr);
18891901 AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
18901902
1891 IrInstruction *value = ir_gen_node_extra(irb, expr_node, node->scope, lval);
1903 IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval);
18921904 if (value == irb->codegen->invalid_instruction)
18931905 return value;
18941906
......@@ -1896,27 +1908,27 @@ static IrInstruction *ir_gen_prefix_op_id_lval(IrBuilder *irb, AstNode *node, Ir
18961908 return value;
18971909 }
18981910
1899 return ir_build_un_op(irb, node, op_id, value);
1911 return ir_build_un_op(irb, scope, node, op_id, value);
19001912}
19011913
1902static IrInstruction *ir_gen_prefix_op_id(IrBuilder *irb, AstNode *node, IrUnOp op_id) {
1903 return ir_gen_prefix_op_id_lval(irb, node, op_id, LValPurposeNone);
1914static IrInstruction *ir_gen_prefix_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrUnOp op_id) {
1915 return ir_gen_prefix_op_id_lval(irb, scope, node, op_id, LValPurposeNone);
19041916}
19051917
1906static IrInstruction *ir_gen_prefix_op_unwrap_maybe(IrBuilder *irb, AstNode *node, LValPurpose lval) {
1918static IrInstruction *ir_gen_prefix_op_unwrap_maybe(IrBuilder *irb, Scope *scope, AstNode *node, LValPurpose lval) {
19071919 AstNode *expr = node->data.prefix_op_expr.primary_expr;
1908 IrInstruction *value = ir_gen_node_extra(irb, expr, node->scope, LValPurposeAddressOf);
1920 IrInstruction *value = ir_gen_node_extra(irb, expr, scope, LValPurposeAddressOf);
19091921 if (value == irb->codegen->invalid_instruction)
19101922 return value;
19111923
1912 IrInstruction *unwrapped_ptr = ir_build_unwrap_maybe(irb, node, value, true);
1924 IrInstruction *unwrapped_ptr = ir_build_unwrap_maybe(irb, scope, node, value, true);
19131925 if (lval == LValPurposeNone)
1914 return ir_build_load_ptr(irb, node, unwrapped_ptr);
1926 return ir_build_load_ptr(irb, scope, node, unwrapped_ptr);
19151927 else
19161928 return unwrapped_ptr;
19171929}
19181930
1919static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, AstNode *node, LValPurpose lval) {
1931static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNode *node, LValPurpose lval) {
19201932 assert(node->type == NodeTypePrefixOpExpr);
19211933
19221934 PrefixOp prefix_op = node->data.prefix_op_expr.prefix_op;
......@@ -1925,38 +1937,38 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, AstNode *node, LValP
19251937 case PrefixOpInvalid:
19261938 zig_unreachable();
19271939 case PrefixOpBoolNot:
1928 return ir_gen_prefix_op_id(irb, node, IrUnOpBoolNot);
1940 return ir_gen_prefix_op_id(irb, scope, node, IrUnOpBoolNot);
19291941 case PrefixOpBinNot:
1930 return ir_gen_prefix_op_id(irb, node, IrUnOpBinNot);
1942 return ir_gen_prefix_op_id(irb, scope, node, IrUnOpBinNot);
19311943 case PrefixOpNegation:
1932 return ir_gen_prefix_op_id(irb, node, IrUnOpNegation);
1944 return ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegation);
19331945 case PrefixOpNegationWrap:
1934 return ir_gen_prefix_op_id(irb, node, IrUnOpNegationWrap);
1946 return ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegationWrap);
19351947 case PrefixOpAddressOf:
1936 return ir_gen_prefix_op_id_lval(irb, node, IrUnOpAddressOf, LValPurposeAddressOf);
1948 return ir_gen_prefix_op_id_lval(irb, scope, node, IrUnOpAddressOf, LValPurposeAddressOf);
19371949 case PrefixOpConstAddressOf:
1938 return ir_gen_prefix_op_id_lval(irb, node, IrUnOpConstAddressOf, LValPurposeAddressOf);
1950 return ir_gen_prefix_op_id_lval(irb, scope, node, IrUnOpConstAddressOf, LValPurposeAddressOf);
19391951 case PrefixOpDereference:
1940 return ir_gen_prefix_op_id_lval(irb, node, IrUnOpDereference, lval);
1952 return ir_gen_prefix_op_id_lval(irb, scope, node, IrUnOpDereference, lval);
19411953 case PrefixOpMaybe:
1942 return ir_gen_prefix_op_id(irb, node, IrUnOpMaybe);
1954 return ir_gen_prefix_op_id(irb, scope, node, IrUnOpMaybe);
19431955 case PrefixOpError:
1944 return ir_gen_prefix_op_id(irb, node, IrUnOpError);
1956 return ir_gen_prefix_op_id(irb, scope, node, IrUnOpError);
19451957 case PrefixOpUnwrapError:
1946 return ir_gen_prefix_op_id(irb, node, IrUnOpUnwrapError);
1958 return ir_gen_prefix_op_id(irb, scope, node, IrUnOpUnwrapError);
19471959 case PrefixOpUnwrapMaybe:
1948 return ir_gen_prefix_op_unwrap_maybe(irb, node, lval);
1960 return ir_gen_prefix_op_unwrap_maybe(irb, scope, node, lval);
19491961 }
19501962 zig_unreachable();
19511963}
19521964
1953static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, AstNode *node) {
1965static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
19541966 assert(node->type == NodeTypeContainerInitExpr);
19551967
19561968 AstNodeContainerInitExpr *container_init_expr = &node->data.container_init_expr;
19571969 ContainerInitKind kind = container_init_expr->kind;
19581970
1959 IrInstruction *container_type = ir_gen_node(irb, container_init_expr->type, node->scope);
1971 IrInstruction *container_type = ir_gen_node(irb, container_init_expr->type, scope);
19601972 if (container_type == irb->codegen->invalid_instruction)
19611973 return container_type;
19621974
......@@ -1969,7 +1981,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, AstNode *node)
19691981
19701982 Buf *name = entry_node->data.struct_val_field.name;
19711983 AstNode *expr_node = entry_node->data.struct_val_field.expr;
1972 IrInstruction *expr_value = ir_gen_node(irb, expr_node, node->scope);
1984 IrInstruction *expr_value = ir_gen_node(irb, expr_node, scope);
19731985 if (expr_value == irb->codegen->invalid_instruction)
19741986 return expr_value;
19751987
......@@ -1977,39 +1989,39 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, AstNode *node)
19771989 fields[i].value = expr_value;
19781990 fields[i].source_node = entry_node;
19791991 }
1980 return ir_build_container_init_fields(irb, node, container_type, field_count, fields);
1992 return ir_build_container_init_fields(irb, scope, node, container_type, field_count, fields);
19811993 } else if (kind == ContainerInitKindArray) {
19821994 size_t item_count = container_init_expr->entries.length;
19831995 IrInstruction **values = allocate<IrInstruction *>(item_count);
19841996 for (size_t i = 0; i < item_count; i += 1) {
19851997 AstNode *expr_node = container_init_expr->entries.at(i);
1986 IrInstruction *expr_value = ir_gen_node(irb, expr_node, node->scope);
1998 IrInstruction *expr_value = ir_gen_node(irb, expr_node, scope);
19871999 if (expr_value == irb->codegen->invalid_instruction)
19882000 return expr_value;
19892001
19902002 values[i] = expr_value;
19912003 }
1992 return ir_build_container_init_list(irb, node, container_type, item_count, values);
2004 return ir_build_container_init_list(irb, scope, node, container_type, item_count, values);
19932005 } else {
19942006 zig_unreachable();
19952007 }
19962008}
19972009
1998static IrInstruction *ir_gen_var_decl(IrBuilder *irb, AstNode *node) {
2010static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *node) {
19992011 assert(node->type == NodeTypeVariableDeclaration);
20002012
20012013 AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;
20022014
20032015 IrInstruction *type_instruction;
20042016 if (variable_declaration->type != nullptr) {
2005 type_instruction = ir_gen_node(irb, variable_declaration->type, node->scope);
2017 type_instruction = ir_gen_node(irb, variable_declaration->type, scope);
20062018 if (type_instruction == irb->codegen->invalid_instruction)
20072019 return type_instruction;
20082020 } else {
20092021 type_instruction = nullptr;
20102022 }
20112023
2012 IrInstruction *init_value = ir_gen_node(irb, variable_declaration->expr, node->scope);
2024 IrInstruction *init_value = ir_gen_node(irb, variable_declaration->expr, scope);
20132025 if (init_value == irb->codegen->invalid_instruction)
20142026 return init_value;
20152027
......@@ -2017,7 +2029,7 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, AstNode *node) {
20172029 bool is_const = variable_declaration->is_const;
20182030 bool is_extern = variable_declaration->is_extern;
20192031 bool is_inline = ir_should_inline(irb) || variable_declaration->is_inline;
2020 VariableTableEntry *var = ir_create_var(irb, node, node->scope,
2032 VariableTableEntry *var = ir_create_var(irb, node, scope,
20212033 variable_declaration->symbol, is_const, is_const, is_shadowable, is_inline);
20222034 // we detect IrInstructionIdDeclVar in gen_block to make sure the next node
20232035 // is inside var->child_scope
......@@ -2028,10 +2040,10 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, AstNode *node) {
20282040 return irb->codegen->invalid_instruction;
20292041 }
20302042
2031 return ir_build_var_decl(irb, node, var, type_instruction, init_value);
2043 return ir_build_var_decl(irb, scope, node, var, type_instruction, init_value);
20322044}
20332045
2034static IrInstruction *ir_gen_while_expr(IrBuilder *irb, AstNode *node) {
2046static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
20352047 assert(node->type == NodeTypeWhileExpr);
20362048
20372049 AstNode *continue_expr_node = node->data.while_expr.continue_expr;
......@@ -2043,37 +2055,35 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, AstNode *node) {
20432055 IrBasicBlock *end_block = ir_build_basic_block(irb, "WhileEnd");
20442056
20452057 bool is_inline = ir_should_inline(irb) || node->data.while_expr.is_inline;
2046 ir_build_br(irb, node, cond_block, is_inline);
2058 ir_build_br(irb, scope, node, cond_block, is_inline);
20472059
20482060 if (continue_expr_node) {
20492061 ir_set_cursor_at_end(irb, continue_block);
2050 ir_gen_node(irb, continue_expr_node, node->scope);
2051 ir_build_br(irb, node, cond_block, is_inline);
2062 ir_gen_node(irb, continue_expr_node, scope);
2063 ir_build_br(irb, scope, node, cond_block, is_inline);
20522064 }
20532065
20542066 ir_set_cursor_at_end(irb, cond_block);
2055 IrInstruction *cond_val = ir_gen_node(irb, node->data.while_expr.condition, node->scope);
2056 ir_build_cond_br(irb, node->data.while_expr.condition, cond_val, body_block, end_block, is_inline);
2067 IrInstruction *cond_val = ir_gen_node(irb, node->data.while_expr.condition, scope);
2068 ir_build_cond_br(irb, scope, node->data.while_expr.condition, cond_val, body_block, end_block, is_inline);
20572069
20582070 ir_set_cursor_at_end(irb, body_block);
20592071
20602072 irb->break_block_stack.append(end_block);
20612073 irb->continue_block_stack.append(continue_block);
2062 ir_gen_node(irb, node->data.while_expr.body, node->scope);
2074 ir_gen_node(irb, node->data.while_expr.body, scope);
20632075 irb->break_block_stack.pop();
20642076 irb->continue_block_stack.pop();
20652077
2066 ir_build_br(irb, node, continue_block, is_inline);
2078 ir_build_br(irb, scope, node, continue_block, is_inline);
20672079 ir_set_cursor_at_end(irb, end_block);
20682080
2069 return ir_build_const_void(irb, node);
2081 return ir_build_const_void(irb, scope, node);
20702082}
20712083
2072static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
2084static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
20732085 assert(node->type == NodeTypeForExpr);
20742086
2075 Scope *parent_scope = node->scope;
2076
20772087 AstNode *array_node = node->data.for_expr.array_expr;
20782088 AstNode *elem_node = node->data.for_expr.elem_node;
20792089 AstNode *index_node = node->data.for_expr.index_node;
......@@ -2089,48 +2099,45 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
20892099 if (array_val == irb->codegen->invalid_instruction)
20902100 return array_val;
20912101
2092 IrInstruction *array_type = ir_build_typeof(irb, array_node, array_val);
2093 IrInstruction *pointer_type = ir_build_to_ptr_type(irb, array_node, array_type);
2102 IrInstruction *array_type = ir_build_typeof(irb, parent_scope, array_node, array_val);
2103 IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_type);
20942104 IrInstruction *elem_var_type;
20952105 if (node->data.for_expr.elem_is_ptr) {
20962106 elem_var_type = pointer_type;
20972107 } else {
2098 elem_var_type = ir_build_ptr_type_child(irb, elem_node, pointer_type);
2108 elem_var_type = ir_build_ptr_type_child(irb, parent_scope, elem_node, pointer_type);
20992109 }
21002110 bool is_inline = ir_should_inline(irb) || node->data.for_expr.is_inline;
21012111
21022112 Scope *child_scope = create_loop_scope(node, parent_scope);
2103 elem_node->scope = child_scope;
21042113
21052114 // TODO make it an error to write to element variable or i variable.
21062115 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
2107 node->data.for_expr.elem_var = ir_create_var(irb, elem_node, child_scope, elem_var_name,
2116 VariableTableEntry *elem_var = ir_create_var(irb, elem_node, child_scope, elem_var_name,
21082117 true, false, false, is_inline);
2109 child_scope = node->data.for_expr.elem_var->child_scope;
2118 child_scope = elem_var->child_scope;
21102119
2111 IrInstruction *undefined_value = ir_build_const_undefined(irb, elem_node);
2112 ir_build_var_decl(irb, elem_node, node->data.for_expr.elem_var, elem_var_type, undefined_value);
2113 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, node, node->data.for_expr.elem_var);
2120 IrInstruction *undefined_value = ir_build_const_undefined(irb, child_scope, elem_node);
2121 ir_build_var_decl(irb, child_scope, elem_node, elem_var, elem_var_type, undefined_value);
2122 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, child_scope, node, elem_var);
21142123
21152124 AstNode *index_var_source_node;
2125 VariableTableEntry *index_var;
21162126 if (index_node) {
21172127 index_var_source_node = index_node;
21182128 Buf *index_var_name = index_node->data.symbol_expr.symbol;
2119 index_node->scope = child_scope;
2120 node->data.for_expr.index_var = ir_create_var(irb, index_node, child_scope, index_var_name,
2121 true, false, false, is_inline);
2129 index_var = ir_create_var(irb, index_node, child_scope, index_var_name, true, false, false, is_inline);
21222130 } else {
21232131 index_var_source_node = node;
2124 node->data.for_expr.index_var = ir_create_var(irb, node, child_scope, nullptr,
2125 true, false, true, is_inline);
2132 index_var = ir_create_var(irb, node, child_scope, nullptr, true, false, true, is_inline);
21262133 }
2127 child_scope = node->data.for_expr.index_var->child_scope;
2134 child_scope = index_var->child_scope;
21282135
2129 IrInstruction *usize = ir_build_const_type(irb, node, irb->codegen->builtin_types.entry_usize);
2130 IrInstruction *zero = ir_build_const_usize(irb, node, 0);
2131 IrInstruction *one = ir_build_const_usize(irb, node, 1);
2132 ir_build_var_decl(irb, index_var_source_node, node->data.for_expr.index_var, usize, zero);
2133 IrInstruction *index_ptr = ir_build_var_ptr(irb, node, node->data.for_expr.index_var);
2136 IrInstruction *usize = ir_build_const_type(irb, child_scope, node, irb->codegen->builtin_types.entry_usize);
2137 IrInstruction *zero = ir_build_const_usize(irb, child_scope, node, 0);
2138 IrInstruction *one = ir_build_const_usize(irb, child_scope, node, 1);
2139 ir_build_var_decl(irb, child_scope, index_var_source_node, index_var, usize, zero);
2140 IrInstruction *index_ptr = ir_build_var_ptr(irb, child_scope, node, index_var);
21342141
21352142
21362143 IrBasicBlock *cond_block = ir_build_basic_block(irb, "ForCond");
......@@ -2138,23 +2145,23 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
21382145 IrBasicBlock *end_block = ir_build_basic_block(irb, "ForEnd");
21392146 IrBasicBlock *continue_block = ir_build_basic_block(irb, "ForContinue");
21402147
2141 IrInstruction *len_val = ir_build_array_len(irb, node, array_val);
2142 ir_build_br(irb, node, cond_block, is_inline);
2148 IrInstruction *len_val = ir_build_array_len(irb, child_scope, node, array_val);
2149 ir_build_br(irb, child_scope, node, cond_block, is_inline);
21432150
21442151 ir_set_cursor_at_end(irb, cond_block);
2145 IrInstruction *index_val = ir_build_load_ptr(irb, node, index_ptr);
2146 IrInstruction *cond = ir_build_bin_op(irb, node, IrBinOpCmpLessThan, index_val, len_val);
2147 ir_build_cond_br(irb, node, cond, body_block, end_block, is_inline);
2152 IrInstruction *index_val = ir_build_load_ptr(irb, child_scope, node, index_ptr);
2153 IrInstruction *cond = ir_build_bin_op(irb, child_scope, node, IrBinOpCmpLessThan, index_val, len_val);
2154 ir_build_cond_br(irb, child_scope, node, cond, body_block, end_block, is_inline);
21482155
21492156 ir_set_cursor_at_end(irb, body_block);
2150 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, node, array_val, index_val, true);
2157 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, child_scope, node, array_val, index_val, true);
21512158 IrInstruction *elem_val;
21522159 if (node->data.for_expr.elem_is_ptr) {
21532160 elem_val = elem_ptr;
21542161 } else {
2155 elem_val = ir_build_load_ptr(irb, node, elem_ptr);
2162 elem_val = ir_build_load_ptr(irb, child_scope, node, elem_ptr);
21562163 }
2157 ir_build_store_ptr(irb, node, elem_var_ptr, elem_val);
2164 ir_build_store_ptr(irb, child_scope, node, elem_var_ptr, elem_val);
21582165
21592166 irb->break_block_stack.append(end_block);
21602167 irb->continue_block_stack.append(continue_block);
......@@ -2162,61 +2169,60 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
21622169 irb->break_block_stack.pop();
21632170 irb->continue_block_stack.pop();
21642171
2165 ir_build_br(irb, node, continue_block, is_inline);
2172 ir_build_br(irb, child_scope, node, continue_block, is_inline);
21662173
21672174 ir_set_cursor_at_end(irb, continue_block);
2168 IrInstruction *new_index_val = ir_build_bin_op(irb, node, IrBinOpAdd, index_val, one);
2169 ir_build_store_ptr(irb, node, index_ptr, new_index_val);
2170 ir_build_br(irb, node, cond_block, is_inline);
2175 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one);
2176 ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val);
2177 ir_build_br(irb, child_scope, node, cond_block, is_inline);
21712178
21722179 ir_set_cursor_at_end(irb, end_block);
2173 return ir_build_const_void(irb, node);
2180 return ir_build_const_void(irb, child_scope, node);
21742181
21752182}
21762183
2177static IrInstruction *ir_gen_this_literal(IrBuilder *irb, AstNode *node) {
2184static IrInstruction *ir_gen_this_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
21782185 assert(node->type == NodeTypeThisLiteral);
21792186
2180 Scope *scope = node->scope;
2181
21822187 if (!scope->parent)
2183 return ir_build_const_import(irb, node, node->owner);
2188 return ir_build_const_import(irb, scope, node, node->owner);
21842189
2185 FnTableEntry *fn_entry = scope_fn_entry(scope);
2190 FnTableEntry *fn_entry = exec_fn_entry(irb->exec);
21862191 if (fn_entry && scope->parent && scope->parent->parent &&
21872192 !scope_fn_entry(scope->parent->parent))
21882193 {
2189 return ir_build_const_fn(irb, node, fn_entry);
2194 return ir_build_const_fn(irb, scope, node, fn_entry);
21902195 }
21912196
21922197 if (scope->node->type == NodeTypeContainerDecl) {
2193 TypeTableEntry *container_type = scope->node->data.struct_decl.type_entry;
2198 ScopeDecls *decls_scope = (ScopeDecls *)scope;
2199 TypeTableEntry *container_type = decls_scope->container_type;
21942200 assert(container_type);
2195 return ir_build_const_type(irb, node, container_type);
2201 return ir_build_const_type(irb, scope, node, container_type);
21962202 }
21972203
21982204 if (scope->node->type == NodeTypeBlock)
2199 return ir_build_const_scope(irb, node, scope);
2205 return ir_build_const_scope(irb, scope, node, scope);
22002206
22012207 zig_unreachable();
22022208}
22032209
2204static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, AstNode *node) {
2210static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
22052211 assert(node->type == NodeTypeBoolLiteral);
2206 return ir_build_const_bool(irb, node, node->data.bool_literal.value);
2212 return ir_build_const_bool(irb, scope, node, node->data.bool_literal.value);
22072213}
22082214
2209static IrInstruction *ir_gen_string_literal(IrBuilder *irb, AstNode *node) {
2215static IrInstruction *ir_gen_string_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
22102216 assert(node->type == NodeTypeStringLiteral);
22112217
22122218 if (node->data.string_literal.c) {
2213 return ir_build_const_c_str_lit(irb, node, node->data.string_literal.buf);
2219 return ir_build_const_c_str_lit(irb, scope, node, node->data.string_literal.buf);
22142220 } else {
2215 return ir_build_const_str_lit(irb, node, node->data.string_literal.buf);
2221 return ir_build_const_str_lit(irb, scope, node, node->data.string_literal.buf);
22162222 }
22172223}
22182224
2219static IrInstruction *ir_gen_array_type(IrBuilder *irb, AstNode *node) {
2225static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *node) {
22202226 assert(node->type == NodeTypeArrayType);
22212227
22222228 AstNode *size_node = node->data.array_type.size;
......@@ -2229,35 +2235,36 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, AstNode *node) {
22292235 return irb->codegen->invalid_instruction;
22302236 }
22312237
2232 IrInstruction *size_value = ir_gen_node(irb, size_node, node->scope);
2238 IrInstruction *size_value = ir_gen_node(irb, size_node, scope);
22332239 if (size_value == irb->codegen->invalid_instruction)
22342240 return size_value;
22352241
2236 IrInstruction *child_type = ir_gen_node(irb, child_type_node, node->scope);
2242 IrInstruction *child_type = ir_gen_node(irb, child_type_node, scope);
22372243 if (child_type == irb->codegen->invalid_instruction)
22382244 return child_type;
22392245
2240 return ir_build_array_type(irb, node, size_value, child_type);
2246 return ir_build_array_type(irb, scope, node, size_value, child_type);
22412247 } else {
22422248 IrInstruction *child_type = ir_gen_node_extra(irb, child_type_node,
2243 node->scope, LValPurposeAddressOf);
2249 scope, LValPurposeAddressOf);
22442250 if (child_type == irb->codegen->invalid_instruction)
22452251 return child_type;
22462252
2247 return ir_build_slice_type(irb, node, is_const, child_type);
2253 return ir_build_slice_type(irb, scope, node, is_const, child_type);
22482254 }
22492255}
22502256
2251static IrInstruction *ir_gen_undefined_literal(IrBuilder *irb, AstNode *node) {
2257static IrInstruction *ir_gen_undefined_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
22522258 assert(node->type == NodeTypeUndefinedLiteral);
2253 return ir_build_const_undefined(irb, node);
2259 return ir_build_const_undefined(irb, scope, node);
22542260}
22552261
2256static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, AstNode *node) {
2262static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
22572263 assert(node->type == NodeTypeAsmExpr);
22582264
22592265 IrInstruction **input_list = allocate<IrInstruction *>(node->data.asm_expr.input_list.length);
22602266 IrInstruction **output_types = allocate<IrInstruction *>(node->data.asm_expr.output_list.length);
2267 VariableTableEntry **output_vars = allocate<VariableTableEntry *>(node->data.asm_expr.output_list.length);
22612268 size_t return_count = 0;
22622269 bool is_volatile = node->data.asm_expr.is_volatile;
22632270 if (!is_volatile && node->data.asm_expr.output_list.length == 0) {
......@@ -2270,7 +2277,7 @@ static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, AstNode *node) {
22702277 if (asm_output->return_type) {
22712278 return_count += 1;
22722279
2273 IrInstruction *return_type = ir_gen_node(irb, asm_output->return_type, node->scope);
2280 IrInstruction *return_type = ir_gen_node(irb, asm_output->return_type, scope);
22742281 if (return_type == irb->codegen->invalid_instruction)
22752282 return irb->codegen->invalid_instruction;
22762283 if (return_count > 1) {
......@@ -2281,9 +2288,9 @@ static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, AstNode *node) {
22812288 output_types[i] = return_type;
22822289 } else {
22832290 Buf *variable_name = asm_output->variable_name;
2284 VariableTableEntry *var = find_variable(irb->codegen, node->scope, variable_name);
2291 VariableTableEntry *var = find_variable(irb->codegen, scope, variable_name);
22852292 if (var) {
2286 asm_output->variable = var;
2293 output_vars[i] = var;
22872294 } else {
22882295 add_node_error(irb->codegen, node,
22892296 buf_sprintf("use of undeclared identifier '%s'", buf_ptr(variable_name)));
......@@ -2293,17 +2300,17 @@ static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, AstNode *node) {
22932300 }
22942301 for (size_t i = 0; i < node->data.asm_expr.input_list.length; i += 1) {
22952302 AsmInput *asm_input = node->data.asm_expr.input_list.at(i);
2296 IrInstruction *input_value = ir_gen_node(irb, asm_input->expr, node->scope);
2303 IrInstruction *input_value = ir_gen_node(irb, asm_input->expr, scope);
22972304 if (input_value == irb->codegen->invalid_instruction)
22982305 return irb->codegen->invalid_instruction;
22992306
23002307 input_list[i] = input_value;
23012308 }
23022309
2303 return ir_build_asm(irb, node, input_list, output_types, return_count, is_volatile);
2310 return ir_build_asm(irb, scope, node, input_list, output_types, output_vars, return_count, is_volatile);
23042311}
23052312
2306static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, AstNode *node) {
2313static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
23072314 assert(node->type == NodeTypeIfVarExpr);
23082315
23092316 AstNodeVariableDeclaration *var_decl = &node->data.if_var_expr.var_decl;
......@@ -2312,51 +2319,51 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, AstNode *node) {
23122319 AstNode *else_node = node->data.if_var_expr.else_node;
23132320 bool var_is_ptr = node->data.if_var_expr.var_is_ptr;
23142321
2315 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, node->scope, LValPurposeAddressOf);
2322 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, scope, LValPurposeAddressOf);
23162323 if (expr_value == irb->codegen->invalid_instruction)
23172324 return expr_value;
23182325
2319 IrInstruction *is_nonnull_value = ir_build_test_null(irb, node, expr_value);
2326 IrInstruction *is_nonnull_value = ir_build_test_null(irb, scope, node, expr_value);
23202327
23212328 IrBasicBlock *then_block = ir_build_basic_block(irb, "MaybeThen");
23222329 IrBasicBlock *else_block = ir_build_basic_block(irb, "MaybeElse");
23232330 IrBasicBlock *endif_block = ir_build_basic_block(irb, "MaybeEndIf");
23242331
23252332 bool is_inline = ir_should_inline(irb) || node->data.if_var_expr.is_inline;
2326 ir_build_cond_br(irb, node, is_nonnull_value, then_block, else_block, is_inline);
2333 ir_build_cond_br(irb, scope, node, is_nonnull_value, then_block, else_block, is_inline);
23272334
23282335 ir_set_cursor_at_end(irb, then_block);
23292336 IrInstruction *var_type = nullptr;
23302337 if (var_decl->type) {
2331 var_type = ir_gen_node(irb, var_decl->type, node->scope);
2338 var_type = ir_gen_node(irb, var_decl->type, scope);
23322339 if (var_type == irb->codegen->invalid_instruction)
23332340 return irb->codegen->invalid_instruction;
23342341 }
23352342 bool is_shadowable = false;
23362343 bool is_const = var_decl->is_const;
2337 VariableTableEntry *var = ir_create_var(irb, node, node->scope,
2344 VariableTableEntry *var = ir_create_var(irb, node, scope,
23382345 var_decl->symbol, is_const, is_const, is_shadowable, is_inline);
23392346
2340 IrInstruction *var_ptr_value = ir_build_unwrap_maybe(irb, node, expr_value, false);
2341 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, node, var_ptr_value);
2342 ir_build_var_decl(irb, node, var, var_type, var_value);
2347 IrInstruction *var_ptr_value = ir_build_unwrap_maybe(irb, scope, node, expr_value, false);
2348 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, scope, node, var_ptr_value);
2349 ir_build_var_decl(irb, scope, node, var, var_type, var_value);
23432350 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, var->child_scope);
23442351 if (then_expr_result == irb->codegen->invalid_instruction)
23452352 return then_expr_result;
23462353 IrBasicBlock *after_then_block = irb->current_basic_block;
2347 ir_build_br(irb, node, endif_block, is_inline);
2354 ir_build_br(irb, scope, node, endif_block, is_inline);
23482355
23492356 ir_set_cursor_at_end(irb, else_block);
23502357 IrInstruction *else_expr_result;
23512358 if (else_node) {
2352 else_expr_result = ir_gen_node(irb, else_node, node->scope);
2359 else_expr_result = ir_gen_node(irb, else_node, scope);
23532360 if (else_expr_result == irb->codegen->invalid_instruction)
23542361 return else_expr_result;
23552362 } else {
2356 else_expr_result = ir_build_const_void(irb, node);
2363 else_expr_result = ir_build_const_void(irb, scope, node);
23572364 }
23582365 IrBasicBlock *after_else_block = irb->current_basic_block;
2359 ir_build_br(irb, node, endif_block, is_inline);
2366 ir_build_br(irb, scope, node, endif_block, is_inline);
23602367
23612368 ir_set_cursor_at_end(irb, endif_block);
23622369 IrInstruction **incoming_values = allocate<IrInstruction *>(2);
......@@ -2366,10 +2373,10 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, AstNode *node) {
23662373 incoming_blocks[0] = after_then_block;
23672374 incoming_blocks[1] = after_else_block;
23682375
2369 return ir_build_phi(irb, node, 2, incoming_blocks, incoming_values);
2376 return ir_build_phi(irb, scope, node, 2, incoming_blocks, incoming_values);
23702377}
23712378
2372static bool ir_gen_switch_prong_expr(IrBuilder *irb, AstNode *switch_node, AstNode *prong_node,
2379static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *switch_node, AstNode *prong_node,
23732380 IrBasicBlock *end_block, bool is_inline, IrInstruction *target_value_ptr, IrInstruction *prong_value,
23742381 ZigList<IrBasicBlock *> *incoming_blocks, ZigList<IrInstruction *> *incoming_values)
23752382{
......@@ -2386,39 +2393,39 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, AstNode *switch_node, AstNo
23862393
23872394 bool is_shadowable = false;
23882395 bool is_const = true;
2389 VariableTableEntry *var = ir_create_var(irb, var_symbol_node, switch_node->scope,
2396 VariableTableEntry *var = ir_create_var(irb, var_symbol_node, scope,
23902397 var_name, is_const, is_const, is_shadowable, is_inline);
23912398 child_scope = var->child_scope;
23922399 IrInstruction *var_value;
23932400 if (prong_value) {
2394 IrInstruction *var_ptr_value = ir_build_switch_var(irb, var_symbol_node, target_value_ptr, prong_value);
2395 var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, var_symbol_node, var_ptr_value);
2401 IrInstruction *var_ptr_value = ir_build_switch_var(irb, scope, var_symbol_node, target_value_ptr, prong_value);
2402 var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, scope, var_symbol_node, var_ptr_value);
23962403 } else {
2397 var_value = var_is_ptr ? target_value_ptr : ir_build_load_ptr(irb, var_symbol_node, target_value_ptr);
2404 var_value = var_is_ptr ? target_value_ptr : ir_build_load_ptr(irb, scope, var_symbol_node, target_value_ptr);
23982405 }
23992406 IrInstruction *var_type = nullptr; // infer the type
2400 ir_build_var_decl(irb, var_symbol_node, var, var_type, var_value);
2407 ir_build_var_decl(irb, scope, var_symbol_node, var, var_type, var_value);
24012408 } else {
2402 child_scope = switch_node->scope;
2409 child_scope = scope;
24032410 }
24042411
24052412 IrInstruction *expr_result = ir_gen_node(irb, expr_node, child_scope);
24062413 if (expr_result == irb->codegen->invalid_instruction)
24072414 return false;
2408 ir_build_br(irb, switch_node, end_block, is_inline);
2415 ir_build_br(irb, scope, switch_node, end_block, is_inline);
24092416 incoming_blocks->append(irb->current_basic_block);
24102417 incoming_values->append(expr_result);
24112418 return true;
24122419}
24132420
2414static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
2421static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *node) {
24152422 assert(node->type == NodeTypeSwitchExpr);
24162423
24172424 AstNode *target_node = node->data.switch_expr.expr;
2418 IrInstruction *target_value_ptr = ir_gen_node_extra(irb, target_node, node->scope, LValPurposeAddressOf);
2425 IrInstruction *target_value_ptr = ir_gen_node_extra(irb, target_node, scope, LValPurposeAddressOf);
24192426 if (target_value_ptr == irb->codegen->invalid_instruction)
24202427 return target_value_ptr;
2421 IrInstruction *target_value = ir_build_switch_target(irb, node, target_value_ptr);
2428 IrInstruction *target_value = ir_build_switch_target(irb, scope, node, target_value_ptr);
24222429
24232430 IrBasicBlock *else_block = ir_build_basic_block(irb, "SwitchElse");
24242431 IrBasicBlock *end_block = ir_build_basic_block(irb, "SwitchEnd");
......@@ -2446,7 +2453,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
24462453
24472454 IrBasicBlock *prev_block = irb->current_basic_block;
24482455 ir_set_cursor_at_end(irb, else_block);
2449 if (!ir_gen_switch_prong_expr(irb, node, prong_node, end_block,
2456 if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
24502457 is_inline, target_value_ptr, nullptr, &incoming_blocks, &incoming_values))
24512458 {
24522459 return irb->codegen->invalid_instruction;
......@@ -2460,41 +2467,40 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
24602467 AstNode *item_node = prong_node->data.switch_prong.items.at(item_i);
24612468 last_item_node = item_node;
24622469 if (item_node->type == NodeTypeSwitchRange) {
2463 item_node->scope = node->scope;
24642470 AstNode *start_node = item_node->data.switch_range.start;
24652471 AstNode *end_node = item_node->data.switch_range.end;
24662472
2467 IrInstruction *start_value = ir_gen_node(irb, start_node, node->scope);
2473 IrInstruction *start_value = ir_gen_node(irb, start_node, scope);
24682474 if (start_value == irb->codegen->invalid_instruction)
24692475 return irb->codegen->invalid_instruction;
24702476
2471 IrInstruction *end_value = ir_gen_node(irb, end_node, node->scope);
2477 IrInstruction *end_value = ir_gen_node(irb, end_node, scope);
24722478 if (end_value == irb->codegen->invalid_instruction)
24732479 return irb->codegen->invalid_instruction;
24742480
2475 IrInstruction *start_value_const = ir_build_static_eval(irb, start_node, start_value);
2476 IrInstruction *end_value_const = ir_build_static_eval(irb, start_node, end_value);
2481 IrInstruction *start_value_const = ir_build_static_eval(irb, scope, start_node, start_value);
2482 IrInstruction *end_value_const = ir_build_static_eval(irb, scope, start_node, end_value);
24772483
2478 IrInstruction *lower_range_ok = ir_build_bin_op(irb, item_node, IrBinOpCmpGreaterOrEq,
2484 IrInstruction *lower_range_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpCmpGreaterOrEq,
24792485 target_value, start_value_const);
2480 IrInstruction *upper_range_ok = ir_build_bin_op(irb, item_node, IrBinOpCmpLessOrEq,
2486 IrInstruction *upper_range_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpCmpLessOrEq,
24812487 target_value, end_value_const);
2482 IrInstruction *both_ok = ir_build_bin_op(irb, item_node, IrBinOpBoolAnd,
2488 IrInstruction *both_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolAnd,
24832489 lower_range_ok, upper_range_ok);
24842490 if (ok_bit) {
2485 ok_bit = ir_build_bin_op(irb, item_node, IrBinOpBoolOr, both_ok, ok_bit);
2491 ok_bit = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolOr, both_ok, ok_bit);
24862492 } else {
24872493 ok_bit = both_ok;
24882494 }
24892495 } else {
2490 IrInstruction *item_value = ir_gen_node(irb, item_node, node->scope);
2496 IrInstruction *item_value = ir_gen_node(irb, item_node, scope);
24912497 if (item_value == irb->codegen->invalid_instruction)
24922498 return irb->codegen->invalid_instruction;
24932499
2494 IrInstruction *cmp_ok = ir_build_bin_op(irb, item_node, IrBinOpCmpEq,
2500 IrInstruction *cmp_ok = ir_build_bin_op(irb, scope, item_node, IrBinOpCmpEq,
24952501 item_value, target_value);
24962502 if (ok_bit) {
2497 ok_bit = ir_build_bin_op(irb, item_node, IrBinOpBoolOr, cmp_ok, ok_bit);
2503 ok_bit = ir_build_bin_op(irb, scope, item_node, IrBinOpBoolOr, cmp_ok, ok_bit);
24982504 } else {
24992505 ok_bit = cmp_ok;
25002506 }
......@@ -2506,10 +2512,10 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
25062512
25072513 assert(ok_bit);
25082514 assert(last_item_node);
2509 ir_build_cond_br(irb, last_item_node, ok_bit, range_block_yes, range_block_no, is_inline);
2515 ir_build_cond_br(irb, scope, last_item_node, ok_bit, range_block_yes, range_block_no, is_inline);
25102516
25112517 ir_set_cursor_at_end(irb, range_block_yes);
2512 if (!ir_gen_switch_prong_expr(irb, node, prong_node, end_block,
2518 if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
25132519 is_inline, target_value_ptr, nullptr, &incoming_blocks, &incoming_values))
25142520 {
25152521 return irb->codegen->invalid_instruction;
......@@ -2524,7 +2530,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
25242530 AstNode *item_node = prong_node->data.switch_prong.items.at(item_i);
25252531 assert(item_node->type != NodeTypeSwitchRange);
25262532
2527 IrInstruction *item_value = ir_gen_node(irb, item_node, node->scope);
2533 IrInstruction *item_value = ir_gen_node(irb, item_node, scope);
25282534 if (item_value == irb->codegen->invalid_instruction)
25292535 return irb->codegen->invalid_instruction;
25302536
......@@ -2538,7 +2544,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
25382544
25392545 IrBasicBlock *prev_block = irb->current_basic_block;
25402546 ir_set_cursor_at_end(irb, prong_block);
2541 if (!ir_gen_switch_prong_expr(irb, node, prong_node, end_block,
2547 if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
25422548 is_inline, target_value_ptr, only_item_value, &incoming_blocks, &incoming_values))
25432549 {
25442550 return irb->codegen->invalid_instruction;
......@@ -2551,19 +2557,19 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
25512557 }
25522558
25532559 if (cases.length == 0) {
2554 ir_build_br(irb, node, else_block, is_inline);
2560 ir_build_br(irb, scope, node, else_block, is_inline);
25552561 } else {
2556 ir_build_switch_br(irb, node, target_value, else_block, cases.length, cases.items, is_inline);
2562 ir_build_switch_br(irb, scope, node, target_value, else_block, cases.length, cases.items, is_inline);
25572563 }
25582564
25592565 if (!else_prong) {
25602566 ir_set_cursor_at_end(irb, else_block);
2561 ir_build_unreachable(irb, node);
2567 ir_build_unreachable(irb, scope, node);
25622568 }
25632569
25642570 ir_set_cursor_at_end(irb, end_block);
25652571 assert(incoming_blocks.length == incoming_values.length);
2566 return ir_build_phi(irb, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
2572 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
25672573}
25682574
25692575static LabelTableEntry *find_label(IrExecutable *exec, Scope *scope, Buf *name) {
......@@ -2589,7 +2595,7 @@ static ScopeBlock *find_block_scope(IrExecutable *exec, Scope *scope) {
25892595 return nullptr;
25902596}
25912597
2592static IrInstruction *ir_gen_label(IrBuilder *irb, AstNode *node) {
2598static IrInstruction *ir_gen_label(IrBuilder *irb, Scope *scope, AstNode *node) {
25932599 assert(node->type == NodeTypeLabel);
25942600
25952601 Buf *label_name = node->data.label.name;
......@@ -2599,35 +2605,38 @@ static IrInstruction *ir_gen_label(IrBuilder *irb, AstNode *node) {
25992605 label->bb = label_block;
26002606 irb->exec->all_labels.append(label);
26012607
2602 LabelTableEntry *existing_label = find_label(irb->exec, node->scope, label_name);
2608 LabelTableEntry *existing_label = find_label(irb->exec, scope, label_name);
26032609 if (existing_label) {
26042610 ErrorMsg *msg = add_node_error(irb->codegen, node,
26052611 buf_sprintf("duplicate label name '%s'", buf_ptr(label_name)));
26062612 add_error_note(irb->codegen, msg, existing_label->decl_node, buf_sprintf("other label here"));
26072613 return irb->codegen->invalid_instruction;
26082614 } else {
2609 ScopeBlock *scope_block = find_block_scope(irb->exec, node->scope);
2615 ScopeBlock *scope_block = find_block_scope(irb->exec, scope);
26102616 scope_block->label_table.put(label_name, label);
26112617 }
26122618
26132619 bool is_inline = ir_should_inline(irb);
2614 ir_build_br(irb, node, label_block, is_inline);
2620 ir_build_br(irb, scope, node, label_block, is_inline);
26152621 ir_set_cursor_at_end(irb, label_block);
2616 return ir_build_const_void(irb, node);
2622 return ir_build_const_void(irb, scope, node);
26172623}
26182624
2619static IrInstruction *ir_gen_goto(IrBuilder *irb, AstNode *node) {
2625static IrInstruction *ir_gen_goto(IrBuilder *irb, Scope *scope, AstNode *node) {
26202626 assert(node->type == NodeTypeGoto);
26212627
26222628 // make a placeholder unreachable statement and a note to come back and
26232629 // replace the instruction with a branch instruction
2624 node->data.goto_expr.bb = irb->current_basic_block;
2625 node->data.goto_expr.instruction_index = irb->current_basic_block->instruction_list.length;
2626 irb->exec->goto_list.append(node);
2627 return ir_build_unreachable(irb, node);
2630 IrGotoItem *goto_item = irb->exec->goto_list.add_one();
2631 goto_item->bb = irb->current_basic_block;
2632 goto_item->instruction_index = irb->current_basic_block->instruction_list.length;
2633 goto_item->source_node = node;
2634 goto_item->scope = scope;
2635
2636 return ir_build_unreachable(irb, scope, node);
26282637}
26292638
2630static IrInstruction *ir_lval_wrap(IrBuilder *irb, IrInstruction *value, LValPurpose lval) {
2639static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LValPurpose lval) {
26312640 if (lval == LValPurposeNone)
26322641 return value;
26332642 if (value == irb->codegen->invalid_instruction)
......@@ -2635,75 +2644,73 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, IrInstruction *value, LValPur
26352644
26362645 // We needed a pointer to a value, but we got a value. So we create
26372646 // an instruction which just makes a const pointer of it.
2638 return ir_build_ref(irb, value->source_node, value);
2647 return ir_build_ref(irb, scope, value->source_node, value);
26392648}
26402649
2641static IrInstruction *ir_gen_type_literal(IrBuilder *irb, AstNode *node) {
2650static IrInstruction *ir_gen_type_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
26422651 assert(node->type == NodeTypeTypeLiteral);
2643 return ir_build_const_type(irb, node, irb->codegen->builtin_types.entry_type);
2652 return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_type);
26442653}
26452654
26462655static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scope,
26472656 LValPurpose lval)
26482657{
26492658 assert(scope);
2650 node->scope = scope;
2651
26522659 switch (node->type) {
26532660 case NodeTypeStructValueField:
26542661 zig_unreachable();
26552662 case NodeTypeBlock:
2656 return ir_lval_wrap(irb, ir_gen_block(irb, node), lval);
2663 return ir_lval_wrap(irb, scope, ir_gen_block(irb, scope, node), lval);
26572664 case NodeTypeBinOpExpr:
2658 return ir_lval_wrap(irb, ir_gen_bin_op(irb, node), lval);
2665 return ir_lval_wrap(irb, scope, ir_gen_bin_op(irb, scope, node), lval);
26592666 case NodeTypeNumberLiteral:
2660 return ir_lval_wrap(irb, ir_gen_num_lit(irb, node), lval);
2667 return ir_lval_wrap(irb, scope, ir_gen_num_lit(irb, scope, node), lval);
26612668 case NodeTypeSymbol:
2662 return ir_gen_symbol(irb, node, lval);
2669 return ir_gen_symbol(irb, scope, node, lval);
26632670 case NodeTypeFnCallExpr:
2664 return ir_lval_wrap(irb, ir_gen_fn_call(irb, node), lval);
2671 return ir_lval_wrap(irb, scope, ir_gen_fn_call(irb, scope, node), lval);
26652672 case NodeTypeIfBoolExpr:
2666 return ir_lval_wrap(irb, ir_gen_if_bool_expr(irb, node), lval);
2673 return ir_lval_wrap(irb, scope, ir_gen_if_bool_expr(irb, scope, node), lval);
26672674 case NodeTypePrefixOpExpr:
2668 return ir_gen_prefix_op_expr(irb, node, lval);
2675 return ir_gen_prefix_op_expr(irb, scope, node, lval);
26692676 case NodeTypeContainerInitExpr:
2670 return ir_lval_wrap(irb, ir_gen_container_init_expr(irb, node), lval);
2677 return ir_lval_wrap(irb, scope, ir_gen_container_init_expr(irb, scope, node), lval);
26712678 case NodeTypeVariableDeclaration:
2672 return ir_lval_wrap(irb, ir_gen_var_decl(irb, node), lval);
2679 return ir_lval_wrap(irb, scope, ir_gen_var_decl(irb, scope, node), lval);
26732680 case NodeTypeWhileExpr:
2674 return ir_lval_wrap(irb, ir_gen_while_expr(irb, node), lval);
2681 return ir_lval_wrap(irb, scope, ir_gen_while_expr(irb, scope, node), lval);
26752682 case NodeTypeForExpr:
2676 return ir_lval_wrap(irb, ir_gen_for_expr(irb, node), lval);
2683 return ir_lval_wrap(irb, scope, ir_gen_for_expr(irb, scope, node), lval);
26772684 case NodeTypeArrayAccessExpr:
2678 return ir_gen_array_access(irb, node, lval);
2685 return ir_gen_array_access(irb, scope, node, lval);
26792686 case NodeTypeReturnExpr:
2680 return ir_lval_wrap(irb, ir_gen_return(irb, node), lval);
2687 return ir_lval_wrap(irb, scope, ir_gen_return(irb, scope, node), lval);
26812688 case NodeTypeFieldAccessExpr:
2682 return ir_gen_field_access(irb, node, lval);
2689 return ir_gen_field_access(irb, scope, node, lval);
26832690 case NodeTypeThisLiteral:
2684 return ir_lval_wrap(irb, ir_gen_this_literal(irb, node), lval);
2691 return ir_lval_wrap(irb, scope, ir_gen_this_literal(irb, scope, node), lval);
26852692 case NodeTypeBoolLiteral:
2686 return ir_lval_wrap(irb, ir_gen_bool_literal(irb, node), lval);
2693 return ir_lval_wrap(irb, scope, ir_gen_bool_literal(irb, scope, node), lval);
26872694 case NodeTypeArrayType:
2688 return ir_lval_wrap(irb, ir_gen_array_type(irb, node), lval);
2695 return ir_lval_wrap(irb, scope, ir_gen_array_type(irb, scope, node), lval);
26892696 case NodeTypeStringLiteral:
2690 return ir_lval_wrap(irb, ir_gen_string_literal(irb, node), lval);
2697 return ir_lval_wrap(irb, scope, ir_gen_string_literal(irb, scope, node), lval);
26912698 case NodeTypeUndefinedLiteral:
2692 return ir_lval_wrap(irb, ir_gen_undefined_literal(irb, node), lval);
2699 return ir_lval_wrap(irb, scope, ir_gen_undefined_literal(irb, scope, node), lval);
26932700 case NodeTypeAsmExpr:
2694 return ir_lval_wrap(irb, ir_gen_asm_expr(irb, node), lval);
2701 return ir_lval_wrap(irb, scope, ir_gen_asm_expr(irb, scope, node), lval);
26952702 case NodeTypeNullLiteral:
2696 return ir_lval_wrap(irb, ir_gen_null_literal(irb, node), lval);
2703 return ir_lval_wrap(irb, scope, ir_gen_null_literal(irb, scope, node), lval);
26972704 case NodeTypeIfVarExpr:
2698 return ir_lval_wrap(irb, ir_gen_if_var_expr(irb, node), lval);
2705 return ir_lval_wrap(irb, scope, ir_gen_if_var_expr(irb, scope, node), lval);
26992706 case NodeTypeSwitchExpr:
2700 return ir_lval_wrap(irb, ir_gen_switch_expr(irb, node), lval);
2707 return ir_lval_wrap(irb, scope, ir_gen_switch_expr(irb, scope, node), lval);
27012708 case NodeTypeLabel:
2702 return ir_lval_wrap(irb, ir_gen_label(irb, node), lval);
2709 return ir_lval_wrap(irb, scope, ir_gen_label(irb, scope, node), lval);
27032710 case NodeTypeGoto:
2704 return ir_lval_wrap(irb, ir_gen_goto(irb, node), lval);
2711 return ir_lval_wrap(irb, scope, ir_gen_goto(irb, scope, node), lval);
27052712 case NodeTypeTypeLiteral:
2706 return ir_lval_wrap(irb, ir_gen_type_literal(irb, node), lval);
2713 return ir_lval_wrap(irb, scope, ir_gen_type_literal(irb, scope, node), lval);
27072714 case NodeTypeUnwrapErrorExpr:
27082715 case NodeTypeDefer:
27092716 case NodeTypeSliceExpr:
......@@ -2744,22 +2751,23 @@ static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope) {
27442751
27452752static bool ir_goto_pass2(IrBuilder *irb) {
27462753 for (size_t i = 0; i < irb->exec->goto_list.length; i += 1) {
2747 AstNode *goto_node = irb->exec->goto_list.at(i);
2748 size_t instruction_index = goto_node->data.goto_expr.instruction_index;
2749 IrInstruction **slot = &goto_node->data.goto_expr.bb->instruction_list.at(instruction_index);
2754 IrGotoItem *goto_item = &irb->exec->goto_list.at(i);
2755 AstNode *source_node = goto_item->source_node;
2756 size_t instruction_index = goto_item->instruction_index;
2757 IrInstruction **slot = &goto_item->bb->instruction_list.at(instruction_index);
27502758 IrInstruction *old_instruction = *slot;
27512759
2752 Buf *label_name = goto_node->data.goto_expr.name;
2753 LabelTableEntry *label = find_label(irb->exec, goto_node->scope, label_name);
2760 Buf *label_name = source_node->data.goto_expr.name;
2761 LabelTableEntry *label = find_label(irb->exec, goto_item->scope, label_name);
27542762 if (!label) {
2755 add_node_error(irb->codegen, goto_node,
2763 add_node_error(irb->codegen, source_node,
27562764 buf_sprintf("no label in scope named '%s'", buf_ptr(label_name)));
27572765 return false;
27582766 }
27592767 label->used = true;
27602768
2761 bool is_inline = ir_should_inline(irb) || goto_node->data.goto_expr.is_inline;
2762 IrInstruction *new_instruction = ir_create_br(irb, goto_node, label->bb, is_inline);
2769 bool is_inline = ir_should_inline(irb) || source_node->data.goto_expr.is_inline;
2770 IrInstruction *new_instruction = ir_create_br(irb, goto_item->scope, source_node, label->bb, is_inline);
27632771 new_instruction->ref_count = old_instruction->ref_count;
27642772 *slot = new_instruction;
27652773 }
......@@ -2795,7 +2803,7 @@ IrInstruction *ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutabl
27952803 if (irb->exec->invalid)
27962804 return codegen->invalid_instruction;
27972805
2798 IrInstruction *return_instruction = ir_build_return(irb, result->source_node, result);
2806 IrInstruction *return_instruction = ir_build_return(irb, scope, result->source_node, result);
27992807 assert(return_instruction);
28002808
28012809 if (!ir_goto_pass2(irb)) {
......@@ -2814,21 +2822,27 @@ IrInstruction *ir_gen_fn(CodeGen *codegn, FnTableEntry *fn_entry) {
28142822 assert(fn_def_node->type == NodeTypeFnDef);
28152823
28162824 AstNode *body_node = fn_def_node->data.fn_def.body;
2817 Scope *child_scope = fn_def_node->data.fn_def.child_scope;
2825
2826 assert(fn_entry->child_scope);
2827 Scope *child_scope = fn_entry->child_scope;
28182828
28192829 return ir_gen(codegn, body_node, child_scope, ir_executable);
28202830}
28212831
2832static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction, Buf *msg) {
2833 return add_node_error(ira->codegen, source_instruction->source_node, msg);
2834}
2835
28222836static IrInstruction *ir_eval_fn(IrAnalyze *ira, IrInstruction *source_instruction,
28232837 size_t arg_count, IrInstruction **args)
28242838{
2839 // TODO count this as part of the backward branch quota
28252840 zig_panic("TODO ir_eval_fn");
28262841}
28272842
28282843static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *source_instruction) {
28292844 if (ir_should_inline(&ira->new_irb)) {
2830 add_node_error(ira->codegen, source_instruction->source_node,
2831 buf_sprintf("unable to evaluate constant expression"));
2845 ir_add_error(ira, source_instruction, buf_sprintf("unable to evaluate constant expression"));
28322846 return false;
28332847 }
28342848 return true;
......@@ -2863,7 +2877,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
28632877
28642878 const char *num_lit_str = (const_val->data.x_bignum.kind == BigNumKindFloat) ? "float" : "integer";
28652879
2866 add_node_error(ira->codegen, instruction->source_node,
2880 ir_add_error(ira, instruction,
28672881 buf_sprintf("%s value %s cannot be implicitly casted to type '%s'",
28682882 num_lit_str,
28692883 buf_ptr(bignum_to_buf(&const_val->data.x_bignum)),
......@@ -3052,15 +3066,15 @@ static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_inst
30523066 TypeTableEntry *wanted_type, CastOp cast_op, bool need_alloca)
30533067{
30543068 if (value->static_value.special != ConstValSpecialRuntime) {
3055 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->source_node, wanted_type, false);
3069 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope, source_instr->source_node, wanted_type, false);
30563070 eval_const_expr_implicit_cast(cast_op, &value->static_value, value->type_entry,
30573071 &result->static_value, wanted_type);
30583072 return result;
30593073 } else {
3060 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->source_node, wanted_type, value, cast_op);
3074 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->scope, source_instr->source_node, wanted_type, value, cast_op);
30613075 result->type_entry = wanted_type;
30623076 if (need_alloca) {
3063 FnTableEntry *fn_entry = scope_fn_entry(source_instr->source_node->scope);
3077 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
30643078 if (fn_entry)
30653079 fn_entry->alloca_list.append(result);
30663080 }
......@@ -3153,20 +3167,20 @@ static ConstExprValue *ir_build_const_from(IrAnalyze *ira, IrInstruction *old_in
31533167 if (old_instruction->id == IrInstructionIdVarPtr) {
31543168 IrInstructionVarPtr *old_var_ptr_instruction = (IrInstructionVarPtr *)old_instruction;
31553169 IrInstructionVarPtr *var_ptr_instruction = ir_create_instruction<IrInstructionVarPtr>(ira->new_irb.exec,
3156 old_instruction->source_node);
3170 old_instruction->scope, old_instruction->source_node);
31573171 var_ptr_instruction->var = old_var_ptr_instruction->var;
31583172 new_instruction = &var_ptr_instruction->base;
31593173 } else if (old_instruction->id == IrInstructionIdFieldPtr) {
31603174 IrInstructionFieldPtr *field_ptr_instruction = ir_create_instruction<IrInstructionFieldPtr>(ira->new_irb.exec,
3161 old_instruction->source_node);
3175 old_instruction->scope, old_instruction->source_node);
31623176 new_instruction = &field_ptr_instruction->base;
31633177 } else if (old_instruction->id == IrInstructionIdElemPtr) {
31643178 IrInstructionElemPtr *elem_ptr_instruction = ir_create_instruction<IrInstructionElemPtr>(ira->new_irb.exec,
3165 old_instruction->source_node);
3179 old_instruction->scope, old_instruction->source_node);
31663180 new_instruction = &elem_ptr_instruction->base;
31673181 } else {
31683182 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
3169 old_instruction->source_node);
3183 old_instruction->scope, old_instruction->source_node);
31703184 new_instruction = &const_instruction->base;
31713185 }
31723186 ir_link_new_instruction(new_instruction, old_instruction);
......@@ -3553,13 +3567,13 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
35533567 if (ptr->static_value.special != ConstValSpecialRuntime) {
35543568 ConstExprValue *pointee = const_ptr_pointee(&ptr->static_value);
35553569 if (pointee->special != ConstValSpecialRuntime) {
3556 IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->source_node,
3557 child_type, pointee->depends_on_compile_var);
3570 IrInstruction *result = ir_create_const(&ira->new_irb, source_instruction->scope,
3571 source_instruction->source_node, child_type, pointee->depends_on_compile_var);
35583572 result->static_value = *pointee;
35593573 return result;
35603574 }
35613575 }
3562 IrInstruction *load_ptr_instruction = ir_build_load_ptr(&ira->new_irb, source_instruction->source_node, ptr);
3576 IrInstruction *load_ptr_instruction = ir_build_load_ptr(&ira->new_irb, source_instruction->scope, source_instruction->source_node, ptr);
35633577 load_ptr_instruction->type_entry = child_type;
35643578 return load_ptr_instruction;
35653579 } else {
......@@ -3588,7 +3602,7 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst
35883602 ir_link_new_instruction(value, source_instruction);
35893603 return ptr_type;
35903604 } else {
3591 FnTableEntry *fn_entry = scope_fn_entry(source_instruction->source_node->scope);
3605 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
35923606 assert(fn_entry);
35933607 IrInstruction *new_instruction = ir_build_ref_from(&ira->new_irb, source_instruction, value);
35943608 fn_entry->alloca_list.append(new_instruction);
......@@ -3754,7 +3768,6 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
37543768 case TypeTableEntryIdTypeDecl:
37553769 case TypeTableEntryIdNamespace:
37563770 case TypeTableEntryIdBlock:
3757 case TypeTableEntryIdGenericFn:
37583771 case TypeTableEntryIdBoundFn:
37593772 if (!is_equality_cmp) {
37603773 add_node_error(ira->codegen, source_node,
......@@ -4057,7 +4070,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
40574070 }
40584071
40594072 AstNodeVariableDeclaration *variable_declaration = &var->decl_node->data.variable_declaration;
4060 bool is_export = (variable_declaration->top_level_decl.visib_mod == VisibModExport);
4073 bool is_export = (variable_declaration->visib_mod == VisibModExport);
40614074 bool is_extern = variable_declaration->is_extern;
40624075
40634076 var->ref_count = 0;
......@@ -4119,7 +4132,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
41194132 case TypeTableEntryIdEnum:
41204133 case TypeTableEntryIdUnion:
41214134 case TypeTableEntryIdFn:
4122 case TypeTableEntryIdGenericFn:
41234135 case TypeTableEntryIdBoundFn:
41244136 // OK
41254137 break;
......@@ -4136,8 +4148,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
41364148
41374149 ir_build_var_decl_from(&ira->new_irb, &decl_var_instruction->base, var, var_type, casted_init_value);
41384150
4139 Scope *scope = decl_var_instruction->base.source_node->scope;
4140 FnTableEntry *fn_entry = scope_fn_entry(scope);
4151 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
41414152 if (fn_entry)
41424153 fn_entry->variable_list.append(var);
41434154
......@@ -4241,7 +4252,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
42414252 fn_entry, fn_ref, call_param_count, casted_args);
42424253
42434254 if (type_has_bits(return_type) && handle_is_ptr(return_type)) {
4244 FnTableEntry *fn_entry = scope_fn_entry(call_instruction->base.source_node->scope);
4255 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
42454256 assert(fn_entry);
42464257 fn_entry->alloca_list.append(new_call_instruction);
42474258 }
......@@ -4288,8 +4299,6 @@ static TypeTableEntry *ir_analyze_instruction_call(IrAnalyze *ira, IrInstruction
42884299 IrInstruction *first_arg_ptr = fn_ref->static_value.data.x_bound_fn.first_arg;
42894300 return ir_analyze_fn_call(ira, call_instruction, fn_table_entry, fn_table_entry->type_entry,
42904301 nullptr, first_arg_ptr, is_inline);
4291 } else if (fn_ref->type_entry->id == TypeTableEntryIdGenericFn) {
4292 zig_panic("TODO generic fn call");
42934302 } else {
42944303 add_node_error(ira->codegen, fn_ref->source_node,
42954304 buf_sprintf("type '%s' not a function", buf_ptr(&fn_ref->type_entry->name)));
......@@ -4360,7 +4369,6 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
43604369 case TypeTableEntryIdEnum:
43614370 case TypeTableEntryIdUnion:
43624371 case TypeTableEntryIdFn:
4363 case TypeTableEntryIdGenericFn:
43644372 case TypeTableEntryIdBoundFn:
43654373 {
43664374 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base,
......@@ -4409,7 +4417,6 @@ static TypeTableEntry *ir_analyze_unary_address_of(IrAnalyze *ira, IrInstruction
44094417 case TypeTableEntryIdBlock:
44104418 case TypeTableEntryIdUnreachable:
44114419 case TypeTableEntryIdVar:
4412 case TypeTableEntryIdGenericFn:
44134420 case TypeTableEntryIdBoundFn:
44144421 add_node_error(ira->codegen, un_op_instruction->base.source_node,
44154422 buf_sprintf("unable to get address of type '%s'", buf_ptr(&target_type->name)));
......@@ -4508,7 +4515,6 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op
45084515 case TypeTableEntryIdFn:
45094516 case TypeTableEntryIdNamespace:
45104517 case TypeTableEntryIdBlock:
4511 case TypeTableEntryIdGenericFn:
45124518 case TypeTableEntryIdBoundFn:
45134519 {
45144520 ConstExprValue *out_val = ir_build_const_from(ira, &un_op_instruction->base,
......@@ -4781,9 +4787,7 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc
47814787 if (var->mem_slot_index != SIZE_MAX)
47824788 mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
47834789 } else if (var->src_is_const) {
4784 AstNode *var_decl_node = var->decl_node;
4785 assert(var_decl_node->type == NodeTypeVariableDeclaration);
4786 mem_slot = &var_decl_node->data.variable_declaration.top_level_decl.value->static_value;
4790 mem_slot = var->value;
47874791 assert(mem_slot->special != ConstValSpecialRuntime);
47884792 }
47894793
......@@ -4925,15 +4929,15 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira,
49254929 if (!is_slice(bare_struct_type)) {
49264930 ScopeDecls *container_scope = get_container_scope(bare_struct_type);
49274931 auto entry = container_scope->decl_table.maybe_get(field_name);
4928 AstNode *fn_decl_node = entry ? entry->value : nullptr;
4929 if (fn_decl_node && fn_decl_node->type == NodeTypeFnProto) {
4930 resolve_top_level_decl(ira->codegen, fn_decl_node, false);
4931 TopLevelDecl *tld = get_as_top_level_decl(fn_decl_node);
4932 Tld *tld = entry ? entry->value : nullptr;
4933 if (tld && tld->id == TldIdFn) {
4934 resolve_top_level_decl(ira->codegen, tld, false);
49324935 if (tld->resolution == TldResolutionInvalid)
49334936 return ira->codegen->builtin_types.entry_invalid;
4934 FnTableEntry *fn_entry = fn_decl_node->data.fn_proto.fn_table_entry;
4937 TldFn *tld_fn = (TldFn *)tld;
4938 FnTableEntry *fn_entry = tld_fn->fn_entry;
49354939 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;
4936 IrInstruction *bound_fn_value = ir_build_const_bound_fn(&ira->new_irb,
4940 IrInstruction *bound_fn_value = ir_build_const_bound_fn(&ira->new_irb, field_ptr_instruction->base.scope,
49374941 field_ptr_instruction->base.source_node, fn_entry, container_ptr, depends_on_compile_var);
49384942 return ir_analyze_ref(ira, &field_ptr_instruction->base, bound_fn_value);
49394943 }
......@@ -4976,53 +4980,63 @@ static TypeTableEntry *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field
49764980 }
49774981}
49784982
4979static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, AstNode *decl_node,
4983static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld,
49804984 bool depends_on_compile_var)
49814985{
49824986 bool pointer_only = false;
4983 resolve_top_level_decl(ira->codegen, decl_node, pointer_only);
4984 TopLevelDecl *tld = get_as_top_level_decl(decl_node);
4987 resolve_top_level_decl(ira->codegen, tld, pointer_only);
49854988 if (tld->resolution == TldResolutionInvalid)
49864989 return ira->codegen->builtin_types.entry_invalid;
49874990
4988 if (decl_node->type == NodeTypeVariableDeclaration) {
4989 VariableTableEntry *var = decl_node->data.variable_declaration.variable;
4990 return ir_analyze_var_ptr(ira, source_instruction, var);
4991 } else if (decl_node->type == NodeTypeFnProto) {
4992 FnTableEntry *fn_entry = decl_node->data.fn_proto.fn_table_entry;
4993 assert(fn_entry->type_entry);
4994
4995 // TODO instead of allocating this every time, put it in the tld value and we can reference
4996 // the same one every time
4997 ConstExprValue *const_val = allocate<ConstExprValue>(1);
4998 const_val->special = ConstValSpecialStatic;
4999 if (fn_entry->type_entry->id == TypeTableEntryIdGenericFn) {
5000 const_val->data.x_type = fn_entry->type_entry;
5001 } else {
4991 switch (tld->id) {
4992 case TldIdVar:
4993 {
4994 TldVar *tld_var = (TldVar *)tld;
4995 VariableTableEntry *var = tld_var->var;
4996 return ir_analyze_var_ptr(ira, source_instruction, var);
4997 }
4998 case TldIdFn:
4999 {
5000 TldFn *tld_fn = (TldFn *)tld;
5001 FnTableEntry *fn_entry = tld_fn->fn_entry;
5002 assert(fn_entry->type_entry);
5003
5004 // TODO instead of allocating this every time, put it in the tld value and we can reference
5005 // the same one every time
5006 ConstExprValue *const_val = allocate<ConstExprValue>(1);
5007 const_val->special = ConstValSpecialStatic;
50025008 const_val->data.x_fn = fn_entry;
5009
5010 return ir_analyze_const_ptr(ira, source_instruction, const_val, fn_entry->type_entry, depends_on_compile_var);
50035011 }
5012 case TldIdContainer:
5013 {
5014 TldContainer *tld_container = (TldContainer *)tld;
5015 assert(tld_container->type_entry);
50045016
5005 return ir_analyze_const_ptr(ira, source_instruction, const_val, fn_entry->type_entry, depends_on_compile_var);
5006 } else if (decl_node->type == NodeTypeContainerDecl) {
5007 zig_panic("TODO");
5008 //ConstExprValue *out_val = ir_build_const_from(ira, source_instruction, depends_on_compile_var);
5009 //if (decl_node->data.struct_decl.generic_params.length > 0) {
5010 // TypeTableEntry *type_entry = decl_node->data.struct_decl.generic_fn_type;
5011 // assert(type_entry);
5012 // out_val->data.x_type = type_entry;
5013 // return type_entry;
5014 //} else {
5015 // out_val->data.x_type = decl_node->data.struct_decl.type_entry;
5016 // return ira->codegen->builtin_types.entry_type;
5017 //}
5018 } else if (decl_node->type == NodeTypeTypeDecl) {
5019 zig_panic("TODO");
5020 //ConstExprValue *out_val = ir_build_const_from(ira, source_instruction, depends_on_compile_var);
5021 //out_val->data.x_type = decl_node->data.type_decl.child_type_entry;
5022 //return ira->codegen->builtin_types.entry_type;
5023 } else {
5024 zig_unreachable();
5017 // TODO instead of allocating this every time, put it in the tld value and we can reference
5018 // the same one every time
5019 ConstExprValue *const_val = allocate<ConstExprValue>(1);
5020 const_val->special = ConstValSpecialStatic;
5021 const_val->data.x_type = tld_container->type_entry;
5022
5023 return ir_analyze_const_ptr(ira, source_instruction, const_val, tld_container->type_entry, depends_on_compile_var);
5024 }
5025 case TldIdTypeDef:
5026 {
5027 TldTypeDef *tld_typedef = (TldTypeDef *)tld;
5028 assert(tld_typedef->type_entry);
5029
5030 // TODO instead of allocating this every time, put it in the tld value and we can reference
5031 // the same one every time
5032 ConstExprValue *const_val = allocate<ConstExprValue>(1);
5033 const_val->special = ConstValSpecialStatic;
5034 const_val->data.x_type = tld_typedef->type_entry;
5035
5036 return ir_analyze_const_ptr(ira, source_instruction, const_val, tld_typedef->type_entry, depends_on_compile_var);
5037 }
50255038 }
5039 zig_unreachable();
50265040}
50275041
50285042static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstructionFieldPtr *field_ptr_instruction) {
......@@ -5068,10 +5082,10 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
50685082 } else if (child_type->id == TypeTableEntryIdStruct) {
50695083 ScopeDecls *container_scope = get_container_scope(child_type);
50705084 auto entry = container_scope->decl_table.maybe_get(field_name);
5071 AstNode *decl_node = entry ? entry->value : nullptr;
5072 if (decl_node) {
5085 Tld *tld = entry ? entry->value : nullptr;
5086 if (tld) {
50735087 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;
5074 return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, decl_node, depends_on_compile_var);
5088 return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld, depends_on_compile_var);
50755089 } else {
50765090 add_node_error(ira->codegen, source_node,
50775091 buf_sprintf("container '%s' has no member called '%s'",
......@@ -5098,30 +5112,29 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
50985112 ImportTableEntry *namespace_import = namespace_val->data.x_import;
50995113
51005114 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;
5101 AstNode *decl_node = find_decl(&namespace_import->decls_scope->base, field_name);
5102 if (!decl_node) {
5115 Tld *tld = find_decl(&namespace_import->decls_scope->base, field_name);
5116 if (!tld) {
51035117 // we must now resolve all the use decls
5118 // TODO move this check to find_decl?
51045119 for (size_t i = 0; i < namespace_import->use_decls.length; i += 1) {
51055120 AstNode *use_decl_node = namespace_import->use_decls.at(i);
5106 TopLevelDecl *tld = get_as_top_level_decl(use_decl_node);
5107 if (tld->resolution == TldResolutionUnresolved) {
5121 if (use_decl_node->data.use.resolution == TldResolutionUnresolved) {
51085122 preview_use_decl(ira->codegen, use_decl_node);
51095123 }
51105124 resolve_use_decl(ira->codegen, use_decl_node);
51115125 }
5112 decl_node = find_decl(&namespace_import->decls_scope->base, field_name);
5126 tld = find_decl(&namespace_import->decls_scope->base, field_name);
51135127 }
5114 if (decl_node) {
5115 TopLevelDecl *tld = get_as_top_level_decl(decl_node);
5128 if (tld) {
51165129 if (tld->visib_mod == VisibModPrivate &&
5117 decl_node->owner != source_node->owner)
5130 tld->import != source_node->owner)
51185131 {
51195132 ErrorMsg *msg = add_node_error(ira->codegen, source_node,
51205133 buf_sprintf("'%s' is private", buf_ptr(field_name)));
5121 add_error_note(ira->codegen, msg, decl_node, buf_sprintf("declared here"));
5134 add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
51225135 return ira->codegen->builtin_types.entry_invalid;
51235136 }
5124 return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, decl_node, depends_on_compile_var);
5137 return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld, depends_on_compile_var);
51255138 } else {
51265139 const char *import_name = namespace_import->path ? buf_ptr(namespace_import->path) : "(C import)";
51275140 add_node_error(ira->codegen, source_node,
......@@ -5176,7 +5189,8 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
51765189 if (ptr->id == IrInstructionIdVarPtr) {
51775190 IrInstructionVarPtr *var_ptr_inst = (IrInstructionVarPtr *)ptr;
51785191 VariableTableEntry *var = var_ptr_inst->var;
5179 new_ptr_inst = ir_build_var_ptr(&ira->new_irb, store_ptr_instruction->base.source_node, var);
5192 new_ptr_inst = ir_build_var_ptr(&ira->new_irb, store_ptr_instruction->base.scope,
5193 store_ptr_instruction->base.source_node, var);
51805194 assert(var->mem_slot_index != SIZE_MAX);
51815195 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
51825196 mem_slot->special = ConstValSpecialRuntime;
......@@ -5188,7 +5202,8 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
51885202 zig_unreachable();
51895203 }
51905204 new_ptr_inst->type_entry = ptr->type_entry;
5191 ir_build_store_ptr(&ira->new_irb, store_ptr_instruction->base.source_node, new_ptr_inst, casted_value);
5205 ir_build_store_ptr(&ira->new_irb, store_ptr_instruction->base.scope,
5206 store_ptr_instruction->base.source_node, new_ptr_inst, casted_value);
51925207 return ir_analyze_void(ira, &store_ptr_instruction->base);
51935208 }
51945209
......@@ -5212,7 +5227,6 @@ static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructi
52125227 case TypeTableEntryIdNullLit:
52135228 case TypeTableEntryIdNamespace:
52145229 case TypeTableEntryIdBlock:
5215 case TypeTableEntryIdGenericFn:
52165230 case TypeTableEntryIdBoundFn:
52175231 case TypeTableEntryIdMetaType:
52185232 case TypeTableEntryIdVoid:
......@@ -5342,14 +5356,13 @@ static TypeTableEntry *ir_analyze_instruction_set_fn_visible(IrAnalyze *ira,
53425356 fn_entry->fn_export_set_node = source_node;
53435357
53445358 AstNodeFnProto *fn_proto = &fn_entry->proto_node->data.fn_proto;
5345 if (fn_proto->top_level_decl.visib_mod != VisibModExport) {
5359 if (fn_proto->visib_mod != VisibModExport) {
53465360 ErrorMsg *msg = add_node_error(ira->codegen, source_node,
53475361 buf_sprintf("function must be marked export to set function visibility"));
53485362 add_error_note(ira->codegen, msg, fn_entry->proto_node, buf_sprintf("function declared here"));
53495363 return ira->codegen->builtin_types.entry_invalid;
53505364 }
5351 if (!want_export)
5352 LLVMSetLinkage(fn_entry->fn_value, LLVMInternalLinkage);
5365 fn_entry->internal_linkage = !want_export;
53535366
53545367 ir_build_const_from(ira, &set_fn_visible_instruction->base, false);
53555368 return ira->codegen->builtin_types.entry_void;
......@@ -5366,24 +5379,33 @@ static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira,
53665379 if (!target_val)
53675380 return ira->codegen->builtin_types.entry_invalid;
53685381
5369 Scope *target_context;
5382 bool *safety_off_ptr;
5383 AstNode **safety_set_node_ptr;
53705384 if (target_type->id == TypeTableEntryIdBlock) {
5371 target_context = target_val->data.x_block;
5385 ScopeBlock *block_scope = (ScopeBlock *)target_val->data.x_block;
5386 safety_off_ptr = &block_scope->safety_off;
5387 safety_set_node_ptr = &block_scope->safety_set_node;
53725388 } else if (target_type->id == TypeTableEntryIdFn) {
5373 target_context = target_val->data.x_fn->fn_def_node->data.fn_def.child_scope;
5389 FnTableEntry *target_fn = target_val->data.x_fn;
5390 assert(target_fn->def_scope);
5391 safety_off_ptr = &target_fn->def_scope->safety_off;
5392 safety_set_node_ptr = &target_fn->def_scope->safety_set_node;
53745393 } else if (target_type->id == TypeTableEntryIdMetaType) {
5394 ScopeDecls *decls_scope;
53755395 TypeTableEntry *type_arg = target_val->data.x_type;
53765396 if (type_arg->id == TypeTableEntryIdStruct) {
5377 target_context = &type_arg->data.structure.decls_scope->base;
5397 decls_scope = type_arg->data.structure.decls_scope;
53785398 } else if (type_arg->id == TypeTableEntryIdEnum) {
5379 target_context = &type_arg->data.enumeration.decls_scope->base;
5399 decls_scope = type_arg->data.enumeration.decls_scope;
53805400 } else if (type_arg->id == TypeTableEntryIdUnion) {
5381 target_context = &type_arg->data.unionation.decls_scope->base;
5401 decls_scope = type_arg->data.unionation.decls_scope;
53825402 } else {
53835403 add_node_error(ira->codegen, target_instruction->source_node,
53845404 buf_sprintf("expected scope reference, found type '%s'", buf_ptr(&type_arg->name)));
53855405 return ira->codegen->builtin_types.entry_invalid;
53865406 }
5407 safety_off_ptr = &decls_scope->safety_off;
5408 safety_set_node_ptr = &decls_scope->safety_set_node;
53875409 } else {
53885410 add_node_error(ira->codegen, target_instruction->source_node,
53895411 buf_sprintf("expected scope reference, found type '%s'", buf_ptr(&target_type->name)));
......@@ -5396,14 +5418,14 @@ static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira,
53965418 return ira->codegen->builtin_types.entry_invalid;
53975419
53985420 AstNode *source_node = set_debug_safety_instruction->base.source_node;
5399 if (target_context->safety_set_node) {
5421 if (*safety_set_node_ptr) {
54005422 ErrorMsg *msg = add_node_error(ira->codegen, source_node,
54015423 buf_sprintf("function test attribute set twice"));
5402 add_error_note(ira->codegen, msg, target_context->safety_set_node, buf_sprintf("first set here"));
5424 add_error_note(ira->codegen, msg, *safety_set_node_ptr, buf_sprintf("first set here"));
54035425 return ira->codegen->builtin_types.entry_invalid;
54045426 }
5405 target_context->safety_set_node = source_node;
5406 target_context->safety_off = !want_debug_safety;
5427 *safety_set_node_ptr = source_node;
5428 *safety_off_ptr = !want_debug_safety;
54075429
54085430 ir_build_const_from(ira, &set_debug_safety_instruction->base, false);
54095431 return ira->codegen->builtin_types.entry_void;
......@@ -5450,7 +5472,6 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
54505472 case TypeTableEntryIdUnion:
54515473 case TypeTableEntryIdFn:
54525474 case TypeTableEntryIdNamespace:
5453 case TypeTableEntryIdGenericFn:
54545475 case TypeTableEntryIdBoundFn:
54555476 {
54565477 TypeTableEntry *result_type = get_slice_type(ira->codegen, resolved_child_type, is_const);
......@@ -5494,7 +5515,7 @@ static TypeTableEntry *ir_analyze_instruction_asm(IrAnalyze *ira, IrInstructionA
54945515 }
54955516
54965517 ir_build_asm_from(&ira->new_irb, &asm_instruction->base, input_list, output_types,
5497 asm_instruction->return_count, asm_instruction->has_side_effects);
5518 asm_instruction->output_vars, asm_instruction->return_count, asm_instruction->has_side_effects);
54985519 return return_type;
54995520}
55005521
......@@ -5540,7 +5561,6 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
55405561 case TypeTableEntryIdUnion:
55415562 case TypeTableEntryIdFn:
55425563 case TypeTableEntryIdNamespace:
5543 case TypeTableEntryIdGenericFn:
55445564 case TypeTableEntryIdBoundFn:
55455565 {
55465566 TypeTableEntry *result_type = get_array_type(ira->codegen, child_type, size);
......@@ -5611,7 +5631,6 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
56115631 case TypeTableEntryIdBlock:
56125632 case TypeTableEntryIdNumLitFloat:
56135633 case TypeTableEntryIdNumLitInt:
5614 case TypeTableEntryIdGenericFn:
56155634 case TypeTableEntryIdBoundFn:
56165635 case TypeTableEntryIdMetaType:
56175636 case TypeTableEntryIdFn:
......@@ -5905,7 +5924,6 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
59055924 case TypeTableEntryIdMaybe:
59065925 case TypeTableEntryIdUnion:
59075926 case TypeTableEntryIdBlock:
5908 case TypeTableEntryIdGenericFn:
59095927 case TypeTableEntryIdBoundFn:
59105928 add_node_error(ira->codegen, switch_target_instruction->base.source_node,
59115929 buf_sprintf("invalid switch target type '%s'", buf_ptr(&target_type->name)));
......@@ -6009,7 +6027,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi
60096027 ImportTableEntry *target_import = add_source_file(ira->codegen, target_package,
60106028 abs_full_path, search_dir, import_target_path, import_code);
60116029
6012 scan_decls(ira->codegen, target_import, target_import->decls_scope, target_import->root);
6030 scan_decls(ira->codegen, target_import, target_import->decls_scope, target_import->root, nullptr);
60136031
60146032 ConstExprValue *out_val = ir_build_const_from(ira, &import_instruction->base, depends_on_compile_var);
60156033 out_val->data.x_import = target_import;
......@@ -6064,7 +6082,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
60646082
60656083 IrInstructionStructInitField *new_fields = allocate<IrInstructionStructInitField>(actual_field_count);
60666084
6067 FnTableEntry *fn_entry = scope_fn_entry(instruction->source_node->scope);
6085 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
60686086 bool outside_fn = (fn_entry == nullptr);
60696087
60706088 ConstExprValue const_val = {};
......@@ -6167,7 +6185,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
61676185 const_val.data.x_array.elements = allocate<ConstExprValue>(elem_count);
61686186 const_val.data.x_array.size = elem_count;
61696187
6170 FnTableEntry *fn_entry = scope_fn_entry(instruction->base.source_node->scope);
6188 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
61716189 bool outside_fn = (fn_entry == nullptr);
61726190
61736191 IrInstruction **new_items = allocate<IrInstruction *>(elem_count);
src/ir_print.cpp-8
......@@ -140,14 +140,6 @@ static void ir_print_const_value(IrPrint *irp, TypeTableEntry *type_entry, Const
140140 fprintf(irp->f, "(namespace: %s)", buf_ptr(import->path));
141141 return;
142142 }
143 case TypeTableEntryIdGenericFn:
144 {
145 TypeTableEntry *type_entry = const_val->data.x_type;
146 AstNode *decl_node = type_entry->data.generic_fn.decl_node;
147 assert(decl_node->type == NodeTypeFnProto);
148 fprintf(irp->f, "%s", buf_ptr(decl_node->data.fn_proto.name));
149 return;
150 }
151143 case TypeTableEntryIdBoundFn:
152144 {
153145 FnTableEntry *fn_entry = const_val->data.x_bound_fn.fn;
src/parseh.cpp+33-66
......@@ -121,7 +121,7 @@ static AstNode *create_typed_var_decl_node(Context *c, bool is_const, const char
121121 AstNode *node = create_node(c, NodeTypeVariableDeclaration);
122122 node->data.variable_declaration.symbol = buf_create_from_str(var_name);
123123 node->data.variable_declaration.is_const = is_const;
124 node->data.variable_declaration.top_level_decl.visib_mod = c->visib_mod;
124 node->data.variable_declaration.visib_mod = c->visib_mod;
125125 node->data.variable_declaration.expr = init_node;
126126 node->data.variable_declaration.type = type_node;
127127 return node;
......@@ -139,16 +139,6 @@ static AstNode *create_prefix_node(Context *c, PrefixOp op, AstNode *child_node)
139139 return node;
140140}
141141
142static AstNode *create_struct_field_node(Context *c, const char *name, AstNode *type_node) {
143 assert(type_node);
144 AstNode *node = create_node(c, NodeTypeStructField);
145 node->data.struct_field.name = buf_create_from_str(name);
146 node->data.struct_field.top_level_decl.visib_mod = VisibModPub;
147 node->data.struct_field.type = type_node;
148
149 return node;
150}
151
152142static AstNode *create_param_decl_node(Context *c, const char *name, AstNode *type_node, bool is_noalias) {
153143 assert(type_node);
154144 AstNode *node = create_node(c, NodeTypeParamDecl);
......@@ -214,15 +204,6 @@ static AstNode *create_num_lit_signed(Context *c, int64_t x) {
214204 return create_prefix_node(c, PrefixOpNegation, num_lit_node);
215205}
216206
217static AstNode *create_type_decl_node(Context *c, const char *name, AstNode *child_type_node) {
218 AstNode *node = create_node(c, NodeTypeTypeDecl);
219 node->data.type_decl.symbol = buf_create_from_str(name);
220 node->data.type_decl.top_level_decl.visib_mod = c->visib_mod;
221 node->data.type_decl.child_type = child_type_node;
222
223 return node;
224}
225
226207static AstNode *make_type_node(Context *c, TypeTableEntry *type_entry) {
227208 zig_panic("TODO bypass AST in parseh");
228209}
......@@ -231,7 +212,7 @@ static AstNode *create_fn_proto_node(Context *c, Buf *name, TypeTableEntry *fn_t
231212 assert(fn_type->id == TypeTableEntryIdFn);
232213 AstNode *node = create_node(c, NodeTypeFnProto);
233214 node->data.fn_proto.is_inline = true;
234 node->data.fn_proto.top_level_decl.visib_mod = c->visib_mod;
215 node->data.fn_proto.visib_mod = c->visib_mod;
235216 node->data.fn_proto.name = name;
236217 node->data.fn_proto.return_type = make_type_node(c, fn_type->data.fn.fn_type_id.return_type);
237218
......@@ -280,25 +261,40 @@ static const char *decl_name(const Decl *decl) {
280261 return (const char *)named_decl->getName().bytes_begin();
281262}
282263
283static AstNode *add_typedef_node(Context *c, TypeTableEntry *type_decl) {
264static void add_typedef_node(Context *c, TypeTableEntry *type_decl) {
284265 assert(type_decl);
285266 assert(type_decl->id == TypeTableEntryIdTypeDecl);
286267
287 AstNode *node = create_type_decl_node(c, buf_ptr(&type_decl->name),
288 make_type_node(c, type_decl->data.type_decl.child_type));
289 node->data.type_decl.override_type = type_decl;
268 ScopeDecls *decls_scope = c->import->decls_scope;
269 TldTypeDef *tld_typedef = allocate<TldTypeDef>(1);
270 init_tld(&tld_typedef->base, TldIdTypeDef, &type_decl->name, c->visib_mod, c->source_node, &decls_scope->base, nullptr);
271 tld_typedef->type_entry = type_decl;
290272
273 decls_scope->decl_table.put(&type_decl->name, &tld_typedef->base);
291274 c->global_type_table.put(&type_decl->name, type_decl);
292 c->root->data.root.top_level_decls.append(node);
293 return node;
294275}
295276
296static AstNode *add_const_var_node(Context *c, Buf *name, TypeTableEntry *type_entry) {
297 AstNode *node = create_var_decl_node(c, buf_ptr(name), make_type_node(c, type_entry));
298
277static void add_const_var_node(Context *c, Buf *name, TypeTableEntry *type_entry) {
278 ScopeDecls *decls_scope = c->import->decls_scope;
279 TldVar *tld_var = allocate<TldVar>(1);
280 init_tld(&tld_var->base, TldIdVar, name, c->visib_mod, c->source_node, &decls_scope->base, nullptr);
281 bool is_const = true;
282 ConstExprValue *init_value = allocate<ConstExprValue>(1);
283 init_value->special = ConstValSpecialStatic;
284 init_value->data.x_type = type_entry;
285 tld_var->var = add_variable(c->codegen, c->source_node, &decls_scope->base, name, type_entry, is_const, init_value);
286
287 decls_scope->decl_table.put(name, &tld_var->base);
299288 c->global_type_table.put(name, type_entry);
300 c->root->data.root.top_level_decls.append(node);
301 return node;
289}
290
291static void add_container_tld(Context *c, TypeTableEntry *type_entry) {
292 ScopeDecls *decls_scope = c->import->decls_scope;
293 TldContainer *tld_container = allocate<TldContainer>(1);
294 init_tld(&tld_container->base, TldIdContainer, &type_entry->name, c->visib_mod, c->source_node, &decls_scope->base, nullptr);
295 tld_container->type_entry = type_entry;
296
297 decls_scope->decl_table.put(&type_entry->name, &tld_container->base);
302298}
303299
304300static AstNode *create_ap_num_lit_node(Context *c, const Decl *source_decl,
......@@ -617,7 +613,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const
617613 param_info->is_noalias = qt.isRestrictQualified();
618614 }
619615
620 return get_fn_type(c->codegen, &fn_type_id, true);
616 return get_fn_type(c->codegen, &fn_type_id);
621617 }
622618 case Type::Record:
623619 {
......@@ -724,7 +720,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
724720 node->data.fn_proto.name = fn_name;
725721
726722 node->data.fn_proto.is_extern = fn_type->data.fn.fn_type_id.is_extern;
727 node->data.fn_proto.top_level_decl.visib_mod = c->visib_mod;
723 node->data.fn_proto.visib_mod = c->visib_mod;
728724 node->data.fn_proto.is_var_args = fn_type->data.fn.fn_type_id.is_var_args;
729725 node->data.fn_proto.return_type = make_type_node(c, fn_type->data.fn.fn_type_id.return_type);
730726
......@@ -939,9 +935,8 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
939935static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {
940936 TypeTableEntry *enum_type = resolve_enum_decl(c, enum_decl);
941937
942 if (enum_type->id == TypeTableEntryIdInvalid) {
938 if (enum_type->id == TypeTableEntryIdInvalid)
943939 return;
944 }
945940
946941 // make an alias without the "enum_" prefix. this will get emitted at the
947942 // end if it doesn't conflict with anything else
......@@ -951,21 +946,7 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {
951946
952947 if (enum_type->id == TypeTableEntryIdEnum) {
953948 if (enum_type->data.enumeration.complete) {
954 // now create top level decl for the type
955 AstNode *enum_node = create_node(c, NodeTypeContainerDecl);
956 enum_node->data.struct_decl.name = &enum_type->name;
957 enum_node->data.struct_decl.kind = ContainerKindEnum;
958 enum_node->data.struct_decl.top_level_decl.visib_mod = VisibModExport;
959 enum_node->data.struct_decl.type_entry = enum_type;
960
961 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
962 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i];
963 AstNode *type_node = make_type_node(c, type_enum_field->type_entry);
964 AstNode *field_node = create_struct_field_node(c, buf_ptr(type_enum_field->name), type_node);
965 enum_node->data.struct_decl.fields.append(field_node);
966 }
967
968 c->root->data.root.top_level_decls.append(enum_node);
949 add_container_tld(c, enum_type);
969950 } else {
970951 TypeTableEntry *typedecl_type = get_typedecl_type(c->codegen, buf_ptr(&enum_type->name),
971952 c->codegen->builtin_types.entry_u8);
......@@ -1127,21 +1108,7 @@ static void visit_record_decl(Context *c, const RecordDecl *record_decl) {
11271108 }
11281109
11291110 if (struct_type->data.structure.complete) {
1130 // now create a top level decl node for the type
1131 AstNode *struct_node = create_node(c, NodeTypeContainerDecl);
1132 struct_node->data.struct_decl.name = &struct_type->name;
1133 struct_node->data.struct_decl.kind = ContainerKindStruct;
1134 struct_node->data.struct_decl.top_level_decl.visib_mod = VisibModExport;
1135 struct_node->data.struct_decl.type_entry = struct_type;
1136
1137 for (uint32_t i = 0; i < struct_type->data.structure.src_field_count; i += 1) {
1138 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
1139 AstNode *type_node = make_type_node(c, type_struct_field->type_entry);
1140 AstNode *field_node = create_struct_field_node(c, buf_ptr(type_struct_field->name), type_node);
1141 struct_node->data.struct_decl.fields.append(field_node);
1142 }
1143
1144 c->root->data.root.top_level_decls.append(struct_node);
1111 add_container_tld(c, struct_type);
11451112 } else {
11461113 TypeTableEntry *typedecl_type = get_typedecl_type(c->codegen, buf_ptr(&struct_type->name),
11471114 c->codegen->builtin_types.entry_u8);
src/parser.cpp+8-7
......@@ -1501,7 +1501,7 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *to
15011501
15021502 node->data.variable_declaration.is_inline = is_inline;
15031503 node->data.variable_declaration.is_const = is_const;
1504 node->data.variable_declaration.top_level_decl.visib_mod = visib_mod;
1504 node->data.variable_declaration.visib_mod = visib_mod;
15051505
15061506 Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol);
15071507 node->data.variable_declaration.symbol = token_buf(name_token);
......@@ -2079,7 +2079,7 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m
20792079 }
20802080
20812081 AstNode *node = ast_create_node(pc, NodeTypeFnProto, fn_token);
2082 node->data.fn_proto.top_level_decl.visib_mod = visib_mod;
2082 node->data.fn_proto.visib_mod = visib_mod;
20832083 node->data.fn_proto.is_coldcc = is_coldcc;
20842084 node->data.fn_proto.is_nakedcc = is_nakedcc;
20852085
......@@ -2144,6 +2144,7 @@ static AstNode *ast_parse_fn_def(ParseContext *pc, size_t *token_index, bool man
21442144 AstNode *node = ast_create_node(pc, NodeTypeFnDef, first_token);
21452145 node->data.fn_def.fn_proto = fn_proto;
21462146 node->data.fn_def.body = ast_parse_block(pc, token_index, true);
2147 fn_proto->data.fn_proto.fn_def_node = node;
21472148 return node;
21482149}
21492150
......@@ -2193,7 +2194,7 @@ static AstNode *ast_parse_use(ParseContext *pc, size_t *token_index, VisibMod vi
21932194 *token_index += 1;
21942195
21952196 AstNode *node = ast_create_node(pc, NodeTypeUse, use_kw);
2196 node->data.use.top_level_decl.visib_mod = visib_mod;
2197 node->data.use.visib_mod = visib_mod;
21972198 node->data.use.expr = ast_parse_expression(pc, token_index, true);
21982199
21992200 ast_eat_token(pc, token_index, TokenIdSemicolon);
......@@ -2227,7 +2228,7 @@ static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index,
22272228 AstNode *node = ast_create_node(pc, NodeTypeContainerDecl, first_token);
22282229 node->data.struct_decl.kind = kind;
22292230 node->data.struct_decl.name = token_buf(struct_name);
2230 node->data.struct_decl.top_level_decl.visib_mod = visib_mod;
2231 node->data.struct_decl.visib_mod = visib_mod;
22312232
22322233 Token *paren_or_brace = &pc->tokens->at(*token_index);
22332234 if (paren_or_brace->id == TokenIdLParen) {
......@@ -2281,7 +2282,7 @@ static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index,
22812282 AstNode *field_node = ast_create_node(pc, NodeTypeStructField, token);
22822283 *token_index += 1;
22832284
2284 field_node->data.struct_field.top_level_decl.visib_mod = visib_mod;
2285 field_node->data.struct_field.visib_mod = visib_mod;
22852286 field_node->data.struct_field.name = token_buf(token);
22862287
22872288 Token *expr_or_comma = &pc->tokens->at(*token_index);
......@@ -2318,7 +2319,7 @@ static AstNode *ast_parse_error_value_decl(ParseContext *pc, size_t *token_index
23182319 ast_eat_token(pc, token_index, TokenIdSemicolon);
23192320
23202321 AstNode *node = ast_create_node(pc, NodeTypeErrorValueDecl, first_token);
2321 node->data.error_value_decl.top_level_decl.visib_mod = visib_mod;
2322 node->data.error_value_decl.visib_mod = visib_mod;
23222323 node->data.error_value_decl.name = token_buf(name_tok);
23232324
23242325 return node;
......@@ -2344,7 +2345,7 @@ static AstNode *ast_parse_type_decl(ParseContext *pc, size_t *token_index, Visib
23442345
23452346 ast_eat_token(pc, token_index, TokenIdSemicolon);
23462347
2347 node->data.type_decl.top_level_decl.visib_mod = visib_mod;
2348 node->data.type_decl.visib_mod = visib_mod;
23482349
23492350 return node;
23502351}