Add @const attribute for macros, for better error messages with constant macros #1293

This commit is contained in:
Christoffer Lerno
2024-08-02 15:01:02 +02:00
parent 7a72f44f64
commit 8498cb6258
14 changed files with 146 additions and 52 deletions

View File

@@ -4,7 +4,7 @@
module std::core::builtin;
/**
* @require types::is_comparable_value(a) && types::is_comparable_value(b)
* @require types::@comparable_value(a) && types::@comparable_value(b)
**/
macro less(a, b) @builtin
{
@@ -19,7 +19,7 @@ macro less(a, b) @builtin
}
/**
* @require types::is_comparable_value(a) && types::is_comparable_value(b)
* @require types::@comparable_value(a) && types::@comparable_value(b)
**/
macro less_eq(a, b) @builtin
{
@@ -34,7 +34,7 @@ macro less_eq(a, b) @builtin
}
/**
* @require types::is_comparable_value(a) && types::is_comparable_value(b)
* @require types::@comparable_value(a) && types::@comparable_value(b)
**/
macro greater(a, b) @builtin
{
@@ -49,7 +49,7 @@ macro greater(a, b) @builtin
}
/**
* @require types::is_comparable_value(a) && types::is_comparable_value(b)
* @require types::@comparable_value(a) && types::@comparable_value(b)
**/
macro int compare_to(a, b) @builtin
{
@@ -63,7 +63,7 @@ macro int compare_to(a, b) @builtin
$endswitch
}
/**
* @require types::is_comparable_value(a) && types::is_comparable_value(b)
* @require types::@comparable_value(a) && types::@comparable_value(b)
**/
macro greater_eq(a, b) @builtin
{
@@ -78,7 +78,7 @@ macro greater_eq(a, b) @builtin
}
/**
* @require types::is_equatable_value(a) && types::is_equatable_value(b) `values must be equatable`
* @require types::@equatable_value(a) && types::@equatable_value(b) `values must be equatable`
**/
macro bool equals(a, b) @builtin
{

View File

@@ -149,7 +149,7 @@ const bool NETBSD = LIBC && OS_TYPE == NETBSD;
const bool WASI = LIBC && OS_TYPE == WASI;
const bool WASM_NOLIBC @builtin = !LIBC && ARCH_TYPE == ArchType.WASM32 || ARCH_TYPE == ArchType.WASM64;
macro bool os_is_darwin()
macro bool os_is_darwin() @const
{
$switch (OS_TYPE)
$case IOS:
@@ -162,7 +162,7 @@ macro bool os_is_darwin()
$endswitch
}
macro bool os_is_posix()
macro bool os_is_posix() @const
{
$switch (OS_TYPE)
$case IOS:

View File

@@ -8,7 +8,7 @@ import std::math;
const MAX_MEMORY_ALIGNMENT = 0x1000_0000;
const DEFAULT_MEM_ALIGNMENT = (void*.alignof) * 2;
macro bool @constant_is_power_of_2($x) @private
macro bool @constant_is_power_of_2($x) @const @private
{
return $x != 0 && ($x & ($x - 1)) == 0;
}

View File

@@ -120,20 +120,20 @@ macro bool is_slice_convertable($Type)
$endswitch
}
macro bool is_bool($Type) => $Type.kindof == TypeKind.BOOL;
macro bool is_int($Type) => $Type.kindof == TypeKind.SIGNED_INT || $Type.kindof == TypeKind.UNSIGNED_INT;
macro bool is_bool($Type) @const => $Type.kindof == TypeKind.BOOL;
macro bool is_int($Type) @const => $Type.kindof == TypeKind.SIGNED_INT || $Type.kindof == TypeKind.UNSIGNED_INT;
macro bool is_indexable($Type)
macro bool is_indexable($Type) @const
{
return $defined($Type{}[0]);
}
macro bool is_ref_indexable($Type)
macro bool is_ref_indexable($Type) @const
{
return $defined(&$Type{}[0]);
}
macro bool is_intlike($Type)
macro bool is_intlike($Type) @const
{
$switch ($Type.kindof)
$case SIGNED_INT:
@@ -146,7 +146,7 @@ macro bool is_intlike($Type)
$endswitch
}
macro bool is_underlying_int($Type)
macro bool is_underlying_int($Type) @const
{
$switch ($Type.kindof)
$case SIGNED_INT:
@@ -159,9 +159,9 @@ macro bool is_underlying_int($Type)
$endswitch
}
macro bool is_float($Type) => $Type.kindof == TypeKind.FLOAT;
macro bool is_float($Type) @const => $Type.kindof == TypeKind.FLOAT;
macro bool is_floatlike($Type)
macro bool is_floatlike($Type) @const
{
$switch ($Type.kindof)
$case FLOAT:
@@ -173,12 +173,12 @@ macro bool is_floatlike($Type)
$endswitch
}
macro bool is_vector($Type)
macro bool is_vector($Type) @const
{
return $Type.kindof == TypeKind.VECTOR;
}
macro TypeKind inner_kind($Type)
macro TypeKind inner_kind($Type) @const
{
$if $Type.kindof == TypeKind.DISTINCT:
return inner_kind($typefrom($Type.inner));
@@ -187,12 +187,12 @@ macro TypeKind inner_kind($Type)
$endif
}
macro bool is_same($TypeA, $TypeB)
macro bool is_same($TypeA, $TypeB) @const
{
return $TypeA.typeid == $TypeB.typeid;
}
macro bool @has_same(#a, #b, ...)
macro bool @has_same(#a, #b, ...) @const
{
var $type_a = @typeid(#a);
$if $type_a != @typeid(#b):
@@ -206,7 +206,7 @@ macro bool @has_same(#a, #b, ...)
return true;
}
macro bool may_load_atomic($Type)
macro bool may_load_atomic($Type) @const
{
$switch ($Type.kindof)
$case SIGNED_INT:
@@ -221,7 +221,7 @@ macro bool may_load_atomic($Type)
$endswitch
}
macro lower_to_atomic_compatible_type($Type)
macro lower_to_atomic_compatible_type($Type) @const
{
$switch ($Type.kindof)
$case SIGNED_INT:
@@ -247,10 +247,10 @@ macro lower_to_atomic_compatible_type($Type)
$endswitch
}
macro bool is_promotable_to_floatlike($Type) => types::is_floatlike($Type) || types::is_int($Type);
macro bool is_promotable_to_float($Type) => types::is_float($Type) || types::is_int($Type);
macro bool is_promotable_to_floatlike($Type) @const => types::is_floatlike($Type) || types::is_int($Type);
macro bool is_promotable_to_float($Type) @const => types::is_float($Type) || types::is_int($Type);
macro bool is_same_vector_type($Type1, $Type2)
macro bool is_same_vector_type($Type1, $Type2) @const
{
$if $Type1.kindof != TypeKind.VECTOR:
return $Type2.kindof != TypeKind.VECTOR;
@@ -259,7 +259,7 @@ macro bool is_same_vector_type($Type1, $Type2)
$endif
}
macro bool is_equatable_type($Type)
macro bool is_equatable_type($Type) @const
{
$if $defined($Type.less) || $defined($Type.compare_to) || $defined($Type.equals):
return true;
@@ -271,17 +271,31 @@ macro bool is_equatable_type($Type)
/**
* Checks if a type implements the copy protocol.
**/
macro bool implements_copy($Type)
macro bool implements_copy($Type) @const
{
return $defined($Type.copy) && $defined($Type.free);
}
macro bool is_equatable_value(value)
macro bool is_equatable_value(value) @deprecated
{
return is_equatable_type($typeof(value));
}
macro bool is_comparable_value(value)
macro bool @equatable_value(#value) @const
{
return is_equatable_type($typeof(#value));
}
macro bool @comparable_value(#value) @const
{
$if $defined(#value.less) || $defined(#value.compare_to):
return true;
$else
return $typeof(#value).is_ordered;
$endif
}
macro bool is_comparable_value(value) @deprecated
{
$if $defined(value.less) || $defined(value.compare_to):
return true;

View File

@@ -1,21 +1,21 @@
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;
macro typeid @typeid(#value) @const @builtin => $typeof(#value).typeid;
macro TypeKind @typekind(#value) @const @builtin => $typeof(#value).kindof;
macro bool @typeis(#value, $Type) @const @builtin => $typeof(#value).typeid == $Type.typeid;
/**
* Return true if two values have the same type before any conversions.
**/
macro bool @is_same_type(#value1, #value2) => $typeof(#value1).typeid == $typeof(#value2).typeid;
macro bool @is_bool(#value) => types::is_bool($typeof(#value));
macro bool @is_int(#value) => types::is_int($typeof(#value));
macro bool @is_floatlike(#value) => types::is_floatlike($typeof(#value));
macro bool @is_float(#value) => types::is_float($typeof(#value));
macro bool @is_promotable_to_floatlike(#value) => types::is_promotable_to_floatlike($typeof(#value));
macro bool @is_promotable_to_float(#value) => types::is_promotable_to_float($typeof(#value));
macro bool @is_vector(#value) => types::is_vector($typeof(#value));
macro bool @is_same_vector_type(#value1, #value2) => types::is_same_vector_type($typeof(#value1), $typeof(#value2));
macro bool @assign_to(#value1, #value2) => $assignable(#value1, $typeof(#value2));
macro bool @is_same_type(#value1, #value2) @const => $typeof(#value1).typeid == $typeof(#value2).typeid;
macro bool @is_bool(#value) @const => types::is_bool($typeof(#value));
macro bool @is_int(#value) @const => types::is_int($typeof(#value));
macro bool @is_floatlike(#value) @const => types::is_floatlike($typeof(#value));
macro bool @is_float(#value) @const => types::is_float($typeof(#value));
macro bool @is_promotable_to_floatlike(#value) @const => types::is_promotable_to_floatlike($typeof(#value));
macro bool @is_promotable_to_float(#value) @const => types::is_promotable_to_float($typeof(#value));
macro bool @is_vector(#value) @const => types::is_vector($typeof(#value));
macro bool @is_same_vector_type(#value1, #value2) @const => types::is_same_vector_type($typeof(#value1), $typeof(#value2));
macro bool @assign_to(#value1, #value2) @const => $assignable(#value1, $typeof(#value2));
macro promote_int(x)
{
@@ -42,5 +42,5 @@ macro promote_int_same(x, y)
$endif
}
macro TypeKind @inner_kind(#value) => types::inner_kind($typeof(#value));
macro TypeKind @inner_kind(#value) @const => types::inner_kind($typeof(#value));