mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Dev (#500)
Single code path for kind/inner/len/sizeof on type and typeid. Fix of #493. Bump to 0.2.24. Remove ´func´ deprecated keyword. Unify builtin access. Enum and fault name reflection.
This commit is contained in:
committed by
GitHub
parent
cc1bc58ed0
commit
cdff5c3e26
@@ -88,6 +88,13 @@ struct Allocator
|
||||
AllocatorFunction function;
|
||||
}
|
||||
|
||||
macro @clone(&value) @builtin
|
||||
{
|
||||
$typeof(value)* x = malloc($typeof(value));
|
||||
*x = value;
|
||||
return x;
|
||||
}
|
||||
|
||||
macro malloc($Type) @builtin
|
||||
{
|
||||
return ($Type*)(mem::alloc($Type.sizeof));
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
module std::core::mem::array;
|
||||
|
||||
/**
|
||||
* @require usize.typeid.max / elements > $Type.sizeof
|
||||
* @require usize.max / elements > $Type.sizeof
|
||||
**/
|
||||
macro alloc($Type, usize elements)
|
||||
{
|
||||
@@ -13,7 +13,7 @@ macro alloc($Type, usize elements)
|
||||
}
|
||||
|
||||
/**
|
||||
* @require (usize.typeid.max / elements > $Type.sizeof)
|
||||
* @require (usize.max / elements > $Type.sizeof)
|
||||
**/
|
||||
macro make($Type, usize elements)
|
||||
{
|
||||
|
||||
@@ -268,9 +268,9 @@ macro void String.append(String* str, value)
|
||||
$case Char32:
|
||||
str.append_char32(value);
|
||||
$default:
|
||||
$if ($convertable($Type, Char32)):
|
||||
$if ($convertible($Type, Char32)):
|
||||
str.append_char32(value);
|
||||
$elif ($convertable($Type, char[])):
|
||||
$elif ($convertible($Type, char[])):
|
||||
str.append_chars(value);
|
||||
$else:
|
||||
$assert("Unsupported type for appending");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
module std::core::types;
|
||||
|
||||
import libc;
|
||||
macro bool kind_is_int(TypeKind kind)
|
||||
{
|
||||
return kind == TypeKind.SIGNED_INT || kind == TypeKind.UNSIGNED_INT;
|
||||
@@ -11,37 +11,44 @@ fault ConversionResult
|
||||
VALUE_OUT_OF_UNSIGNED_RANGE,
|
||||
}
|
||||
/**
|
||||
* @require type.typeid == SIGNED_INT || type.typeid == UNSIGNED_INT, "Argument was not an integer"
|
||||
* @require type.kind == SIGNED_INT || type.kind == UNSIGNED_INT || type.kind == ENUM, "Argument was not an integer"
|
||||
**/
|
||||
macro variant_to_int(variant v, $Type)
|
||||
{
|
||||
bool is_mixed_signed = $Type.typeid.kind != v.type.kind;
|
||||
typeid variant_type = v.type;
|
||||
TypeKind kind = variant_type.kind;
|
||||
if (kind == TypeKind.ENUM)
|
||||
{
|
||||
variant_type = variant_type.inner;
|
||||
kind = variant_type.kind;
|
||||
}
|
||||
bool is_mixed_signed = $Type.kind != variant_type.kind;
|
||||
$Type max = $Type.max;
|
||||
$Type min = $Type.min;
|
||||
switch (v)
|
||||
switch (variant_type)
|
||||
{
|
||||
case ichar:
|
||||
ichar c = *v;
|
||||
ichar c = *(char*)v.ptr;
|
||||
if (is_mixed_signed && c < 0) return ConversionResult.VALUE_OUT_OF_UNSIGNED_RANGE!;
|
||||
return ($Type)c;
|
||||
case short:
|
||||
short s = *v;
|
||||
short s = *(short*)v.ptr;
|
||||
if (is_mixed_signed && s < 0) return ConversionResult.VALUE_OUT_OF_UNSIGNED_RANGE!;
|
||||
if (s > max || s < min) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)s;
|
||||
case int:
|
||||
int i = *v;
|
||||
int i = *(int*)v.ptr;
|
||||
if (is_mixed_signed && i < 0) return ConversionResult.VALUE_OUT_OF_UNSIGNED_RANGE!;
|
||||
if (i > max || i < min) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)i;
|
||||
case long:
|
||||
long l = *v;
|
||||
long l = *(long*)v.ptr;;
|
||||
if (is_mixed_signed && l < 0) return ConversionResult.VALUE_OUT_OF_UNSIGNED_RANGE!;
|
||||
if (l > max || l < min) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)l;
|
||||
case int128:
|
||||
$if (env::I128_SUPPORT):
|
||||
int128 i = *v;
|
||||
int128 i = *(int128*)v.ptr;
|
||||
if (is_mixed_signed && i < 0) return ConversionResult.VALUE_OUT_OF_UNSIGNED_RANGE!;
|
||||
if (i > max || i < min) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)i;
|
||||
@@ -49,24 +56,24 @@ macro variant_to_int(variant v, $Type)
|
||||
unreachable();
|
||||
$endif;
|
||||
case char:
|
||||
char c = *v;
|
||||
char c = *(char*)v.ptr;
|
||||
if (c > max) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)c;
|
||||
case ushort:
|
||||
ushort s = *v;
|
||||
ushort s = *(ushort*)v.ptr;;
|
||||
if (s > max || s < min) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)s;
|
||||
case uint:
|
||||
uint i = *v;
|
||||
uint i = *(uint*)v.ptr;;
|
||||
if (i > max || i < min) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)i;
|
||||
case ulong:
|
||||
ulong l = *v;
|
||||
ulong l = *(ulong*)v.ptr;;
|
||||
if (l > max || l < min) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)l;
|
||||
case uint128:
|
||||
$if (env::I128_SUPPORT):
|
||||
uint128 i = *v;
|
||||
uint128 i = *(uint128*)v.ptr;
|
||||
if (i > max || i < min) return ConversionResult.VALUE_OUT_OF_RANGE!;
|
||||
return ($Type)i;
|
||||
$else:
|
||||
@@ -79,9 +86,9 @@ macro variant_to_int(variant v, $Type)
|
||||
|
||||
macro bool is_numerical($Type)
|
||||
{
|
||||
var $kind = $Type.typeid.kind;
|
||||
var $kind = $Type.kind;
|
||||
$if ($kind == TypeKind.DISTINCT):
|
||||
return is_numerical($Type.typeid.inner);
|
||||
return is_numerical($Type.inner);
|
||||
$else:
|
||||
return $kind == TypeKind.SIGNED_INT || $kind == TypeKind.UNSIGNED_INT || $kind == TypeKind.FLOAT
|
||||
|| $kind == TypeKind.VECTOR;
|
||||
@@ -90,9 +97,9 @@ macro bool is_numerical($Type)
|
||||
|
||||
macro bool is_comparable($Type)
|
||||
{
|
||||
var $kind = $Type.typeid.kind;
|
||||
var $kind = $Type.kind;
|
||||
$if ($kind == TypeKind.DISTINCT):
|
||||
return is_comparable($Type.typeid.inner);
|
||||
return is_comparable($Type.inner);
|
||||
$else:
|
||||
return $kind == TypeKind.SIGNED_INT || $kind == TypeKind.UNSIGNED_INT || $kind == TypeKind.FLOAT
|
||||
|| $kind == TypeKind.VECTOR || $kind == TypeKind.BOOL || $kind == TypeKind.POINTER
|
||||
|
||||
@@ -52,13 +52,12 @@ private fn void! out_str(PrintParam* param, variant arg)
|
||||
case VOID:
|
||||
return out_substr(param, "void");
|
||||
case ANYERR:
|
||||
return out_substr(param, "<anyerr>");
|
||||
case FAULT:
|
||||
return out_substr(param, (*(anyerr*)arg.ptr).nameof);
|
||||
case VARIANT:
|
||||
return out_substr(param, "<variant>");
|
||||
case ENUM:
|
||||
return out_substr(param, "<enum>");
|
||||
case FAULT:
|
||||
return out_substr(param, "<fault>");
|
||||
return out_substr(param, arg.type.names[types::variant_to_int(arg, usize)!!]);
|
||||
case STRUCT:
|
||||
return out_substr(param, "<struct>");
|
||||
case UNION:
|
||||
|
||||
Reference in New Issue
Block a user