Browse Source

Comment based build tags for packages (basic and temporary)

gingerBill 7 years ago
parent
commit
6eb505a677

+ 10 - 0
core/mem/alloc.odin

@@ -47,6 +47,16 @@ free_map :: proc(m: $T/map[$K]$V, loc := #caller_location) {
 	free_ptr(raw.entries.data, loc);
 	free_ptr(raw.entries.data, loc);
 }
 }
 
 
+free :: proc[
+	free_ptr,
+	free_string,
+	free_cstring,
+	free_dynamic_array,
+	free_slice,
+	free_map,
+];
+
+
 
 
 
 
 default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, loc := #caller_location) -> rawptr {
 default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, loc := #caller_location) -> rawptr {

+ 0 - 0
core/opengl/opengl_constants.odin → core/opengl/constants.odin


+ 1 - 0
core/os/os_windows.odin

@@ -1,3 +1,4 @@
+// +build windows
 package os
 package os
 
 
 import "core:sys/win32"
 import "core:sys/win32"

+ 3 - 2
core/runtime/core.odin

@@ -1,3 +1,6 @@
+// This is the runtime code required by the compiler
+// IMPORTANT NOTE(bill): Do not change the order of any of this data
+// The compiler relies upon this _exact_ order
 package runtime
 package runtime
 
 
 import "core:os"
 import "core:os"
@@ -30,8 +33,6 @@ Calling_Convention :: enum {
 	Std         = 4,
 	Std         = 4,
 	Fast        = 5,
 	Fast        = 5,
 }
 }
-// IMPORTANT NOTE(bill): Do not change the order of any of this data
-// The compiler relies upon this _exact_ order
 
 
 Type_Info_Enum_Value :: union {
 Type_Info_Enum_Value :: union {
 	rune,
 	rune,

+ 1 - 1
examples/demo/demo.odin

@@ -735,7 +735,6 @@ deprecated_attribute :: proc() {
 	// foo_v1(1);
 	// foo_v1(1);
 }
 }
 
 
-
 main :: proc() {
 main :: proc() {
 	when true {
 	when true {
 		general_stuff();
 		general_stuff();
@@ -752,3 +751,4 @@ main :: proc() {
 		deprecated_attribute();
 		deprecated_attribute();
 	}
 	}
 }
 }
+

+ 1 - 0
src/build_settings.cpp

@@ -121,6 +121,7 @@ bool is_excluded_target_filename(String name) {
 	}
 	}
 
 
 
 
+
 	TargetOsKind   os1   = get_target_os_from_string(str1);
 	TargetOsKind   os1   = get_target_os_from_string(str1);
 	TargetArchKind arch1 = get_target_arch_from_string(str1);
 	TargetArchKind arch1 = get_target_arch_from_string(str1);
 	TargetOsKind   os2   = get_target_os_from_string(str2);
 	TargetOsKind   os2   = get_target_os_from_string(str2);

+ 7 - 7
src/checker.cpp

@@ -2263,23 +2263,23 @@ void check_all_global_entities(Checker *c) {
 			continue;
 			continue;
 		}
 		}
 
 
-
 		CheckerContext ctx = c->init_ctx;
 		CheckerContext ctx = c->init_ctx;
 
 
 		GB_ASSERT(d->scope->is_file);
 		GB_ASSERT(d->scope->is_file);
 		AstFile *file = d->scope->file;
 		AstFile *file = d->scope->file;
 		add_curr_ast_file(&ctx, file);
 		add_curr_ast_file(&ctx, file);
-		Scope *package_scope = file->pkg->scope;
+		AstPackage *pkg = file->pkg;
+
 		GB_ASSERT(ctx.pkg != nullptr);
 		GB_ASSERT(ctx.pkg != nullptr);
 		GB_ASSERT(e->pkg != nullptr);
 		GB_ASSERT(e->pkg != nullptr);
 
 
 		if (e->token.string == "main") {
 		if (e->token.string == "main") {
 			if (e->kind != Entity_Procedure) {
 			if (e->kind != Entity_Procedure) {
-				if (package_scope->is_init) {
+				if (pkg->kind == Package_Init) {
 					error(e->token, "'main' is reserved as the entry point procedure in the initial scope");
 					error(e->token, "'main' is reserved as the entry point procedure in the initial scope");
 					continue;
 					continue;
 				}
 				}
-			} else if (package_scope->is_global) {
+			} else if (pkg->kind == Package_Runtime) {
 				error(e->token, "'main' is reserved as the entry point procedure in the initial scope");
 				error(e->token, "'main' is reserved as the entry point procedure in the initial scope");
 				continue;
 				continue;
 			}
 			}
@@ -2290,7 +2290,7 @@ void check_all_global_entities(Checker *c) {
 		check_entity_decl(&ctx, e, d, nullptr);
 		check_entity_decl(&ctx, e, d, nullptr);
 
 
 
 
-		if (!package_scope->is_global) {
+		if (pkg->kind != Package_Runtime) {
 			processing_preload = false;
 			processing_preload = false;
 		}
 		}
 
 
@@ -3033,7 +3033,7 @@ void check_proc_bodies(Checker *c) {
 }
 }
 
 
 void check_parsed_files(Checker *c) {
 void check_parsed_files(Checker *c) {
-#if 1
+#if 0
 	Timings timings = {};
 	Timings timings = {};
 	timings_init(&timings, str_lit("check_parsed_files"), 16);
 	timings_init(&timings, str_lit("check_parsed_files"), 16);
 	defer ({
 	defer ({
@@ -3046,7 +3046,6 @@ void check_parsed_files(Checker *c) {
 #endif
 #endif
 
 
 	TIME_SECTION("map full filepaths to scope");
 	TIME_SECTION("map full filepaths to scope");
-
 	add_type_info_type(&c->init_ctx, t_invalid);
 	add_type_info_type(&c->init_ctx, t_invalid);
 
 
 	// Map full filepaths to Scopes
 	// Map full filepaths to Scopes
@@ -3146,6 +3145,7 @@ void check_parsed_files(Checker *c) {
 		}
 		}
 	}
 	}
 
 
+	TIME_SECTION("check for type cycles");
 	// NOTE(bill): Check for illegal cyclic type declarations
 	// NOTE(bill): Check for illegal cyclic type declarations
 	for_array(i, c->info.definitions) {
 	for_array(i, c->info.definitions) {
 		Entity *e = c->info.definitions[i];
 		Entity *e = c->info.definitions[i];

+ 82 - 6
src/parser.cpp

@@ -1063,6 +1063,8 @@ Token consume_comment(AstFile *f, isize *end_line_) {
 				end_line++;
 				end_line++;
 			}
 			}
 		}
 		}
+	} else {
+		end_line++;
 	}
 	}
 
 
 	if (end_line_) *end_line_ = end_line;
 	if (end_line_) *end_line_ = end_line;
@@ -1107,8 +1109,9 @@ void comsume_comment_groups(AstFile *f, Token prev) {
 	while (f->curr_token.kind == Token_Comment) {
 	while (f->curr_token.kind == Token_Comment) {
 		comment = consume_comment_group(f, 1, &end_line);
 		comment = consume_comment_group(f, 1, &end_line);
 	}
 	}
-
-	if (end_line+1 == f->curr_token.pos.line) {
+	if (end_line < 0) {
+		f->lead_comment = comment;
+	} else if (end_line+1 == f->curr_token.pos.line) {
 		f->lead_comment = comment;
 		f->lead_comment = comment;
 	}
 	}
 
 
@@ -4214,6 +4217,65 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNod
 	}
 	}
 }
 }
 
 
+bool parse_build_tag(Token token_for_pos, String s) {
+	String const prefix = str_lit("+build");
+	GB_ASSERT(string_starts_with(s, prefix));
+	s = string_trim_whitespace(substring(s, prefix.len, s.len));
+
+	if (s.len == 0) {
+		return true;
+	}
+
+	auto platforms = array_make<String>(heap_allocator());
+	defer (array_free(&platforms));
+
+	isize n = 0;
+	while (n < s.len) {
+		Rune rune = 0;
+		isize width = gb_utf8_decode(&s[n], s.len, &rune);
+		if (rune_is_whitespace(rune)) {
+			String f = substring(s, 0, n);
+			array_add(&platforms, f);
+			s = substring(s, n+width, s.len);
+			n = 0;
+			continue;
+		} else if (n+width == s.len) {
+			String f = substring(s, 0, n+width);
+			array_add(&platforms, f);
+			break;
+		}
+		n += width;
+	}
+
+
+	bool any_correct = false;
+	for_array(i, platforms) {
+		String p = platforms[i];
+		TargetOsKind   os   = get_target_os_from_string(p);
+		TargetArchKind arch = get_target_arch_from_string(p);
+		if (os != TargetOs_Invalid) {
+			GB_ASSERT(arch == TargetArch_Invalid);
+			any_correct = true;
+			if (os == build_context.target_os) {
+				return true;
+			}
+		} else if (arch != TargetArch_Invalid) {
+			any_correct = true;
+			if (arch == build_context.target_arch) {
+				return true;
+			}
+		}
+		if (os == TargetOs_Invalid && arch == TargetArch_Invalid) {
+			error(token_for_pos, "Invalid build tag platform: %.*s", LIT(p));
+		}
+	}
+	if (any_correct) {
+		return false;
+	}
+
+	return true;
+}
+
 bool parse_file(Parser *p, AstFile *f) {
 bool parse_file(Parser *p, AstFile *f) {
 	if (f->tokens.count == 0) {
 	if (f->tokens.count == 0) {
 		return true;
 		return true;
@@ -4247,8 +4309,26 @@ bool parse_file(Parser *p, AstFile *f) {
 	}
 	}
 	f->package_name = package_name.string;
 	f->package_name = package_name.string;
 
 
+	if (docs.list.count > 0) {
+		for_array(i, docs.list) {
+			Token tok = docs.list[i]; GB_ASSERT(tok.kind == Token_Comment);
+			String str = tok.string;
+			if (string_starts_with(str, str_lit("//"))) {
+				String lc = string_trim_whitespace(substring(str, 2, str.len));
+				if (lc.len > 0 && lc[0] == '+') {
+					if (string_starts_with(lc, str_lit("+build"))) {
+						if (!parse_build_tag(tok, lc)) {
+							return false;
+						}
+					}
+				}
+			}
+		}
+	}
+
 	AstNode *pd = ast_package_decl(f, f->package_token, package_name, docs, f->line_comment);
 	AstNode *pd = ast_package_decl(f, f->package_token, package_name, docs, f->line_comment);
 	expect_semicolon(f, pd);
 	expect_semicolon(f, pd);
+	f->pkg_decl = pd;
 
 
 	if (f->error_count > 0) {
 	if (f->error_count > 0) {
 		return false;
 		return false;
@@ -4346,10 +4426,6 @@ GB_THREAD_PROC(parse_worker_file_proc) {
 	return cast(isize)err;
 	return cast(isize)err;
 }
 }
 
 
-void add_shared_package(Parser *p, String name, TokenPos pos) {
-
-}
-
 ParseFileError parse_packages(Parser *p, String init_filename) {
 ParseFileError parse_packages(Parser *p, String init_filename) {
 	GB_ASSERT(init_filename.text[init_filename.len] == 0);
 	GB_ASSERT(init_filename.text[init_filename.len] == 0);
 
 

+ 1 - 0
src/parser.hpp

@@ -52,6 +52,7 @@ struct AstFile {
 	AstPackage *        pkg;
 	AstPackage *        pkg;
 	Scope *             scope;
 	Scope *             scope;
 
 
+	AstNode *           pkg_decl;
 	String              fullpath;
 	String              fullpath;
 	gbArena             arena;
 	gbArena             arena;
 	Tokenizer           tokenizer;
 	Tokenizer           tokenizer;