aboutsummaryrefslogtreecommitdiff
path: root/src/analyze.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/analyze.cpp')
-rw-r--r--src/analyze.cpp41
1 files changed, 17 insertions, 24 deletions
diff --git a/src/analyze.cpp b/src/analyze.cpp
index beaa9f1486..1f9b1ffbed 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -2718,6 +2718,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) {
@@ -2737,8 +2744,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;
}
@@ -2786,12 +2793,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
fn_table_entry->type_entry = analyze_fn_type(g, source_node, child_scope, fn_table_entry);
if (fn_proto->section_expr != nullptr) {
- if (fn_table_entry->body_node == nullptr) {
- add_node_error(g, fn_proto->section_expr,
- buf_sprintf("cannot set section of external function '%s'", buf_ptr(&fn_table_entry->symbol_name)));
- } else {
- analyze_const_string(g, child_scope, fn_proto->section_expr, &fn_table_entry->section_name);
- }
+ analyze_const_string(g, child_scope, fn_proto->section_expr, &fn_table_entry->section_name);
}
if (fn_table_entry->type_entry->id == ZigTypeIdInvalid) {
@@ -3200,15 +3202,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
@@ -3223,7 +3216,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))
@@ -3238,7 +3231,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;
}
@@ -3250,7 +3243,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)) {
@@ -3264,10 +3256,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
}
if (var_decl->section_expr != nullptr) {
- if (var_decl->is_extern) {
- add_node_error(g, var_decl->section_expr,
- buf_sprintf("cannot set section of external variable '%s'", buf_ptr(var_decl->symbol)));
- } else if (!analyze_const_string(g, tld_var->base.parent_scope, var_decl->section_expr, &tld_var->section_name)) {
+ if (!analyze_const_string(g, tld_var->base.parent_scope, var_decl->section_expr, &tld_var->section_name)) {
tld_var->section_name = nullptr;
}
}
@@ -3276,6 +3265,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);
}