Browse Source

Replace `#optional_second` with `#optional_allocator_error`

gingerBill 2 years ago
parent
commit
ef372bd861

+ 1 - 1
core/odin/ast/ast.odin

@@ -6,7 +6,7 @@ Proc_Tag :: enum {
 	Bounds_Check,
 	Bounds_Check,
 	No_Bounds_Check,
 	No_Bounds_Check,
 	Optional_Ok,
 	Optional_Ok,
-	Optional_Second,
+	Optional_Allocator_Error,
 }
 }
 Proc_Tags :: distinct bit_set[Proc_Tag; u32]
 Proc_Tags :: distinct bit_set[Proc_Tag; u32]
 
 

+ 1 - 1
core/odin/parser/parser.odin

@@ -2062,7 +2062,7 @@ parse_proc_tags :: proc(p: ^Parser) -> (tags: ast.Proc_Tags) {
 		case "bounds_check":    tags += {.Bounds_Check}
 		case "bounds_check":    tags += {.Bounds_Check}
 		case "no_bounds_check": tags += {.No_Bounds_Check}
 		case "no_bounds_check": tags += {.No_Bounds_Check}
 		case "optional_ok":     tags += {.Optional_Ok}
 		case "optional_ok":     tags += {.Optional_Ok}
-		case "optional_second": tags += {.Optional_Second}
+		case "optional_allocator_error": tags += {.Optional_Allocator_Error}
 		case:
 		case:
 		}
 		}
 	}
 	}

+ 8 - 8
core/runtime/core_builtin.odin

@@ -189,7 +189,7 @@ delete :: proc{
 // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
 // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
 // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
 // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
 @builtin
 @builtin
-new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_second {
+new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_allocator_error {
 	return new_aligned(T, align_of(T), allocator, loc)
 	return new_aligned(T, align_of(T), allocator, loc)
 }
 }
 new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
 new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
@@ -199,7 +199,7 @@ new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator,
 }
 }
 
 
 @builtin
 @builtin
-new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_second {
+new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_allocator_error {
 	t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
 	t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
 	t = (^T)(raw_data(t_data))
 	t = (^T)(raw_data(t_data))
 	if t != nil {
 	if t != nil {
@@ -210,7 +210,7 @@ new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_locat
 
 
 DEFAULT_RESERVE_CAPACITY :: 16
 DEFAULT_RESERVE_CAPACITY :: 16
 
 
-make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
+make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
 	make_slice_error_loc(loc, len)
 	make_slice_error_loc(loc, len)
 	data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
 	data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
 	if data == nil && size_of(E) != 0 {
 	if data == nil && size_of(E) != 0 {
@@ -221,19 +221,19 @@ make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocat
 }
 }
 
 
 @(builtin)
 @(builtin)
-make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
+make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
 	return make_aligned(T, len, align_of(E), allocator, loc)
 	return make_aligned(T, len, align_of(E), allocator, loc)
 }
 }
 @(builtin)
 @(builtin)
-make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
+make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
 	return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
 	return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
 }
 }
 @(builtin)
 @(builtin)
-make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
+make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
 	return make_dynamic_array_len_cap(T, len, len, allocator, loc)
 	return make_dynamic_array_len_cap(T, len, len, allocator, loc)
 }
 }
 @(builtin)
 @(builtin)
-make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_second {
+make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
 	make_dynamic_array_error_loc(loc, len, cap)
 	make_dynamic_array_error_loc(loc, len, cap)
 	data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
 	data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
 	s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
 	s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
@@ -253,7 +253,7 @@ make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPAC
 	return m
 	return m
 }
 }
 @(builtin)
 @(builtin)
-make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) #optional_second {
+make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) #optional_allocator_error {
 	make_slice_error_loc(loc, len)
 	make_slice_error_loc(loc, len)
 	data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
 	data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
 	if data == nil && size_of(E) != 0 {
 	if data == nil && size_of(E) != 0 {

+ 2 - 2
core/runtime/core_builtin_soa.odin

@@ -79,7 +79,7 @@ raw_soa_footer :: proc{
 
 
 
 
 @builtin
 @builtin
-make_soa_aligned :: proc($T: typeid/#soa[]$E, length: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_second {
+make_soa_aligned :: proc($T: typeid/#soa[]$E, length: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
 	if length <= 0 {
 	if length <= 0 {
 		return
 		return
 	}
 	}
@@ -138,7 +138,7 @@ make_soa_aligned :: proc($T: typeid/#soa[]$E, length: int, alignment: int, alloc
 }
 }
 
 
 @builtin
 @builtin
-make_soa_slice :: proc($T: typeid/#soa[]$E, length: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_second {
+make_soa_slice :: proc($T: typeid/#soa[]$E, length: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
 	return make_soa_aligned(T, length, align_of(E), allocator, loc)
 	return make_soa_aligned(T, length, align_of(E), allocator, loc)
 }
 }
 
 

+ 9 - 10
src/check_type.cpp

@@ -2006,22 +2006,21 @@ bool check_procedure_type(CheckerContext *ctx, Type *type, Ast *proc_type_node,
 			}
 			}
 		}
 		}
 	}
 	}
-	if (pt->tags & ProcTag_optional_second) {
+	if (pt->tags & ProcTag_optional_allocator_error) {
 		if (optional_ok) {
 		if (optional_ok) {
-			error(proc_type_node, "A procedure type cannot have both an #optional_ok tag and #optional_second");
+			error(proc_type_node, "A procedure type cannot have both an #optional_ok tag and #optional_allocator_error");
 		}
 		}
 		optional_ok = true;
 		optional_ok = true;
 		if (result_count != 2) {
 		if (result_count != 2) {
-			error(proc_type_node, "A procedure type with the #optional_second tag requires 2 return values, got %td", result_count);
+			error(proc_type_node, "A procedure type with the #optional_allocator_error tag requires 2 return values, got %td", result_count);
 		} else {
 		} else {
-			bool ok = false;
-			AstFile *file = proc_type_node->file();
-			if (file && file->pkg) {
-				ok = file->pkg->scope == ctx->info->runtime_package->scope;
-			}
+			init_mem_allocator(c->checker);
 
 
-			if (!ok) {
-				error(proc_type_node, "A procedure type with the #optional_second may only be allowed within 'package runtime'");
+			Type *type = results->Tuple.variables[1]->type;
+			if (!are_types_identical(type, t_allocator_error)) {
+				gbString t = type_to_string(type);
+				error(proc_type_node, "A procedure type with the #optional_allocator_error expects a `runtime.Allocator_Error`, got '%s'", t);
+				gb_string_free(t);
 			}
 			}
 		}
 		}
 	}
 	}

+ 2 - 11
src/checker.cpp

@@ -2809,17 +2809,9 @@ void init_mem_allocator(Checker *c) {
 	if (t_allocator != nullptr) {
 	if (t_allocator != nullptr) {
 		return;
 		return;
 	}
 	}
-	AstPackage *pkg = get_core_package(&c->info, str_lit("runtime"));
-
-	String name = str_lit("Allocator");
-	Entity *e = scope_lookup_current(pkg->scope, name);
-	if (e == nullptr) {
-		compiler_error("Could not find type declaration for '%.*s'\n", LIT(name));
-		// NOTE(bill): This will exit the program as it's cannot continue without it!
-	}
-
-	t_allocator = e->type;
+	t_allocator = find_core_type(c, str_lit("Allocator"));
 	t_allocator_ptr = alloc_type_pointer(t_allocator);
 	t_allocator_ptr = alloc_type_pointer(t_allocator);
+	t_allocator_error = find_core_type(c, str_lit("Allocator_Error"));
 }
 }
 
 
 void init_core_context(Checker *c) {
 void init_core_context(Checker *c) {
@@ -2827,7 +2819,6 @@ void init_core_context(Checker *c) {
 		return;
 		return;
 	}
 	}
 	t_context = find_core_type(c, str_lit("Context"));
 	t_context = find_core_type(c, str_lit("Context"));
-	GB_ASSERT(t_context != nullptr);
 	t_context_ptr = alloc_type_pointer(t_context);
 	t_context_ptr = alloc_type_pointer(t_context);
 }
 }
 
 

+ 1 - 1
src/parser.cpp

@@ -1857,7 +1857,7 @@ void parse_proc_tags(AstFile *f, u64 *tags) {
 
 
 		if (false) {}
 		if (false) {}
 		ELSE_IF_ADD_TAG(optional_ok)
 		ELSE_IF_ADD_TAG(optional_ok)
-		ELSE_IF_ADD_TAG(optional_second)
+		ELSE_IF_ADD_TAG(optional_allocator_error)
 		ELSE_IF_ADD_TAG(require_results)
 		ELSE_IF_ADD_TAG(require_results)
 		ELSE_IF_ADD_TAG(bounds_check)
 		ELSE_IF_ADD_TAG(bounds_check)
 		ELSE_IF_ADD_TAG(no_bounds_check)
 		ELSE_IF_ADD_TAG(no_bounds_check)

+ 1 - 1
src/parser.hpp

@@ -232,7 +232,7 @@ enum ProcTag {
 
 
 	ProcTag_require_results = 1<<4,
 	ProcTag_require_results = 1<<4,
 	ProcTag_optional_ok     = 1<<5,
 	ProcTag_optional_ok     = 1<<5,
-	ProcTag_optional_second = 1<<6,
+	ProcTag_optional_allocator_error = 1<<6,
 };
 };
 
 
 enum ProcCallingConvention : i32 {
 enum ProcCallingConvention : i32 {

+ 1 - 0
src/types.cpp

@@ -681,6 +681,7 @@ gb_global Type *t_allocator                      = nullptr;
 gb_global Type *t_allocator_ptr                  = nullptr;
 gb_global Type *t_allocator_ptr                  = nullptr;
 gb_global Type *t_context                        = nullptr;
 gb_global Type *t_context                        = nullptr;
 gb_global Type *t_context_ptr                    = nullptr;
 gb_global Type *t_context_ptr                    = nullptr;
+gb_global Type *t_allocator_error                = nullptr;
 
 
 gb_global Type *t_source_code_location           = nullptr;
 gb_global Type *t_source_code_location           = nullptr;
 gb_global Type *t_source_code_location_ptr       = nullptr;
 gb_global Type *t_source_code_location_ptr       = nullptr;