mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-08 00:28:49 +00:00
Fix file load order and allow when statements at file scope
This commit is contained in:
@@ -19,6 +19,4 @@ if [[ "$(uname)" == "Darwin" ]]; then
|
||||
other_args="${other_args} -liconv"
|
||||
fi
|
||||
|
||||
${compiler} src/main.cpp ${warnings_to_disable} ${libraries} ${other_args} -o odin
|
||||
|
||||
./odin run code/demo.odin
|
||||
${compiler} src/main.cpp ${warnings_to_disable} ${libraries} ${other_args} -o odin && ./odin run examples/demo.odin
|
||||
|
||||
+3
-3
@@ -22,11 +22,11 @@ when ODIN_OS == "windows" {
|
||||
}
|
||||
|
||||
when ODIN_OS == "windows" {
|
||||
c_long :: u32;
|
||||
c_ulong :: u32;
|
||||
} else when size_of(uint) == 4 {
|
||||
c_long :: u32;
|
||||
c_ulong :: u32;
|
||||
} else {
|
||||
c_long :: u64;
|
||||
c_ulong :: u64;
|
||||
}
|
||||
|
||||
c_longlong :: i64;
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import "core:fmt.odin";
|
||||
import "core:os.odin";
|
||||
import "core:raw.odin";
|
||||
|
||||
foreign __llvm_core {
|
||||
@@ -146,7 +144,6 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator.Mode,
|
||||
total_size := size + alignment;
|
||||
|
||||
if len(arena.memory) + total_size > cap(arena.memory) {
|
||||
fmt.fprintln(os.stderr, "Arena out of memory");
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -1,26 +1,25 @@
|
||||
import "core:fmt.odin";
|
||||
import "core:strconv.odin";
|
||||
import "core:mem.odin";
|
||||
import "core:atomics.odin";
|
||||
import "core:bits.odin";
|
||||
import "core:hash.odin";
|
||||
import "core:math.odin";
|
||||
import "core:opengl.odin";
|
||||
import "core:os.odin";
|
||||
import "core:raw.odin";
|
||||
import "core:sort.odin";
|
||||
import "core:strings.odin";
|
||||
import "core:sync.odin";
|
||||
import "core:types.odin";
|
||||
import "core:utf8.odin";
|
||||
import "core:utf16.odin";
|
||||
import "core:utf8.odin";
|
||||
// import "core:sync.odin";
|
||||
|
||||
when ODIN_OS == "windows" {
|
||||
import "core:atomics.odin";
|
||||
import "core:opengl.odin";
|
||||
import "core:thread.odin";
|
||||
import win32 "core:sys/windows.odin";
|
||||
}
|
||||
|
||||
|
||||
general_stuff :: proc() {
|
||||
{ // `do` for inline statmes rather than block
|
||||
foo :: proc() do fmt.println("Foo!");
|
||||
@@ -585,6 +584,7 @@ threading_example :: proc() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
main :: proc() {
|
||||
when false {
|
||||
fmt.println("\n# general_stuff"); general_stuff();
|
||||
|
||||
+353
-76
@@ -227,6 +227,7 @@ struct Scope {
|
||||
Map<bool> implicit; // Key: Entity *
|
||||
|
||||
Array<Scope *> shared;
|
||||
Array<AstNode *> delayed_file_decls;
|
||||
PtrSet<Scope *> import_succ;
|
||||
PtrSet<Scope *> imported;
|
||||
bool is_proc;
|
||||
@@ -337,7 +338,7 @@ struct CheckerContext {
|
||||
DeclInfo * curr_proc_decl;
|
||||
AstNode * curr_foreign_library;
|
||||
|
||||
bool allow_file_when_statement;
|
||||
bool collect_delayed_decls;
|
||||
bool allow_polymorphic_types;
|
||||
bool no_polymorphic_errors;
|
||||
Scope * polymorphic_scope;
|
||||
@@ -388,6 +389,9 @@ struct Checker {
|
||||
|
||||
Array<Type *> proc_stack;
|
||||
bool done_preload;
|
||||
|
||||
PtrSet<AstFile *> checked_files;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -487,6 +491,8 @@ Scope *create_scope_from_file(Checker *c, AstFile *f) {
|
||||
|
||||
Scope *s = create_scope(c->global_scope, c->allocator);
|
||||
|
||||
array_init(&s->delayed_file_decls, heap_allocator());
|
||||
|
||||
s->file = f;
|
||||
f->scope = s;
|
||||
s->is_file = true;
|
||||
@@ -527,6 +533,7 @@ void destroy_scope(Scope *scope) {
|
||||
map_destroy(&scope->elements);
|
||||
map_destroy(&scope->implicit);
|
||||
array_free(&scope->shared);
|
||||
array_free(&scope->delayed_file_decls);
|
||||
ptr_set_destroy(&scope->imported);
|
||||
ptr_set_destroy(&scope->import_succ);
|
||||
|
||||
@@ -887,6 +894,7 @@ void init_checker(Checker *c, Parser *parser) {
|
||||
c->context.scope = c->global_scope;
|
||||
|
||||
map_init(&c->file_scopes, heap_allocator());
|
||||
ptr_set_init(&c->checked_files, heap_allocator());
|
||||
}
|
||||
|
||||
void destroy_checker(Checker *c) {
|
||||
@@ -899,7 +907,7 @@ void destroy_checker(Checker *c) {
|
||||
gb_arena_free(&c->tmp_arena);
|
||||
|
||||
map_destroy(&c->file_scopes);
|
||||
// gb_arena_free(&c->arena);
|
||||
ptr_set_destroy(&c->checked_files);
|
||||
}
|
||||
|
||||
|
||||
@@ -1476,6 +1484,7 @@ Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info) {
|
||||
EntityGraphNode *n = G[i];
|
||||
n->index = i;
|
||||
n->dep_count = n->succ.entries.count;
|
||||
GB_ASSERT(n->dep_count >= 0);
|
||||
}
|
||||
|
||||
return G;
|
||||
@@ -1770,6 +1779,7 @@ bool check_arity_match(Checker *c, AstNodeValueDecl *vd, bool is_global) {
|
||||
|
||||
void check_collect_entities_from_when_stmt(Checker *c, AstNodeWhenStmt *ws) {
|
||||
Operand operand = {Addressing_Invalid};
|
||||
if (!ws->is_cond_determined) {
|
||||
check_expr(c, &operand, ws->cond);
|
||||
if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
|
||||
error(ws->cond, "Non-boolean condition in `when` statement");
|
||||
@@ -1777,11 +1787,15 @@ void check_collect_entities_from_when_stmt(Checker *c, AstNodeWhenStmt *ws) {
|
||||
if (operand.mode != Addressing_Constant) {
|
||||
error(ws->cond, "Non-constant condition in `when` statement");
|
||||
}
|
||||
|
||||
ws->is_cond_determined = true;
|
||||
ws->determined_cond = operand.value.kind == ExactValue_Bool && operand.value.value_bool;
|
||||
}
|
||||
|
||||
if (ws->body == nullptr || ws->body->kind != AstNode_BlockStmt) {
|
||||
error(ws->cond, "Invalid body for `when` statement");
|
||||
} else {
|
||||
if (operand.value.kind == ExactValue_Bool &&
|
||||
operand.value.value_bool) {
|
||||
if (ws->determined_cond) {
|
||||
check_collect_entities(c, ws->body->BlockStmt.stmts);
|
||||
} else if (ws->else_stmt) {
|
||||
switch (ws->else_stmt->kind) {
|
||||
@@ -1799,27 +1813,16 @@ void check_collect_entities_from_when_stmt(Checker *c, AstNodeWhenStmt *ws) {
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE(bill): If file_scopes == nullptr, this will act like a local scope
|
||||
void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
|
||||
for_array(decl_index, nodes) {
|
||||
AstNode *decl = nodes[decl_index];
|
||||
if (!is_ast_node_decl(decl) && !is_ast_node_when_stmt(decl)) {
|
||||
continue;
|
||||
}
|
||||
void check_collect_value_decl(Checker *c, AstNode *decl) {
|
||||
ast_node(vd, ValueDecl, decl);
|
||||
|
||||
switch (decl->kind) {
|
||||
case_ast_node(bd, BadDecl, decl);
|
||||
case_end;
|
||||
if (vd->been_handled) return;
|
||||
vd->been_handled = true;
|
||||
|
||||
case_ast_node(ws, WhenStmt, decl);
|
||||
// Will be handled later
|
||||
case_end;
|
||||
|
||||
case_ast_node(vd, ValueDecl, decl);
|
||||
if (vd->is_mutable) {
|
||||
if (!c->context.scope->is_file) {
|
||||
// NOTE(bill): local scope -> handle later and in order
|
||||
break;
|
||||
return;
|
||||
}
|
||||
|
||||
// NOTE(bill): You need to store the entity information here unline a constant declaration
|
||||
@@ -1944,13 +1947,50 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
|
||||
// continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
add_entity_and_decl_info(c, name, e, d);
|
||||
}
|
||||
|
||||
check_arity_match(c, vd, true);
|
||||
}
|
||||
}
|
||||
|
||||
void check_add_foreign_block_decl(Checker *c, AstNode *decl) {
|
||||
ast_node(fb, ForeignBlockDecl, decl);
|
||||
|
||||
if (fb->been_handled) return;
|
||||
fb->been_handled = true;
|
||||
|
||||
AstNode *foreign_library = fb->foreign_library;
|
||||
if (foreign_library->kind != AstNode_Ident) {
|
||||
error(foreign_library, "foreign library name must be an identifier");
|
||||
foreign_library = nullptr;
|
||||
}
|
||||
|
||||
CheckerContext prev_context = c->context;
|
||||
c->context.curr_foreign_library = foreign_library;
|
||||
c->context.collect_delayed_decls = true;
|
||||
check_collect_entities(c, fb->decls);
|
||||
c->context = prev_context;
|
||||
}
|
||||
|
||||
// NOTE(bill): If file_scopes == nullptr, this will act like a local scope
|
||||
void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
|
||||
for_array(decl_index, nodes) {
|
||||
AstNode *decl = nodes[decl_index];
|
||||
if (!is_ast_node_decl(decl) && !is_ast_node_when_stmt(decl)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (decl->kind) {
|
||||
case_ast_node(bd, BadDecl, decl);
|
||||
case_end;
|
||||
|
||||
case_ast_node(ws, WhenStmt, decl);
|
||||
// Will be handled later
|
||||
case_end;
|
||||
|
||||
case_ast_node(vd, ValueDecl, decl);
|
||||
check_collect_value_decl(c, decl);
|
||||
case_end;
|
||||
|
||||
case_ast_node(id, ImportDecl, decl);
|
||||
@@ -1960,7 +2000,7 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
|
||||
// TODO(bill): Better error handling if it isn't
|
||||
continue;
|
||||
}
|
||||
if (c->context.allow_file_when_statement) {
|
||||
if (c->context.collect_delayed_decls) {
|
||||
check_delayed_file_import_entity(c, decl);
|
||||
}
|
||||
case_end;
|
||||
@@ -1972,7 +2012,7 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
|
||||
// TODO(bill): Better error handling if it isn't
|
||||
continue;
|
||||
}
|
||||
if (c->context.allow_file_when_statement) {
|
||||
if (c->context.collect_delayed_decls) {
|
||||
check_delayed_file_import_entity(c, decl);
|
||||
}
|
||||
case_end;
|
||||
@@ -1984,22 +2024,13 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
|
||||
// TODO(bill): Better error handling if it isn't
|
||||
continue;
|
||||
}
|
||||
if (c->context.allow_file_when_statement) {
|
||||
if (c->context.collect_delayed_decls) {
|
||||
check_delayed_file_import_entity(c, decl);
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(fb, ForeignBlockDecl, decl);
|
||||
AstNode *foreign_library = fb->foreign_library;
|
||||
if (foreign_library->kind != AstNode_Ident) {
|
||||
error(foreign_library, "foreign library name must be an identifier");
|
||||
foreign_library = nullptr;
|
||||
}
|
||||
|
||||
CheckerContext prev_context = c->context;
|
||||
c->context.curr_foreign_library = foreign_library;
|
||||
check_collect_entities(c, fb->decls);
|
||||
c->context = prev_context;
|
||||
check_add_foreign_block_decl(c, decl);
|
||||
case_end;
|
||||
|
||||
default:
|
||||
@@ -2012,7 +2043,7 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
|
||||
|
||||
// NOTE(bill): `when` stmts need to be handled after the other as the condition may refer to something
|
||||
// declared after this stmt in source
|
||||
if (!c->context.scope->is_file || c->context.allow_file_when_statement) {
|
||||
if (!c->context.scope->is_file || c->context.collect_delayed_decls) {
|
||||
for_array(i, nodes) {
|
||||
AstNode *node = nodes[i];
|
||||
switch (node->kind) {
|
||||
@@ -2028,6 +2059,7 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
|
||||
void check_all_global_entities(Checker *c) {
|
||||
Scope *prev_file = nullptr;
|
||||
|
||||
bool processing_preload = true;
|
||||
for_array(i, c->info.entities.entries) {
|
||||
auto *entry = &c->info.entities.entries[i];
|
||||
Entity *e = cast(Entity *)entry->key.ptr;
|
||||
@@ -2036,7 +2068,6 @@ void check_all_global_entities(Checker *c) {
|
||||
if (d->scope != e->scope) {
|
||||
continue;
|
||||
}
|
||||
add_curr_ast_file(c, d->scope->file);
|
||||
|
||||
if (!d->scope->has_been_imported) {
|
||||
// NOTE(bill): All of these unchecked entities could mean a lot of unused allocations
|
||||
@@ -2044,6 +2075,10 @@ void check_all_global_entities(Checker *c) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
AstFile *file = d->scope->file;
|
||||
add_curr_ast_file(c, file);
|
||||
|
||||
if (e->token.string == "main") {
|
||||
if (e->kind != Entity_Procedure) {
|
||||
if (e->scope->is_init) {
|
||||
@@ -2063,7 +2098,11 @@ void check_all_global_entities(Checker *c) {
|
||||
c->context = prev_context;
|
||||
|
||||
|
||||
if (d->scope->is_init && !c->done_preload) {
|
||||
if (!d->scope->is_global) {
|
||||
processing_preload = false;
|
||||
}
|
||||
|
||||
if (!processing_preload) {
|
||||
init_preload(c);
|
||||
}
|
||||
}
|
||||
@@ -2247,22 +2286,25 @@ void add_import_dependency_node(Checker *c, AstNode *decl, Map<ImportGraphNode *
|
||||
Scope *scope = *found;
|
||||
GB_ASSERT(scope != nullptr);
|
||||
|
||||
|
||||
ImportGraphNode **found_node = nullptr;
|
||||
ImportGraphNode *m = nullptr;
|
||||
ImportGraphNode *n = nullptr;
|
||||
|
||||
ImportGraphNode **found_node = map_get(M, hash_pointer(parent_file_scope));
|
||||
found_node = map_get(M, hash_pointer(scope));
|
||||
GB_ASSERT(found_node != nullptr);
|
||||
m = *found_node;
|
||||
|
||||
found_node = map_get(M, hash_pointer(scope));
|
||||
found_node = map_get(M, hash_pointer(parent_file_scope));
|
||||
GB_ASSERT(found_node != nullptr);
|
||||
n = *found_node;
|
||||
|
||||
// TODO(bill): How should the edges be attched for `import`?
|
||||
if (id->is_using) {
|
||||
import_graph_node_set_add(&n->pred, m);
|
||||
import_graph_node_set_add(&m->succ, n);
|
||||
ptr_set_add(&m->scope->imported, n->scope);
|
||||
}
|
||||
import_graph_node_set_add(&n->succ, m);
|
||||
import_graph_node_set_add(&m->pred, n);
|
||||
ptr_set_add(&m->scope->imported, n->scope);
|
||||
case_end;
|
||||
|
||||
|
||||
@@ -2282,19 +2324,20 @@ void add_import_dependency_node(Checker *c, AstNode *decl, Map<ImportGraphNode *
|
||||
Scope *scope = *found;
|
||||
GB_ASSERT(scope != nullptr);
|
||||
|
||||
ImportGraphNode **found_node = nullptr;
|
||||
ImportGraphNode *m = nullptr;
|
||||
ImportGraphNode *n = nullptr;
|
||||
|
||||
ImportGraphNode **found_node = map_get(M, hash_pointer(parent_file_scope));
|
||||
found_node = map_get(M, hash_pointer(scope));
|
||||
GB_ASSERT(found_node != nullptr);
|
||||
m = *found_node;
|
||||
|
||||
found_node = map_get(M, hash_pointer(scope));
|
||||
found_node = map_get(M, hash_pointer(parent_file_scope));
|
||||
GB_ASSERT(found_node != nullptr);
|
||||
n = *found_node;
|
||||
|
||||
import_graph_node_set_add(&n->pred, m);
|
||||
import_graph_node_set_add(&m->succ, n);
|
||||
import_graph_node_set_add(&n->succ, m);
|
||||
import_graph_node_set_add(&m->pred, n);
|
||||
ptr_set_add(&m->scope->imported, n->scope);
|
||||
case_end;
|
||||
|
||||
@@ -2357,6 +2400,7 @@ Array<ImportGraphNode *> generate_import_dependency_graph(Checker *c) {
|
||||
ImportGraphNode *n = G[i];
|
||||
n->index = i;
|
||||
n->dep_count = n->succ.entries.count;
|
||||
GB_ASSERT(n->dep_count >= 0);
|
||||
}
|
||||
|
||||
return G;
|
||||
@@ -2405,18 +2449,13 @@ Array<Scope *> find_import_path(Map<Scope *> *file_scopes, Scope *start, Scope *
|
||||
return empty_path;
|
||||
}
|
||||
|
||||
void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
GB_ASSERT(c->context.allow_file_when_statement);
|
||||
void check_add_import_decl(Checker *c, AstNodeImportDecl *id) {
|
||||
if (id->been_handled) return;
|
||||
id->been_handled = true;
|
||||
|
||||
Scope *parent_scope = c->context.scope;
|
||||
GB_ASSERT(parent_scope->is_file);
|
||||
|
||||
switch (decl->kind) {
|
||||
case_ast_node(ws, WhenStmt, decl);
|
||||
check_collect_entities_from_when_stmt(c, ws);
|
||||
case_end;
|
||||
|
||||
case_ast_node(id, ImportDecl, decl);
|
||||
Token token = id->relpath;
|
||||
HashKey key = hash_string(id->fullpath);
|
||||
Scope **found = map_get(&c->file_scopes, key);
|
||||
@@ -2441,12 +2480,11 @@ void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
ptr_set_add(&parent_scope->imported, scope);
|
||||
}
|
||||
|
||||
scope->has_been_imported = true;
|
||||
|
||||
if (id->is_using) {
|
||||
if (parent_scope->is_global) {
|
||||
error(id->import_name, "#shared_global_scope imports cannot use using");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
// NOTE(bill): Add imported entities to this file's scope
|
||||
for_array(elem_index, scope->elements.entries) {
|
||||
Entity *e = scope->elements.entries[elem_index].value;
|
||||
@@ -2461,7 +2499,6 @@ void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
if (ok) map_set(&parent_scope->implicit, hash_entity(e), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
String import_name = path_to_entity_name(id->import_name.string, id->fullpath);
|
||||
if (is_blank_ident(import_name)) {
|
||||
@@ -2476,9 +2513,17 @@ void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
add_entity(c, parent_scope, nullptr, e);
|
||||
}
|
||||
}
|
||||
case_end;
|
||||
ptr_set_add(&c->checked_files, scope->file);
|
||||
scope->has_been_imported = true;
|
||||
}
|
||||
|
||||
void check_add_export_decl(Checker *c, AstNodeExportDecl *ed) {
|
||||
if (ed->been_handled) return;
|
||||
ed->been_handled = true;
|
||||
|
||||
Scope *parent_scope = c->context.scope;
|
||||
GB_ASSERT(parent_scope->is_file);
|
||||
|
||||
case_ast_node(ed, ExportDecl, decl);
|
||||
Token token = ed->relpath;
|
||||
HashKey key = hash_string(ed->fullpath);
|
||||
Scope **found = map_get(&c->file_scopes, key);
|
||||
@@ -2497,16 +2542,18 @@ void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (parent_scope->is_global) {
|
||||
error(ed->token, "`export` cannot be used on #shared_global_scope");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ptr_set_exists(&parent_scope->imported, scope)) {
|
||||
// error(token, "Multiple import of the same file within this scope");
|
||||
} else {
|
||||
ptr_set_add(&parent_scope->imported, scope);
|
||||
}
|
||||
|
||||
scope->has_been_imported = true;
|
||||
if (parent_scope->is_global) {
|
||||
error(decl, "`export` cannot be used on #shared_global_scope");
|
||||
} else {
|
||||
// NOTE(bill): Add imported entities to this file's scope
|
||||
for_array(elem_index, scope->elements.entries) {
|
||||
Entity *e = scope->elements.entries[elem_index].value;
|
||||
@@ -2516,10 +2563,20 @@ void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
add_entity(c, parent_scope, e->identifier, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(fl, ForeignLibraryDecl, decl);
|
||||
ptr_set_add(&c->checked_files, scope->file);
|
||||
scope->has_been_imported = true;
|
||||
}
|
||||
|
||||
void check_add_foreign_library_decl(Checker *c, AstNode *decl) {
|
||||
ast_node(fl, ForeignLibraryDecl, decl);
|
||||
|
||||
if (fl->been_handled) return;
|
||||
fl->been_handled = true;
|
||||
|
||||
Scope *parent_scope = c->context.scope;
|
||||
GB_ASSERT(parent_scope->is_file);
|
||||
|
||||
String file_str = fl->filepath.string;
|
||||
String base_dir = fl->base_dir;
|
||||
|
||||
@@ -2539,7 +2596,7 @@ void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
|
||||
String library_name = path_to_entity_name(fl->library_name.string, file_str);
|
||||
if (is_blank_ident(library_name)) {
|
||||
error(decl, "File name, %.*s, cannot be as a library name as it is not a valid identifier", LIT(fl->library_name.string));
|
||||
error(fl->token, "File name, %.*s, cannot be as a library name as it is not a valid identifier", LIT(fl->library_name.string));
|
||||
} else {
|
||||
GB_ASSERT(fl->library_name.pos.line != 0);
|
||||
fl->library_name.string = library_name;
|
||||
@@ -2547,8 +2604,197 @@ void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
file_str, library_name);
|
||||
add_entity(c, parent_scope, nullptr, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool collect_checked_files_from_import_decl_list(Checker *c, Array<AstNode *> decls) {
|
||||
bool new_files = false;
|
||||
for_array(i, decls) {
|
||||
AstNode *decl = decls[i];
|
||||
switch (decl->kind) {
|
||||
case_ast_node(id, ImportDecl, decl);
|
||||
HashKey key = hash_string(id->fullpath);
|
||||
Scope **found = map_get(&c->file_scopes, key);
|
||||
if (found == nullptr) continue;
|
||||
Scope *s = *found;
|
||||
if (!ptr_set_exists(&c->checked_files, s->file)) {
|
||||
new_files = true;
|
||||
ptr_set_add(&c->checked_files, s->file);
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(ed, ExportDecl, decl);
|
||||
HashKey key = hash_string(ed->fullpath);
|
||||
Scope **found = map_get(&c->file_scopes, key);
|
||||
if (found == nullptr) continue;
|
||||
Scope *s = *found;
|
||||
if (!ptr_set_exists(&c->checked_files, s->file)) {
|
||||
new_files = true;
|
||||
ptr_set_add(&c->checked_files, s->file);
|
||||
}
|
||||
case_end;
|
||||
}
|
||||
}
|
||||
return new_files;
|
||||
}
|
||||
|
||||
|
||||
bool collect_checked_files_from_when_stmt(Checker *c, AstNodeWhenStmt *ws) {
|
||||
Operand operand = {Addressing_Invalid};
|
||||
if (!ws->is_cond_determined) {
|
||||
check_expr(c, &operand, ws->cond);
|
||||
if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
|
||||
error(ws->cond, "Non-boolean condition in `when` statement");
|
||||
}
|
||||
if (operand.mode != Addressing_Constant) {
|
||||
error(ws->cond, "Non-constant condition in `when` statement");
|
||||
}
|
||||
|
||||
ws->is_cond_determined = true;
|
||||
ws->determined_cond = operand.value.kind == ExactValue_Bool && operand.value.value_bool;
|
||||
}
|
||||
|
||||
if (ws->body == nullptr || ws->body->kind != AstNode_BlockStmt) {
|
||||
error(ws->cond, "Invalid body for `when` statement");
|
||||
} else {
|
||||
if (ws->determined_cond) {
|
||||
return collect_checked_files_from_import_decl_list(c, ws->body->BlockStmt.stmts);
|
||||
} else if (ws->else_stmt) {
|
||||
switch (ws->else_stmt->kind) {
|
||||
case AstNode_BlockStmt:
|
||||
return collect_checked_files_from_import_decl_list(c, ws->else_stmt->BlockStmt.stmts);
|
||||
case AstNode_WhenStmt:
|
||||
return collect_checked_files_from_when_stmt(c, &ws->else_stmt->WhenStmt);
|
||||
default:
|
||||
error(ws->else_stmt, "Invalid `else` statement in `when` statement");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
|
||||
GB_ASSERT(c->context.collect_delayed_decls);
|
||||
|
||||
Scope *parent_scope = c->context.scope;
|
||||
GB_ASSERT(parent_scope->is_file);
|
||||
|
||||
switch (decl->kind) {
|
||||
case_ast_node(ws, WhenStmt, decl);
|
||||
check_collect_entities_from_when_stmt(c, ws);
|
||||
case_end;
|
||||
|
||||
case_ast_node(id, ImportDecl, decl);
|
||||
check_add_import_decl(c, id);
|
||||
case_end;
|
||||
|
||||
case_ast_node(ed, ExportDecl, decl);
|
||||
check_add_export_decl(c, ed);
|
||||
case_end;
|
||||
|
||||
case_ast_node(fl, ForeignLibraryDecl, decl);
|
||||
check_add_foreign_library_decl(c, decl);
|
||||
case_end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// NOTE(bill): Returns true if a new file is present
|
||||
bool collect_file_decls(Checker *c, Array<AstNode *> decls);
|
||||
bool collect_file_decls_from_when_stmt(Checker *c, AstNodeWhenStmt *ws);
|
||||
|
||||
bool collect_file_decls_from_when_stmt(Checker *c, AstNodeWhenStmt *ws) {
|
||||
Operand operand = {Addressing_Invalid};
|
||||
if (!ws->is_cond_determined) {
|
||||
check_expr(c, &operand, ws->cond);
|
||||
if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
|
||||
error(ws->cond, "Non-boolean condition in `when` statement");
|
||||
}
|
||||
if (operand.mode != Addressing_Constant) {
|
||||
error(ws->cond, "Non-constant condition in `when` statement");
|
||||
}
|
||||
|
||||
ws->is_cond_determined = true;
|
||||
ws->determined_cond = operand.value.kind == ExactValue_Bool && operand.value.value_bool;
|
||||
}
|
||||
|
||||
if (ws->body == nullptr || ws->body->kind != AstNode_BlockStmt) {
|
||||
error(ws->cond, "Invalid body for `when` statement");
|
||||
} else {
|
||||
if (ws->determined_cond) {
|
||||
return collect_file_decls(c, ws->body->BlockStmt.stmts);
|
||||
} else if (ws->else_stmt) {
|
||||
switch (ws->else_stmt->kind) {
|
||||
case AstNode_BlockStmt:
|
||||
return collect_file_decls(c, ws->else_stmt->BlockStmt.stmts);
|
||||
case AstNode_WhenStmt:
|
||||
return collect_file_decls_from_when_stmt(c, &ws->else_stmt->WhenStmt);
|
||||
default:
|
||||
error(ws->else_stmt, "Invalid `else` statement in `when` statement");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool collect_file_decls(Checker *c, Array<AstNode *> decls) {
|
||||
for_array(i, decls) {
|
||||
Scope *parent_scope = c->context.scope;
|
||||
GB_ASSERT(parent_scope->is_file);
|
||||
|
||||
AstNode *decl = decls[i];
|
||||
switch (decl->kind) {
|
||||
case_ast_node(vd, ValueDecl, decl);
|
||||
check_collect_value_decl(c, decl);
|
||||
case_end;
|
||||
|
||||
case_ast_node(id, ImportDecl, decl);
|
||||
check_add_import_decl(c, id);
|
||||
case_end;
|
||||
|
||||
case_ast_node(ed, ExportDecl, decl);
|
||||
check_add_export_decl(c, ed);
|
||||
case_end;
|
||||
|
||||
case_ast_node(fl, ForeignLibraryDecl, decl);
|
||||
check_add_foreign_library_decl(c, decl);
|
||||
case_end;
|
||||
|
||||
case_ast_node(fb, ForeignBlockDecl, decl);
|
||||
check_add_foreign_block_decl(c, decl);
|
||||
case_end;
|
||||
|
||||
case_ast_node(ws, WhenStmt, decl);
|
||||
if (ws->is_cond_determined) {
|
||||
CheckerContext prev_context = c->context;
|
||||
defer (c->context = prev_context);
|
||||
c->context.collect_delayed_decls = true;
|
||||
|
||||
if (collect_file_decls_from_when_stmt(c, ws)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (collect_checked_files_from_when_stmt(c, ws)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
CheckerContext prev_context = c->context;
|
||||
defer (c->context = prev_context);
|
||||
c->context.collect_delayed_decls = true;
|
||||
|
||||
if (collect_file_decls_from_when_stmt(c, ws)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
case_end;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void check_import_entities(Checker *c) {
|
||||
@@ -2605,7 +2851,7 @@ void check_import_entities(Checker *c) {
|
||||
|
||||
for_array(i, n->pred.entries) {
|
||||
ImportGraphNode *p = n->pred.entries[i].ptr;
|
||||
p->dep_count -= 1;
|
||||
p->dep_count = gb_max(p->dep_count-1, 0);
|
||||
priority_queue_fix(&pq, p->index);
|
||||
}
|
||||
|
||||
@@ -2620,20 +2866,52 @@ void check_import_entities(Checker *c) {
|
||||
array_add(&file_order, n);
|
||||
}
|
||||
|
||||
for_array(file_index, c->parser->files) {
|
||||
AstFile *f = c->parser->files[file_index];
|
||||
Scope *s = f->scope;
|
||||
if (s->is_init || s->is_global) {
|
||||
ptr_set_add(&c->checked_files, f);
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
bool new_files = false;
|
||||
for_array(file_index, c->parser->files) {
|
||||
AstFile *f = c->parser->files[file_index];
|
||||
|
||||
if (!ptr_set_exists(&c->checked_files, f)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
new_files |= collect_checked_files_from_import_decl_list(c, f->decls);
|
||||
}
|
||||
if (new_files) break;
|
||||
}
|
||||
|
||||
for_array(file_index, file_order) {
|
||||
ImportGraphNode *node = file_order[file_index];
|
||||
AstFile *f = node->scope->file;
|
||||
|
||||
if (!ptr_set_exists(&c->checked_files, f)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// gb_printf_err("%.*s\n", LIT(f->fullpath));
|
||||
|
||||
CheckerContext prev_context = c->context;
|
||||
defer (c->context = prev_context);
|
||||
c->context.collect_delayed_decls = true;
|
||||
add_curr_ast_file(c, f);
|
||||
|
||||
c->context.allow_file_when_statement = true;
|
||||
bool new_files = collect_file_decls(c, f->decls);
|
||||
if (new_files) {
|
||||
file_index = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
for_array(i, f->decls) {
|
||||
check_delayed_file_import_entity(c, f->decls[i]);
|
||||
}
|
||||
}
|
||||
// gb_printf_err("End here!\n");
|
||||
// gb_exit(1);
|
||||
}
|
||||
|
||||
Array<Entity *> find_entity_path(Map<DeclInfo *> *map, Entity *start, Entity *end, Map<Entity *> *visited = nullptr) {
|
||||
@@ -2782,7 +3060,6 @@ void check_parsed_files(Checker *c) {
|
||||
}
|
||||
|
||||
check_import_entities(c);
|
||||
|
||||
check_all_global_entities(c);
|
||||
init_preload(c); // NOTE(bill): This could be setup previously through the use of `type_info(_of_val)`
|
||||
|
||||
|
||||
+21
-4
@@ -37,6 +37,7 @@ struct ImportedFile {
|
||||
|
||||
struct AstFile {
|
||||
isize id;
|
||||
String fullpath;
|
||||
gbArena arena;
|
||||
Tokenizer tokenizer;
|
||||
Array<Token> tokens;
|
||||
@@ -51,6 +52,7 @@ struct AstFile {
|
||||
bool allow_range; // NOTE(bill): Ranges are only allowed in certain cases
|
||||
bool in_foreign_block;
|
||||
bool allow_type;
|
||||
isize when_level;
|
||||
|
||||
Array<AstNode *> decls;
|
||||
ImportedFileKind file_kind;
|
||||
@@ -249,6 +251,8 @@ AST_NODE_KIND(_ComplexStmtBegin, "", i32) \
|
||||
AstNode *cond; \
|
||||
AstNode *body; \
|
||||
AstNode *else_stmt; \
|
||||
bool is_cond_determined; \
|
||||
bool determined_cond; \
|
||||
}) \
|
||||
AST_NODE_KIND(ReturnStmt, "return statement", struct { \
|
||||
Token token; \
|
||||
@@ -328,6 +332,7 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
|
||||
AstNode * foreign_library; \
|
||||
Token open, close; \
|
||||
Array<AstNode *> decls; \
|
||||
bool been_handled; \
|
||||
CommentGroup docs; \
|
||||
}) \
|
||||
AST_NODE_KIND(Label, "label", struct { \
|
||||
@@ -340,15 +345,17 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
|
||||
Array<AstNode *> values; \
|
||||
u64 flags; \
|
||||
bool is_mutable; \
|
||||
bool been_handled; \
|
||||
CommentGroup docs; \
|
||||
CommentGroup comment; \
|
||||
}) \
|
||||
AST_NODE_KIND(ImportDecl, "import declaration", struct { \
|
||||
Token token; \
|
||||
bool is_using; \
|
||||
Token relpath; \
|
||||
String fullpath; \
|
||||
Token import_name; \
|
||||
bool is_using; \
|
||||
bool been_handled; \
|
||||
CommentGroup docs; \
|
||||
CommentGroup comment; \
|
||||
}) \
|
||||
@@ -356,6 +363,7 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
|
||||
Token token; \
|
||||
Token relpath; \
|
||||
String fullpath; \
|
||||
bool been_handled; \
|
||||
CommentGroup docs; \
|
||||
CommentGroup comment; \
|
||||
}) \
|
||||
@@ -364,6 +372,7 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
|
||||
Token filepath; \
|
||||
Token library_name; \
|
||||
String base_dir; \
|
||||
bool been_handled; \
|
||||
CommentGroup docs; \
|
||||
CommentGroup comment; \
|
||||
}) \
|
||||
@@ -3993,7 +4002,10 @@ AstNode *parse_when_stmt(AstFile *f) {
|
||||
AstNode *else_stmt = nullptr;
|
||||
|
||||
isize prev_level = f->expr_level;
|
||||
isize when_level = f->when_level;
|
||||
defer (f->when_level = when_level);
|
||||
f->expr_level = -1;
|
||||
f->when_level += 1;
|
||||
|
||||
cond = parse_expr(f, false);
|
||||
|
||||
@@ -4028,6 +4040,11 @@ AstNode *parse_when_stmt(AstFile *f) {
|
||||
}
|
||||
}
|
||||
|
||||
// if (f->curr_proc == nullptr && f->when_level > 1) {
|
||||
// syntax_error(token, "Nested when statements are not currently supported at the file scope");
|
||||
// return ast_bad_stmt(f, token, f->curr_token);
|
||||
// }
|
||||
|
||||
return ast_when_stmt(f, token, cond, body, else_stmt);
|
||||
}
|
||||
|
||||
@@ -4635,11 +4652,11 @@ Array<AstNode *> parse_stmt_list(AstFile *f) {
|
||||
|
||||
|
||||
ParseFileError init_ast_file(AstFile *f, String fullpath) {
|
||||
fullpath = string_trim_whitespace(fullpath); // Just in case
|
||||
if (!string_has_extension(fullpath, str_lit("odin"))) {
|
||||
f->fullpath = string_trim_whitespace(fullpath); // Just in case
|
||||
if (!string_has_extension(f->fullpath, str_lit("odin"))) {
|
||||
return ParseFile_WrongExtension;
|
||||
}
|
||||
TokenizerInitError err = init_tokenizer(&f->tokenizer, fullpath);
|
||||
TokenizerInitError err = init_tokenizer(&f->tokenizer, f->fullpath);
|
||||
if (err != TokenizerInit_None) {
|
||||
switch (err) {
|
||||
case TokenizerInit_NotExists:
|
||||
|
||||
Reference in New Issue
Block a user