Type information¶
There is a possibility in Ü to obtain in compile-type various information about a type.
Special operator typeinfo
exists for obtaining of type information.
Type name is specified in <//>
.
The result of this operator is a reference to a constant structure that contains information about the given type.
fn Foo()
{
auto &constexpr int_info = typeinfo</i32/>;
}
Type information contents¶
Typeinfo for each type has following fields:
size_type size_of
- type size in bytessize_type align_of
- type alignment in bytesbool is_fundamental
- is a type fundamentalbool is_enum
- is a type an enumbool is_array
- is a type an arraybool is_tuple
- is a type a tuplebool is_raw_pointer
- is a type a raw pointerbool is_class
- is a type a struct or classbool is_function_pointer
- is a type a function pointersize_type reference_tag_count
- number of inner reference tagsbool is_default_constructible
- is a type default-constructiblebool is_copy_constructible
- is a type copy-constructiblebool is_copy_assignable
- is a type copy-assignablebool is_equality_comparable
- is a type equality-comparable
static_assert( typeinfo</ char8 />.size_of == 1s );
static_assert( typeinfo</ f64 />.align_of == 8s );
static_assert( typeinfo</i32/>.is_fundamental );
static_assert( typeinfo</ [f64, 4] />.is_array );
static_assert( typeinfo</ tup[i32, bool] />.is_tuple );
static_assert( typeinfo</ fn() />.is_function_pointer );
struct S{}
static_assert( typeinfo</ S />.is_class );
There are also additional fields depending on the type kind.
Type information for fundamental types¶
bool is_integer
- is a type integerbool is_numeric
- is a type numericbool is_signed_integer
- is a type signed integerbool is_unsigned_integer
- is a type unsigned integerbool is_float
- is a type floating pointbool is_char
- is a type charbool is_bool
- this type isbool
bool is_void
- this type isvoid
static_assert( typeinfo</i16/>.is_integer );
static_assert( typeinfo</u64/>.is_numeric );
static_assert( typeinfo</i32/>.is_signed_integer );
static_assert( typeinfo</u32/>.is_unsigned_integer );
static_assert( typeinfo</f32/>.is_float );
static_assert( typeinfo</char16/>.is_char );
static_assert( typeinfo</bool/>.is_bool );
static_assert( typeinfo</void/>.is_void );
Type information for enums¶
size_type element_count
- number of members in an enumtypeinfo & underlying_type
- type information for enum underlying typetup[] elements_list
- a tuple, each element of which contains information about an enum member
Each enum member description contains:
[char8, size]& name
- member nameunderlying_type value
- member value
enum E : u8 { A, B, C }
auto &info = typeinfo</E/>;
static_assert( info.element_count == 3s );
static_assert( info.underlying_type.is_unsigned_integer );
static_assert( info.underlying_type.size_of == 1s );
static_assert( info.elements_list[0].value == 0u8 );
static_assert( info.elements_list[1].value == 1u8 );
static_assert( info.elements_list[2].value == 2u8 );
static_assert( info.elements_list[0].name[0] == "A"c8 );
static_assert( info.elements_list[1].name[0] == "B"c8 );
static_assert( info.elements_list[2].name[0] == "C"c8 );
Type information for arrays¶
size_type element_count
- number of elements in an arraytypeinfo & element_type
- type information for array element type
static_assert( typeinfo</ [ i32, 7 ] />.element_count == 7s );
static_assert( typeinfo</ [ f64, 1 ] />.element_type.is_float );
Type information for tuples¶
size_type element_count
- number of elements in a tupletup[] elements_list
- a tuple, each element of which contains information about a tuple element
Each tuple element description contains:
typeinfo & type
- type information for element typesize_type index
- index of this element in the tuplesize_type offset
- offset in bytes of address of this element relative to address of the tuple
static_assert( typeinfo</ tup[] />.element_count == 0s );
static_assert( typeinfo</ tup[ f32, i32 ] />.element_count == 2s );
static_assert( typeinfo</ tup[ f32, bool, i32 ] />.elements_list[1].type.is_bool );
static_assert( typeinfo</ tup[ f64 ] />.elements_list[0].type.size_of == 8s );
static_assert( typeinfo</ tup[ i32, bool ] />.elements_list[1].offset == 4s );
static_assert( typeinfo</ tup[ i16, i16, i16, bool ] />.elements_list[3].index == 3s );
Type information for structs/classes¶
size_type field_count
- number of fieldssize_type parent_count
- number of parentsbool is_struct
- is a type a structbool is_polymorph
- is a type polymorphbool is_final
- is a type final (from which it’s not possible to inherit)bool is_abstract
- is a type abstract (values of this type can’t be constructed)bool is_interface
- is a type an interfacebool is_typeinfo
- is a type atypeinfo
struct or its partbool is_coroutine
- is a type a coroutine typetup[] fields_list
- a tuple, each element of which contains information about a field of the struct or classtup[] types_list
- a tuple, each element of which contains information about a nested type of the struct or classtup[] functions_list
- a tuple, each element of which contains information about a struct or class functiontup[] parents_list
- a tuple, each element of which contains information about a parent of the class
Each field, nested type, function description contains:
[char8, size]& name
- a name of a memberbool is_public
- is a memberpublic
bool is_private
- is a memberprivate
bool is_protected
- is a memberprotected
Each field description contains:
typeinfo & type
- type information for field typetypeinfo & class_type
- type information for struct or class in which this field is locatedsize_type offset
- offset in bytes of address of this field relative to address of the struct or classbool is_reference
- is a field referencebool is_mutable
- is a field mutable
Each nested type description contains:
typeinfo & type
- type information
Each function description contains:
typeinfo & type
- function type descriptionbool is_this_call
- is first parameterthis
bool is_generated
- is function generated by the compilerbool is_deleted
- is function marked as (= delete
)bool is_virtual
- is this a virtual method
Each parent class description contains:
typeinfo & type
- type information for parent classsize_type offset
- offset in bytes of address of this parent relative to address of the class
Type information for polymorph classes contains also field size_type& type_id
. See. “type_id”.
Type information for coroutines also contains following fields:
is_generator
- is this a generator coroutinetypeinfo & coroutine_return_type
- return type of a coroutinebool coroutine_return_value_is_reference
- does coroutine return referencebool coroutine_return_value_is_mutable
- does coroutine return mutable value
struct S{ i32 a; f32 b; bool c; }
class I interface {}
class A abstract {}
class NP {}
class PNF : I {}
class PF final : I {}
static_assert( typeinfo</S/>.is_struct );
static_assert( typeinfo</S/>.is_final );
static_assert( typeinfo</I/>.is_polymorph );
static_assert( typeinfo</I/>.is_abstract );
static_assert( typeinfo</I/>.is_interface );
static_assert( typeinfo</A/>.is_polymorph );
static_assert( typeinfo</A/>.is_abstract );
static_assert( typeinfo</NP/>.is_final );
static_assert( typeinfo</PNF/>.is_polymorph );
static_assert( typeinfo</PF/>.is_polymorph );
static_assert( typeinfo</PF/>.is_final );
static_assert( typeinfo</S/>.parent_count == 0s );
static_assert( typeinfo</PNF/>.parent_count == 1s );
static_assert( typeinfo</S/>.field_count == 3s );
Type information for function pointers¶
typeinfo & return_type
- type information for return typebool return_value_is_reference
- does function return referencebool return_value_is_mutable
- does function return mutable valuebool unsafe
- is function marked asunsafe
tup[] params_list
- a tuple, each element of which contains information about a function parameterreturn_references
- an array with return references description (as in notation with@
)return_inner_references
- a tuple with return inner references description (as in notation with@
)references_pollution
- an array with references pollution description (as in notation with@
)
Each parameter description contains:
typeinfo & type
- type information for parameter typebool is_reference
- is this parameter referencebool is_mutable
- is this parameter mutable
type fn_ptr= fn( i32 x, f32& y, bool &mut z ) : i32;
auto& info = typeinfo</fn_ptr/>;
static_assert( info.return_type.is_signed_integer );
static_assert( info.return_type.size_of == 4s );
static_assert( !info.unsafe );
static_assert( info.arguments_list[1].type.is_float );
static_assert( info.arguments_list[1].is_reference );
static_assert( info.arguments_list[2].is_mutable );
Type information for raw pointers¶
typeinfo & element_type
- type information for pointer element type