mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Added @likely, @unlikely and @expect macros. (#727)
This commit is contained in:
@@ -120,17 +120,37 @@ macro enum_by_name($Type, String enum_name) @builtin
|
||||
return SearchResult.MISSING!;
|
||||
}
|
||||
|
||||
macro bool @expect_true(bool value) @builtin => $$expect(value, true);
|
||||
macro bool @expect_false(bool value) @builtin => $$expect(value, false);
|
||||
macro bool @likely(bool value, $probability = 1.0) @builtin
|
||||
{
|
||||
$if ($probability == 1.0):
|
||||
return $$expect(value, true);
|
||||
$else:
|
||||
return $$expect_with_probability(value, true, $probability);
|
||||
$endif;
|
||||
}
|
||||
|
||||
macro bool @unlikely(bool value, $probability = 1.0) @builtin
|
||||
{
|
||||
$if ($probability == 1.0):
|
||||
return $$expect(value, false);
|
||||
$else:
|
||||
return $$expect_with_probability(value, false, $probability);
|
||||
$endif;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require values::@is_int(value)
|
||||
* @require values::@is_int(value) || values::@is_bool(value)
|
||||
* @checked $typeof(value) a = expected
|
||||
**/
|
||||
macro @expect(value, expected) @builtin
|
||||
macro @expect(value, expected, $probability = 1.0) @builtin
|
||||
{
|
||||
$if ($probability == 1.0):
|
||||
return $$expect(value, ($typeof(value))expected);
|
||||
$else:
|
||||
return $$expect_with_probability(value, expected, $probability);
|
||||
$endif;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locality for prefetch, levels 0 - 3, corresponding
|
||||
* to "extremely local" to "no locality"
|
||||
|
||||
@@ -123,6 +123,7 @@ macro bool is_subarray_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_intlike($Type)
|
||||
|
||||
@@ -2,6 +2,7 @@ module std::core::values;
|
||||
|
||||
macro TypeKind @typekind(#value) @builtin => $typeof(#value).kindof;
|
||||
macro bool @typeis(#value, $Type) @builtin => $typeof(#value).typeid == $Type.typeid;
|
||||
macro bool @is_bool(#value) => types::is_bool($typeof(#value));
|
||||
macro bool @is_int(#value) => types::is_int($typeof(#value));
|
||||
macro bool @convertable_to(#a, #b) => $checks($typeof(#b) x = #a);
|
||||
macro bool @is_floatlike(#value) => types::is_floatlike($typeof(#value));
|
||||
|
||||
Reference in New Issue
Block a user