From: Brendan Hansen Date: Sun, 12 Feb 2023 22:27:21 +0000 (-0600) Subject: cleaned up more of builtin.onyx X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=f355eaa88140f70487f32c62a2e6196dd7ca2f37;p=onyx.git cleaned up more of builtin.onyx --- diff --git a/core/builtin.onyx b/core/builtin.onyx index 04fc8533..e7926033 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -260,61 +260,73 @@ cfree :: (ptr: rawptr) => raw_free(context.allocator, ptr); // This cannot be used in a custom runtime, as the other core // packages are not included. #if runtime.runtime != .Custom { - new :: #match { - ($T: type_expr, allocator := context.allocator) -> ^T { - use package core.intrinsics.onyx { __initialize } - memory :: package core.memory + new :: #match #local {} - res := cast(^T) raw_alloc(allocator, sizeof T); - memory.set(res, 0, sizeof T); - __initialize(res); + #overload + new :: ($T: type_expr, allocator := context.allocator) -> ^T { + use package core.intrinsics.onyx { __initialize } + memory :: package core.memory - return res; - }, + res := cast(^T) raw_alloc(allocator, sizeof T); + memory.set(res, 0, sizeof T); + __initialize(res); - (T: type_expr, allocator := context.allocator) -> rawptr { - memory :: package core.memory - type_info :: package runtime.info - - info := type_info.get_type_info(T); - size := type_info.size_of(T); - if size == 0 do return null; - - res := raw_alloc(allocator, size); - memory.set(res, 0, size); - - if info.kind == .Struct { - s_info := cast(^type_info.Type_Info_Struct) info; - for s_info.members { - if it.default != null { - member_size := type_info.size_of(it.type); - memory.copy(cast(^u8) res + it.offset, it.default, member_size); - } + return res; + } + + #overload + new :: (T: type_expr, allocator := context.allocator) -> rawptr { + memory :: package core.memory + type_info :: package runtime.info + + info := type_info.get_type_info(T); + size := type_info.size_of(T); + if size == 0 do return null; + + res := raw_alloc(allocator, size); + memory.set(res, 0, size); + + if info.kind == .Struct { + s_info := cast(^type_info.Type_Info_Struct) info; + for s_info.members { + if it.default != null { + member_size := type_info.size_of(it.type); + memory.copy(cast(^u8) res + it.offset, it.default, member_size); } } - - return res; } + + return res; } new_temp :: macro (T: type_expr) => { return new(T, allocator=context.temp_allocator); } - make :: #match { - macro ($T: type_expr, allocator := context.allocator) => { - return __make_overload(cast(^T) null, allocator=allocator); - }, + make :: #match #local {} - macro ($T: type_expr, n: u32, allocator := context.allocator) => { - return __make_overload(cast(^T) null, n, allocator=allocator); - }, + #overload + make :: macro ($T: type_expr, allocator := context.allocator) => { + return __make_overload(cast(^T) null, allocator=allocator); } + #overload + make :: macro ($T: type_expr, n: u32, allocator := context.allocator) => { + return __make_overload(cast(^T) null, n, allocator=allocator); + } + + make_temp :: #match #local {} + + #overload make_temp :: macro (T: type_expr) => { return make(T, allocator=context.temp_allocator); } + #overload + make_temp :: macro (T: type_expr, n: u32) => { + return make(T, n, allocator=context.temp_allocator); + } + // // This is a rather unique way of using the type matching system // to select an overload. What is desired here is that when you say: