Browse Source

Add `@(priority_index=<int>)` for `foreign import`

gingerBill 3 years ago
parent
commit
e4743b15b1
4 changed files with 20 additions and 0 deletions
  1. 11 0
      src/checker.cpp
  2. 1 0
      src/checker.hpp
  3. 1 0
      src/entity.cpp
  4. 7 0
      src/llvm_backend.cpp

+ 11 - 0
src/checker.cpp

@@ -4414,6 +4414,14 @@ DECL_ATTRIBUTE_PROC(foreign_import_decl_attribute) {
 		}
 		}
 		ac->require_declaration = true;
 		ac->require_declaration = true;
 		return true;
 		return true;
+	} else if (name == "priority_index") {
+		ExactValue ev = check_decl_attribute_value(c, value);
+		if (ev.kind != ExactValue_Integer) {
+			error(elem, "Expected an integer value for '%.*s'", LIT(name));
+		} else {
+			ac->foreign_import_priority_index = exact_value_to_i64(ev);
+		}
+		return true;
 	}
 	}
 	return false;
 	return false;
 }
 }
@@ -4470,6 +4478,9 @@ void check_add_foreign_import_decl(CheckerContext *ctx, Ast *decl) {
 		mpmc_enqueue(&ctx->info->required_foreign_imports_through_force_queue, e);
 		mpmc_enqueue(&ctx->info->required_foreign_imports_through_force_queue, e);
 		add_entity_use(ctx, nullptr, e);
 		add_entity_use(ctx, nullptr, e);
 	}
 	}
+	if (ac.foreign_import_priority_index != 0) {
+		e->LibraryName.priority_index = ac.foreign_import_priority_index;
+	}
 
 
 	if (has_asm_extension(fullpath)) {
 	if (has_asm_extension(fullpath)) {
 		if (build_context.metrics.arch != TargetArch_amd64 ||
 		if (build_context.metrics.arch != TargetArch_amd64 ||

+ 1 - 0
src/checker.hpp

@@ -118,6 +118,7 @@ struct AttributeContext {
 	bool    init                : 1;
 	bool    init                : 1;
 	bool    set_cold            : 1;
 	bool    set_cold            : 1;
 	u32 optimization_mode; // ProcedureOptimizationMode
 	u32 optimization_mode; // ProcedureOptimizationMode
+	i64 foreign_import_priority_index;
 
 
 	String  objc_class;
 	String  objc_class;
 	String  objc_name;
 	String  objc_name;

+ 1 - 0
src/entity.cpp

@@ -252,6 +252,7 @@ struct Entity {
 		struct {
 		struct {
 			Slice<String> paths;
 			Slice<String> paths;
 			String name;
 			String name;
+			i64 priority_index;
 		} LibraryName;
 		} LibraryName;
 		i32 Nil;
 		i32 Nil;
 		struct {
 		struct {

+ 7 - 0
src/llvm_backend.cpp

@@ -43,6 +43,13 @@ GB_COMPARE_PROC(foreign_library_cmp) {
 	if (x == y) {
 	if (x == y) {
 		return 0;
 		return 0;
 	}
 	}
+	GB_ASSERT(x->kind == Entity_LibraryName);
+	GB_ASSERT(y->kind == Entity_LibraryName);
+
+	cmp = i64_cmp(x->LibraryName.priority_index, y->LibraryName.priority_index);
+	if (cmp) {
+		return cmp;
+	}
 
 
 	if (x->pkg != y->pkg) {
 	if (x->pkg != y->pkg) {
 		isize order_x = x->pkg ? x->pkg->order : 0;
 		isize order_x = x->pkg ? x->pkg->order : 0;