authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-01 00:01:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-01 00:01:30-07:00
log6b2e29c6ac58d5f4370828bfa0f54b83d1931a97
tree9d562684b949af519f5013234b910a1a0fd23a02
parentc1b5518a65acd646c23e763a57c9eb78e533d61a

parseh: do not create macro alias for extern vars


2 files changed, 39 insertions(+), 5 deletions(-)

src/parseh.cpp+26-5
......@@ -26,6 +26,11 @@ struct MacroSymbol {
2626 Buf *value;
2727};
2828
29struct GlobalValue {
30 TypeTableEntry *type;
31 bool is_const;
32};
33
2934struct Context {
3035 ImportTableEntry *import;
3136 ZigList<ErrorMsg *> *errors;
......@@ -34,7 +39,7 @@ struct Context {
3439 TypeTableEntry *c_void_type;
3540 AstNode *root;
3641 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;
3843 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> struct_type_table;
3944 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> struct_decl_table;
4045 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) {
737742 AstNode *field_access_node = create_field_access_node(c, buf_ptr(full_type_name), buf_ptr(field_name));
738743 AstNode *var_node = create_var_decl_node(c, buf_ptr(enum_val_name), field_access_node);
739744 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});
741746 }
742747
743748 // create llvm type for root struct
......@@ -1036,7 +1041,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {
10361041 AstNode *type_node = make_type_node(c, var_type);
10371042 AstNode *var_node = create_typed_var_decl_node(c, true, buf_ptr(name), type_node, init_node);
10381043 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});
10401045 return;
10411046 }
10421047
......@@ -1045,7 +1050,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {
10451050 AstNode *var_node = create_typed_var_decl_node(c, is_const, buf_ptr(name), type_node, nullptr);
10461051 var_node->data.variable_declaration.is_extern = true;
10471052 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});
10491054 return;
10501055 }
10511056
......@@ -1095,6 +1100,22 @@ static bool name_exists(Context *c, Buf *name) {
10951100 return false;
10961101}
10971102
1103static 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
10981119static void render_aliases(Context *c) {
10991120 for (int i = 0; i < c->aliases.length; i += 1) {
11001121 AstNode *alias_node = c->aliases.at(i);
......@@ -1232,7 +1253,7 @@ static void process_macro(Context *c, Buf *name, Buf *value) {
12321253static void process_symbol_macros(Context *c) {
12331254 for (int i = 0; i < c->macro_symbols.length; i += 1) {
12341255 MacroSymbol ms = c->macro_symbols.at(i);
1235 if (name_exists(c, ms.value)) {
1256 if (name_exists_and_const(c, ms.value)) {
12361257 AstNode *var_node = create_var_decl_node(c, buf_ptr(ms.name),
12371258 create_symbol_node(c, buf_ptr(ms.value)));
12381259 c->macro_table.put(ms.name, var_node);
test/run_tests.cpp+13
......@@ -2108,6 +2108,19 @@ Foo fun(Foo *a);
21082108 "pub type c_void = u8;",
21092109 "pub const Foo = c_void;",
21102110 "pub extern fn fun(a: ?&c_void);");
2111
2112 add_parseh_case("ignore #define for non-const", R"SOURCE(
2113struct Foo {
2114 int x;
2115};
2116extern void (*fn_ptr)(void);
2117#define Foo fn_ptr
2118 )SOURCE", 3,
2119 "pub type c_void = u8;",
2120 "pub const Foo = struct_Foo;",
2121 R"OUTPUT(export struct struct_Foo {
2122 x: c_int,
2123})OUTPUT");
21112124}
21122125
21132126static void print_compiler_invocation(TestCase *test_case) {