From 18a0fa02c5a05e0b1aa23ff6b84119698161ed2d Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 21 Aug 2021 23:16:14 +0100 Subject: [PATCH] Add multi pointers to core --- core/encoding/json/marshal.odin | 3 ++ core/fmt/fmt.odin | 42 +++++++++++++++++++++++ core/reflect/reflect.odin | 11 ++++++ core/reflect/types.odin | 60 +++++++++++++++------------------ core/runtime/core.odin | 7 +++- core/runtime/error_checks.odin | 18 ++++++++++ core/runtime/print.odin | 3 ++ 7 files changed, 111 insertions(+), 33 deletions(-) diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index 9295e647f..d9a674d33 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -139,6 +139,9 @@ marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error { case runtime.Type_Info_Pointer: return .Unsupported_Type; + case runtime.Type_Info_Multi_Pointer: + return .Unsupported_Type; + case runtime.Type_Info_Procedure: return .Unsupported_Type; diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 092726a2f..7a5e85354 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -1574,6 +1574,48 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) { fmt_pointer(fi, ptr, verb); } + case runtime.Type_Info_Multi_Pointer: + ptr := (^rawptr)(v.data)^; + if verb != 'p' && info.elem != nil { + a := any{ptr, info.elem.id}; + + elem := runtime.type_info_base(info.elem); + if elem != nil { + #partial switch e in elem.variant { + case runtime.Type_Info_Array, + runtime.Type_Info_Slice, + runtime.Type_Info_Dynamic_Array, + runtime.Type_Info_Map: + if ptr == nil { + io.write_string(fi.writer, ""); + return; + } + if fi.record_level < 1 { + fi.record_level += 1; + defer fi.record_level -= 1; + io.write_byte(fi.writer, '&'); + fmt_value(fi, a, verb); + return; + } + + case runtime.Type_Info_Struct, + runtime.Type_Info_Union: + if ptr == nil { + io.write_string(fi.writer, ""); + return; + } + if fi.record_level < 1 { + fi.record_level += 1; + defer fi.record_level -= 1; + io.write_byte(fi.writer, '&'); + fmt_value(fi, a, verb); + return; + } + } + } + } + fmt_pointer(fi, ptr, verb); + case runtime.Type_Info_Array: if (verb == 's' || verb == 'q') && reflect.is_byte(info.elem) { s := strings.string_from_ptr((^byte)(v.data), info.count); diff --git a/core/reflect/reflect.odin b/core/reflect/reflect.odin index 54cd2aea6..a7aec0d19 100644 --- a/core/reflect/reflect.odin +++ b/core/reflect/reflect.odin @@ -18,6 +18,7 @@ Type_Info_Boolean :: runtime.Type_Info_Boolean; Type_Info_Any :: runtime.Type_Info_Any; Type_Info_Type_Id :: runtime.Type_Info_Type_Id; Type_Info_Pointer :: runtime.Type_Info_Pointer; +Type_Info_Multi_Pointer :: runtime.Type_Info_Multi_Pointer; Type_Info_Procedure :: runtime.Type_Info_Procedure; Type_Info_Array :: runtime.Type_Info_Array; Type_Info_Enumerated_Array :: runtime.Type_Info_Enumerated_Array; @@ -50,6 +51,7 @@ Type_Kind :: enum { Any, Type_Id, Pointer, + Multi_Pointer, Procedure, Array, Enumerated_Array, @@ -82,6 +84,7 @@ type_kind :: proc(T: typeid) -> Type_Kind { case Type_Info_Any: return .Any; case Type_Info_Type_Id: return .Type_Id; case Type_Info_Pointer: return .Pointer; + case Type_Info_Multi_Pointer: return .Multi_Pointer; case Type_Info_Procedure: return .Procedure; case Type_Info_Array: return .Array; case Type_Info_Enumerated_Array: return .Enumerated_Array; @@ -172,6 +175,7 @@ typeid_elem :: proc(id: typeid) -> typeid { case 256: return f64; } case Type_Info_Pointer: return v.elem.id; + case Type_Info_Multi_Pointer: return v.elem.id; case Type_Info_Array: return v.elem.id; case Type_Info_Enumerated_Array: return v.elem.id; case Type_Info_Slice: return v.elem.id; @@ -298,6 +302,13 @@ index :: proc(val: any, i: int, loc := #caller_location) -> any { } return index({ptr, a.elem.id}, i, loc); + case Type_Info_Multi_Pointer: + ptr := (^rawptr)(val.data)^; + if ptr == nil { + return nil; + } + return index({ptr, a.elem.id}, i, loc); + case Type_Info_Array: runtime.bounds_check_error_loc(loc, i, a.count); offset := uintptr(a.elem.size * i); diff --git a/core/reflect/types.odin b/core/reflect/types.odin index 8c7dc1735..74b0e23b9 100644 --- a/core/reflect/types.odin +++ b/core/reflect/types.odin @@ -21,13 +21,11 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool { switch x in a.variant { case Type_Info_Named: - y, ok := b.variant.(Type_Info_Named); - if !ok { return false; } + y := b.variant.(Type_Info_Named) or_return; return x.base == y.base; case Type_Info_Integer: - y, ok := b.variant.(Type_Info_Integer); - if !ok { return false; } + y := b.variant.(Type_Info_Integer) or_return; return x.signed == y.signed && x.endianness == y.endianness; case Type_Info_Rune: @@ -63,13 +61,15 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool { return ok; case Type_Info_Pointer: - y, ok := b.variant.(Type_Info_Pointer); - if !ok { return false; } + y := b.variant.(Type_Info_Pointer) or_return; + return are_types_identical(x.elem, y.elem); + + case Type_Info_Multi_Pointer: + y := b.variant.(Type_Info_Multi_Pointer) or_return; return are_types_identical(x.elem, y.elem); case Type_Info_Procedure: - y, ok := b.variant.(Type_Info_Procedure); - if !ok { return false; } + y := b.variant.(Type_Info_Procedure) or_return; switch { case x.variadic != y.variadic, x.convention != y.convention: @@ -79,31 +79,26 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool { return are_types_identical(x.params, y.params) && are_types_identical(x.results, y.results); case Type_Info_Array: - y, ok := b.variant.(Type_Info_Array); - if !ok { return false; } + y := b.variant.(Type_Info_Array) or_return; if x.count != y.count { return false; } return are_types_identical(x.elem, y.elem); case Type_Info_Enumerated_Array: - y, ok := b.variant.(Type_Info_Enumerated_Array); - if !ok { return false; } + y := b.variant.(Type_Info_Enumerated_Array) or_return; if x.count != y.count { return false; } return are_types_identical(x.index, y.index) && are_types_identical(x.elem, y.elem); case Type_Info_Dynamic_Array: - y, ok := b.variant.(Type_Info_Dynamic_Array); - if !ok { return false; } + y := b.variant.(Type_Info_Dynamic_Array) or_return; return are_types_identical(x.elem, y.elem); case Type_Info_Slice: - y, ok := b.variant.(Type_Info_Slice); - if !ok { return false; } + y := b.variant.(Type_Info_Slice) or_return; return are_types_identical(x.elem, y.elem); case Type_Info_Tuple: - y, ok := b.variant.(Type_Info_Tuple); - if !ok { return false; } + y := b.variant.(Type_Info_Tuple) or_return; if len(x.types) != len(y.types) { return false; } for _, i in x.types { xt, yt := x.types[i], y.types[i]; @@ -114,8 +109,7 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool { return true; case Type_Info_Struct: - y, ok := b.variant.(Type_Info_Struct); - if !ok { return false; } + y := b.variant.(Type_Info_Struct) or_return; switch { case len(x.types) != len(y.types), x.is_packed != y.is_packed, @@ -138,8 +132,7 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool { return true; case Type_Info_Union: - y, ok := b.variant.(Type_Info_Union); - if !ok { return false; } + y := b.variant.(Type_Info_Union) or_return; if len(x.variants) != len(y.variants) { return false; } for _, i in x.variants { @@ -153,28 +146,23 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool { return false; case Type_Info_Map: - y, ok := b.variant.(Type_Info_Map); - if !ok { return false; } + y := b.variant.(Type_Info_Map) or_return; return are_types_identical(x.key, y.key) && are_types_identical(x.value, y.value); case Type_Info_Bit_Set: - y, ok := b.variant.(Type_Info_Bit_Set); - if !ok { return false; } + y := b.variant.(Type_Info_Bit_Set) or_return; return x.elem == y.elem && x.lower == y.lower && x.upper == y.upper; case Type_Info_Simd_Vector: - y, ok := b.variant.(Type_Info_Simd_Vector); - if !ok { return false; } + y := b.variant.(Type_Info_Simd_Vector) or_return; return x.count == y.count && x.elem == y.elem; case Type_Info_Relative_Pointer: - y, ok := b.variant.(Type_Info_Relative_Pointer); - if !ok { return false; } + y := b.variant.(Type_Info_Relative_Pointer) or_return; return x.base_integer == y.base_integer && x.pointer == y.pointer; case Type_Info_Relative_Slice: - y, ok := b.variant.(Type_Info_Relative_Slice); - if !ok { return false; } + y := b.variant.(Type_Info_Relative_Slice) or_return; return x.base_integer == y.base_integer && x.slice == y.slice; } @@ -257,6 +245,11 @@ is_pointer :: proc(info: ^Type_Info) -> bool { _, ok := type_info_base(info).variant.(Type_Info_Pointer); return ok; } +is_multi_pointer :: proc(info: ^Type_Info) -> bool { + if info == nil { return false; } + _, ok := type_info_base(info).variant.(Type_Info_Multi_Pointer); + return ok; +} is_procedure :: proc(info: ^Type_Info) -> bool { if info == nil { return false; } _, ok := type_info_base(info).variant.(Type_Info_Procedure); @@ -424,6 +417,9 @@ write_type_writer :: proc(w: io.Writer, ti: ^Type_Info) -> (n: int) { write_string(w, "^"); write_type(w, info.elem); } + case Type_Info_Multi_Pointer: + write_string(w, "[^]"); + write_type(w, info.elem); case Type_Info_Procedure: n += write_string(w, "proc"); if info.params == nil { diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 73af39864..e4c3dd30a 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -74,6 +74,9 @@ Type_Info_Type_Id :: struct {}; Type_Info_Pointer :: struct { elem: ^Type_Info, // nil -> rawptr }; +Type_Info_Multi_Pointer :: struct { + elem: ^Type_Info, +}; Type_Info_Procedure :: struct { params: ^Type_Info, // Type_Info_Tuple results: ^Type_Info, // Type_Info_Tuple @@ -184,6 +187,7 @@ Type_Info :: struct { Type_Info_Any, Type_Info_Type_Id, Type_Info_Pointer, + Type_Info_Multi_Pointer, Type_Info_Procedure, Type_Info_Array, Type_Info_Enumerated_Array, @@ -214,6 +218,7 @@ Typeid_Kind :: enum u8 { Any, Type_Id, Pointer, + Multi_Pointer, Procedure, Array, Enumerated_Array, @@ -340,7 +345,7 @@ Context :: struct { Raw_String :: struct { - data: ^byte, + data: [^]byte, len: int, } diff --git a/core/runtime/error_checks.odin b/core/runtime/error_checks.odin index 3e4632215..fb8ccab64 100644 --- a/core/runtime/error_checks.odin +++ b/core/runtime/error_checks.odin @@ -45,6 +45,24 @@ slice_handle_error :: proc "contextless" (file: string, line, column: i32, lo, h bounds_trap(); } +multi_pointer_slice_handle_error :: proc "contextless" (file: string, line, column: i32, lo, hi: int) -> ! { + print_caller_location(Source_Code_Location{file, line, column, ""}); + print_string(" Invalid slice indices: "); + print_i64(i64(lo)); + print_string(":"); + print_i64(i64(hi)); + print_byte('\n'); + bounds_trap(); +} + + +multi_pointer_slice_expr_error :: proc "contextless" (file: string, line, column: i32, lo, hi: int) { + if lo <= hi { + return; + } + multi_pointer_slice_handle_error(file, line, column, lo, hi); +} + slice_expr_error_hi :: proc "contextless" (file: string, line, column: i32, hi: int, len: int) { if 0 <= hi && hi <= len { return; diff --git a/core/runtime/print.odin b/core/runtime/print.odin index fd756410a..5a3abcf2f 100644 --- a/core/runtime/print.odin +++ b/core/runtime/print.odin @@ -203,6 +203,9 @@ print_type :: proc "contextless" (ti: ^Type_Info) { print_string("^"); print_type(info.elem); } + case Type_Info_Multi_Pointer: + print_string("[^]"); + print_type(info.elem); case Type_Info_Procedure: print_string("proc"); if info.params == nil {