From 1cdb975c9183cb1be269ae394cc11b3a55cf9dfc Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 22 Aug 2023 12:05:25 +0100 Subject: Add attribute `@(require) import "foo"` to replace/supplement `_ :: foo` --- src/checker.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/checker.cpp') diff --git a/src/checker.cpp b/src/checker.cpp index 139bf9690..a8efa39ab 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -4505,6 +4505,23 @@ gb_internal String get_invalid_import_name(String input) { return input; } +gb_internal DECL_ATTRIBUTE_PROC(import_decl_attribute) { + if (name == ATTRIBUTE_USER_TAG_NAME) { + ExactValue ev = check_decl_attribute_value(c, value); + if (ev.kind != ExactValue_String) { + error(elem, "Expected a string value for '%.*s'", LIT(name)); + } + return true; + } else if (name == "require") { + if (value != nullptr) { + error(elem, "Expected no parameter for '%.*s'", LIT(name)); + } + ac->require_declaration = true; + return true; + } + return false; +} + gb_internal void check_add_import_decl(CheckerContext *ctx, Ast *decl) { if (decl->state_flags & StateFlag_BeenHandled) return; decl->state_flags |= StateFlag_BeenHandled; @@ -4553,10 +4570,12 @@ gb_internal void check_add_import_decl(CheckerContext *ctx, Ast *decl) { force_use = true; } - // NOTE(bill, 2019-05-19): If the directory path is not a valid entity name, force the user to assign a custom one - // if (import_name.len == 0 || import_name == "_") { - // import_name = scope->pkg->name; - // } + AttributeContext ac = {}; + check_decl_attributes(ctx, id->attributes, import_decl_attribute, &ac); + if (ac.require_declaration) { + force_use = true; + } + if (import_name.len == 0) { String invalid_name = id->fullpath; -- cgit v1.2.3