From ae3672608dcf77fdc642c77c9a3db378c0e16cd9 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 9 Nov 2017 23:36:10 +0000 Subject: [PATCH] Fix link_name overriding --- src/check_decl.cpp | 8 ++++---- src/check_stmt.cpp | 2 +- src/checker.cpp | 22 +++++++++++++++------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 7b71f43b6..940e9cc3b 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -429,9 +429,9 @@ void init_entity_foreign_library(Checker *c, Entity *e) { } } -String handle_link_name(Checker *c, Token token, String link_name, String link_prefix, bool link_prefix_overridden) { +String handle_link_name(Checker *c, Token token, String link_name, String link_prefix) { if (link_prefix.len > 0) { - if (link_name.len > 0 && !link_prefix_overridden) { + if (link_name.len > 0) { error(token, "`link_name` and `link_prefix` cannot be used together"); } else { isize len = link_prefix.len + token.string.len; @@ -485,7 +485,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) { } - ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix, ac.link_prefix_overridden); + ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix); if (d->scope->file != nullptr && e->token.string == "main") { if (pt->param_count != 0 || @@ -635,7 +635,7 @@ void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count check_decl_attributes(c, decl->attributes, var_decl_attribute, &ac); } - ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix, ac.link_prefix_overridden); + ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix); e->Variable.thread_local_model = ac.thread_local_model; String context_name = str_lit("variable declaration"); diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 371931b1f..3c0714b77 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1762,7 +1762,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { if (e->type == nullptr) { e->type = init_type; } - ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix, ac.link_prefix_overridden); + ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix); e->Variable.thread_local_model = ac.thread_local_model; if (ac.link_name.len > 0) { diff --git a/src/checker.cpp b/src/checker.cpp index f48eef16e..61c2a4015 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1862,7 +1862,6 @@ void check_procedure_overloading(Checker *c, Entity *e) { struct AttributeContext { String link_name; String link_prefix; - bool link_prefix_overridden; isize init_expr_list_count; String thread_local_model; }; @@ -1922,9 +1921,6 @@ DECL_ATTRIBUTE_PROC(proc_decl_attribute) { return true; } else if (name == "link_prefix") { if (value.kind == ExactValue_String) { - if (ac->link_prefix.len > 0) { - ac->link_prefix_overridden = true; - } ac->link_prefix = value.value_string; if (!is_foreign_name_valid(ac->link_prefix)) { error(elem, "Invalid link prefix: %.*s", LIT(ac->link_prefix)); @@ -1955,9 +1951,6 @@ DECL_ATTRIBUTE_PROC(var_decl_attribute) { return true; } else if (name == "link_prefix") { if (value.kind == ExactValue_String) { - if (ac->link_prefix.len > 0) { - ac->link_prefix_overridden = true; - } ac->link_prefix = value.value_string; if (!is_foreign_name_valid(ac->link_prefix)) { error(elem, "Invalid link prefix: %.*s", LIT(ac->link_prefix)); @@ -2002,6 +1995,12 @@ DECL_ATTRIBUTE_PROC(var_decl_attribute) { void check_decl_attributes(Checker *c, Array attributes, DeclAttributeProc *proc, AttributeContext *ac) { if (attributes.count == 0) return; + + String original_link_prefix = {}; + if (ac) { + original_link_prefix = ac->link_prefix; + } + StringSet set = {}; string_set_init(&set, heap_allocator()); defer (string_set_destroy(&set)); @@ -2051,6 +2050,15 @@ void check_decl_attributes(Checker *c, Array attributes, DeclAttribut } } } + + if (ac) { + if (ac->link_prefix.text == original_link_prefix.text) { + if (ac->link_name.len > 0) { + ac->link_prefix.text = nullptr; + ac->link_prefix.len = 0; + } + } + } }