Compare commits

...

2 Commits

Author SHA1 Message Date
hl-valdemar 7f7de7a8d2 feat: infer complete ptrcast target types 2026-09-20 00:04:18 +02:00
hl-valdemar 5dac5baa05 fix: resolve distinct type factory backings 2026-09-20 00:04:18 +02:00
12 changed files with 216 additions and 70 deletions
+2 -2
View File
@@ -35,7 +35,7 @@ roadmap and milestone history.
- arrays `[N]T`, inferred-count arrays `[_]T`, sentinel arrays `[N;S]T`, compile-time expression array counts, slices `[]T` / `[;S]T`, single-item pointers `@T`, many-item pointers `*T`, and sentinel many-item pointers `[*;S]T`
- pointer mutability via `mut`, optional pointers as nullable pointers, pointer arithmetic for many-item pointers, postfix dereference `^`, and trapping optional unwrap `?`
- pointer-to-array `.len`, indexing, slicing, `.ptr` on slices and pointers-to-arrays, implicit address-taking for array-variable slices, and pointer/slice sentinel weakening
- `ptrcast!(T, ptr)` as a first-pass pointer-child retype that preserves optionality, pointer kind, mutability, and sentinel shape
- `ptrcast!(ptr)` infers its complete pointer result from context; `ptrcast!(P, ptr)` supplies `P` explicitly; both change the child type, then permit only ordinary implicit pointer conversions to the requested target
- unsafe `constcast!(value)` for restoring mutability to pointers, optional pointers, and slices without changing their child type or shape
- UTF-8 string literals as immutable pointers to static zero-terminated byte arrays, supporting `\\`, `\"`, `\n`, `\r`, `\t`, `\0`, and `\xNN` escapes, plus raw backtick multiline strings
- narrow immutable zero-terminated byte pointer/slice conversion to `*c_char` / `?*c_char` without general `u8`/`c_char` interchange
@@ -289,7 +289,7 @@ exactly once. Bare functions named `memcopy` or `memset` remain ordinary user fu
- non-plain C record layouts such as bitfields, packed records, flexible arrays, qualified fields, and C variadic record arguments
- arenas, pools, build-mode heap policy, and escaping-allocation diagnostics
- recursive type factories, reflection payloads beyond records/enums, and type-producing unions/enums
- broader Zig-style pointer/result casts beyond V1 `ptrcast!(T, ptr)`
- pointer-shape-changing casts beyond the safe implicit conversions accepted by `ptrcast!`
- sum-type ABI/layout polish, including dynamic tag-width shrinking, all-void channel collapse, and cross-module global-id determinism
- backed/C enum composition and must-consume fallible linting
- result-to-argument type-demand propagation through function call boundaries
+1 -1
View File
@@ -228,7 +228,7 @@ Current prototype features:
- `#` comments
- Immutable inferred/typed `::` bindings, mutable inferred/typed `:=` locals/globals, and `_` sinks
- Exact-width signed/unsigned integers, `f32`, `f64`, `isize`, `usize`, and loose integer-constrained `int`
- Target-dependent atomic `c_*` primitive types, `c_func`, complete `c_struct`, `opaque`, `anyopaque`, and V1 `ptrcast!(T, ptr)`
- Target-dependent atomic `c_*` primitive types, `c_func`, complete `c_struct`, `opaque`, `anyopaque`, and contextual or explicit full-result `ptrcast!`
- Arrays, sentinel arrays, single-item pointers, many-item pointers, sentinel many-item pointers, slices, sentinel slices, strings, character literals, optionals, and native structs
- String literals as immutable pointers to static zero-terminated byte arrays
- Pointer-preserving `.ptr`/`.len`, pointer-to-array indexing and slicing, postfix pointer dereference and optional unwrap, and keyed struct literals
+7 -5
View File
@@ -176,8 +176,8 @@
- `Name :: opaque` is the incomplete nominal record spelling; bodyless `c_struct` is invalid
- `anyopaque` is the erased object type used behind pointers for C `void*` and allocator contexts
- C `void` function results remain `void`; C `void*` / `const void*` import and render as `?*mut anyopaque` / `?*anyopaque`
- `ptrcast!(T, ptr)` preserves pointer shape and only changes the child type in v1
- future direction: generalize toward Zig-style arbitrary pointer-result casts once casts have a broader result-type story
- `ptrcast!(ptr)` infers its complete pointer result from context; `ptrcast!(P, ptr)` accepts an explicit complete pointer type
- both forms change the child type, then permit only ordinary implicit pointer conversions to the requested target; broader pointer-shape changes remain deferred
12. `undefined` as inspired by zig (implemented):
- allow mutable local and global declarations with `undefined`
@@ -973,7 +973,7 @@ For casts that bypass safety checks, Honey provides builtin functions:
| -- | -- | -- |
| `truncate(x, T)` | Keep low bits, discard rest | Never |
| `bitcast(x, T)` | Reinterpret bits, no cast | Sizes don't match (compile error) |
| `ptrcast!(T, p)` | Change a pointer's child type while preserving its shape | Invalid child or non-pointer operand (compile error) |
| `ptrcast!(p)` / `ptrcast!(P, p)` | Infer or explicitly supply a complete pointer target, changing the child before ordinary pointer coercion | Missing target context, invalid target, incompatible shape, or non-pointer operand (compile error) |
| `constcast!(p)` | Restore pointer or slice mutability | Non-pointer/slice operand (compile error) |
```honey
@@ -989,8 +989,10 @@ bits := bitcast(f, u32) # IEEE 754 representation
# pointer casts
buf *u8 := get_buffer()
ints *u32 := ptrcast!(u32, buf) # element type change, same pointer shape
writable *mut u8 := constcast!(buf) # explicit unsafe mutability restoration
ints *u32 := ptrcast!(buf) # target inferred from the declaration
raw ?*mut anyopaque := get_raw_buffer()
bytes := ptrcast!(?*mut u8, raw) # complete target supplied explicitly
writable *mut u8 := constcast!(buf) # explicit unsafe mutability restoration
```
## A word on multi-unwrap
+89 -24
View File
@@ -934,6 +934,30 @@ valid_ptrcast_child :: proc(checker: ^Checker, value: types.Type) -> bool {
types.is_opaque_struct(value, &checker.module.types))
}
ptrcast_target_child :: proc(checker: ^Checker, value: types.Type) -> (types.Type, bool) {
item, ok := types.node(&checker.module.types, types.resolve_alias(value, &checker.module.types))
if !ok {
return types.INVALID, false
}
if item.kind == .Optional {
item, ok = types.node(&checker.module.types, types.resolve_alias(item.child, &checker.module.types))
}
if !ok || item.kind != .Pointer || !valid_ptrcast_child(checker, item.child) {
return types.INVALID, false
}
return item.child, true
}
ptrcast_retyped_source :: proc(checker: ^Checker, source, target: types.Type) -> (types.Type, bool) {
child, child_ok := ptrcast_target_child(checker, target)
if !child_ok {
return types.INVALID, false
}
retyped, retyped_ok := types.replace_pointer_child(&checker.module.types, source, child)
return retyped, retyped_ok &&
can_implicitly_convert_type(checker, retyped, target)
}
valid_layout_type :: proc(checker: ^Checker, value: types.Type) -> bool {
return types.is_runtime_value(value, &checker.module.types)
}
@@ -3032,6 +3056,15 @@ infer_call_comptime_values :: proc(
{type_call=true, final=true},
}
for pass in evidence_passes {
if pass.final {
all_bound = true
for value_bound in bound {
all_bound = all_bound && value_bound
}
if all_bound {
continue
}
}
for arg_id, source_index in args {
param_index := call_param_index(mapping, source_index)
if param_index >= len(function.params) || param_index >= len(actual_args) {
@@ -4540,6 +4573,12 @@ validate_type_nodes :: proc(checker: ^Checker) {
node_count := len(checker.module.types.nodes)
for index in 0..<node_count {
item := checker.module.types.nodes[index]
if item.kind == .Distinct {
resolved := type_from_syntax(
checker, item.child, ast.Package_Id(item.pkg), ast.File_Id(item.file),
)
checker.module.types.nodes[index].child = resolved
}
if item.kind != .Struct && item.kind != .Union {
continue
}
@@ -5640,18 +5679,20 @@ infer_expr :: proc(
continue
}
if is_ptrcast_call(checker, expr) {
if len(expr.args) != 2 {
if len(expr.args) != 1 && len(expr.args) != 2 {
last = types.INVALID
_ = pop(&stack)
continue
}
child, child_ok := resolve_type_argument(checker, expr.args[0], pkg, file)
operand := infer_nested_expr(checker, expr.args[1], locals, pkg, file, demanded, local_types)
result := types.INVALID
if child_ok && valid_ptrcast_child(checker, child) {
result, _ = types.replace_pointer_child(&checker.module.types, operand, child)
target := frame.expected
operand_index := 0
if len(expr.args) == 2 {
target, _ = resolve_type_argument(checker, expr.args[0], pkg, file)
operand_index = 1
}
last = result
operand := infer_nested_expr(checker, expr.args[operand_index], locals, pkg, file, demanded, local_types)
_, target_ok := ptrcast_retyped_source(checker, operand, target)
last = target if target_ok else types.INVALID
_ = pop(&stack)
continue
}
@@ -7747,7 +7788,8 @@ sequence_item :: proc(value: types.Type, store: ^types.Store) -> (types.Node, bo
}
find_struct_field :: proc(checker: ^Checker, struct_type: types.Type, name: symbol.Id) -> (int, types.Field, bool) {
for field, index in types.fields_for(&checker.module.types, struct_type) {
record_type := types.runtime_representation(struct_type, &checker.module.types)
for field, index in types.fields_for(&checker.module.types, record_type) {
if field.name == u32(name) {
return index, field, true
}
@@ -7811,20 +7853,22 @@ find_struct_field_default :: proc(
}
find_tuple_field :: proc(checker: ^Checker, tuple_type: types.Type, index: u64) -> (int, types.Field, bool) {
item, ok := types.node(&checker.module.types, tuple_type)
record_type := types.runtime_representation(tuple_type, &checker.module.types)
item, ok := types.node(&checker.module.types, record_type)
if !ok || item.kind != .Struct || !item.tuple || index >= u64(item.field_count) {
return 0, {}, false
}
fields := types.fields_for(&checker.module.types, tuple_type)
fields := types.fields_for(&checker.module.types, record_type)
return int(index), fields[index], true
}
find_struct_field_slot :: proc(checker: ^Checker, struct_type: types.Type, name: symbol.Id) -> (int, types.Field, bool) {
item, ok := types.node(&checker.module.types, struct_type)
record_type := types.runtime_representation(struct_type, &checker.module.types)
item, ok := types.node(&checker.module.types, record_type)
if !ok || (item.kind != .Struct && item.kind != .Union) {
return -1, {}, false
}
for field, index in types.fields_for(&checker.module.types, struct_type) {
for field, index in types.fields_for(&checker.module.types, record_type) {
if field.name == u32(name) {
return int(item.field_start)+index, field, true
}
@@ -9784,28 +9828,39 @@ build_expr :: proc(
continue
}
if is_ptrcast_call(checker, expr) {
if len(expr.args) != 2 {
id := source.addf(checker.diagnostics, expr.span, "ptrcast! expects 2 arguments, got %d", len(expr.args))
if len(expr.args) != 1 && len(expr.args) != 2 {
id := source.addf(checker.diagnostics, expr.span, "ptrcast! expects 1 or 2 arguments, got %d", len(expr.args))
last = invalid_hir_expr(checker, expr.span, id)
_ = pop(&stack)
continue
}
target, target_ok := resolve_type_argument(checker, expr.args[0], pkg, file)
if !target_ok {
id := source.add(checker.diagnostics, checker.ast_module.exprs[expr.args[0]].span, "ptrcast! target must be a type")
target := frame.expected
operand_index := 0
if len(expr.args) == 2 {
target_ok: bool
target, target_ok = resolve_type_argument(checker, expr.args[0], pkg, file)
if !target_ok {
id := source.add(checker.diagnostics, checker.ast_module.exprs[expr.args[0]].span, "ptrcast! target must be a type")
last = invalid_hir_expr(checker, expr.span, id)
_ = pop(&stack)
continue
}
operand_index = 1
} else if !types.is_valid(target) {
id := source.add(checker.diagnostics, expr.span, "cannot infer ptrcast! target type; pass a pointer type explicitly")
last = invalid_hir_expr(checker, expr.span, id)
_ = pop(&stack)
continue
}
if !valid_ptrcast_child(checker, target) {
id := source.addf(checker.diagnostics, checker.ast_module.exprs[expr.args[0]].span, "ptrcast! target must be a sized runtime object type, got %s", type_label(checker, target))
if _, target_ok := ptrcast_target_child(checker, target); !target_ok {
id := source.addf(checker.diagnostics, expr.span, "ptrcast! target must be a pointer or optional pointer to a sized runtime object type, got %s", type_label(checker, target))
last = invalid_hir_expr(checker, expr.span, id)
_ = pop(&stack)
continue
}
stack[frame_index].target_type = target
stack[frame_index].stage = 9
append(&stack, Build_Expr_Frame{expr=expr.args[1], expected=types.INVALID, template=ast.INVALID_FUNCTION})
append(&stack, Build_Expr_Frame{expr=expr.args[operand_index], expected=types.INVALID, template=ast.INVALID_FUNCTION})
continue
}
if is_intrinsic_call(checker, expr, "constcast") {
@@ -10538,20 +10593,30 @@ build_expr :: proc(
_ = pop(&stack)
}
if frame.stage == 9 {
result, ok := types.replace_pointer_child(&checker.module.types, checker.module.exprs[last].type, frame.target_type)
if !ok {
actual := checker.module.exprs[last].type
retyped, ok := ptrcast_retyped_source(checker, actual, frame.target_type)
if !types.is_pointer(actual, &checker.module.types) &&
!types.is_optional_pointer(actual, &checker.module.types) {
id := source.add(checker.diagnostics, expr.span, "ptrcast! operand must be a pointer or optional pointer")
last = invalid_hir_expr(checker, expr.span, id)
} else if !ok {
id := source.addf(
checker.diagnostics, expr.span,
"ptrcast! cannot convert pointer shape from %s to %s",
type_label(checker, actual), type_label(checker, frame.target_type),
)
last = invalid_hir_expr(checker, expr.span, id)
} else {
last = add_hir_expr(checker, hir.Expr{
cast_expr := add_hir_expr(checker, hir.Expr{
kind=.Pointer_Cast,
span=expr.span,
type=result,
type=retyped,
left=last,
target=hir.INVALID_REF,
right=hir.INVALID_EXPR,
diagnostic=source.INVALID_DIAGNOSTIC,
})
last = coerce_expr(checker, cast_expr, frame.target_type, expr.span)
}
_ = pop(&stack)
}
+1
View File
@@ -1332,6 +1332,7 @@ emit_instruction_stream :: proc(
if types.is_pointer(base_type, &emitter.module.types) {
base_type = types.child_type(base_type, &emitter.module.types)
}
base_type = types.runtime_representation(base_type, &emitter.module.types)
if types.kind(base_type, &emitter.module.types) == .Fallible {
fmt.sbprintf(&emitter.builder, " %%v%d = getelementptr i8, ptr %%v%d, i64 %d\n", instruction_index, instruction.a, types.fallible_payload_offset(base_type, &emitter.module.types, emitter.module.target))
continue
+88 -9
View File
@@ -2498,19 +2498,21 @@ main func() void {
opaque_anyopaque_and_ptrcast_compile_and_lower :: proc(t: ^testing.T) {
text := `Handle :: opaque
take func(_ ?*mut anyopaque) void {}
take_bytes func(_ ?*mut u8) void {}
use_handle func(_ ?@mut Handle) void {}
main func() void {
values [2]mut u8 := [1, 2]
raw ?*mut anyopaque := (&values).ptr
bytes ?*mut u8 := ptrcast!(u8, raw)
bytes ?*mut u8 := ptrcast!(raw)
take(bytes)
take_bytes(ptrcast!(raw))
if bytes |p| {
p[1] = 5
}
one u8 := 1
single ?@mut anyopaque := &one
typed ?@mut u8 := ptrcast!(u8, single)
typed ?@mut u8 := ptrcast!(?@mut u8, single)
if typed |p| {
p^ = 2
}
@@ -2545,8 +2547,8 @@ main func() void {
}
testing.expect_value(t, len(diagnostics.items), 0)
testing.expect_value(t, hir_casts, 2)
testing.expect_value(t, ir_casts, 2)
testing.expect_value(t, hir_casts, 3)
testing.expect_value(t, ir_casts, 3)
}
@(test)
@@ -2555,10 +2557,11 @@ anyopaque_by_value_and_invalid_ptrcasts_are_rejected :: proc(t: ^testing.T) {
main func() void {
raw ?*mut anyopaque := null
value anyopaque := undefined
_ = ptrcast!(void, raw)
_ = ptrcast!(anyopaque, raw)
_ = ptrcast!(Callback, raw)
_ = ptrcast!(u8, 1)
_ = ptrcast!(raw)
_ = ptrcast!(u8, raw)
_ = ptrcast!(?*mut void, raw)
_ = ptrcast!(?@mut u8, raw)
_ = ptrcast!(?*mut u8, 1)
_ = ptrcast!(1, raw)
}
`
@@ -2575,19 +2578,25 @@ main func() void {
defer hir.destroy_module(&hir_module)
found_by_value := false
found_inference := false
found_bad_target := false
found_bad_operand := false
found_target_type := false
found_shape := false
for diagnostic in diagnostics.items {
found_by_value = found_by_value || strings.contains(diagnostic.message, "could not infer a concrete type")
found_bad_target = found_bad_target || strings.contains(diagnostic.message, "ptrcast! target must be a sized runtime object type")
found_inference = found_inference || strings.contains(diagnostic.message, "cannot infer ptrcast! target type")
found_bad_target = found_bad_target || strings.contains(diagnostic.message, "ptrcast! target must be a pointer or optional pointer")
found_bad_operand = found_bad_operand || strings.contains(diagnostic.message, "ptrcast! operand must be a pointer")
found_target_type = found_target_type || strings.contains(diagnostic.message, "ptrcast! target must be a type")
found_shape = found_shape || strings.contains(diagnostic.message, "ptrcast! cannot convert pointer shape")
}
testing.expect(t, found_by_value)
testing.expect(t, found_inference)
testing.expect(t, found_bad_target)
testing.expect(t, found_bad_operand)
testing.expect(t, found_target_type)
testing.expect(t, found_shape)
}
@(test)
@@ -5442,6 +5451,76 @@ main func() void {
testing.expect(t, unrecoverable >= 3)
}
@(test)
distinct_type_factory_backings_reject_generic_calls_without_stalling :: proc(t: ^testing.T) {
text := `ArrayList func($T type) type {
return struct { items []mut T }
}
append func($T type, list @mut ArrayList(T), value T) void {
_ = list
_ = value
}
Item :: struct { value i32 }
Items :: distinct ArrayList(Item)
main func() void {
items Items := undefined
append(&items, Item { value = 1 })
}
`
source_file := source.Source{path="test.bro", text=text}
diagnostics := source.init_diagnostics(&source_file)
defer source.destroy_diagnostics(&diagnostics)
symbols := symbol.init_table()
defer symbol.destroy_table(&symbols)
stream := lexer.lex(&source_file, &diagnostics, &symbols)
defer delete(stream.items)
ast_module := parser.parse(&stream, &source_file, &diagnostics)
defer ast.destroy_module(&ast_module)
hir_module := checker.check(&ast_module, &diagnostics, &symbols)
defer hir.destroy_module(&hir_module)
found_mismatch := false
found_backing_error := false
found_comptime_error := false
for diagnostic in diagnostics.items {
message := diagnostic.message
found_mismatch = found_mismatch || strings.contains(message, "cannot implicitly convert")
found_backing_error = found_backing_error || strings.contains(message, "requires a concrete runtime backing type")
found_comptime_error = found_comptime_error || strings.contains(message, "invalid comptime argument")
}
testing.expect(t, found_mismatch)
testing.expect(t, !found_backing_error)
testing.expect(t, !found_comptime_error)
}
@(test)
distinct_type_factory_backings_expose_record_fields :: proc(t: ^testing.T) {
directory := "/tmp/brolang-test-distinct-factory-fields"
main_path := "/tmp/brolang-test-distinct-factory-fields/main.bro"
output := "/tmp/brolang-test-distinct-factory-fields-output"
_ = os2.remove_all(directory)
defer _ = os2.remove_all(directory)
defer _ = os.remove(output)
testing.expect(t, os2.make_directory_all(directory) == nil)
text := `Box func($T type) type {
return struct { value T }
}
IntBox :: distinct Box(i32)
increment func(box @mut IntBox) void {
box.value += 1
}
main func() i32 {
box IntBox := IntBox(Box(i32) { value = 41 })
increment(&box)
return box.value - 42
}
`
testing.expect(t, os.write_entire_file(main_path, transmute([]byte)text))
testing.expect_value(t, compiler_core.compile_package(directory, output), 0)
state := run_executable(output)
testing.expect_value(t, state.exit_code, 0)
}
@(test)
string_literals_are_contextual_comptime_type_inference_evidence :: proc(t: ^testing.T) {
text := `pair func($T type, expected, actual T) T {
@@ -2,7 +2,7 @@ mem :: import "@std/mem"
@hide probe_count func(context ?@mut anyopaque) void {
if context |raw| {
count @mut usize :: ptrcast!(usize, raw)
count @mut usize :: ptrcast!(raw)
count^ += 1
}
}
+1 -1
View File
@@ -98,7 +98,7 @@ writer func(file File) Writer {
.read_write: flags = c.O_RDWR
}
while true {
fd c_int :: c.open(ptrcast!(c_char, path.ptr), flags)
fd c_int :: c.open(ptrcast!(path.ptr), flags)
if fd >= 0 {
return Handle {file_desc = fd}
}
+5 -5
View File
@@ -317,7 +317,7 @@ print func(output Writer, $format []u8, $Args type, args Args) void ! WriteError
}
}
@hide distinct_value func($Backing, $Distinct type, value Distinct) Backing {
return ptrcast!(Backing, &value)^
return ptrcast!(@Backing, &value)^
}
@hide scalar_or_distinct_type func($T type) bool {
@@ -357,14 +357,14 @@ print func(output Writer, $format []u8, $Args type, args Args) void ! WriteError
count c_int := 0
if sizeof!(T) == 4 {
if scientific {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.8e", value)
count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.8e", value)
} else {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.9g", value)
count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.9g", value)
}
} else if scientific {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.16e", value)
count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.16e", value)
} else {
count = c.snprintf(ptrcast!(c_char, (&buffer).ptr), c_ulong(buffer.len), "%.17g", value)
count = c.snprintf(ptrcast!((&buffer).ptr), c_ulong(buffer.len), "%.17g", value)
}
if count < 0 or usize(count) >= buffer.len {
return .write_failed
+7 -8
View File
@@ -48,7 +48,7 @@ alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
memory ?*mut u8 := raw_alloc(allocator, count * element_size, alignof!(T))
if memory |bytes| {
pointer *mut T :: ptrcast!(T, bytes)
pointer *mut T :: ptrcast!(bytes)
return pointer[..count]
}
return .out_of_memory
@@ -78,7 +78,7 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
old_memory ?*mut u8 := null
old_size usize := 0
if memory.len != 0 {
old_memory = ptrcast!(u8, memory.ptr)
old_memory = ptrcast!(memory.ptr)
old_size = memory.len * element_size
}
resized ?*mut u8 := raw_realloc(
@@ -89,7 +89,7 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
alignof!(T),
)
if resized |bytes| {
pointer *mut T :: ptrcast!(T, bytes)
pointer *mut T :: ptrcast!(bytes)
return pointer[..new_count]
}
@@ -101,7 +101,6 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
free func($T type, allocator Allocator, memory []T) void {
if (memory.len == 0 or sizeof!(T) == 0) return
raw_free(allocator, ptrcast!(
u8,
constcast!(memory).ptr),
memory.len * sizeof!(T),
alignof!(T),
@@ -110,7 +109,7 @@ free func($T type, allocator Allocator, memory []T) void {
#! get an empty slice of type `T` with `count` elements.
empty_slice func($T type, count usize) []mut T {
pointer *mut T :: ptrcast!(T, (&empty_storage).ptr)
pointer *mut T :: ptrcast!((&empty_storage).ptr)
return pointer[..count]
}
@@ -138,14 +137,14 @@ empty func($T type) []mut T {
if (power_of_two(alignment) == false) return null
if alignment <= malloc_alignment {
return ptrcast!(u8, c.malloc(c_ulong(size)))
return ptrcast!(c.malloc(c_ulong(size)))
}
memory [1]mut ?*mut anyopaque := [null]
status c_int := c.posix_memalign((&memory).ptr, c_ulong(alignment), c_ulong(size))
if (status != 0) return null
return ptrcast!(u8, memory[0])
return ptrcast!(memory[0])
}
@hide c_realloc func(_ ?@mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
@@ -158,7 +157,7 @@ empty func($T type) []mut T {
if memory |old_memory| {
if alignment <= malloc_alignment {
return ptrcast!(u8, c.realloc(old_memory, c_ulong(new_size)))
return ptrcast!(c.realloc(old_memory, c_ulong(new_size)))
}
new_memory ?*mut u8 := c_alloc(null, new_size, alignment)
+8 -8
View File
@@ -44,7 +44,7 @@ eql func($T type, left, right []T) bool {
@hide empty_storage [1]mut u64 := [0]
@hide empty_slice func($T type, count usize) []mut T {
pointer *mut T :: ptrcast!(T, (&empty_storage).ptr)
pointer *mut T :: ptrcast!((&empty_storage).ptr)
return pointer[..count]
}
@@ -67,7 +67,7 @@ alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
memory ?*mut u8 := raw_alloc(allocator, count * element_size, alignof!(T))
if memory |bytes| {
pointer *mut T :: ptrcast!(T, bytes)
pointer *mut T :: ptrcast!(bytes)
return pointer[..count]
}
return .out_of_memory
@@ -93,7 +93,7 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
old_memory ?*mut u8 := null
old_size usize := 0
if memory.len != 0 {
old_memory = ptrcast!(u8, memory.ptr)
old_memory = ptrcast!(memory.ptr)
old_size = memory.len * element_size
}
resized ?*mut u8 := raw_realloc(
@@ -104,7 +104,7 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
alignof!(T),
)
if resized |bytes| {
pointer *mut T :: ptrcast!(T, bytes)
pointer *mut T :: ptrcast!(bytes)
return pointer[..new_count]
}
return .out_of_memory
@@ -112,7 +112,7 @@ realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mu
free func($T type, allocator Allocator, memory []mut T) void {
if memory.len != 0 and sizeof!(T) != 0 {
raw_free(allocator, ptrcast!(u8, memory.ptr), memory.len * sizeof!(T), alignof!(T))
raw_free(allocator, ptrcast!(memory.ptr), memory.len * sizeof!(T), alignof!(T))
}
}
@@ -141,7 +141,7 @@ free func($T type, allocator Allocator, memory []mut T) void {
}
if alignment <= malloc_alignment {
return ptrcast!(u8, c.malloc(c_ulong(size)))
return ptrcast!(c.malloc(c_ulong(size)))
}
memory [1]mut ?*mut anyopaque := [null]
@@ -150,7 +150,7 @@ free func($T type, allocator Allocator, memory []mut T) void {
return null
}
return ptrcast!(u8, memory[0])
return ptrcast!(memory[0])
}
@hide c_realloc func(_ ?@mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
@@ -165,7 +165,7 @@ free func($T type, allocator Allocator, memory []mut T) void {
if memory |old_memory| {
if alignment <= malloc_alignment {
return ptrcast!(u8, c.realloc(old_memory, c_ulong(new_size)))
return ptrcast!(c.realloc(old_memory, c_ulong(new_size)))
}
new_memory ?*mut u8 := c_alloc(null, new_size, alignment)
+6 -6
View File
@@ -30,7 +30,7 @@ raw_free func(allocator Allocator, memory ?*mut u8, size usize, alignment usize)
@hide empty_storage [1]mut u64 := [0]
@hide empty_slice func($T type, count usize) []mut T {
pointer *mut T :: ptrcast!(T, (&empty_storage).ptr)
pointer *mut T :: ptrcast!((&empty_storage).ptr)
return pointer[..count]
}
@@ -49,7 +49,7 @@ alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
memory ?*mut u8 := raw_alloc(allocator, count * element_size, alignof!(T))
if memory |bytes| {
pointer *mut T :: ptrcast!(T, bytes)
pointer *mut T :: ptrcast!(bytes)
return pointer[..count]
}
return .out_of_memory
@@ -57,7 +57,7 @@ alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
free func($T type, allocator Allocator, memory []mut T) void {
if memory.len != 0 and sizeof!(T) != 0 {
raw_free(allocator, ptrcast!(u8, memory.ptr), memory.len * sizeof!(T), alignof!(T))
raw_free(allocator, ptrcast!(memory.ptr), memory.len * sizeof!(T), alignof!(T))
}
}
@@ -86,7 +86,7 @@ free func($T type, allocator Allocator, memory []mut T) void {
}
if alignment <= malloc_alignment {
return ptrcast!(u8, c.malloc(c_ulong(size)))
return ptrcast!(c.malloc(c_ulong(size)))
}
memory [1]mut ?*mut anyopaque := [null]
@@ -95,7 +95,7 @@ free func($T type, allocator Allocator, memory []mut T) void {
return null
}
return ptrcast!(u8, memory[0])
return ptrcast!(memory[0])
}
@hide c_realloc func(_ ?@mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
@@ -110,7 +110,7 @@ free func($T type, allocator Allocator, memory []mut T) void {
if memory |old_memory| {
if alignment <= malloc_alignment {
return ptrcast!(u8, c.realloc(old_memory, c_ulong(new_size)))
return ptrcast!(c.realloc(old_memory, c_ulong(new_size)))
}
new_memory ?*mut u8 := c_alloc(null, new_size, alignment)