diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/checker.cpp | 51 | ||||
| -rw-r--r-- | src/main.cpp | 6 | ||||
| -rw-r--r-- | src/parser.cpp | 63 | ||||
| -rw-r--r-- | src/tokenizer.cpp | 1 |
4 files changed, 76 insertions, 45 deletions
diff --git a/src/checker.cpp b/src/checker.cpp index 4b2ddef8a..ca2780f49 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1213,6 +1213,9 @@ void add_entity_use(Checker *c, AstNode *identifier, Entity *entity) { if (identifier->kind != AstNode_Ident) { return; } + if (entity == nullptr) { + return; + } if (entity->identifier == nullptr) { entity->identifier = identifier; } @@ -2604,33 +2607,37 @@ void check_add_foreign_library_decl(Checker *c, AstNode *decl) { Scope *parent_scope = c->context.scope; GB_ASSERT(parent_scope->is_file); - String file_str = fl->filepath.string; - String base_dir = fl->base_dir; + String fullpath = fl->fullpath; + String library_name = path_to_entity_name(fl->library_name.string, fullpath); + if (is_blank_ident(library_name)) { + error(fl->token, "File name, %.*s, cannot be as a library name as it is not a valid identifier", LIT(fl->library_name.string)); + return; + } - if (fl->token.kind == Token_foreign_library) { - gbAllocator a = heap_allocator(); // TODO(bill): Change this allocator + if (fl->collection_name != "system") { + char *c_str = gb_alloc_array(heap_allocator(), char, fullpath.len+1); + defer (gb_free(heap_allocator(), c_str)); + gb_memcopy(c_str, fullpath.text, fullpath.len); + c_str[fullpath.len] = '\0'; - String rel_path = get_fullpath_relative(a, base_dir, file_str); - String 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(a, file_str); - if (gb_file_exists(cast(char *)abs_path.text)) { - import_file = abs_path; - } + gbFile f = {}; + gbFileError file_err = gb_file_open(&f, c_str); + + switch (file_err) { + case gbFileError_Invalid: + error(decl, "Invalid file or cannot be found (`%.*s`)", LIT(fullpath)); + return; + case gbFileError_NotExists: + error(decl, "File cannot be found (`%.*s`)", LIT(fullpath)); + return; } - file_str = import_file; } - String library_name = path_to_entity_name(fl->library_name.string, file_str); - if (is_blank_ident(library_name)) { - 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; - Entity *e = make_entity_library_name(c->allocator, parent_scope, fl->library_name, t_invalid, - file_str, library_name); - add_entity(c, parent_scope, nullptr, e); - } + GB_ASSERT(fl->library_name.pos.line != 0); + fl->library_name.string = library_name; + Entity *e = make_entity_library_name(c->allocator, parent_scope, fl->library_name, t_invalid, + fl->fullpath, library_name); + add_entity(c, parent_scope, nullptr, e); } diff --git a/src/main.cpp b/src/main.cpp index 5da7105c3..cd7215168 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -430,6 +430,12 @@ bool parse_build_flags(Array<String> args) { break; } + if (name == "system") { + gb_printf_err("Library collection name `system` is reserved\n"); + bad_flags = true; + break; + } + String prev_path = {}; bool found = find_library_collection_path(name, &prev_path); if (found) { diff --git a/src/parser.cpp b/src/parser.cpp index 166b37ca7..3214befb8 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -373,13 +373,15 @@ AST_NODE_KIND(_DeclBegin, "", i32) \ CommentGroup comment; \ }) \ AST_NODE_KIND(ForeignLibraryDecl, "foreign library declaration", struct { \ - Token token; \ - Token filepath; \ - Token library_name; \ - String base_dir; \ - bool been_handled; \ - CommentGroup docs; \ - CommentGroup comment; \ + Token token; \ + Token filepath; \ + Token library_name; \ + String base_dir; \ + String collection_name; \ + String fullpath; \ + bool been_handled; \ + CommentGroup docs; \ + CommentGroup comment; \ }) \ AST_NODE_KIND(_DeclEnd, "", i32) \ AST_NODE_KIND(Field, "field", struct { \ @@ -1772,7 +1774,6 @@ void fix_advance_to_next_stmt(AstFile *f) { case Token_foreign: case Token_foreign_library: - case Token_foreign_system_library: case Token_if: case Token_for: @@ -4406,13 +4407,15 @@ AstNode *parse_foreign_decl(AstFile *f) { CommentGroup docs = f->lead_comment; Token token = {}; switch (f->curr_token.kind) { + case Token_foreign: + return parse_foreign_block(f); + case Token_foreign_library: - case Token_foreign_system_library: token = advance_token(f); break; default: token = advance_token(f); - syntax_error(token, "Expected either foreign_library or foreign_system_library, got `%.*s`", LIT(token.string)); + syntax_error(token, "Expected either foreign or foreign_library, got `%.*s`", LIT(token.string)); return ast_bad_decl(f, token, token); } @@ -4432,7 +4435,7 @@ AstNode *parse_foreign_decl(AstFile *f) { Token file_path = expect_token(f, Token_String); AstNode *s = nullptr; if (f->curr_proc != nullptr) { - syntax_error(lib_name, "You cannot use foreign_system_library within a procedure. This must be done at the file scope"); + syntax_error(lib_name, "You cannot use foreign_library within a procedure. This must be done at the file scope"); s = ast_bad_decl(f, lib_name, file_path); } else { s = ast_foreign_library_decl(f, token, file_path, lib_name, docs, f->line_comment); @@ -4468,10 +4471,7 @@ AstNode *parse_stmt(AstFile *f) { case Token_foreign: - return parse_foreign_block(f); - case Token_foreign_library: - case Token_foreign_system_library: return parse_foreign_decl(f); case Token_import: @@ -4859,10 +4859,23 @@ bool determine_path_from_string(Parser *p, AstNode *node, String base_dir, Strin gb_mutex_lock(&p->file_decl_mutex); defer (gb_mutex_unlock(&p->file_decl_mutex)); + + if (node->kind == AstNode_ForeignLibraryDecl) { + node->ForeignLibraryDecl.collection_name = collection_name; + } + if (collection_name.len > 0) { - if (!find_library_collection_path(collection_name, &base_dir)) { + if (collection_name == "system") { + if (node->kind != AstNode_ForeignLibraryDecl) { + syntax_error(node, "The library collection `system` is restrict for `foreign_library`"); + return false; + } else { + *path = file_str; + return true; + } + } else if (!find_library_collection_path(collection_name, &base_dir)) { // NOTE(bill): It's a naughty name - syntax_error(node, "Unknown library colleciton: `%.*s`", LIT(base_dir)); + syntax_error(node, "Unknown library collection: `%.*s`", LIT(collection_name)); return false; } } @@ -4935,13 +4948,19 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNod ast_node(fl, ForeignLibraryDecl, node); String file_str = fl->filepath.string; - if (!is_import_path_valid(file_str)) { - syntax_error(node, "Invalid `%.*s` path", LIT(fl->token.string)); - // NOTE(bill): It's a naughty name - decls[i] = ast_bad_decl(f, fl->filepath, fl->filepath); - } else { - fl->base_dir = base_dir; + fl->base_dir = base_dir; + fl->fullpath = file_str; + + if (fl->token.kind == Token_foreign_library) { + String foreign_path = {}; + bool ok = determine_path_from_string(p, node, base_dir, file_str, &foreign_path); + if (!ok) { + decls[i] = ast_bad_decl(f, fl->filepath, fl->filepath); + continue; + } + fl->fullpath = foreign_path; } + } else if (node->kind == AstNode_WhenStmt) { ast_node(ws, WhenStmt, node); parse_setup_file_when_stmt(p, f, base_dir, ws); diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 1560ac857..81edabe28 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -88,7 +88,6 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \ TOKEN_KIND(Token_export, "export"), \ TOKEN_KIND(Token_foreign, "foreign"), \ TOKEN_KIND(Token_foreign_library, "foreign_library"), \ - TOKEN_KIND(Token_foreign_system_library, "foreign_system_library"), \ TOKEN_KIND(Token_type, "type"), \ TOKEN_KIND(Token_when, "when"), \ TOKEN_KIND(Token_if, "if"), \ |