Reflection
C3 allows both compile time and runtime reflection.
During compile time the type information may be directly used as compile time constants, the same data is then available dynamically at runtime.
Note that not all reflection is implemented in the compiler at this point in time.
Compile time reflection
During compile time there are a number of compile time fields that may be accessed directly.
Type properties
It is possible to access properties on the type itself:
alignof
associated
elements
extnameof
inf
inner
kindof
len
max
membersof
min
nan
nameof
names
params
parentof
qnameof
returns
sizeof
typeid
values
alignof
Returns the alignment in bytes needed for the type.
associated
Only available for enums. Returns an array containing the types of associated values if any.
elements
Returns the element count of an enum or fault.
inf
Only available for floating point types
Returns a representation of floating point “infinity”.
inner
This returns a typeid to an “inner” type. What this means is different for each type:
- Array -> the array base type.
- Bitstruct -> underlying base type.
- Distinct -> the underlying type.
- Enum -> underlying enum base type.
- Pointer -> the type being pointed to.
- Vector -> the vector base type.
It is not defined for other types.
kindof
Returns the underlying TypeKind
as defined in std::core::types.
len
Returns the length of the array.
max
Returns the maximum value of the type (only valid for integer and float types).
membersof
Only available for bitstruct, struct and union types.
Returns a compile time list containing the fields in a bitstruct, struct or union. The
elements have the compile time only type of member_ref
.
Note: As the list is an “untyped” list, you are limited to iterating and accessing it at compile time.
A member_ref
has properties alignof
, kindof
, membersof
, nameof
, offsetof
, sizeof
and typeid
.
min
Returns the minimum value of the type (only valid for integer and float types).
nameof
Returns the name of the type.
names
Returns a slice containing the names of an enum or fault.
paramsof
Only available for function pointer types. Returns a ReflectParam struct for all function pointer parameters.
parentof
Only available for bitstruct and struct types. Returns the typeid of the parent type.
returns
Only available for function types. Returns the typeid of the return type.
sizeof
Returns the size in bytes for the given type, like C sizeof
.
typeid
Returns the typeid for the given type. def
s will return the typeid of the underlying type. The typeid size is the same as that of an iptr
.
values
Returns a slice containing the values of an enum or fault.
Compile time functions
There are several built-in functions to inspect the code during compile time.
$alignof
$defined
$eval
$evaltype
$extnameof
$nameof
$offsetof
$qnameof
$sizeof
$stringify
$typeof
$alignof
Returns the alignment in bytes needed for the type or member.
$defined
Returns true if the expression inside is defined and all sub expressions are valid.
$eval
Converts a compile time string with the corresponding variable:
$eval
is limited to a single, optionally path prefixed, identifier.
Consequently methods cannot be evaluated directly:
$evaltype
Similar to $eval
but for types:
$extnameof
Returns the external name of a type, variable or function. The external name is the one used by the linker.
$nameof
Returns the name of a function or variable as a string without module prefixes.
$offsetof
Returns the offset of a member in a struct.
$qnameof
Returns the same as $nameof
, but with the full module name prepended.
$sizeof
This is used on a value to determine the allocation size needed. $sizeof(a)
is equivalent
to doing $typeof(a).sizeof
. Note that this is only used on values and not on types.
$stringify
Returns the expression as a string. It has a special behaviour for macro expression parameters,
where $stringify(#foo)
will return the expression contained in #foo
rather than simply return
“#foo”
$typeof
Returns the type of an expression or variable as a type itself.