Allow for import _ "foo" to allow for @(init) procedures; Remove using import code

This commit is contained in:
gingerBill
2022-05-14 14:44:24 +01:00
parent 5af7004f44
commit 5bc866e420
3 changed files with 14 additions and 49 deletions

View File

@@ -4356,6 +4356,9 @@ void check_add_import_decl(CheckerContext *ctx, Ast *decl) {
}
String import_name = path_to_entity_name(id->import_name.string, id->fullpath, false);
if (is_blank_ident(import_name)) {
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 == "_") {
@@ -4363,17 +4366,13 @@ void check_add_import_decl(CheckerContext *ctx, Ast *decl) {
// }
if (import_name.len == 0 || is_blank_ident(import_name)) {
if (id->is_using) {
// TODO(bill): Should this be a warning?
} else {
if (id->import_name.string == "") {
String invalid_name = id->fullpath;
invalid_name = get_invalid_import_name(invalid_name);
if (id->import_name.string == "") {
String invalid_name = id->fullpath;
invalid_name = get_invalid_import_name(invalid_name);
error(id->token, "Import name %.*s, is not a valid identifier. Perhaps you want to reference the package by a different name like this: import <new_name> \"%.*s\" ", LIT(invalid_name), LIT(invalid_name));
} else {
error(token, "Import name, %.*s, cannot be use as an import name as it is not a valid identifier", LIT(id->import_name.string));
}
error(id->token, "Import name %.*s, is not a valid identifier. Perhaps you want to reference the package by a different name like this: import <new_name> \"%.*s\" ", LIT(invalid_name), LIT(invalid_name));
} else {
error(token, "Import name, %.*s, cannot be use as an import name as it is not a valid identifier", LIT(id->import_name.string));
}
} else {
GB_ASSERT(id->import_name.pos.line != 0);
@@ -4383,38 +4382,11 @@ void check_add_import_decl(CheckerContext *ctx, Ast *decl) {
scope);
add_entity(ctx, parent_scope, nullptr, e);
if (force_use || id->is_using) {
if (force_use) {
add_entity_use(ctx, nullptr, e);
}
}
if (id->is_using) {
if (parent_scope->flags & ScopeFlag_Global) {
error(id->import_name, "built-in package imports cannot use using");
return;
}
// NOTE(bill): Add imported entities to this file's scope
for_array(elem_index, scope->elements.entries) {
String name = scope->elements.entries[elem_index].key.string;
Entity *e = scope->elements.entries[elem_index].value;
if (e->scope == parent_scope) continue;
if (is_entity_exported(e, true)) {
Entity *found = scope_lookup_current(parent_scope, name);
if (found != nullptr) {
// NOTE(bill):
// Date: 2019-03-17
// The order has to be the other way around as `using` adds the entity into the that
// file scope otherwise the error would be the wrong way around
redeclaration_error(name, found, e);
} else {
add_entity_with_name(ctx, parent_scope, e->identifier, e, name);
}
}
}
}
scope->flags |= ScopeFlag_HasBeenImported;
}

View File

@@ -1160,11 +1160,10 @@ Ast *ast_package_decl(AstFile *f, Token token, Token name, CommentGroup *docs, C
return result;
}
Ast *ast_import_decl(AstFile *f, Token token, bool is_using, Token relpath, Token import_name,
Ast *ast_import_decl(AstFile *f, Token token, Token relpath, Token import_name,
CommentGroup *docs, CommentGroup *comment) {
Ast *result = alloc_ast_node(f, Ast_ImportDecl);
result->ImportDecl.token = token;
result->ImportDecl.is_using = is_using;
result->ImportDecl.relpath = relpath;
result->ImportDecl.import_name = import_name;
result->ImportDecl.docs = docs;
@@ -4382,7 +4381,6 @@ Ast *parse_import_decl(AstFile *f, ImportDeclKind kind) {
CommentGroup *docs = f->lead_comment;
Token token = expect_token(f, Token_import);
Token import_name = {};
bool is_using = kind != ImportDecl_Standard;
switch (f->curr_token.kind) {
case Token_Ident:
@@ -4393,22 +4391,18 @@ Ast *parse_import_decl(AstFile *f, ImportDeclKind kind) {
break;
}
if (!is_using && is_blank_ident(import_name)) {
syntax_error(import_name, "Illegal import name: '_'");
}
Token file_path = expect_token_after(f, Token_String, "import");
Ast *s = nullptr;
if (f->curr_proc != nullptr) {
syntax_error(import_name, "You cannot use 'import' within a procedure. This must be done at the file scope");
syntax_error(import_name, "Cannot use 'import' within a procedure. This must be done at the file scope");
s = ast_bad_decl(f, import_name, file_path);
} else {
s = ast_import_decl(f, token, is_using, file_path, import_name, docs, f->line_comment);
s = ast_import_decl(f, token, file_path, import_name, docs, f->line_comment);
array_add(&f->imports, s);
}
if (is_using) {
if (kind != ImportDecl_Standard) {
syntax_error(import_name, "'using import' is not allowed, please use the import name explicitly");
}

View File

@@ -585,7 +585,6 @@ AST_KIND(_DeclBegin, "", bool) \
Token import_name; \
CommentGroup *docs; \
CommentGroup *comment; \
bool is_using; \
}) \
AST_KIND(ForeignImportDecl, "foreign import declaration", struct { \
Token token; \