Add foreign variables

This commit is contained in:
Ginger Bill
2017-06-15 14:42:08 +01:00
parent d3c24d159f
commit c5ef5279d4
6 changed files with 261 additions and 63 deletions
+81 -25
View File
@@ -67,6 +67,7 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, Array<AstNo
return; return;
} }
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena); gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
// NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be // NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
@@ -90,6 +91,7 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, Array<AstNo
error(lhs[0]->token, "Assignment count mismatch `%td` = `%td`", lhs_count, rhs_count); error(lhs[0]->token, "Assignment count mismatch `%td` = `%td`", lhs_count, rhs_count);
} }
gb_temp_arena_memory_end(tmp); gb_temp_arena_memory_end(tmp);
} }
@@ -257,6 +259,46 @@ bool are_signatures_similar_enough(Type *a_, Type *b_) {
return true; return true;
} }
void init_entity_foreign_library(Checker *c, Entity *e) {
AstNode *ident = NULL;
Entity **foreign_library = NULL;
switch (e->kind) {
case Entity_Procedure:
ident = e->Procedure.foreign_library_ident;
foreign_library = &e->Procedure.foreign_library;
break;
case Entity_Variable:
ident = e->Variable.foreign_library_ident;
foreign_library = &e->Variable.foreign_library;
break;
default:
return;
}
if (ident == NULL) {
error(e->token, "foreign entiies must declare which library they are from");
} else if (ident->kind != AstNode_Ident) {
error_node(ident, "foreign library names must be an identifier");
} else {
String name = ident->Ident.string;
Entity *found = scope_lookup_entity(c->context.scope, name);
if (found == NULL) {
if (name == "_") {
error_node(ident, "`_` cannot be used as a value type");
} else {
error_node(ident, "Undeclared name: %.*s", LIT(name));
}
} else if (found->kind != Entity_LibraryName) {
error_node(ident, "`%.*s` cannot be used as a library name", LIT(name));
} else {
// TODO(bill): Extra stuff to do with library names?
*foreign_library = found;
add_entity_use(c, ident, found);
}
}
}
void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) { void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
GB_ASSERT(e->type == NULL); GB_ASSERT(e->type == NULL);
if (d->proc_decl->kind != AstNode_ProcDecl) { if (d->proc_decl->kind != AstNode_ProcDecl) {
@@ -326,38 +368,17 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
} }
if (is_foreign) { if (is_foreign) {
auto *fp = &c->info.foreigns;
String name = e->token.string; String name = e->token.string;
if (pd->link_name.len > 0) { if (pd->link_name.len > 0) {
name = pd->link_name; name = pd->link_name;
} }
AstNode *foreign_library = e->Procedure.foreign_library_ident;
if (foreign_library == NULL) {
error(e->token, "foreign procedures must declare which library they are from");
} else if (foreign_library->kind != AstNode_Ident) {
error_node(foreign_library, "foreign library names must be an identifier");
} else {
String name = foreign_library->Ident.string;
Entity *found = scope_lookup_entity(c->context.scope, name);
if (found == NULL) {
if (name == "_") {
error_node(foreign_library, "`_` cannot be used as a value type");
} else {
error_node(foreign_library, "Undeclared name: %.*s", LIT(name));
}
} else if (found->kind != Entity_LibraryName) {
error_node(foreign_library, "`%.*s` cannot be used as a library name", LIT(name));
} else {
// TODO(bill): Extra stuff to do with library names?
e->Procedure.foreign_library = found;
add_entity_use(c, foreign_library, found);
}
}
e->Procedure.is_foreign = true; e->Procedure.is_foreign = true;
e->Procedure.link_name = name; e->Procedure.link_name = name;
init_entity_foreign_library(c, e);
auto *fp = &c->info.foreigns;
HashKey key = hash_string(name); HashKey key = hash_string(name);
Entity **found = map_get(fp, key); Entity **found = map_get(fp, key);
if (found) { if (found) {
@@ -365,12 +386,19 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
TokenPos pos = f->token.pos; TokenPos pos = f->token.pos;
Type *this_type = base_type(e->type); Type *this_type = base_type(e->type);
Type *other_type = base_type(f->type); Type *other_type = base_type(f->type);
if (is_type_proc(this_type) && is_type_proc(other_type)) {
if (!are_signatures_similar_enough(this_type, other_type)) { if (!are_signatures_similar_enough(this_type, other_type)) {
error_node(d->proc_decl, error_node(d->proc_decl,
"Redeclaration of foreign procedure `%.*s` with different type signatures\n" "Redeclaration of foreign procedure `%.*s` with different type signatures\n"
"\tat %.*s(%td:%td)", "\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column); LIT(name), LIT(pos.file), pos.line, pos.column);
} }
} else if (!are_types_identical(this_type, other_type)) {
error_node(d->proc_decl,
"Foreign entity `%.*s` previously declared elsewhere with a different type\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
}
} else { } else {
map_set(fp, key, e); map_set(fp, key, e);
} }
@@ -418,6 +446,33 @@ void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count
e->type = check_type(c, type_expr); e->type = check_type(c, type_expr);
} }
if (e->Variable.is_foreign) {
if (init_expr != NULL) {
error(e->token, "A foreign variable declaration cannot have a default value");
}
init_entity_foreign_library(c, e);
String name = e->token.string;
auto *fp = &c->info.foreigns;
HashKey key = hash_string(name);
Entity **found = map_get(fp, key);
if (found) {
Entity *f = *found;
TokenPos pos = f->token.pos;
Type *this_type = base_type(e->type);
Type *other_type = base_type(f->type);
if (!are_types_identical(this_type, other_type)) {
error(e->token,
"Foreign entity `%.*s` previously declared elsewhere with a different type\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
}
} else {
map_set(fp, key, e);
}
}
if (init_expr == NULL) { if (init_expr == NULL) {
if (type_expr == NULL) { if (type_expr == NULL) {
e->type = t_invalid; e->type = t_invalid;
@@ -438,6 +493,7 @@ void check_var_decl(Checker *c, Entity *e, Entity **entities, isize entity_count
} }
} }
Array<AstNode *> inits; Array<AstNode *> inits;
array_init(&inits, c->allocator, 1); array_init(&inits, c->allocator, 1);
array_add(&inits, init_expr); array_add(&inits, init_expr);
+63 -1
View File
@@ -1535,6 +1535,35 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
check_stmt(c, pa->body, mod_flags); check_stmt(c, pa->body, mod_flags);
case_end; case_end;
case_ast_node(fb, ForeignBlockDecl, node);
AstNode *foreign_library = fb->foreign_library;
bool ok = true;
if (foreign_library->kind != AstNode_Ident) {
error_node(foreign_library, "foreign library name must be an identifier");
ok = false;
}
CheckerContext prev_context = c->context;
if (ok) {
c->context.curr_foreign_library = foreign_library;
}
for_array(i, fb->decls) {
AstNode *decl = fb->decls[i];
if (decl->kind == AstNode_GenDecl) {
switch (decl->GenDecl.token.kind) {
case Token_var:
case Token_let:
check_stmt(c, decl, flags);
break;
}
}
}
c->context = prev_context;
case_end;
case_ast_node(gd, GenDecl, node); case_ast_node(gd, GenDecl, node);
GB_ASSERT(!c->context.scope->is_file); GB_ASSERT(!c->context.scope->is_file);
for_array(i, gd->specs) { for_array(i, gd->specs) {
@@ -1568,6 +1597,13 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
if (found == NULL) { if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, (gd->flags&VarDeclFlag_immutable) != 0); entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, (gd->flags&VarDeclFlag_immutable) != 0);
entity->identifier = name; entity->identifier = name;
AstNode *fl = c->context.curr_foreign_library;
if (fl != NULL) {
GB_ASSERT(fl->kind == AstNode_Ident);
entity->Variable.is_foreign = true;
entity->Variable.foreign_library_ident = fl;
}
} else { } else {
TokenPos pos = found->token.pos; TokenPos pos = found->token.pos;
error(token, error(token,
@@ -1610,7 +1646,33 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
check_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration")); check_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration"));
for (isize i = 0; i < entity_count; i++) { for (isize i = 0; i < entity_count; i++) {
add_entity(c, c->context.scope, entities[i]->identifier, entities[i]); Entity *e = entities[i];
if (e->Variable.is_foreign) {
if (vd->values.count > 0) {
error(e->token, "A foreign variable declaration cannot have a default value");
}
init_entity_foreign_library(c, e);
String name = e->token.string;
auto *fp = &c->info.foreigns;
HashKey key = hash_string(name);
Entity **found = map_get(fp, key);
if (found) {
Entity *f = *found;
TokenPos pos = f->token.pos;
Type *this_type = base_type(e->type);
Type *other_type = base_type(f->type);
if (!are_types_identical(this_type, other_type)) {
error(e->token,
"Foreign entity `%.*s` previously declared elsewhere with a different type\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
}
} else {
map_set(fp, key, e);
}
}
add_entity(c, c->context.scope, e->identifier, e);
} }
if ((gd->flags & VarDeclFlag_using) != 0) { if ((gd->flags & VarDeclFlag_using) != 0) {
+8
View File
@@ -1555,6 +1555,14 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
gd->flags &= ~VarDeclFlag_using; // NOTE(bill): This error will be only caught once gd->flags &= ~VarDeclFlag_using; // NOTE(bill): This error will be only caught once
error_node(name, "`using` is not allowed at the file scope"); error_node(name, "`using` is not allowed at the file scope");
} }
AstNode *fl = c->context.curr_foreign_library;
if (fl != NULL) {
GB_ASSERT(fl->kind == AstNode_Ident);
e->Variable.is_foreign = true;
e->Variable.foreign_library_ident = fl;
}
entities[entity_count++] = e; entities[entity_count++] = e;
DeclInfo *d = di; DeclInfo *d = di;
+5 -2
View File
@@ -84,10 +84,13 @@ struct Entity {
struct { struct {
i32 field_index; i32 field_index;
i32 field_src_index; i32 field_src_index;
bool is_immutable;
bool is_thread_local;
ExactValue default_value; ExactValue default_value;
bool default_is_nil; bool default_is_nil;
bool is_immutable;
bool is_thread_local;
bool is_foreign;
Entity * foreign_library;
AstNode * foreign_library_ident;
} Variable; } Variable;
struct { struct {
bool is_type_alias; bool is_type_alias;
+17
View File
@@ -1292,6 +1292,23 @@ irValue *ir_add_local_for_identifier(irProcedure *proc, AstNode *name, bool zero
Entity *e = entity_of_ident(proc->module->info, name); Entity *e = entity_of_ident(proc->module->info, name);
if (e != NULL) { if (e != NULL) {
ir_emit_comment(proc, e->token.string); ir_emit_comment(proc, e->token.string);
if (e->kind == Entity_Variable &&
e->Variable.is_foreign) {
HashKey key = hash_string(e->token.string);
irValue **prev_value = map_get(&proc->module->members, key);
if (prev_value == NULL) {
// NOTE(bill): Don't do mutliple declarations in the IR
irValue *g = ir_value_global(proc->module->allocator, e, NULL);
g->Global.name = e->token.string;
g->Global.is_foreign = true;
ir_module_add_value(proc->module, e, g);
map_set(&proc->module->members, key, g);
return g;
} else {
return *prev_value;
}
}
return ir_add_local(proc, e, name); return ir_add_local(proc, e, name);
} }
return NULL; return NULL;
+64 -12
View File
@@ -1704,6 +1704,24 @@ bool is_semicolon_optional_for_node(AstFile *f, AstNode *s) {
case AstNode_ProcDecl: case AstNode_ProcDecl:
return s->ProcDecl.body != NULL; return s->ProcDecl.body != NULL;
case AstNode_GenDecl:
if (s->GenDecl.close.pos.line != 0) {
return true;
}
if (s->GenDecl.specs.count == 1) {
return is_semicolon_optional_for_node(f, s->GenDecl.specs[0]);
}
break;
case AstNode_ForeignBlockDecl:
if (s->ForeignBlockDecl.close.pos.line != 0) {
return true;
}
if (s->ForeignBlockDecl.decls.count == 1) {
return is_semicolon_optional_for_node(f, s->ForeignBlockDecl.decls[0]);
}
break;
case AstNode_TypeSpec: case AstNode_TypeSpec:
return is_semicolon_optional_for_node(f, s->TypeSpec.type); return is_semicolon_optional_for_node(f, s->TypeSpec.type);
} }
@@ -1716,6 +1734,9 @@ void expect_semicolon(AstFile *f, AstNode *s) {
return; return;
} }
Token prev_token = f->prev_token; Token prev_token = f->prev_token;
if (prev_token.kind == Token_Semicolon) {
return;
}
switch (f->curr_token.kind) { switch (f->curr_token.kind) {
case Token_EOF: case Token_EOF:
@@ -1736,8 +1757,32 @@ void expect_semicolon(AstFile *f, AstNode *s) {
// break; // break;
// } // }
} }
String node_string = ast_node_strings[s->kind];
if (s->kind == AstNode_GenDecl) {
switch (s->GenDecl.token.kind) {
case Token_var:
case Token_let:
node_string = str_lit("variable declaration");
break;
case Token_const:
node_string = str_lit("const declaration");
break;
case Token_type:
node_string = str_lit("type declaration");
break;
case Token_import:
case Token_import_load:
node_string = str_lit("import declaration");
break;
case Token_foreign_library:
case Token_foreign_system_library:
node_string = str_lit("foreign library declaration");
break;
}
}
syntax_error(prev_token, "Expected `;` after %.*s, got %.*s", syntax_error(prev_token, "Expected `;` after %.*s, got %.*s",
LIT(ast_node_strings[s->kind]), LIT(token_strings[prev_token.kind])); LIT(node_string), LIT(token_strings[prev_token.kind]));
} else { } else {
syntax_error(prev_token, "Expected `;`"); syntax_error(prev_token, "Expected `;`");
} }
@@ -2572,7 +2617,8 @@ AstNode *parse_gen_decl(AstFile *f, Token token, ParseSpecFunc *func) {
} }
} else { } else {
array_init(&specs, heap_allocator(), 1); array_init(&specs, heap_allocator(), 1);
array_add(&specs, func(f, token)); AstNode *spec = func(f, token);
array_add(&specs, spec);
} }
if (specs.count == 0) { if (specs.count == 0) {
@@ -2764,25 +2810,29 @@ PARSE_SPEC_FUNC(parse_foreign_library_spec) {
AstNode *parse_decl(AstFile *f); AstNode *parse_decl(AstFile *f);
void parse_foreign_block_decl(AstFile *f, Array<AstNode *> *decls) { void parse_foreign_block_decl(AstFile *f, Array<AstNode *> *decls) {
AstNode *decl = parse_decl(f); AstNode *decl = parse_stmt(f);
switch (decl->kind) { switch (decl->kind) {
case AstNode_EmptyStmt:
case AstNode_BadStmt:
case AstNode_BadDecl: case AstNode_BadDecl:
break; return;
case AstNode_ProcDecl: case AstNode_ProcDecl:
array_add(decls, decl); array_add(decls, decl);
break; return;
// case AstNode_GenDecl: case AstNode_GenDecl:
// if (decl->GenDecl.token.kind == Token_var) { switch (decl->GenDecl.token.kind) {
// array_add(decls, decl); case Token_var:
// break; case Token_let:
// } array_add(decls, decl);
return;
}
/* fallthrough */ /* fallthrough */
default: default:
error_node(decl, "Only procedures declarations are allowed within a foreign block at the moment"); error_node(decl, "Only procedures declarations are allowed within a foreign block at the moment");
break; return;
} }
} }
@@ -3944,7 +3994,9 @@ AstNode *parse_stmt(AstFile *f) {
case Token_foreign: case Token_foreign:
case Token_foreign_library: case Token_foreign_library:
case Token_foreign_system_library: case Token_foreign_system_library:
return parse_decl(f); s = parse_decl(f);
expect_semicolon(f, s);
return s;
case Token_if: return parse_if_stmt(f); case Token_if: return parse_if_stmt(f);