Fix using import to work correctly

This commit is contained in:
Ginger Bill
2017-10-12 20:28:32 +01:00
parent 42312d9def
commit bbb0e14633
2 changed files with 33 additions and 25 deletions
+23 -18
View File
@@ -2540,18 +2540,10 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_h
bool is_not_exported = !is_entity_exported(entity); bool is_not_exported = !is_entity_exported(entity);
if (entity->kind == Entity_ImportName) { if (entity->kind == Entity_ImportName) {
is_not_exported = true; is_not_exported = true;
} else if (!implicit_is_found) { } else if (implicit_is_found) {
is_not_exported = false; is_not_exported = !is_overloaded;
} }
if (is_not_exported) {
gbString sel_str = expr_to_string(selector);
error(op_expr, "`%s` is not exported by `%.*s`", sel_str, LIT(import_name));
gb_string_free(sel_str);
operand->mode = Addressing_Invalid;
operand->expr = node;
return nullptr;
}
if (is_overloaded) { if (is_overloaded) {
HashKey key = hash_string(entity_name); HashKey key = hash_string(entity_name);
@@ -2567,7 +2559,7 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_h
} }
// NOTE(bill): Check to see if it's imported // NOTE(bill): Check to see if it's imported
if (map_get(&import_scope->implicit, hash_entity(procs[i]))) { if (is_entity_implicitly_imported(e, procs[i])) {
gb_swap(Entity *, procs[i], procs[overload_count-1]); gb_swap(Entity *, procs[i], procs[overload_count-1]);
overload_count--; overload_count--;
i--; // NOTE(bill): Counteract the post event i--; // NOTE(bill): Counteract the post event
@@ -2586,15 +2578,28 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_h
} }
} }
if (overload_count > 0 && !skip) { if (!skip) {
operand->mode = Addressing_Overload; if (overload_count > 0) {
operand->type = t_invalid; operand->mode = Addressing_Overload;
operand->expr = node; operand->type = t_invalid;
operand->overload_count = overload_count; operand->expr = node;
operand->overload_entities = procs; operand->overload_count = overload_count;
return procs[0]; operand->overload_entities = procs;
return procs[0];
} else {
is_not_exported = true;
}
} }
} }
if (is_not_exported) {
gbString sel_str = expr_to_string(selector);
error(op_expr, "`%s` is not exported by `%.*s`", sel_str, LIT(import_name));
gb_string_free(sel_str);
operand->mode = Addressing_Invalid;
operand->expr = node;
return nullptr;
}
} }
} }
+10 -7
View File
@@ -787,8 +787,6 @@ Entity *scope_insert_entity(Scope *s, Entity *entity) {
} }
if (prev != nullptr && entity->kind == Entity_Procedure) { if (prev != nullptr && entity->kind == Entity_Procedure) {
// if (s->is_global) return prev;
multi_map_insert(&s->elements, key, entity); multi_map_insert(&s->elements, key, entity);
} else { } else {
map_set(&s->elements, key, entity); map_set(&s->elements, key, entity);
@@ -2520,10 +2518,16 @@ void check_add_import_decl(Checker *c, AstNodeImportDecl *id) {
// NOTE(bill): Add imported entities to this file's scope // NOTE(bill): Add imported entities to this file's scope
for_array(elem_index, scope->elements.entries) { for_array(elem_index, scope->elements.entries) {
Entity *e = scope->elements.entries[elem_index].value; Entity *e = scope->elements.entries[elem_index].value;
if (e->scope == parent_scope) return; if (e->scope == parent_scope) continue;
if (is_entity_exported(e)) { if (e->token.string == "get_proc_address") {
// TODO(bill): Should these entities be imported but cause an error when used? // gb_printf_err("%.*s %.*s get_proc_address\n", LIT(scope->file->fullpath), LIT(parent_scope->file->fullpath));
}
bool implicit_is_found = map_get(&scope->implicit, hash_entity(e)) != nullptr;
if (is_entity_exported(e) && !implicit_is_found) {
Entity *prev = scope_lookup_entity(parent_scope, e->token.string);
// if (prev) gb_printf_err("%.*s\n", LIT(prev->token.string));
bool ok = add_entity(c, parent_scope, e->identifier, e); bool ok = add_entity(c, parent_scope, e->identifier, e);
if (ok) map_set(&parent_scope->implicit, hash_entity(e), true); if (ok) map_set(&parent_scope->implicit, hash_entity(e), true);
} }
@@ -2558,7 +2562,6 @@ void check_add_export_decl(Checker *c, AstNodeExportDecl *ed) {
return; return;
} }
if (parent_scope->is_global) { if (parent_scope->is_global) {
error(ed->token, "`export` cannot be used on #shared_global_scope"); error(ed->token, "`export` cannot be used on #shared_global_scope");
return; return;
@@ -2573,7 +2576,7 @@ void check_add_export_decl(Checker *c, AstNodeExportDecl *ed) {
// NOTE(bill): Add imported entities to this file's scope // NOTE(bill): Add imported entities to this file's scope
for_array(elem_index, scope->elements.entries) { for_array(elem_index, scope->elements.entries) {
Entity *e = scope->elements.entries[elem_index].value; Entity *e = scope->elements.entries[elem_index].value;
if (e->scope == parent_scope) return; if (e->scope == parent_scope) continue;
if (is_entity_kind_exported(e->kind)) { if (is_entity_kind_exported(e->kind)) {
add_entity(c, parent_scope, e->identifier, e); add_entity(c, parent_scope, e->identifier, e);