From f8f054b354088eb9e76d9207972022bc1d3dfc28 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 14 Jun 2019 17:23:24 -0400 Subject: fix `@export` for arrays not respecting the symbol name Previously, the symbol name parameter of `@export` would be ignored for variables, and the variable name would be used for the symbol name. Now it works as expected. See #2679 --- src/analyze.cpp | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index 04665cc53b..c7e35367c3 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -2712,6 +2712,13 @@ ZigType *get_test_fn_type(CodeGen *g) { return g->test_fn_type; } +void add_var_export(CodeGen *g, ZigVar *var, Buf *symbol_name, GlobalLinkageId linkage) { + GlobalExport *global_export = var->export_list.add_one(); + memset(global_export, 0, sizeof(GlobalExport)); + buf_init_from_buf(&global_export->name, symbol_name); + global_export->linkage = linkage; +} + void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLinkageId linkage, bool ccc) { if (ccc) { if (buf_eql_str(symbol_name, "main") && g->libc_link_lib != nullptr) { @@ -2731,8 +2738,8 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLi } } - FnExport *fn_export = fn_table_entry->export_list.add_one(); - memset(fn_export, 0, sizeof(FnExport)); + GlobalExport *fn_export = fn_table_entry->export_list.add_one(); + memset(fn_export, 0, sizeof(GlobalExport)); buf_init_from_buf(&fn_export->name, symbol_name); fn_export->linkage = linkage; } @@ -3189,15 +3196,6 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { assert(!is_export || !is_extern); - VarLinkage linkage; - if (is_export) { - linkage = VarLinkageExportStrong; - } else if (is_extern) { - linkage = VarLinkageExternal; - } else { - linkage = VarLinkageInternal; - } - ConstExprValue *init_value = nullptr; // TODO more validation for types that can't be used for export/extern variables @@ -3212,7 +3210,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { if (implicit_type->id == ZigTypeIdUnreachable) { add_node_error(g, source_node, buf_sprintf("variable initialization is unreachable")); implicit_type = g->builtin_types.entry_invalid; - } else if ((!is_const || linkage == VarLinkageExternal) && + } else if ((!is_const || is_extern) && (implicit_type->id == ZigTypeIdComptimeFloat || implicit_type->id == ZigTypeIdComptimeInt || implicit_type->id == ZigTypeIdEnumLiteral)) @@ -3227,7 +3225,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { implicit_type = g->builtin_types.entry_invalid; } assert(implicit_type->id == ZigTypeIdInvalid || init_value->special != ConstValSpecialRuntime); - } else if (linkage != VarLinkageExternal) { + } else if (!is_extern) { add_node_error(g, source_node, buf_sprintf("variables must be initialized")); implicit_type = g->builtin_types.entry_invalid; } @@ -3239,7 +3237,6 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope, var_decl->symbol, is_const, init_val, &tld_var->base, type); - tld_var->var->linkage = linkage; tld_var->var->is_thread_local = is_thread_local; if (implicit_type != nullptr && type_is_invalid(implicit_type)) { @@ -3262,6 +3259,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { add_node_error(g, source_node, buf_sprintf("threadlocal variable cannot be constant")); } + if (is_export) { + add_var_export(g, tld_var->var, &tld_var->var->name, GlobalLinkageIdStrong); + } + g->global_vars.append(tld_var); } -- cgit v1.2.3