| ... | ... | @@ -41,6 +41,7 @@ struct Context { |
| 41 | 41 | AstNode *root; |
| 42 | 42 | HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table; |
| 43 | 43 | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table; |
| 44 | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> global_table; |
| 44 | 45 | SourceManager *source_manager; |
| 45 | 46 | ZigList<Alias> aliases; |
| 46 | 47 | ZigList<MacroSymbol> macro_symbols; |
| ... | ... | @@ -296,20 +297,10 @@ static AstNode *trans_create_node_unwrap_null(Context *c, AstNode *child) { |
| 296 | 297 | } |
| 297 | 298 | |
| 298 | 299 | static AstNode *get_global(Context *c, Buf *name) { |
| 299 | | for (size_t i = 0; i < c->root->data.root.top_level_decls.length; i += 1) { |
| 300 | | AstNode *decl_node = c->root->data.root.top_level_decls.items[i]; |
| 301 | | if (decl_node->type == NodeTypeVariableDeclaration) { |
| 302 | | if (buf_eql_buf(decl_node->data.variable_declaration.symbol, name)) { |
| 303 | | return decl_node; |
| 304 | | } |
| 305 | | } else if (decl_node->type == NodeTypeFnDef) { |
| 306 | | if (buf_eql_buf(decl_node->data.fn_def.fn_proto->data.fn_proto.name, name)) { |
| 307 | | return decl_node; |
| 308 | | } |
| 309 | | } else if (decl_node->type == NodeTypeFnProto) { |
| 310 | | if (buf_eql_buf(decl_node->data.fn_proto.name, name)) { |
| 311 | | return decl_node; |
| 312 | | } |
| 300 | { |
| 301 | auto entry = c->global_table.maybe_get(name); |
| 302 | if (entry) { |
| 303 | return entry->value; |
| 313 | 304 | } |
| 314 | 305 | } |
| 315 | 306 | { |
| ... | ... | @@ -320,11 +311,16 @@ static AstNode *get_global(Context *c, Buf *name) { |
| 320 | 311 | return nullptr; |
| 321 | 312 | } |
| 322 | 313 | |
| 314 | static void add_top_level_decl(Context *c, Buf *name, AstNode *node) { |
| 315 | c->global_table.put(name, node); |
| 316 | c->root->data.root.top_level_decls.append(node); |
| 317 | } |
| 318 | |
| 323 | 319 | static AstNode *add_global_var(Context *c, Buf *var_name, AstNode *value_node) { |
| 324 | 320 | bool is_const = true; |
| 325 | 321 | AstNode *type_node = nullptr; |
| 326 | 322 | AstNode *node = trans_create_node_var_decl_global(c, is_const, var_name, type_node, value_node); |
| 327 | | c->root->data.root.top_level_decls.append(node); |
| 323 | add_top_level_decl(c, var_name, node); |
| 328 | 324 | return node; |
| 329 | 325 | } |
| 330 | 326 | |
| ... | ... | @@ -2519,7 +2515,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) { |
| 2519 | 2515 | |
| 2520 | 2516 | if (!fn_decl->hasBody()) { |
| 2521 | 2517 | // just a prototype |
| 2522 | | c->root->data.root.top_level_decls.append(proto_node); |
| 2518 | add_top_level_decl(c, proto_node->data.fn_proto.name, proto_node); |
| 2523 | 2519 | return; |
| 2524 | 2520 | } |
| 2525 | 2521 | |
| ... | ... | @@ -2563,7 +2559,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) { |
| 2563 | 2559 | fn_def_node->data.fn_def.body = body_node_with_param_inits; |
| 2564 | 2560 | |
| 2565 | 2561 | proto_node->data.fn_proto.fn_def_node = fn_def_node; |
| 2566 | | c->root->data.root.top_level_decls.append(fn_def_node); |
| 2562 | add_top_level_decl(c, fn_def_node->data.fn_def.fn_proto->data.fn_proto.name, fn_def_node); |
| 2567 | 2563 | } |
| 2568 | 2564 | |
| 2569 | 2565 | static AstNode *resolve_typdef_as_builtin(Context *c, const TypedefNameDecl *typedef_decl, const char *primitive_name) { |
| ... | ... | @@ -2911,14 +2907,14 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) { |
| 2911 | 2907 | } |
| 2912 | 2908 | |
| 2913 | 2909 | AstNode *var_node = trans_create_node_var_decl_global(c, is_const, name, var_type, init_node); |
| 2914 | | c->root->data.root.top_level_decls.append(var_node); |
| 2910 | add_top_level_decl(c, name, var_node); |
| 2915 | 2911 | return; |
| 2916 | 2912 | } |
| 2917 | 2913 | |
| 2918 | 2914 | if (is_extern) { |
| 2919 | 2915 | AstNode *var_node = trans_create_node_var_decl_global(c, is_const, name, var_type, nullptr); |
| 2920 | 2916 | var_node->data.variable_declaration.is_extern = true; |
| 2921 | | c->root->data.root.top_level_decls.append(var_node); |
| 2917 | add_top_level_decl(c, name, var_node); |
| 2922 | 2918 | return; |
| 2923 | 2919 | } |
| 2924 | 2920 | |
| ... | ... | @@ -2976,7 +2972,7 @@ static void render_macros(Context *c) { |
| 2976 | 2972 | |
| 2977 | 2973 | AstNode *value_node = entry->value; |
| 2978 | 2974 | if (value_node->type == NodeTypeFnDef) { |
| 2979 | | c->root->data.root.top_level_decls.append(value_node); |
| 2975 | add_top_level_decl(c, value_node->data.fn_def.fn_proto->data.fn_proto.name, value_node); |
| 2980 | 2976 | } else { |
| 2981 | 2977 | add_global_var(c, entry->key, value_node); |
| 2982 | 2978 | } |
| ... | ... | @@ -3183,6 +3179,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch |
| 3183 | 3179 | } |
| 3184 | 3180 | c->decl_table.init(8); |
| 3185 | 3181 | c->macro_table.init(8); |
| 3182 | c->global_table.init(8); |
| 3186 | 3183 | c->ptr_params.init(8); |
| 3187 | 3184 | c->codegen = codegen; |
| 3188 | 3185 | c->source_node = source_node; |