Commit 554dd52c36

Andrew Kelley <andrew@ziglang.org>
2021-05-28 06:07:20
stage1: remove c_import_buf from Stage1Zir
All we need is a boolean in Stage1AstGen. This is part of an effort to make Stage1Zir immutable.
1 parent 4ea421f
Changed files (4)
src/stage1/all_types.hpp
@@ -114,7 +114,6 @@ struct Stage1Zir {
     ZigList<IrBasicBlockSrc *> basic_block_list;
     Buf *name;
     ZigFn *name_fn;
-    Buf *c_import_buf;
     AstNode *source_node;
     Scope *begin_scope;
     ErrorMsg *first_err_trace_msg;
src/stage1/astgen.cpp
@@ -18,6 +18,7 @@ struct Stage1AstGen {
     AstNode *main_block_node;
     size_t next_debug_id;
     ZigFn *fn;
+    bool in_c_import_scope;
 };
 
 static IrInstSrc *ir_gen_node(Stage1AstGen *ag, AstNode *node, Scope *scope);
@@ -369,10 +370,6 @@ static size_t irb_next_debug_id(Stage1AstGen *ag) {
     return result;
 }
 
-static Buf *exec_c_import_buf(Stage1Zir *exec) {
-    return exec->c_import_buf;
-}
-
 static void ir_ref_bb(IrBasicBlockSrc *bb) {
     bb->ref_count += 1;
 }
@@ -4215,7 +4212,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, AstNode
                 if (arg0_value == ag->codegen->invalid_inst_src)
                     return arg0_value;
 
-                if (!exec_c_import_buf(ag->exec)) {
+                if (!ag->in_c_import_scope) {
                     add_node_error(ag->codegen, node, buf_sprintf("C include valid only inside C import block"));
                     return ag->codegen->invalid_inst_src;
                 }
@@ -4235,7 +4232,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, AstNode
                 if (arg1_value == ag->codegen->invalid_inst_src)
                     return arg1_value;
 
-                if (!exec_c_import_buf(ag->exec)) {
+                if (!ag->in_c_import_scope) {
                     add_node_error(ag->codegen, node, buf_sprintf("C define valid only inside C import block"));
                     return ag->codegen->invalid_inst_src;
                 }
@@ -4250,7 +4247,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, AstNode
                 if (arg0_value == ag->codegen->invalid_inst_src)
                     return arg0_value;
 
-                if (!exec_c_import_buf(ag->exec)) {
+                if (!ag->in_c_import_scope) {
                     add_node_error(ag->codegen, node, buf_sprintf("C undef valid only inside C import block"));
                     return ag->codegen->invalid_inst_src;
                 }
@@ -8035,7 +8032,7 @@ static IrInstSrc *ir_gen_node(Stage1AstGen *ag, AstNode *node, Scope *scope) {
 }
 
 bool stage1_astgen(CodeGen *codegen, AstNode *node, Scope *scope, Stage1Zir *ir_executable,
-        ZigFn *fn)
+        ZigFn *fn, bool in_c_import_scope)
 {
     assert(node->owner);
 
@@ -8044,6 +8041,7 @@ bool stage1_astgen(CodeGen *codegen, AstNode *node, Scope *scope, Stage1Zir *ir_
 
     ag->codegen = codegen;
     ag->fn = fn;
+    ag->in_c_import_scope = in_c_import_scope;
     ag->exec = ir_executable;
     ag->main_block_node = node;
 
@@ -8078,7 +8076,7 @@ bool stage1_astgen(CodeGen *codegen, AstNode *node, Scope *scope, Stage1Zir *ir_
 bool stage1_astgen_fn(CodeGen *codegen, ZigFn *fn) {
     assert(fn != nullptr);
     assert(fn->child_scope != nullptr);
-    return stage1_astgen(codegen, fn->body_node, fn->child_scope, fn->ir_executable, fn);
+    return stage1_astgen(codegen, fn->body_node, fn->child_scope, fn->ir_executable, fn, false);
 }
 
 void invalidate_exec(Stage1Zir *exec, ErrorMsg *msg) {
src/stage1/astgen.hpp
@@ -10,7 +10,8 @@
 
 #include "all_types.hpp"
 
-bool stage1_astgen(CodeGen *g, AstNode *node, Scope *scope, Stage1Zir *ir_executable, ZigFn *fn);
+bool stage1_astgen(CodeGen *g, AstNode *node, Scope *scope, Stage1Zir *ir_executable,
+        ZigFn *fn, bool in_c_import_scope);
 bool stage1_astgen_fn(CodeGen *g, ZigFn *fn_entry);
 
 bool ir_inst_src_has_side_effects(IrInstSrc *inst);
src/stage1/ir.cpp
@@ -5596,10 +5596,11 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
     ir_executable->source_node = source_node;
     ir_executable->name = exec_name;
     ir_executable->is_inline = true;
-    ir_executable->c_import_buf = c_import_buf;
     ir_executable->begin_scope = scope;
 
-    if (!stage1_astgen(codegen, node, scope, ir_executable, fn_entry))
+    bool in_c_import_scope = c_import_buf != nullptr;
+
+    if (!stage1_astgen(codegen, node, scope, ir_executable, fn_entry, in_c_import_scope))
         return ErrorSemanticAnalyzeFail;
 
     if (ir_executable->first_err_trace_msg != nullptr) {