| ... | @@ -26,6 +26,11 @@ struct MacroSymbol { | ... | @@ -26,6 +26,11 @@ struct MacroSymbol { |
| 26 | Buf *value; | 26 | Buf *value; |
| 27 | }; | 27 | }; |
| 28 | | 28 | |
| | 29 | struct GlobalValue { |
| | 30 | TypeTableEntry *type; |
| | 31 | bool is_const; |
| | 32 | }; |
| | 33 | |
| 29 | struct Context { | 34 | struct Context { |
| 30 | ImportTableEntry *import; | 35 | ImportTableEntry *import; |
| 31 | ZigList<ErrorMsg *> *errors; | 36 | ZigList<ErrorMsg *> *errors; |
| ... | @@ -34,7 +39,7 @@ struct Context { | ... | @@ -34,7 +39,7 @@ struct Context { |
| 34 | TypeTableEntry *c_void_type; | 39 | TypeTableEntry *c_void_type; |
| 35 | AstNode *root; | 40 | AstNode *root; |
| 36 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> global_type_table; | 41 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> global_type_table; |
| 37 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> global_value_table; | 42 | HashMap<Buf *, GlobalValue, buf_hash, buf_eql_buf> global_value_table; |
| 38 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> struct_type_table; | 43 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> struct_type_table; |
| 39 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> struct_decl_table; | 44 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> struct_decl_table; |
| 40 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> enum_type_table; | 45 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> enum_type_table; |
| ... | @@ -737,7 +742,7 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) { | ... | @@ -737,7 +742,7 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) { |
| 737 | AstNode *field_access_node = create_field_access_node(c, buf_ptr(full_type_name), buf_ptr(field_name)); | 742 | AstNode *field_access_node = create_field_access_node(c, buf_ptr(full_type_name), buf_ptr(field_name)); |
| 738 | AstNode *var_node = create_var_decl_node(c, buf_ptr(enum_val_name), field_access_node); | 743 | AstNode *var_node = create_var_decl_node(c, buf_ptr(enum_val_name), field_access_node); |
| 739 | var_decls.append(var_node); | 744 | var_decls.append(var_node); |
| 740 | c->global_value_table.put(enum_val_name, enum_type); | 745 | c->global_value_table.put(enum_val_name, {enum_type, true}); |
| 741 | } | 746 | } |
| 742 | | 747 | |
| 743 | // create llvm type for root struct | 748 | // create llvm type for root struct |
| ... | @@ -1036,7 +1041,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) { | ... | @@ -1036,7 +1041,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) { |
| 1036 | AstNode *type_node = make_type_node(c, var_type); | 1041 | AstNode *type_node = make_type_node(c, var_type); |
| 1037 | AstNode *var_node = create_typed_var_decl_node(c, true, buf_ptr(name), type_node, init_node); | 1042 | AstNode *var_node = create_typed_var_decl_node(c, true, buf_ptr(name), type_node, init_node); |
| 1038 | c->root->data.root.top_level_decls.append(var_node); | 1043 | c->root->data.root.top_level_decls.append(var_node); |
| 1039 | c->global_value_table.put(name, var_type); | 1044 | c->global_value_table.put(name, {var_type, true}); |
| 1040 | return; | 1045 | return; |
| 1041 | } | 1046 | } |
| 1042 | | 1047 | |
| ... | @@ -1045,7 +1050,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) { | ... | @@ -1045,7 +1050,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) { |
| 1045 | AstNode *var_node = create_typed_var_decl_node(c, is_const, buf_ptr(name), type_node, nullptr); | 1050 | AstNode *var_node = create_typed_var_decl_node(c, is_const, buf_ptr(name), type_node, nullptr); |
| 1046 | var_node->data.variable_declaration.is_extern = true; | 1051 | var_node->data.variable_declaration.is_extern = true; |
| 1047 | c->root->data.root.top_level_decls.append(var_node); | 1052 | c->root->data.root.top_level_decls.append(var_node); |
| 1048 | c->global_value_table.put(name, var_type); | 1053 | c->global_value_table.put(name, {var_type, is_const}); |
| 1049 | return; | 1054 | return; |
| 1050 | } | 1055 | } |
| 1051 | | 1056 | |
| ... | @@ -1095,6 +1100,22 @@ static bool name_exists(Context *c, Buf *name) { | ... | @@ -1095,6 +1100,22 @@ static bool name_exists(Context *c, Buf *name) { |
| 1095 | return false; | 1100 | return false; |
| 1096 | } | 1101 | } |
| 1097 | | 1102 | |
| | 1103 | static bool name_exists_and_const(Context *c, Buf *name) { |
| | 1104 | if (c->global_type_table.maybe_get(name)) { |
| | 1105 | return true; |
| | 1106 | } |
| | 1107 | if (c->fn_table.maybe_get(name)) { |
| | 1108 | return true; |
| | 1109 | } |
| | 1110 | if (c->macro_table.maybe_get(name)) { |
| | 1111 | return true; |
| | 1112 | } |
| | 1113 | if (auto entry = c->global_value_table.maybe_get(name)) { |
| | 1114 | return entry->value.is_const; |
| | 1115 | } |
| | 1116 | return false; |
| | 1117 | } |
| | 1118 | |
| 1098 | static void render_aliases(Context *c) { | 1119 | static void render_aliases(Context *c) { |
| 1099 | for (int i = 0; i < c->aliases.length; i += 1) { | 1120 | for (int i = 0; i < c->aliases.length; i += 1) { |
| 1100 | AstNode *alias_node = c->aliases.at(i); | 1121 | AstNode *alias_node = c->aliases.at(i); |
| ... | @@ -1232,7 +1253,7 @@ static void process_macro(Context *c, Buf *name, Buf *value) { | ... | @@ -1232,7 +1253,7 @@ static void process_macro(Context *c, Buf *name, Buf *value) { |
| 1232 | static void process_symbol_macros(Context *c) { | 1253 | static void process_symbol_macros(Context *c) { |
| 1233 | for (int i = 0; i < c->macro_symbols.length; i += 1) { | 1254 | for (int i = 0; i < c->macro_symbols.length; i += 1) { |
| 1234 | MacroSymbol ms = c->macro_symbols.at(i); | 1255 | MacroSymbol ms = c->macro_symbols.at(i); |
| 1235 | if (name_exists(c, ms.value)) { | 1256 | if (name_exists_and_const(c, ms.value)) { |
| 1236 | AstNode *var_node = create_var_decl_node(c, buf_ptr(ms.name), | 1257 | AstNode *var_node = create_var_decl_node(c, buf_ptr(ms.name), |
| 1237 | create_symbol_node(c, buf_ptr(ms.value))); | 1258 | create_symbol_node(c, buf_ptr(ms.value))); |
| 1238 | c->macro_table.put(ms.name, var_node); | 1259 | c->macro_table.put(ms.name, var_node); |