Browse Source

Change `foreign_library` to `foreign import`

gingerBill 7 years ago
parent
commit
85f7c2d040
8 changed files with 72 additions and 75 deletions
  1. 2 2
      core/opengl.odin
  2. 2 2
      core/os_linux.odin
  3. 2 2
      core/os_x.odin
  4. 1 1
      core/sys/wgl.odin
  5. 5 5
      core/sys/windows.odin
  6. 9 9
      src/checker.cpp
  7. 51 53
      src/parser.cpp
  8. 0 1
      src/tokenizer.cpp

+ 2 - 2
core/opengl.odin

@@ -1,9 +1,9 @@
 when ODIN_OS == "windows" {
 when ODIN_OS == "windows" {
-	foreign_library lib "system:opengl32.lib"
+	foreign import lib "system:opengl32.lib"
 	import win32 "core:sys/windows.odin"
 	import win32 "core:sys/windows.odin"
 	import "core:sys/wgl.odin"
 	import "core:sys/wgl.odin"
 } else when ODIN_OS == "linux" {
 } else when ODIN_OS == "linux" {
-	foreign_library lib "system:gl"
+	foreign import lib "system:gl"
 }
 }
 
 
 export "core:opengl_constants.odin"
 export "core:opengl_constants.odin"

+ 2 - 2
core/os_linux.odin

@@ -1,5 +1,5 @@
-foreign_library dl   "system:dl"
-foreign_library libc "system:c"
+foreign import dl   "system:dl"
+foreign import libc "system:c"
 
 
 import "core:strings.odin"
 import "core:strings.odin"
 import "core:mem.odin"
 import "core:mem.odin"

+ 2 - 2
core/os_x.odin

@@ -1,5 +1,5 @@
-foreign_library dl   "system:dl"
-foreign_library libc "system:c"
+foreign import dl   "system:dl"
+foreign import libc "system:c"
 
 
 import "core:strings.odin"
 import "core:strings.odin"
 import "core:mem.odin"
 import "core:mem.odin"

+ 1 - 1
core/sys/wgl.odin

@@ -1,5 +1,5 @@
 when ODIN_OS == "windows" {
 when ODIN_OS == "windows" {
-	foreign_library "system:opengl32.lib"
+	foreign import "system:opengl32.lib"
 }
 }
 using import "core:sys/windows.odin"
 using import "core:sys/windows.odin"
 
 

+ 5 - 5
core/sys/windows.odin

@@ -1,9 +1,9 @@
 when ODIN_OS == "windows" {
 when ODIN_OS == "windows" {
-	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"
+	foreign import "system:kernel32.lib"
+	foreign import "system:user32.lib"
+	foreign import "system:gdi32.lib"
+	foreign import "system:winmm.lib"
+	foreign import "system:shell32.lib"
 }
 }
 
 
 Handle    :: rawptr;
 Handle    :: rawptr;

+ 9 - 9
src/checker.cpp

@@ -520,7 +520,7 @@ void add_implicit_entity(Checker *c, AstNode *node, Entity *e);
 
 
 void check_add_import_decl(Checker *c, AstNodeImportDecl *id);
 void check_add_import_decl(Checker *c, AstNodeImportDecl *id);
 void check_add_export_decl(Checker *c, AstNodeExportDecl *ed);
 void check_add_export_decl(Checker *c, AstNodeExportDecl *ed);
-void check_add_foreign_library_decl(Checker *c, AstNode *decl);
+void check_add_foreign_import_decl(Checker *c, AstNode *decl);
 
 
 
 
 void init_declaration_info(DeclInfo *d, Scope *scope, DeclInfo *parent) {
 void init_declaration_info(DeclInfo *d, Scope *scope, DeclInfo *parent) {
@@ -2117,14 +2117,14 @@ void check_collect_entities(Checker *c, Array<AstNode *> nodes) {
 			}
 			}
 		case_end;
 		case_end;
 
 
-		case_ast_node(fl, ForeignLibraryDecl, decl);
+		case_ast_node(fl, ForeignImportDecl, decl);
 			if (!c->context.scope->is_file) {
 			if (!c->context.scope->is_file) {
 				error(decl, "%.*s declarations are only allowed in the file scope", LIT(fl->token.string));
 				error(decl, "%.*s declarations are only allowed in the file scope", LIT(fl->token.string));
 				// NOTE(bill): _Should_ be caught by the parser
 				// NOTE(bill): _Should_ be caught by the parser
 				// TODO(bill): Better error handling if it isn't
 				// TODO(bill): Better error handling if it isn't
 				continue;
 				continue;
 			}
 			}
-			check_add_foreign_library_decl(c, decl);
+			check_add_foreign_import_decl(c, decl);
 		case_end;
 		case_end;
 
 
 		case_ast_node(fb, ForeignBlockDecl, decl);
 		case_ast_node(fb, ForeignBlockDecl, decl);
@@ -2598,8 +2598,8 @@ void check_add_export_decl(Checker *c, AstNodeExportDecl *ed) {
 	scope->has_been_imported = true;
 	scope->has_been_imported = true;
 }
 }
 
 
-void check_add_foreign_library_decl(Checker *c, AstNode *decl) {
-	ast_node(fl, ForeignLibraryDecl, decl);
+void check_add_foreign_import_decl(Checker *c, AstNode *decl) {
+	ast_node(fl, ForeignImportDecl, decl);
 
 
 	if (fl->been_handled) return;
 	if (fl->been_handled) return;
 	fl->been_handled = true;
 	fl->been_handled = true;
@@ -2726,8 +2726,8 @@ void check_delayed_file_import_entity(Checker *c, AstNode *decl) {
 		check_add_export_decl(c, ed);
 		check_add_export_decl(c, ed);
 	case_end;
 	case_end;
 
 
-	case_ast_node(fl, ForeignLibraryDecl, decl);
-		check_add_foreign_library_decl(c, decl);
+	case_ast_node(fl, ForeignImportDecl, decl);
+		check_add_foreign_import_decl(c, decl);
 	case_end;
 	case_end;
 	}
 	}
 }
 }
@@ -2796,8 +2796,8 @@ bool collect_file_decls(Checker *c, Array<AstNode *> decls) {
 			check_add_export_decl(c, ed);
 			check_add_export_decl(c, ed);
 		case_end;
 		case_end;
 
 
-		case_ast_node(fl, ForeignLibraryDecl, decl);
-			check_add_foreign_library_decl(c, decl);
+		case_ast_node(fl, ForeignImportDecl, decl);
+			check_add_foreign_import_decl(c, decl);
 		case_end;
 		case_end;
 
 
 		case_ast_node(fb, ForeignBlockDecl, decl);
 		case_ast_node(fb, ForeignBlockDecl, decl);

+ 51 - 53
src/parser.cpp

@@ -372,7 +372,7 @@ AST_NODE_KIND(_DeclBegin,      "", i32) \
 		CommentGroup docs;      \
 		CommentGroup docs;      \
 		CommentGroup comment;   \
 		CommentGroup comment;   \
 	}) \
 	}) \
-	AST_NODE_KIND(ForeignLibraryDecl, "foreign library declaration", struct { \
+	AST_NODE_KIND(ForeignImportDecl, "foreign import declaration", struct { \
 		Token    token;           \
 		Token    token;           \
 		Token    filepath;        \
 		Token    filepath;        \
 		Token    library_name;    \
 		Token    library_name;    \
@@ -596,7 +596,7 @@ Token ast_node_token(AstNode *node) {
 	case AstNode_ValueDecl:          return ast_node_token(node->ValueDecl.names[0]);
 	case AstNode_ValueDecl:          return ast_node_token(node->ValueDecl.names[0]);
 	case AstNode_ImportDecl:         return node->ImportDecl.token;
 	case AstNode_ImportDecl:         return node->ImportDecl.token;
 	case AstNode_ExportDecl:         return node->ExportDecl.token;
 	case AstNode_ExportDecl:         return node->ExportDecl.token;
-	case AstNode_ForeignLibraryDecl: return node->ForeignLibraryDecl.token;
+	case AstNode_ForeignImportDecl:  return node->ForeignImportDecl.token;
 
 
 	case AstNode_ForeignBlockDecl:   return node->ForeignBlockDecl.token;
 	case AstNode_ForeignBlockDecl:   return node->ForeignBlockDecl.token;
 
 
@@ -1568,14 +1568,14 @@ AstNode *ast_export_decl(AstFile *f, Token token, Token relpath,
 	return result;
 	return result;
 }
 }
 
 
-AstNode *ast_foreign_library_decl(AstFile *f, Token token, Token filepath, Token library_name,
-                                  CommentGroup docs, CommentGroup comment) {
-	AstNode *result = make_ast_node(f, AstNode_ForeignLibraryDecl);
-	result->ForeignLibraryDecl.token        = token;
-	result->ForeignLibraryDecl.filepath     = filepath;
-	result->ForeignLibraryDecl.library_name = library_name;
-	result->ForeignLibraryDecl.docs         = docs;
-	result->ForeignLibraryDecl.comment      = comment;
+AstNode *ast_foreign_import_decl(AstFile *f, Token token, Token filepath, Token library_name,
+                                 CommentGroup docs, CommentGroup comment) {
+	AstNode *result = make_ast_node(f, AstNode_ForeignImportDecl);
+	result->ForeignImportDecl.token        = token;
+	result->ForeignImportDecl.filepath     = filepath;
+	result->ForeignImportDecl.library_name = library_name;
+	result->ForeignImportDecl.docs         = docs;
+	result->ForeignImportDecl.comment      = comment;
 	return result;
 	return result;
 }
 }
 
 
@@ -1772,8 +1772,10 @@ void fix_advance_to_next_stmt(AstFile *f) {
 		case Token_Semicolon:
 		case Token_Semicolon:
 			return;
 			return;
 
 
+
 		case Token_foreign:
 		case Token_foreign:
-		case Token_foreign_library:
+		case Token_import:
+		case Token_export:
 
 
 		case Token_if:
 		case Token_if:
 		case Token_for:
 		case Token_for:
@@ -1852,7 +1854,7 @@ bool is_semicolon_optional_for_node(AstFile *f, AstNode *s) {
 
 
 	case AstNode_ImportDecl:
 	case AstNode_ImportDecl:
 	case AstNode_ExportDecl:
 	case AstNode_ExportDecl:
-	case AstNode_ForeignLibraryDecl:
+	case AstNode_ForeignImportDecl:
 		return true;
 		return true;
 
 
 	case AstNode_ValueDecl:
 	case AstNode_ValueDecl:
@@ -3037,9 +3039,8 @@ void parse_foreign_block_decl(AstFile *f, Array<AstNode *> *decls) {
 	}
 	}
 }
 }
 
 
-AstNode *parse_foreign_block(AstFile *f) {
+AstNode *parse_foreign_block(AstFile *f, Token token) {
 	CommentGroup docs = f->lead_comment;
 	CommentGroup docs = f->lead_comment;
-	Token token = expect_token(f, Token_foreign);
 	AstNode *foreign_library = parse_ident(f);
 	AstNode *foreign_library = parse_ident(f);
 	Token open = {};
 	Token open = {};
 	Token close = {};
 	Token close = {};
@@ -4405,43 +4406,41 @@ AstNode *parse_export_decl(AstFile *f) {
 
 
 AstNode *parse_foreign_decl(AstFile *f) {
 AstNode *parse_foreign_decl(AstFile *f) {
 	CommentGroup docs = f->lead_comment;
 	CommentGroup docs = f->lead_comment;
-	Token token = {};
-	switch (f->curr_token.kind) {
-	case Token_foreign:
-		return parse_foreign_block(f);
-
-	case Token_foreign_library:
-		token = advance_token(f);
-		break;
-	default:
-		token = advance_token(f);
-		syntax_error(token, "Expected either foreign or foreign_library, got `%.*s`", LIT(token.string));
-		return ast_bad_decl(f, token, token);
-	}
-
-	Token lib_name = {};
+	Token token = expect_token(f, Token_foreign);
 
 
 	switch (f->curr_token.kind) {
 	switch (f->curr_token.kind) {
 	case Token_Ident:
 	case Token_Ident:
-		lib_name = advance_token(f);
-		break;
-	default:
-		lib_name.pos = f->curr_token.pos;
-		break;
-	}
-	if (is_blank_ident(lib_name)) {
-		syntax_error(lib_name, "Illegal foreign_library name: `_`");
+		return parse_foreign_block(f, token);
+
+	case Token_import: {
+		Token import_token = expect_token(f, Token_import);
+		Token lib_name = {};
+		switch (f->curr_token.kind) {
+		case Token_Ident:
+			lib_name = advance_token(f);
+			break;
+		default:
+			lib_name.pos = token.pos;
+			break;
+		}
+		if (is_blank_ident(lib_name)) {
+			syntax_error(lib_name, "Illegal foreign_library name: `_`");
+		}
+		Token file_path = expect_token(f, Token_String);
+		AstNode *s = nullptr;
+		if (f->curr_proc != nullptr) {
+			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_import_decl(f, token, file_path, lib_name, docs, f->line_comment);
+		}
+		expect_semicolon(f, s);
+		return s;
 	}
 	}
-	Token file_path = expect_token(f, Token_String);
-	AstNode *s = nullptr;
-	if (f->curr_proc != nullptr) {
-		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);
 	}
 	}
-	expect_semicolon(f, s);
-	return s;
+
+	syntax_error(token, "Invalid foreign declaration");
+	return ast_bad_decl(f, token, f->curr_token);
 }
 }
 
 
 
 
@@ -4471,7 +4470,6 @@ AstNode *parse_stmt(AstFile *f) {
 
 
 
 
 	case Token_foreign:
 	case Token_foreign:
-	case Token_foreign_library:
 		return parse_foreign_decl(f);
 		return parse_foreign_decl(f);
 
 
 	case Token_import:
 	case Token_import:
@@ -4860,13 +4858,13 @@ bool determine_path_from_string(Parser *p, AstNode *node, String base_dir, Strin
 	defer (gb_mutex_unlock(&p->file_decl_mutex));
 	defer (gb_mutex_unlock(&p->file_decl_mutex));
 
 
 
 
-	if (node->kind == AstNode_ForeignLibraryDecl) {
-		node->ForeignLibraryDecl.collection_name = collection_name;
+	if (node->kind == AstNode_ForeignImportDecl) {
+		node->ForeignImportDecl.collection_name = collection_name;
 	}
 	}
 
 
 	if (collection_name.len > 0) {
 	if (collection_name.len > 0) {
 		if (collection_name == "system") {
 		if (collection_name == "system") {
-			if (node->kind != AstNode_ForeignLibraryDecl) {
+			if (node->kind != AstNode_ForeignImportDecl) {
 				syntax_error(node, "The library collection `system` is restrict for `foreign_library`");
 				syntax_error(node, "The library collection `system` is restrict for `foreign_library`");
 				return false;
 				return false;
 			} else {
 			} else {
@@ -4944,14 +4942,14 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNod
 
 
 			ed->fullpath = export_path;
 			ed->fullpath = export_path;
 			try_add_import_path(p, export_path, original_string, ast_node_token(node).pos);
 			try_add_import_path(p, export_path, original_string, ast_node_token(node).pos);
-		} else if (node->kind == AstNode_ForeignLibraryDecl) {
-			ast_node(fl, ForeignLibraryDecl, node);
+		} else if (node->kind == AstNode_ForeignImportDecl) {
+			ast_node(fl, ForeignImportDecl, node);
 
 
 			String file_str = fl->filepath.string;
 			String file_str = fl->filepath.string;
 			fl->base_dir = base_dir;
 			fl->base_dir = base_dir;
 			fl->fullpath = file_str;
 			fl->fullpath = file_str;
 
 
-			if (fl->token.kind == Token_foreign_library) {
+			if (fl->collection_name != "system") {
 				String foreign_path = {};
 				String foreign_path = {};
 				bool ok = determine_path_from_string(p, node, base_dir, file_str, &foreign_path);
 				bool ok = determine_path_from_string(p, node, base_dir, file_str, &foreign_path);
 				if (!ok) {
 				if (!ok) {

+ 0 - 1
src/tokenizer.cpp

@@ -87,7 +87,6 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
 	TOKEN_KIND(Token_import,                 "import"),                 \
 	TOKEN_KIND(Token_import,                 "import"),                 \
 	TOKEN_KIND(Token_export,                 "export"),                 \
 	TOKEN_KIND(Token_export,                 "export"),                 \
 	TOKEN_KIND(Token_foreign,                "foreign"),                \
 	TOKEN_KIND(Token_foreign,                "foreign"),                \
-	TOKEN_KIND(Token_foreign_library,        "foreign_library"),        \
 	TOKEN_KIND(Token_type,                   "type"),                   \
 	TOKEN_KIND(Token_type,                   "type"),                   \
 	TOKEN_KIND(Token_when,                   "when"),                   \
 	TOKEN_KIND(Token_when,                   "when"),                   \
 	TOKEN_KIND(Token_if,                     "if"),                     \
 	TOKEN_KIND(Token_if,                     "if"),                     \