From d5281b10dddb571d19e7cb1394b21edbb4b73e11 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 18 Nov 2023 23:35:11 +0100 Subject: [PATCH] Cleanup use of macro inspection to use @typekind and @typeid macros. --- lib/std/collections/object.c3 | 2 +- lib/std/core/bitorder.c3 | 8 ++++---- lib/std/core/types.c3 | 6 +++--- lib/std/core/values.c3 | 1 + lib/std/io/io.c3 | 4 ++-- lib/std/io/stream.c3 | 4 ++-- lib/std/math/math.c3 | 14 +++++++------- lib/std/math/math_nolibc/math_nolibc.c3 | 4 ++-- lib/std/math/math_vector.c3 | 2 +- lib/std/math/random/math.seeder.c3 | 2 +- lib/std/sort/binarysearch.c3 | 6 +++--- lib/std/sort/quicksort.c3 | 4 ++-- lib/std/sort/sort.c3 | 2 +- 13 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/std/collections/object.c3 b/lib/std/collections/object.c3 index abc3d4053..597d210eb 100644 --- a/lib/std/collections/object.c3 +++ b/lib/std/collections/object.c3 @@ -100,7 +100,7 @@ fn Object* new_int(int128 i, Allocator* allocator) macro Object* new_enum(e, Allocator* allocator) { Object* o = allocator.new(Object); - *o = { .i = (int128)e, .allocator = allocator, .type = $typeof(e).typeid }; + *o = { .i = (int128)e, .allocator = allocator, .type = @typeid(e) }; return o; } diff --git a/lib/std/core/bitorder.c3 b/lib/std/core/bitorder.c3 index 9239ee595..77a3eb5ff 100644 --- a/lib/std/core/bitorder.c3 +++ b/lib/std/core/bitorder.c3 @@ -94,7 +94,7 @@ bitstruct UInt128LE : uint128 @littleendian macro read(bytes, $Type) { char[] s; - $switch ($typeof(bytes).kindof) + $switch (@typekind(bytes)) $case POINTER: s = (*bytes)[:$Type.sizeof]; $default: @@ -110,7 +110,7 @@ macro read(bytes, $Type) macro write(x, bytes, $Type) { char[] s; - $switch ($typeof(bytes).kindof) + $switch (@typekind(bytes)) $case POINTER: s = (*bytes)[:$Type.sizeof]; $default: @@ -146,7 +146,7 @@ macro is_bitorder($Type) macro bool is_array_or_sub_of_char(bytes) { - $switch ($typeof(bytes).kindof) + $switch (@typekind(bytes)) $case POINTER: var $Inner = $typefrom($typeof(bytes).inner); $if $Inner.kindof == ARRAY: @@ -164,7 +164,7 @@ macro bool is_array_or_sub_of_char(bytes) macro bool is_arrayptr_or_sub_of_char(bytes) { - $switch ($typeof(bytes).kindof) + $switch (@typekind(bytes)) $case POINTER: var $Inner = $typefrom($typeof(bytes).inner); $if $Inner.kindof == ARRAY: diff --git a/lib/std/core/types.c3 b/lib/std/core/types.c3 index 9b7174f40..340e7ca1e 100644 --- a/lib/std/core/types.c3 +++ b/lib/std/core/types.c3 @@ -184,12 +184,12 @@ macro bool is_same($TypeA, $TypeB) macro bool @has_same(#a, #b, ...) { - var $type_a = $typeof(#a).typeid; - $if $type_a != $typeof(#b).typeid: + var $type_a = @typeid(#a); + $if $type_a != @typeid(#b): return false; $endif $for (var $i = 0; $i < $vacount; $i++) - $if $typeof($vaexpr($i)).typeid != $type_a: + $if @typeid($vaexpr($i)) != $type_a: return false; $endif $endfor diff --git a/lib/std/core/values.c3 b/lib/std/core/values.c3 index b0b3eec55..169cdc919 100644 --- a/lib/std/core/values.c3 +++ b/lib/std/core/values.c3 @@ -1,5 +1,6 @@ module std::core::values; +macro typeid @typeid(#value) @builtin => $typeof(#value).typeid; macro TypeKind @typekind(#value) @builtin => $typeof(#value).kindof; macro bool @typeis(#value, $Type) @builtin => $typeof(#value).typeid == $Type.typeid; /** diff --git a/lib/std/io/io.c3 b/lib/std/io/io.c3 index 2e42afea3..3b033ff34 100644 --- a/lib/std/io/io.c3 +++ b/lib/std/io/io.c3 @@ -51,7 +51,7 @@ fault IoError **/ macro String! readline(stream = io::stdin(), Allocator* allocator = mem::heap()) { - bool $is_stream = $typeof(stream).typeid == InStream*.typeid; + bool $is_stream = @typeid(stream) == InStream*.typeid; $if $is_stream: $typeof(&stream.read_byte) func = &stream.read_byte; char val = func((void*)stream)!; @@ -133,7 +133,7 @@ macro usz! fprintn(out, x = "") usz len = fprint(out, x)!; out.write_byte('\n')!; $switch - $case $typeof(out).typeid == OutStream*.typeid: + $case @typeid(out) == OutStream*.typeid: if (&out.flush) out.flush()!; $case $defined(out.flush): out.flush()!; diff --git a/lib/std/io/stream.c3 b/lib/std/io/stream.c3 index 1cc2bf276..dba16d50a 100644 --- a/lib/std/io/stream.c3 +++ b/lib/std/io/stream.c3 @@ -177,7 +177,7 @@ const char[*] MAX_VARS @private = { [2] = 3, [4] = 5, [8] = 10 }; /** * @require @is_instream(stream) - * @require $typeof(x_ptr).kindof == POINTER && $typeof(x_ptr).inner.kindof.is_int() + * @require @typekind(x_ptr) == POINTER && $typeof(x_ptr).inner.kindof.is_int() **/ macro usz! read_varint(stream, x_ptr) { @@ -214,7 +214,7 @@ macro usz! read_varint(stream, x_ptr) } /** * @require @is_outstream(stream) - * @require $typeof(x).kindof.is_int() + * @require @typekind(x).is_int() **/ macro usz! write_varint(stream, x) { diff --git a/lib/std/math/math.c3 b/lib/std/math/math.c3 index 636e53c78..91664c450 100644 --- a/lib/std/math/math.c3 +++ b/lib/std/math/math.c3 @@ -156,7 +156,7 @@ macro atan2(x, y) **/ macro sincos(x, y) { - $if $typeof(y[0]).typeid == float.typeid: + $if @typeid(y[0]) == float.typeid: return _sincosf(x, y); $else return _sincos(x, y); @@ -168,7 +168,7 @@ macro sincos(x, y) **/ macro atan(x) { - $if $typeof(x).typeid == float.typeid: + $if @typeid(x) == float.typeid: return _atanf(x); $else return _atan(x); @@ -180,7 +180,7 @@ macro atan(x) **/ macro atanh(x) { - $if $typeof(x).typeid == float.typeid: + $if @typeid(x) == float.typeid: return _atanhf(x); $else return _atanh(x); @@ -192,7 +192,7 @@ macro atanh(x) **/ macro acos(x) { - $if $typeof(x).typeid == float.typeid: + $if @typeid(x) == float.typeid: return _acosf(x); $else return _acos(x); @@ -204,7 +204,7 @@ macro acos(x) **/ macro acosh(x) { - $if $typeof(x).typeid == float.typeid: + $if @typeid(x) == float.typeid: return _acoshf(x); $else return _acosh(x); @@ -216,7 +216,7 @@ macro acosh(x) **/ macro asin(x) { - $if $typeof(x).typeid == float.typeid: + $if @typeid(x) == float.typeid: return _asinf(x); $else return _asin(x); @@ -228,7 +228,7 @@ macro asin(x) **/ macro asinh(x) { - $if $typeof(x).typeid == float.typeid: + $if @typeid(x) == float.typeid: return _asinhf(x); $else return _asinh(x); diff --git a/lib/std/math/math_nolibc/math_nolibc.c3 b/lib/std/math/math_nolibc/math_nolibc.c3 index 1a0190bb8..e7c455fbc 100644 --- a/lib/std/math/math_nolibc/math_nolibc.c3 +++ b/lib/std/math/math_nolibc/math_nolibc.c3 @@ -244,13 +244,13 @@ macro double __math_oflow(ulong sign) => __math_xflow(sign, 0x1p-767); macro __math_xflow(sign, v) { - $typeof(v) temp; + $typeof(v) temp @noinit; @volatile_store(temp, (sign ? -v : v) * v); return temp; } macro force_eval_add(x, v) { - $typeof(x) temp; + $typeof(x) temp @noinit; @volatile_store(temp, x + v); } \ No newline at end of file diff --git a/lib/std/math/math_vector.c3 b/lib/std/math/math_vector.c3 index b9dfef8d7..916d307f2 100644 --- a/lib/std/math/math_vector.c3 +++ b/lib/std/math/math_vector.c3 @@ -217,7 +217,7 @@ return v; var view_proj = m1.mul(m2); var invert = view_proj.invert(); // Create quaternion from source point - $if $typeof(v[0]).typeid == float.typeid: + $if @typeid(v[0]) == float.typeid: Quaternionf quat = { v.x, v.y, v.z, 1 }; $else Quaternion quat = { v.x, v.y, v.z, 1 }; diff --git a/lib/std/math/random/math.seeder.c3 b/lib/std/math/random/math.seeder.c3 index 2fdb79f6a..e2e527dfb 100644 --- a/lib/std/math/random/math.seeder.c3 +++ b/lib/std/math/random/math.seeder.c3 @@ -69,7 +69,7 @@ fn void seeder(char[] input, char[] out_buffer) macro uint hash(value) @local { - return fnv32a::encode(&&bitcast(value, char[$typeof(value).sizeof])); + return fnv32a::encode(&&bitcast(value, char[$sizeof(value)])); } fn char[8 * 4] entropy() diff --git a/lib/std/sort/binarysearch.c3 b/lib/std/sort/binarysearch.c3 index 30799369b..64a3df7aa 100644 --- a/lib/std/sort/binarysearch.c3 +++ b/lib/std/sort/binarysearch.c3 @@ -4,7 +4,7 @@ module std::sort; * Perform a binary search over the sorted array and return the index * in [0, array.len) where x would be inserted or cmp(i) is true and cmp(j) is true for j in [i, array.len). * @require $defined(list[0]) && $defined(list.len) "The list must be indexable" - * @require $or($typeof(cmp).typeid == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values" + * @require $or(@typeid(cmp) == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values" **/ macro usz binarysearch(list, x, cmp = null) @builtin { @@ -13,7 +13,7 @@ macro usz binarysearch(list, x, cmp = null) @builtin for (usz j = len; i < j;) { usz half = i + (j - i) / 2; - $if $typeof(cmp).typeid == void*.typeid: + $if @typeid(cmp) == void*.typeid: switch { case greater(list[half], x): j = half; @@ -22,7 +22,7 @@ macro usz binarysearch(list, x, cmp = null) @builtin } $else $switch - $case $typeof(cmp).params[0] == $typeof(list[0]).typeid: + $case $typeof(cmp).params[0] == @typeid(list[0]): int res = cmp(list[half], x); $default: int res = cmp(&list[half], &x); diff --git a/lib/std/sort/quicksort.c3 b/lib/std/sort/quicksort.c3 index d01bbd257..8edfa8370 100644 --- a/lib/std/sort/quicksort.c3 +++ b/lib/std/sort/quicksort.c3 @@ -4,7 +4,7 @@ import std::sort::qs; /** * Sort list using the quick sort algorithm. * @require $defined(list[0]) && $defined(list.len) "The list must be indexable and support .len or .len()" - * @require $or($typeof(cmp).typeid == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values" + * @require $or(@typeid(cmp) == void*.typeid, @is_comparer(cmp, list)) "Expected a comparison function which compares values" **/ macro quicksort(list, cmp = null) @builtin { @@ -31,7 +31,7 @@ def Stack = StackElementItem[64] @private; fn void qsort(Type list, isz low, isz high, Comparer cmp) { var $no_cmp = Comparer.typeid == void*.typeid; - var $cmp_by_value = $and(!$no_cmp, Comparer.params[0] == $typeof(list[0]).typeid); + var $cmp_by_value = $and(!$no_cmp, Comparer.params[0] == @typeid(list[0])); if (low >= 0 && high >= 0 && low < high) { Stack stack; diff --git a/lib/std/sort/sort.c3 b/lib/std/sort/sort.c3 index a75c1c4ee..c88bc4920 100644 --- a/lib/std/sort/sort.c3 +++ b/lib/std/sort/sort.c3 @@ -19,7 +19,7 @@ macro bool @is_comparer(#cmp, #list) $if $params[0] != $params[1]: return false; $else - var $element = $typeof(#list[0]).typeid; + var $element = @typeid(#list[0]); $switch $case $element == $params[0]: return true;