From 26ea8f6dcb4f49565817fa5a06187e228c8ee693 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 15 Oct 2017 12:11:33 +0100 Subject: [PATCH] Syntax: Replace `foreign_system_library "kernel.lib"` to `foreign_library "system:kernel.lib"`; Remove keyword: `foreign_system_library` --- core/opengl.odin | 4 +-- core/os_linux.odin | 4 +-- core/os_x.odin | 4 +-- core/sys/wgl.odin | 2 +- core/sys/windows.odin | 10 +++---- src/checker.cpp | 55 ++++++++++++++++++++----------------- src/main.cpp | 6 +++++ src/parser.cpp | 63 ++++++++++++++++++++++++++++--------------- src/tokenizer.cpp | 1 - 9 files changed, 90 insertions(+), 59 deletions(-) diff --git a/core/opengl.odin b/core/opengl.odin index 96f5a977f..8f7750712 100644 --- a/core/opengl.odin +++ b/core/opengl.odin @@ -1,9 +1,9 @@ when ODIN_OS == "windows" { - foreign_system_library lib "opengl32.lib" + foreign_library lib "system:opengl32.lib" import win32 "core:sys/windows.odin" import "core:sys/wgl.odin" } else when ODIN_OS == "linux" { - foreign_system_library lib "gl" + foreign_library lib "system:gl" } export "core:opengl_constants.odin" diff --git a/core/os_linux.odin b/core/os_linux.odin index 6e5c41c98..7c69ba9f1 100644 --- a/core/os_linux.odin +++ b/core/os_linux.odin @@ -1,5 +1,5 @@ -foreign_system_library dl "dl" -foreign_system_library libc "c" +foreign_library dl "system:dl" +foreign_library libc "system:c" import "core:strings.odin" import "core:mem.odin" diff --git a/core/os_x.odin b/core/os_x.odin index f96ba631c..c99638394 100644 --- a/core/os_x.odin +++ b/core/os_x.odin @@ -1,5 +1,5 @@ -foreign_system_library dl "dl" -foreign_system_library libc "c" +foreign_library dl "system:dl" +foreign_library libc "system:c" import "core:strings.odin" import "core:mem.odin" diff --git a/core/sys/wgl.odin b/core/sys/wgl.odin index 1f2699458..e51c22d65 100644 --- a/core/sys/wgl.odin +++ b/core/sys/wgl.odin @@ -1,5 +1,5 @@ when ODIN_OS == "windows" { - foreign_system_library "opengl32.lib" + foreign_library "system:opengl32.lib" } using import "core:sys/windows.odin" diff --git a/core/sys/windows.odin b/core/sys/windows.odin index 59bf6141f..c3bb61a04 100644 --- a/core/sys/windows.odin +++ b/core/sys/windows.odin @@ -1,9 +1,9 @@ when ODIN_OS == "windows" { - foreign_system_library "kernel32.lib" - foreign_system_library "user32.lib" - foreign_system_library "gdi32.lib" - foreign_system_library "winmm.lib" - foreign_system_library "shell32.lib" + foreign_library "system:kernel32.lib" + foreign_library "system:user32.lib" + foreign_library "system:gdi32.lib" + foreign_library "system:winmm.lib" + foreign_library "system:shell32.lib" } Handle :: rawptr; 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; - - if (fl->token.kind == Token_foreign_library) { - gbAllocator a = heap_allocator(); // TODO(bill): Change this allocator - - 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; - } - } - file_str = import_file; - } - - String library_name = path_to_entity_name(fl->library_name.string, file_str); + 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)); - } 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); + return; } + + 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'; + + 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; + } + } + + 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 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, Arrayfilepath.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"), \