Pascal style declaration grouping with ()

This commit is contained in:
Ginger Bill
2017-06-12 15:42:21 +01:00
parent 2ab0d97573
commit 6b5e9aec8e
24 changed files with 2595 additions and 2470 deletions
+105 -100
View File
@@ -1535,121 +1535,126 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
check_stmt(c, pa->body, mod_flags);
case_end;
case_ast_node(vd, ValueDecl, node);
case_ast_node(gd, GenDecl, node);
GB_ASSERT(!c->context.scope->is_file);
if (vd->token.kind == Token_const) {
// NOTE(bill): Handled elsewhere
} else {
Entity **entities = gb_alloc_array(c->allocator, Entity *, vd->names.count);
isize entity_count = 0;
for_array(i, gd->specs) {
AstNode *spec = gd->specs[i];
switch (gd->token.kind) {
case Token_var:
case Token_let: {
ast_node(vd, ValueSpec, spec);
if (vd->flags & VarDeclFlag_thread_local) {
vd->flags &= ~VarDeclFlag_thread_local;
error_node(node, "`thread_local` may only be applied to a variable declaration");
}
Entity **entities = gb_alloc_array(c->allocator, Entity *, vd->names.count);
isize entity_count = 0;
for_array(i, vd->names) {
AstNode *name = vd->names[i];
Entity *entity = NULL;
if (name->kind != AstNode_Ident) {
error_node(name, "A variable declaration must be an identifier");
} else {
Token token = name->Ident;
String str = token.string;
Entity *found = NULL;
// NOTE(bill): Ignore assignments to `_`
if (str != "_") {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, (vd->flags&VarDeclFlag_immutable) != 0);
entity->identifier = name;
if (gd->flags & VarDeclFlag_thread_local) {
gd->flags &= ~VarDeclFlag_thread_local;
error_node(node, "`thread_local` may only be applied to a variable declaration");
}
for_array(i, vd->names) {
AstNode *name = vd->names[i];
Entity *entity = NULL;
if (name->kind != AstNode_Ident) {
error_node(name, "A variable declaration must be an identifier");
} else {
TokenPos pos = found->token.pos;
error(token,
"Redeclaration of `%.*s` in this scope\n"
"\tat %.*s(%td:%td)",
LIT(str), LIT(pos.file), pos.line, pos.column);
entity = found;
Token token = name->Ident;
String str = token.string;
Entity *found = NULL;
// NOTE(bill): Ignore assignments to `_`
if (str != "_") {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, (gd->flags&VarDeclFlag_immutable) != 0);
entity->identifier = name;
} else {
TokenPos pos = found->token.pos;
error(token,
"Redeclaration of `%.*s` in this scope\n"
"\tat %.*s(%td:%td)",
LIT(str), LIT(pos.file), pos.line, pos.column);
entity = found;
}
}
if (entity == NULL) {
entity = make_entity_dummy_variable(c->allocator, c->global_scope, ast_node_token(name));
}
entity->parent_proc_decl = c->context.curr_proc_decl;
entities[entity_count++] = entity;
}
Type *init_type = NULL;
if (vd->type) {
init_type = check_type(c, vd->type, NULL);
if (init_type == NULL) {
init_type = t_invalid;
}
}
if (entity == NULL) {
entity = make_entity_dummy_variable(c->allocator, c->global_scope, ast_node_token(name));
}
entity->parent_proc_decl = c->context.curr_proc_decl;
entities[entity_count++] = entity;
}
Type *init_type = NULL;
if (vd->type) {
init_type = check_type(c, vd->type, NULL);
if (init_type == NULL) {
init_type = t_invalid;
}
}
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
GB_ASSERT(e != NULL);
if (e->flags & EntityFlag_Visited) {
e->type = t_invalid;
continue;
}
e->flags |= EntityFlag_Visited;
if (e->type == NULL) {
e->type = init_type;
}
}
check_arity_match(c, vd);
check_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration"));
for (isize i = 0; i < entity_count; i++) {
add_entity(c, c->context.scope, entities[i]->identifier, entities[i]);
}
if ((vd->flags & VarDeclFlag_using) != 0) {
Token token = ast_node_token(node);
if (vd->type != NULL && entity_count > 1) {
error(token, "`using` can only be applied to one variable of the same type");
// TODO(bill): Should a `continue` happen here?
}
for (isize entity_index = 0; entity_index < entity_count; entity_index++) {
Entity *e = entities[entity_index];
if (e == NULL) {
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
GB_ASSERT(e != NULL);
if (e->flags & EntityFlag_Visited) {
e->type = t_invalid;
continue;
}
if (e->kind != Entity_Variable) {
continue;
}
bool is_immutable = e->Variable.is_immutable;
String name = e->token.string;
Type *t = base_type(type_deref(e->type));
e->flags |= EntityFlag_Visited;
if (is_type_struct(t) || is_type_raw_union(t)) {
Scope **found = map_get(&c->info.scopes, hash_pointer(t->Record.node));
GB_ASSERT(found != NULL);
for_array(i, (*found)->elements.entries) {
Entity *f = (*found)->elements.entries[i].value;
if (f->kind == Entity_Variable) {
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
uvar->Variable.is_immutable = is_immutable;
Entity *prev = scope_insert_entity(c->context.scope, uvar);
if (prev != NULL) {
error(token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
return;
if (e->type == NULL) {
e->type = init_type;
}
}
check_arity_match(c, vd);
check_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration"));
for (isize i = 0; i < entity_count; i++) {
add_entity(c, c->context.scope, entities[i]->identifier, entities[i]);
}
if ((gd->flags & VarDeclFlag_using) != 0) {
Token token = ast_node_token(node);
if (vd->type != NULL && entity_count > 1) {
error(token, "`using` can only be applied to one variable of the same type");
// TODO(bill): Should a `continue` happen here?
}
for (isize entity_index = 0; entity_index < entity_count; entity_index++) {
Entity *e = entities[entity_index];
if (e == NULL) {
continue;
}
if (e->kind != Entity_Variable) {
continue;
}
bool is_immutable = e->Variable.is_immutable;
String name = e->token.string;
Type *t = base_type(type_deref(e->type));
if (is_type_struct(t) || is_type_raw_union(t)) {
Scope **found = map_get(&c->info.scopes, hash_pointer(t->Record.node));
GB_ASSERT(found != NULL);
for_array(i, (*found)->elements.entries) {
Entity *f = (*found)->elements.entries[i].value;
if (f->kind == Entity_Variable) {
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
uvar->Variable.is_immutable = is_immutable;
Entity *prev = scope_insert_entity(c->context.scope, uvar);
if (prev != NULL) {
error(token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
return;
}
}
}
} else {
// NOTE(bill): skip the rest to remove extra errors
error(token, "`using` can only be applied to variables of type struct or raw_union");
return;
}
} else {
// NOTE(bill): skip the rest to remove extra errors
error(token, "`using` can only be applied to variables of type struct or raw_union");
return;
}
}
} break;
}
}
case_end;
+156 -139
View File
@@ -1249,7 +1249,7 @@ void init_preload(Checker *c) {
bool check_arity_match(Checker *c, AstNodeValueDecl *d);
bool check_arity_match(Checker *c, AstNodeValueSpec *s);
void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_scope);
void check_collect_entities_from_when_stmt(Checker *c, AstNodeWhenStmt *ws, bool is_file_scope);
@@ -1362,27 +1362,27 @@ void check_procedure_overloading(Checker *c, Entity *e) {
bool check_arity_match(Checker *c, AstNodeValueDecl *d) {
isize lhs = d->names.count;
isize rhs = d->values.count;
bool check_arity_match(Checker *c, AstNodeValueSpec *spec) {
isize lhs = spec->names.count;
isize rhs = spec->values.count;
if (rhs == 0) {
if (d->type == NULL) {
error_node(d->names[0], "Missing type or initial expression");
if (spec->type == NULL) {
error_node(spec->names[0], "Missing type or initial expression");
return false;
}
} else if (lhs < rhs) {
if (lhs < d->values.count) {
AstNode *n = d->values[lhs];
if (lhs < spec->values.count) {
AstNode *n = spec->values[lhs];
gbString str = expr_to_string(n);
error_node(n, "Extra initial expression `%s`", str);
gb_string_free(str);
} else {
error_node(d->names[0], "Extra initial expression");
error_node(spec->names[0], "Extra initial expression");
}
return false;
} else if (lhs > rhs && rhs != 1) {
AstNode *n = d->names[rhs];
AstNode *n = spec->names[rhs];
gbString str = expr_to_string(n);
error_node(n, "Missing expression for `%s`", str);
gb_string_free(str);
@@ -1450,114 +1450,166 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
}
case_end;
case_ast_node(vd, ValueDecl, decl);
if (vd->token.kind != Token_const) {
if (!c->context.scope->is_file) {
// NOTE(bill): local scope -> handle later and in order
break;
}
// NOTE(bill): You need to store the entity information here unline a constant declaration
isize entity_cap = vd->names.count;
isize entity_count = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_cap);
DeclInfo *di = NULL;
if (vd->values.count > 0) {
di = make_declaration_info(heap_allocator(), c->context.scope, c->context.decl);
di->entities = entities;
di->type_expr = vd->type;
di->init_expr = vd->values[0];
if (vd->flags & VarDeclFlag_thread_local) {
error_node(decl, "#thread_local variable declarations cannot have initialization values");
case_ast_node(gd, GenDecl, decl);
for_array(i, gd->specs) {
AstNode *spec = gd->specs[i];
switch (gd->token.kind) {
case Token_var:
case Token_let: {
if (!c->context.scope->is_file) {
// NOTE(bill): local scope -> handle later and in order
break;
}
}
ast_node(vd, ValueSpec, spec);
// NOTE(bill): You need to store the entity information here unline a constant declaration
isize entity_cap = vd->names.count;
isize entity_count = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_cap);
DeclInfo *di = NULL;
if (vd->values.count > 0) {
di = make_declaration_info(heap_allocator(), c->context.scope, c->context.decl);
di->entities = entities;
di->type_expr = vd->type;
di->init_expr = vd->values[0];
for_array(i, vd->names) {
AstNode *name = vd->names[i];
AstNode *value = NULL;
if (i < vd->values.count) {
value = vd->values[i];
if (gd->flags & VarDeclFlag_thread_local) {
error_node(decl, "#thread_local variable declarations cannot have initialization values");
}
}
for_array(i, vd->names) {
AstNode *name = vd->names[i];
AstNode *value = NULL;
if (i < vd->values.count) {
value = vd->values[i];
}
if (name->kind != AstNode_Ident) {
error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue;
}
Entity *e = make_entity_variable(c->allocator, c->context.scope, name->Ident, NULL, (gd->flags&VarDeclFlag_immutable) != 0);
e->Variable.is_thread_local = (gd->flags & VarDeclFlag_thread_local) != 0;
e->identifier = name;
if (gd->flags & VarDeclFlag_using) {
gd->flags &= ~VarDeclFlag_using; // NOTE(bill): This error will be only caught once
error_node(name, "`using` is not allowed at the file scope");
}
entities[entity_count++] = e;
DeclInfo *d = di;
if (d == NULL) {
AstNode *init_expr = value;
d = make_declaration_info(heap_allocator(), e->scope, c->context.decl);
d->type_expr = vd->type;
d->init_expr = init_expr;
}
add_entity_and_decl_info(c, name, e, d);
}
if (di != NULL) {
di->entity_count = entity_count;
}
check_arity_match(c, vd);
} break;
case Token_const: {
ast_node(vd, ValueSpec, spec);
for_array(i, vd->names) {
AstNode *name = vd->names[i];
if (name->kind != AstNode_Ident) {
error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue;
}
AstNode *init = NULL;
if (i < vd->values.count) {
init = vd->values[i];
}
DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
Entity *e = NULL;
AstNode *up_init = unparen_expr(init);
// if (up_init != NULL && is_ast_node_type(up_init)) {
// AstNode *type = up_init;
// e = make_entity_type_name(c->allocator, d->scope, name->Ident, NULL);
// // TODO(bill): What if vd->type != NULL??? How to handle this case?
// d->type_expr = type;
// d->init_expr = type;
// } else if (up_init != NULL && up_init->kind == AstNode_Alias) {
// #if 1
// error_node(up_init, "#alias declarations are not yet supported");
// continue;
// #else
// e = make_entity_alias(c->allocator, d->scope, name->Ident, NULL, EntityAlias_Invalid, NULL);
// d->type_expr = vd->type;
// d->init_expr = up_init->Alias.expr;
// #endif
// // } else if (init != NULL && up_init->kind == AstNode_ProcLit) {
// // e = make_entity_procedure(c->allocator, d->scope, name->Ident, NULL, up_init->ProcLit.tags);
// // d->proc_lit = up_init;
// // d->type_expr = vd->type;
// } else {
e = make_entity_constant(c->allocator, d->scope, name->Ident, NULL, empty_exact_value);
d->type_expr = vd->type;
d->init_expr = init;
// }
GB_ASSERT(e != NULL);
e->identifier = name;
add_entity_and_decl_info(c, name, e, d);
}
check_arity_match(c, vd);
} break;
case Token_type: {
ast_node(td, TypeSpec, spec);
AstNode *name = td->name;
if (name->kind != AstNode_Ident) {
error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue;
}
Entity *e = make_entity_variable(c->allocator, c->context.scope, name->Ident, NULL, (vd->flags&VarDeclFlag_immutable) != 0);
e->Variable.is_thread_local = (vd->flags & VarDeclFlag_thread_local) != 0;
e->identifier = name;
if (vd->flags & VarDeclFlag_using) {
vd->flags &= ~VarDeclFlag_using; // NOTE(bill): This error will be only caught once
error_node(name, "`using` is not allowed at the file scope");
}
entities[entity_count++] = e;
DeclInfo *d = di;
if (d == NULL) {
AstNode *init_expr = value;
d = make_declaration_info(heap_allocator(), e->scope, c->context.decl);
d->type_expr = vd->type;
d->init_expr = init_expr;
break;
}
add_entity_and_decl_info(c, name, e, d);
}
if (di != NULL) {
di->entity_count = entity_count;
}
check_arity_match(c, vd);
} else {
for_array(i, vd->names) {
AstNode *name = vd->names[i];
if (name->kind != AstNode_Ident) {
error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue;
}
AstNode *init = NULL;
if (i < vd->values.count) {
init = vd->values[i];
}
DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
Entity *e = NULL;
AstNode *up_init = unparen_expr(init);
// if (up_init != NULL && is_ast_node_type(up_init)) {
// AstNode *type = up_init;
// e = make_entity_type_name(c->allocator, d->scope, name->Ident, NULL);
// // TODO(bill): What if vd->type != NULL??? How to handle this case?
// d->type_expr = type;
// d->init_expr = type;
// } else if (up_init != NULL && up_init->kind == AstNode_Alias) {
// #if 1
// error_node(up_init, "#alias declarations are not yet supported");
// continue;
// #else
// e = make_entity_alias(c->allocator, d->scope, name->Ident, NULL, EntityAlias_Invalid, NULL);
// d->type_expr = vd->type;
// d->init_expr = up_init->Alias.expr;
// #endif
// // } else if (init != NULL && up_init->kind == AstNode_ProcLit) {
// // e = make_entity_procedure(c->allocator, d->scope, name->Ident, NULL, up_init->ProcLit.tags);
// // d->proc_lit = up_init;
// // d->type_expr = vd->type;
// } else {
e = make_entity_constant(c->allocator, d->scope, name->Ident, NULL, empty_exact_value);
d->type_expr = vd->type;
d->init_expr = init;
// }
GB_ASSERT(e != NULL);
AstNode *type = unparen_expr(td->type);
e = make_entity_type_name(c->allocator, d->scope, name->Ident, NULL);
// TODO(bill): What if vd->type != NULL??? How to handle this case?
d->type_expr = type;
d->init_expr = type;
e->identifier = name;
add_entity_and_decl_info(c, name, e, d);
}
} break;
check_arity_match(c, vd);
case Token_import:
case Token_import_load: {
ast_node(id, ImportSpec, spec);
if (!c->context.scope->is_file) {
if (id->is_import) {
error_node(decl, "import declarations are only allowed in the file scope");
} else {
error_node(decl, "import_load declarations are only allowed in the file scope");
}
// NOTE(bill): _Should_ be caught by the parser
// TODO(bill): Better error handling if it isn't
continue;
}
DelayedDecl di = {c->context.scope, spec};
array_add(&c->delayed_imports, di);
} break;
}
}
case_end;
@@ -1579,41 +1631,6 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes, bool is_file_sco
add_entity_and_decl_info(c, name, e, d);
case_end;
case_ast_node(td, TypeDecl, decl);
AstNode *name = td->name;
if (name->kind != AstNode_Ident) {
error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
break;
}
DeclInfo *d = make_declaration_info(c->allocator, c->context.scope, c->context.decl);
Entity *e = NULL;
AstNode *type = unparen_expr(td->type);
e = make_entity_type_name(c->allocator, d->scope, name->Ident, NULL);
// TODO(bill): What if vd->type != NULL??? How to handle this case?
d->type_expr = type;
d->init_expr = type;
e->identifier = name;
add_entity_and_decl_info(c, name, e, d);
case_end;
case_ast_node(id, ImportDecl, decl);
if (!c->context.scope->is_file) {
if (id->is_import) {
error_node(decl, "#import declarations are only allowed in the file scope");
} else {
error_node(decl, "#load declarations are only allowed in the file scope");
}
// NOTE(bill): _Should_ be caught by the parser
// TODO(bill): Better error handling if it isn't
continue;
}
DelayedDecl di = {c->context.scope, decl};
array_add(&c->delayed_imports, di);
case_end;
case_ast_node(fl, ForeignLibrary, decl);
if (!c->context.scope->is_file) {
if (fl->is_system) {
@@ -1882,7 +1899,7 @@ void check_import_entities(Checker *c, Map<Scope *> *file_scopes) {
for_array(i, c->delayed_imports) {
Scope *parent_scope = c->delayed_imports[i].parent;
AstNode *decl = c->delayed_imports[i].decl;
ast_node(id, ImportDecl, decl);
ast_node(id, ImportSpec, decl);
Token token = id->relpath;
GB_ASSERT(parent_scope->is_file);
+74 -64
View File
@@ -5793,7 +5793,7 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) {
case_ast_node(us, UsingStmt, node);
for_array(i, us->list) {
AstNode *decl = unparen_expr(us->list[i]);
if (decl->kind == AstNode_ValueDecl) {
if (decl->kind == AstNode_GenDecl) {
ir_build_stmt(proc, decl);
}
}
@@ -5812,56 +5812,88 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) {
ir_build_assign_op(proc, addr, v_one, op);
case_end;
case_ast_node(vd, ValueDecl, node);
if (vd->token.kind != Token_const) {
irModule *m = proc->module;
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena);
case_ast_node(gd, GenDecl, node);
for_array(i, gd->specs) {
AstNode *spec = gd->specs[i];
switch (gd->token.kind) {
case Token_var:
case Token_let: {
ast_node(vd, ValueSpec, spec);
if (vd->values.count == 0) { // declared and zero-initialized
for_array(i, vd->names) {
AstNode *name = vd->names[i];
if (!ir_is_blank_ident(name)) {
ir_add_local_for_identifier(proc, name, true);
}
}
} else { // Tuple(s)
Array<irAddr> lvals = {};
Array<irValue *> inits = {};
array_init(&lvals, m->tmp_allocator, vd->names.count);
array_init(&inits, m->tmp_allocator, vd->names.count);
irModule *m = proc->module;
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena);
for_array(i, vd->names) {
AstNode *name = vd->names[i];
irAddr lval = ir_addr(NULL);
if (!ir_is_blank_ident(name)) {
ir_add_local_for_identifier(proc, name, false);
lval = ir_build_addr(proc, name);
}
array_add(&lvals, lval);
}
for_array(i, vd->values) {
irValue *init = ir_build_expr(proc, vd->values[i]);
Type *t = ir_type(init);
if (t->kind == Type_Tuple) {
for (isize i = 0; i < t->Tuple.variable_count; i++) {
Entity *e = t->Tuple.variables[i];
irValue *v = ir_emit_struct_ev(proc, init, i);
array_add(&inits, v);
if (vd->values.count == 0) { // declared and zero-initialized
for_array(i, vd->names) {
AstNode *name = vd->names[i];
if (!ir_is_blank_ident(name)) {
ir_add_local_for_identifier(proc, name, true);
}
} else {
array_add(&inits, init);
}
} else { // Tuple(s)
Array<irAddr> lvals = {};
Array<irValue *> inits = {};
array_init(&lvals, m->tmp_allocator, vd->names.count);
array_init(&inits, m->tmp_allocator, vd->names.count);
for_array(i, vd->names) {
AstNode *name = vd->names[i];
irAddr lval = ir_addr(NULL);
if (!ir_is_blank_ident(name)) {
ir_add_local_for_identifier(proc, name, false);
lval = ir_build_addr(proc, name);
}
array_add(&lvals, lval);
}
for_array(i, vd->values) {
irValue *init = ir_build_expr(proc, vd->values[i]);
Type *t = ir_type(init);
if (t->kind == Type_Tuple) {
for (isize i = 0; i < t->Tuple.variable_count; i++) {
Entity *e = t->Tuple.variables[i];
irValue *v = ir_emit_struct_ev(proc, init, i);
array_add(&inits, v);
}
} else {
array_add(&inits, init);
}
}
for_array(i, inits) {
ir_addr_store(proc, lvals[i], inits[i]);
}
}
gb_temp_arena_memory_end(tmp);
} break;
for_array(i, inits) {
ir_addr_store(proc, lvals[i], inits[i]);
case Token_type: {
ast_node(td, TypeSpec, node);
AstNode *ident = td->name;
GB_ASSERT(ident->kind == AstNode_Ident);
Entity *e = entity_of_ident(proc->module->info, ident);
GB_ASSERT(e != NULL);
if (e->kind == Entity_TypeName) {
// NOTE(bill): Generate a new name
// parent_proc.name-guid
String ts_name = e->token.string;
isize name_len = proc->name.len + 1 + ts_name.len + 1 + 10 + 1;
u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len);
i32 guid = cast(i32)proc->module->members.entries.count;
name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s.%.*s-%d", LIT(proc->name), LIT(ts_name), guid);
String name = make_string(name_text, name_len-1);
irValue *value = ir_value_type_name(proc->module->allocator,
name, e->type);
map_set(&proc->module->entity_names, hash_pointer(e), name);
ir_gen_global_type_name(proc->module, e, name);
}
} break;
}
gb_temp_arena_memory_end(tmp);
}
case_end;
@@ -5936,28 +5968,6 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) {
}
case_end;
case_ast_node(td, TypeDecl, node);
AstNode *ident = td->name;
GB_ASSERT(ident->kind == AstNode_Ident);
Entity *e = entity_of_ident(proc->module->info, ident);
GB_ASSERT(e != NULL);
if (e->kind == Entity_TypeName) {
// NOTE(bill): Generate a new name
// parent_proc.name-guid
String ts_name = e->token.string;
isize name_len = proc->name.len + 1 + ts_name.len + 1 + 10 + 1;
u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len);
i32 guid = cast(i32)proc->module->members.entries.count;
name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s.%.*s-%d", LIT(proc->name), LIT(ts_name), guid);
String name = make_string(name_text, name_len-1);
irValue *value = ir_value_type_name(proc->module->allocator,
name, e->type);
map_set(&proc->module->entity_names, hash_pointer(e), name);
ir_gen_global_type_name(proc->module, e, name);
}
case_end;
case_ast_node(as, AssignStmt, node);
ir_emit_comment(proc, str_lit("AssignStmt"));
+333 -230
View File
@@ -297,13 +297,6 @@ AST_NODE_KIND(_ComplexStmtEnd, "", i32) \
AST_NODE_KIND(_StmtEnd, "", i32) \
AST_NODE_KIND(_DeclBegin, "", i32) \
AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
AST_NODE_KIND(ValueDecl, "value declaration", struct { \
Token token; \
Array<AstNode *> names; \
AstNode * type; \
Array<AstNode *> values; \
u32 flags; \
}) \
AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \
Token token; \
AstNode *name; \
@@ -314,20 +307,6 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
String foreign_name; \
String link_name; \
}) \
AST_NODE_KIND(TypeDecl, "type declaration", struct { \
Token token; \
AstNode *name; \
AstNode *type; \
}) \
AST_NODE_KIND(ImportDecl, "import declaration", struct { \
Token token; \
bool is_import; \
Token relpath; \
String fullpath; \
Token import_name; \
AstNode *cond; \
AstNode *note; \
}) \
AST_NODE_KIND(ForeignLibrary, "foreign library", struct { \
Token token, filepath; \
Token library_name; \
@@ -339,6 +318,29 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
Token token; \
AstNode *name; \
}) \
AST_NODE_KIND(GenDecl, "generic declaration", struct { \
Token token; \
Token open; \
Token close; \
Array<AstNode *> specs; \
u64 flags; \
}) \
AST_NODE_KIND(ValueSpec, "value specification", struct { \
Array<AstNode *> names; \
AstNode * type; \
Array<AstNode *> values; \
}) \
AST_NODE_KIND(TypeSpec, "type specification", struct { \
AstNode *name; \
AstNode *type; \
}) \
AST_NODE_KIND(ImportSpec, "import specification", struct { \
bool is_import; \
Token relpath; \
String fullpath; \
Token import_name; \
AstNode *cond; \
}) \
AST_NODE_KIND(_DeclEnd, "", i32) \
AST_NODE_KIND(Field, "field", struct { \
Array<AstNode *> names; \
@@ -540,12 +542,15 @@ Token ast_node_token(AstNode *node) {
case AstNode_PushContext: return node->PushContext.token;
case AstNode_BadDecl: return node->BadDecl.begin;
case AstNode_ValueDecl: return node->ValueDecl.token;
case AstNode_ProcDecl: return node->ProcDecl.token;
case AstNode_ImportDecl: return node->ImportDecl.token;
case AstNode_ForeignLibrary: return node->ForeignLibrary.token;
case AstNode_Label: return node->Label.token;
case AstNode_GenDecl: return node->GenDecl.token;
case AstNode_ValueSpec: return ast_node_token(node->ValueSpec.names[0]);
case AstNode_ImportSpec: return node->ImportSpec.import_name;
case AstNode_TypeSpec: return ast_node_token(node->TypeSpec.name);
case AstNode_Field:
if (node->Field.names.count > 0) {
@@ -761,15 +766,6 @@ AstNode *clone_ast_node(gbAllocator a, AstNode *node) {
break;
case AstNode_BadDecl: break;
case AstNode_ValueDecl:
n->ValueDecl.names = clone_ast_node_array(a, n->ValueDecl.names);
n->ValueDecl.type = clone_ast_node(a, n->ValueDecl.type);
n->ValueDecl.values = clone_ast_node_array(a, n->ValueDecl.values);
break;
case AstNode_ImportDecl:
n->ImportDecl.cond = clone_ast_node(a, n->ImportDecl.cond);
n->ImportDecl.note = clone_ast_node(a, n->ImportDecl.note);
break;
case AstNode_ForeignLibrary:
n->ForeignLibrary.cond = clone_ast_node(a, n->ForeignLibrary.cond);
break;
@@ -1428,15 +1424,6 @@ AstNode *ast_map_type(AstFile *f, Token token, AstNode *count, AstNode *key, Ast
}
AstNode *ast_value_decl(AstFile *f, Token token, Array<AstNode *> names, AstNode *type, Array<AstNode *> values) {
AstNode *result = make_ast_node(f, AstNode_ValueDecl);
result->ValueDecl.token = token;
result->ValueDecl.names = names;
result->ValueDecl.type = type;
result->ValueDecl.values = values;
return result;
}
AstNode *ast_proc_decl(AstFile *f, Token token, AstNode *name, AstNode *type, AstNode *body,
u64 tags, AstNode *foreign_library, String foreign_name, String link_name) {
AstNode *result = make_ast_node(f, AstNode_ProcDecl);
@@ -1451,25 +1438,6 @@ AstNode *ast_proc_decl(AstFile *f, Token token, AstNode *name, AstNode *type, As
return result;
}
AstNode *ast_type_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
AstNode *result = make_ast_node(f, AstNode_TypeDecl);
result->TypeDecl.token = token;
result->TypeDecl.name = name;
result->TypeDecl.type = type;
return result;
}
AstNode *ast_import_decl(AstFile *f, Token token, bool is_import, Token relpath, Token import_name, AstNode *cond) {
AstNode *result = make_ast_node(f, AstNode_ImportDecl);
result->ImportDecl.token = token;
result->ImportDecl.is_import = is_import;
result->ImportDecl.relpath = relpath;
result->ImportDecl.import_name = import_name;
result->ImportDecl.cond = cond;
return result;
}
AstNode *ast_foreign_library(AstFile *f, Token token, Token filepath, Token library_name, AstNode *cond, bool is_system) {
AstNode *result = make_ast_node(f, AstNode_ForeignLibrary);
result->ForeignLibrary.token = token;
@@ -1487,6 +1455,41 @@ AstNode *ast_label_decl(AstFile *f, Token token, AstNode *name) {
return result;
}
AstNode *ast_gen_decl(AstFile *f, Token token, Token open, Token close, Array<AstNode *> specs) {
AstNode *result = make_ast_node(f, AstNode_GenDecl);
result->GenDecl.token = token;
result->GenDecl.open = open;
result->GenDecl.close = close;
result->GenDecl.specs = specs;
return result;
}
AstNode *ast_value_spec(AstFile *f, Array<AstNode *> names, AstNode *type, Array<AstNode *> values) {
AstNode *result = make_ast_node(f, AstNode_ValueSpec);
result->ValueSpec.names = names;
result->ValueSpec.type = type;
result->ValueSpec.values = values;
return result;
}
AstNode *ast_type_spec(AstFile *f, AstNode *name, AstNode *type) {
AstNode *result = make_ast_node(f, AstNode_TypeSpec);
result->TypeSpec.name = name;
result->TypeSpec.type = type;
return result;
}
AstNode *ast_import_spec(AstFile *f, bool is_import, Token relpath, Token import_name, AstNode *cond) {
AstNode *result = make_ast_node(f, AstNode_ImportSpec);
result->ImportSpec.is_import = is_import;
result->ImportSpec.relpath = relpath;
result->ImportSpec.import_name = import_name;
result->ImportSpec.cond = cond;
return result;
}
bool next_token(AstFile *f) {
Token prev = f->curr_token;
@@ -1688,18 +1691,9 @@ bool is_semicolon_optional_for_node(AstFile *f, AstNode *s) {
return s->ProcLit.body != NULL;
case AstNode_ProcDecl:
return s->ProcDecl.body != NULL;
case AstNode_TypeDecl:
return is_semicolon_optional_for_node(f, s->TypeDecl.type);
case AstNode_ValueDecl:
if (s->ValueDecl.token.kind != Token_const) {
if (s->ValueDecl.values.count > 0) {
AstNode *last = s->ValueDecl.values[s->ValueDecl.values.count-1];
return is_semicolon_optional_for_node(f, last);
}
}
break;
case AstNode_TypeSpec:
return is_semicolon_optional_for_node(f, s->TypeSpec.type);
}
return false;
@@ -2526,61 +2520,6 @@ AstNode *parse_type(AstFile *f) {
return type;
}
AstNode *parse_value_decl(AstFile *f, Token token) {
Array<AstNode *> lhs = parse_lhs_expr_list(f);
AstNode *type = NULL;
Array<AstNode *> values = {};
bool is_mutable = token.kind != Token_const;
if (allow_token(f, Token_Colon)) {
type = parse_type(f);
} else if (f->curr_token.kind != Token_Eq &&
f->curr_token.kind != Token_Semicolon) {
syntax_error(f->curr_token, "Expected a type separator `:` or `=`");
}
switch (f->curr_token.kind) {
case Token_Eq:
next_token(f);
values = parse_rhs_expr_list(f);
if (values.count > lhs.count) {
syntax_error(f->curr_token, "Too many values on the right hand side of the declaration");
} else if (values.count < lhs.count && !is_mutable) {
syntax_error(f->curr_token, "All constant declarations must be defined");
} else if (values.count == 0) {
syntax_error(f->curr_token, "Expected an expression for this declaration");
}
break;
}
if (is_mutable) {
if (type == NULL && values.count == 0) {
syntax_error(f->curr_token, "Missing variable type or initialization");
return ast_bad_decl(f, f->curr_token, f->curr_token);
}
} else {
if (type == NULL && values.count == 0 && lhs.count > 0) {
syntax_error(f->curr_token, "Missing constant value");
return ast_bad_decl(f, f->curr_token, f->curr_token);
}
}
if (values.data == NULL) {
values = make_ast_node_array(f);
}
Array<AstNode *> specs = {};
array_init(&specs, heap_allocator(), 1);
AstNode *decl = ast_value_decl(f, token, lhs, type, values);
if (token.kind == Token_let) {
decl->ValueDecl.flags |= VarDeclFlag_immutable;
}
return decl;
}
AstNode *parse_proc_decl(AstFile *f) {
TokenKind look_ahead = look_ahead_token_kind(f, 1);
if (look_ahead != Token_Ident) {
@@ -2611,11 +2550,179 @@ AstNode *parse_proc_decl(AstFile *f) {
return ast_proc_decl(f, token, name, type, body, tags, foreign_library, foreign_name, link_name);
}
AstNode *parse_type_decl(AstFile *f) {
Token token = expect_token(f, Token_type);
#define PARSE_SPEC_FUNC(name) AstNode *name(AstFile *f, Token token)
typedef PARSE_SPEC_FUNC(ParseSpecFunc);
AstNode *parse_gen_decl(AstFile *f, Token token, ParseSpecFunc *func) {
Array<AstNode *> specs = {};
Token open = {};
Token close = {};
if (f->curr_token.kind == Token_OpenParen) {
specs = make_ast_node_array(f);
open = expect_token(f, Token_OpenParen);
while (f->curr_token.kind != Token_CloseParen &&
f->curr_token.kind != Token_EOF) {
AstNode *spec = func(f, token);
array_add(&specs, spec);
expect_semicolon(f, spec);
}
close = expect_token(f, Token_CloseParen);
} else {
array_init(&specs, heap_allocator(), 1);
array_add(&specs, func(f, token));
}
AstNode *decl = ast_gen_decl(f, token, open, close, specs);
if (token.kind == Token_let) {
decl->GenDecl.flags |= VarDeclFlag_immutable;
}
return decl;
}
PARSE_SPEC_FUNC(parse_value_spec) {
bool is_mutable = token.kind != Token_const;
Array<AstNode *> names = parse_ident_list(f);
AstNode *type = NULL;
Array<AstNode *> values = {};
if (allow_token(f, Token_Colon)) {
type = parse_type(f);
} else if (f->curr_token.kind != Token_Eq &&
f->curr_token.kind != Token_Semicolon) {
syntax_error(f->curr_token, "Expected a type separator `:` or `=`");
}
if (allow_token(f, Token_Eq)) {
values = parse_rhs_expr_list(f);
if (values.count > names.count) {
syntax_error(f->curr_token, "Too many values on the right hand side of the declaration");
} else if (values.count < names.count && !is_mutable) {
syntax_error(f->curr_token, "All constant declarations must be defined");
} else if (values.count == 0) {
syntax_error(f->curr_token, "Expected an expression for this declaration");
}
}
if (is_mutable) {
if (type == NULL && values.count == 0) {
syntax_error(f->curr_token, "Missing variable type or initialization");
return ast_bad_decl(f, f->curr_token, f->curr_token);
}
} else {
if (type == NULL && values.count == 0 && names.count > 0) {
syntax_error(f->curr_token, "Missing constant value");
return ast_bad_decl(f, f->curr_token, f->curr_token);
}
}
if (values.data == NULL) {
values = make_ast_node_array(f);
}
return ast_value_spec(f, names, type, values);
}
PARSE_SPEC_FUNC(parse_type_spec) {
AstNode *name = parse_ident(f);
AstNode *type = parse_type(f);
return ast_type_decl(f, token, name, type);
return ast_type_spec(f, name, type);
}
PARSE_SPEC_FUNC(parse_import_spec) {
if (token.kind == Token_import) {
AstNode *cond = NULL;
Token import_name = {};
switch (f->curr_token.kind) {
case Token_Period:
import_name = f->curr_token;
import_name.kind = Token_Ident;
next_token(f);
break;
case Token_Ident:
import_name = f->curr_token;
next_token(f);
break;
default:
import_name.pos = f->curr_token.pos;
break;
}
if (import_name.string == "_") {
syntax_error(import_name, "Illegal import name: `_`");
}
Token file_path = expect_token_after(f, Token_String, "import");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
AstNode *spec = NULL;
if (f->curr_proc != NULL) {
syntax_error(import_name, "You cannot use `import` within a procedure. This must be done at the file scope");
spec = ast_bad_decl(f, import_name, file_path);
} else {
spec = ast_import_spec(f, true, file_path, import_name, cond);
}
return spec;
} else {
AstNode *cond = NULL;
Token file_path = expect_token_after(f, Token_String, "import_load");
Token import_name = file_path;
import_name.string = str_lit(".");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
AstNode *spec = NULL;
if (f->curr_proc != NULL) {
syntax_error(import_name, "You cannot use `import_load` within a procedure. This must be done at the file scope");
spec = ast_bad_decl(f, import_name, file_path);
} else {
spec = ast_import_spec(f, false, file_path, import_name, cond);
}
return spec;
}
}
AstNode *parse_decl(AstFile *f) {
ParseSpecFunc *func = NULL;
switch (f->curr_token.kind) {
case Token_var:
case Token_const:
case Token_let:
func = parse_value_spec;
break;
case Token_type:
func = parse_type_spec;
break;
case Token_import:
case Token_import_load:
func = parse_import_spec;
break;
case Token_proc:
return parse_proc_decl(f);
default: {
Token tok = f->curr_token;
fix_advance_to_next_stmt(f);
syntax_error(tok, "Expected a declaration");
return ast_bad_decl(f, tok, f->curr_token);
}
}
Token token = f->curr_token;
next_token(f);
return parse_gen_decl(f, token, func);
}
@@ -2626,8 +2733,7 @@ AstNode *parse_simple_stmt(AstFile *f, StmtAllowFlag flags) {
case Token_var:
case Token_const:
case Token_let:
next_token(f);
return parse_value_decl(f, token);
return parse_decl(f);
}
Array<AstNode *> lhs = parse_lhs_expr_list(f);
@@ -3723,20 +3829,11 @@ AstNode *parse_stmt(AstFile *f) {
case Token_var:
case Token_const:
case Token_let:
s = parse_simple_stmt(f, StmtAllowFlag_None);
expect_semicolon(f, s);
return s;
case Token_proc:
s = parse_proc_decl(f);
expect_semicolon(f, s);
return s;
case Token_type:
s = parse_type_decl(f);
expect_semicolon(f, s);
return s;
case Token_import:
case Token_import_load:
return parse_decl(f);
case Token_if: return parse_if_stmt(f);
case Token_when: return parse_when_stmt(f);
@@ -3767,8 +3864,7 @@ AstNode *parse_stmt(AstFile *f) {
AstNode *decl = NULL;
if (f->curr_token.kind == Token_var ||
f->curr_token.kind == Token_let) {
Token var = f->curr_token; next_token(f);
decl = parse_value_decl(f, var);
decl = parse_decl(f);
expect_semicolon(f, decl);
} else {
Array<AstNode *> list = parse_lhs_expr_list(f);
@@ -3785,20 +3881,17 @@ AstNode *parse_stmt(AstFile *f) {
}
if (decl != NULL && decl->kind == AstNode_ValueDecl) {
#if 1
if (decl->ValueDecl.token.kind == Token_const) {
syntax_error(token, "`using` may not be applied to constant declarations");
if (decl != NULL && decl->kind == AstNode_GenDecl) {
if (decl->GenDecl.token.kind != Token_var &&
decl->GenDecl.token.kind != Token_let) {
syntax_error(token, "`using` may only be applied to variable declarations");
return decl;
}
if (f->curr_proc == NULL) {
syntax_error(token, "`using` is not allowed at the file scope");
} else {
decl->ValueDecl.flags |= VarDeclFlag_using;
decl->GenDecl.flags |= VarDeclFlag_using;
}
#else
decl->ValueDecl.flags |= VarDeclFlag_using;
#endif
return decl;
}
@@ -3846,6 +3939,7 @@ AstNode *parse_stmt(AstFile *f) {
return ast_push_context(f, token, expr, body);
} break;
#if 0
case Token_import: {
Token token = expect_token(f, Token_import);
AstNode *cond = NULL;
@@ -3907,6 +4001,7 @@ AstNode *parse_stmt(AstFile *f) {
expect_semicolon(f, decl);
return decl;
}
#endif
case Token_Hash: {
AstNode *s = NULL;
@@ -3989,14 +4084,15 @@ AstNode *parse_stmt(AstFile *f) {
} else if (tag == "thread_local") {
AstNode *s = parse_stmt(f);
if (s->kind == AstNode_ValueDecl) {
if (s->ValueDecl.token.kind == Token_const) {
syntax_error(token, "`thread_local` may not be applied to constant declarations");
if (s->kind == AstNode_GenDecl) {
if (s->GenDecl.token.kind != Token_var &&
s->GenDecl.token.kind != Token_let) {
syntax_error(token, "`thread_local` may only be applied to variable declarations");
}
if (f->curr_proc != NULL) {
syntax_error(token, "`thread_local` is only allowed at the file scope");
} else {
s->ValueDecl.flags |= VarDeclFlag_thread_local;
s->GenDecl.flags |= VarDeclFlag_thread_local;
}
return s;
}
@@ -4219,87 +4315,94 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNod
node->kind != AstNode_EmptyStmt) {
// NOTE(bill): Sanity check
syntax_error_node(node, "Only declarations are allowed at file scope %.*s", LIT(ast_node_strings[node->kind]));
} else if (node->kind == AstNode_ImportDecl) {
ast_node(id, ImportDecl, node);
String collection_name = {};
String oirignal_string = id->relpath.string;
String file_str = id->relpath.string;
gbAllocator allocator = heap_allocator(); // TODO(bill): Change this allocator
String import_file = {};
} else if (node->kind == AstNode_GenDecl) {
ast_node(gd, GenDecl, node);
if (gd->token.kind == Token_import ||
gd->token.kind == Token_import_load) {
for_array(spec_index, gd->specs) {
AstNode *spec = gd->specs[spec_index];
ast_node(id, ImportSpec, spec);
String collection_name = {};
String oirignal_string = id->relpath.string;
String file_str = id->relpath.string;
gbAllocator allocator = heap_allocator(); // TODO(bill): Change this allocator
String import_file = {};
#if 0
isize colon_pos = -1;
for (isize j = 0; j < file_str.len; j++) {
if (file_str[j] == ':') {
colon_pos = j;
break;
}
}
if (colon_pos > 0) {
collection_name = make_string(file_str.text, colon_pos);
file_str.text += colon_pos+1;
file_str.len -= colon_pos+1;
}
#if 0
isize colon_pos = -1;
for (isize j = 0; j < file_str.len; j++) {
if (file_str[j] == ':') {
colon_pos = j;
break;
}
}
if (colon_pos > 0) {
collection_name = make_string(file_str.text, colon_pos);
file_str.text += colon_pos+1;
file_str.len -= colon_pos+1;
}
if (collection_name.len == 0) {
syntax_error_node(node, "Missing import collection for path: `%.*s`", LIT(oirignal_string));
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
continue;
}
if (collection_name.len == 0) {
syntax_error_node(node, "Missing import collection for path: `%.*s`", LIT(oirignal_string));
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
continue;
}
if (collection_name == "core") {
String abs_path = get_fullpath_core(allocator, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) { // NOTE(bill): This should be null terminated
import_file = abs_path;
}
} else if (collection_name == "local") {
String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
if (gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
if (collection_name == "core") {
String abs_path = get_fullpath_core(allocator, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) { // NOTE(bill): This should be null terminated
import_file = abs_path;
}
} else if (collection_name == "local") {
String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
if (gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
import_file = rel_path;
}
} else {
syntax_error_node(node, "Unknown import collection: `%.*s`", LIT(collection_name));
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
continue;
}
if (!is_import_path_valid(file_str)) {
if (id->is_import) {
syntax_error_node(node, "Invalid import path: `%.*s`", LIT(file_str));
} else {
syntax_error_node(node, "Invalid include path: `%.*s`", LIT(file_str));
}
// NOTE(bill): It's a naughty name
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
continue;
}
#else
if (!is_import_path_valid(file_str)) {
if (id->is_import) {
syntax_error_node(node, "Invalid import path: `%.*s`", LIT(file_str));
} else {
syntax_error_node(node, "Invalid include path: `%.*s`", LIT(file_str));
}
// NOTE(bill): It's a naughty name
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
continue;
}
String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
import_file = rel_path;
}
} else {
syntax_error_node(node, "Unknown import collection: `%.*s`", LIT(collection_name));
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
continue;
}
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(allocator, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path;
}
}
#endif
if (!is_import_path_valid(file_str)) {
if (id->is_import) {
syntax_error_node(node, "Invalid import path: `%.*s`", LIT(file_str));
} else {
syntax_error_node(node, "Invalid include path: `%.*s`", LIT(file_str));
}
// NOTE(bill): It's a naughty name
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
continue;
}
#else
if (!is_import_path_valid(file_str)) {
if (id->is_import) {
syntax_error_node(node, "Invalid import path: `%.*s`", LIT(file_str));
} else {
syntax_error_node(node, "Invalid include path: `%.*s`", LIT(file_str));
}
// NOTE(bill): It's a naughty name
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
continue;
}
String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(allocator, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path;
id->fullpath = import_file;
try_add_import_path(p, import_file, file_str, ast_node_token(node).pos);
}
}
#endif
id->fullpath = import_file;
try_add_import_path(p, import_file, file_str, ast_node_token(node).pos);
} else if (node->kind == AstNode_ForeignLibrary) {
AstNodeForeignLibrary *fl = &node->ForeignLibrary;
String file_str = fl->filepath.string;
+1 -56
View File
@@ -1948,7 +1948,7 @@ void ssa_build_stmt_internal(ssaProc *p, AstNode *node) {
case_ast_node(us, UsingStmt, node);
for_array(i, us->list) {
AstNode *decl = unparen_expr(us->list[i]);
if (decl->kind == AstNode_ValueDecl) {
if (decl->kind == AstNode_GenDecl) {
ssa_build_stmt(p, decl);
}
}
@@ -1968,61 +1968,6 @@ void ssa_build_stmt_internal(ssaProc *p, AstNode *node) {
ssa_build_assign_op(p, addr, ssa_const_int(p, t, 1), op);
case_end;
case_ast_node(vd, ValueDecl, node);
if (vd->token.kind != Token_const) {
ssaModule *m = p->module;
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena);
if (vd->values.count == 0) {
for_array(i, vd->names) {
AstNode *name = vd->names[i];
if (!ssa_is_blank_ident(name)) {
ssa_add_local_for_ident(p, name);
}
}
} else {
Array<ssaAddr> lvals = {0};
Array<ssaValue *> inits = {0};
array_init(&lvals, m->tmp_allocator, vd->names.count);
array_init(&inits, m->tmp_allocator, vd->names.count);
for_array(i, vd->names) {
AstNode *name = vd->names[i];
ssaAddr lval = ssa_addr(NULL);
if (!ssa_is_blank_ident(name)) {
lval = ssa_add_local_for_ident(p, name);
}
array_add(&lvals, lval);
}
for_array(i, vd->values) {
ssaValue *init = ssa_build_expr(p, vd->values[i]);
if (init == NULL) { // TODO(bill): remove this
continue;
}
Type *t = base_type(init->type);
if (t->kind == Type_Tuple) {
for (isize i = 0; i < t->Tuple.variable_count; i++) {
// Entity *e = t->Tuple.variables[i];
ssaValue *v = ssa_emit_value_index(p, init, i);
array_add(&inits, v);
}
} else {
array_add(&inits, init);
}
}
for_array(i, inits) {
ssa_addr_store(p, lvals[i], inits[i]);
}
}
gb_temp_arena_memory_end(tmp);
} else {
GB_PANIC("TODO(bill): ssa_build_stmt Type/Proc Entities");
}
case_end;
case_ast_node(as, AssignStmt, node);
ssa_emit_comment(p, str_lit("AssignStmt"));