|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Context owning all compiler type nodes. More...
#include <Compiler_Types.H>
Public Member Functions | |
| Compiler_Type_Context () | |
| Builds a fresh type context with the built-ins preloaded. | |
| void | clear () noexcept |
| Drops user-created types and keeps the built-ins. | |
| size_t | size () const noexcept |
| Returns the total number of stored types. | |
| size_t | builtin_count () const noexcept |
| Returns the number of eager built-in types. | |
| Compiler_Type_Id | invalid_type () const noexcept |
Returns the preloaded Invalid type id. | |
| Compiler_Type_Id | unit_type () const noexcept |
Returns the preloaded Unit type id. | |
| Compiler_Type_Id | bool_type () const noexcept |
Returns the preloaded Bool type id. | |
| Compiler_Type_Id | integer_type () const noexcept |
Returns the preloaded Int type id. | |
| Compiler_Type_Id | string_type () const noexcept |
Returns the preloaded String type id. | |
| Compiler_Type_Id | character_type () const noexcept |
Returns the preloaded Char type id. | |
| const Compiler_Type & | type (const Compiler_Type_Id id) const |
Returns type id. | |
| bool | is_type_variable (const Compiler_Type_Id id) const |
Returns whether id names a type variable. | |
| bool | is_builtin (const Compiler_Type_Id id) const |
Returns whether id names a built-in type. | |
| bool | is_struct (const Compiler_Type_Id id) const |
Returns whether id names a nominal struct type. | |
| bool | is_enum (const Compiler_Type_Id id) const |
Returns whether id names a nominal enum type. | |
| Compiler_Type_Id | make_type_variable (std::string label="", const bool rigid=false) |
| Creates a fresh type variable. | |
| Compiler_Type_Id | make_tuple_type (const DynArray< Compiler_Type_Id > &members) |
| Creates a tuple type. | |
| Compiler_Type_Id | make_tuple_type (const std::initializer_list< Compiler_Type_Id > members) |
| Creates a tuple type from a small literal list. | |
| Compiler_Type_Id | make_function_type (const DynArray< Compiler_Type_Id > ¶meters, const Compiler_Type_Id result) |
| Creates a function type. | |
| Compiler_Type_Id | make_function_type (const std::initializer_list< Compiler_Type_Id > parameters, const Compiler_Type_Id result) |
| Creates a function type from a small literal list. | |
| Compiler_Type_Id | make_struct_type (std::string name) |
Creates a nominal struct type placeholder. | |
| void | set_struct_fields (const Compiler_Type_Id id, const DynArray< std::string > &field_names, const DynArray< Compiler_Type_Id > &field_types) const |
Assigns field metadata to a previously created struct type. | |
| Compiler_Type_Id | make_enum_type (std::string name) |
Creates a nominal enum type placeholder. | |
| void | set_enum_variants (const Compiler_Type_Id id, const DynArray< std::string > &variant_names) const |
Assigns variant metadata to a previously created enum type. | |
| std::string | to_string (const Compiler_Type_Id id) const |
| Renders one type to a deterministic human-readable string. | |
| std::string | dump_types () const |
| Dumps all known types in allocation order. | |
Private Member Functions | |
| Compiler_Type_Id | append_type (Compiler_Type type) |
| Compiler_Type & | mutable_type (const Compiler_Type_Id id) const |
| void | initialize_builtins () |
| void | append_type_text (std::ostream &out, const Compiler_Type_Id id) const |
Static Private Member Functions | |
| static DynArray< Compiler_Type_Id > | list_to_array (const std::initializer_list< Compiler_Type_Id > ids) |
Private Attributes | |
| DynArray< Compiler_Type > | types |
| size_t | builtin_type_count = 0 |
| size_t | next_type_variable_index = 0 |
| Compiler_Type_Id | invalid_id = 0 |
| Compiler_Type_Id | unit_id = 0 |
| Compiler_Type_Id | bool_id = 0 |
| Compiler_Type_Id | integer_id = 0 |
| Compiler_Type_Id | string_id = 0 |
| Compiler_Type_Id | character_id = 0 |
Context owning all compiler type nodes.
Built-in types are created eagerly and remain stable for the life of the context. User-defined nodes can be discarded with clear(), which keeps the built-ins and resets the fresh type-variable counter. Transparent type aliases are resolved by semantic analysis and therefore do not create separate nodes in this graph.
Definition at line 192 of file Compiler_Types.H.
|
inline |
Builds a fresh type context with the built-ins preloaded.
Definition at line 293 of file Compiler_Types.H.
References initialize_builtins().
|
inlineprivate |
Definition at line 205 of file Compiler_Types.H.
References Aleph::Compiler_Type::id, type(), and types.
Referenced by initialize_builtins(), make_enum_type(), make_function_type(), make_struct_type(), make_tuple_type(), and make_type_variable().
|
inlineprivate |
Definition at line 246 of file Compiler_Types.H.
References append_type_text(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Builtin, Aleph::compiler_builtin_type_name(), Aleph::Enum, Aleph::Function, out, Aleph::Struct, Aleph::Tuple, type(), and Aleph::Type_Variable.
Referenced by append_type_text(), and to_string().
|
inlinenoexcept |
Returns the preloaded Bool type id.
Definition at line 341 of file Compiler_Types.H.
References bool_id.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::analyze_expr(), Aleph::Compiler_Typed_Semantic_Analyzer::analyze_stmt(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_builtin_type_name(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Returns the number of eager built-in types.
Definition at line 323 of file Compiler_Types.H.
References builtin_type_count.
|
inlinenoexcept |
Returns the preloaded Char type id.
Definition at line 359 of file Compiler_Types.H.
References character_id.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::analyze_expr(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_builtin_type_name(), TEST(), and TEST().
|
inlinenoexcept |
Drops user-created types and keeps the built-ins.
Fresh type-variable names restart from T0.
Definition at line 302 of file Compiler_Types.H.
References bool_id, builtin_type_count, character_id, initialize_builtins(), integer_id, invalid_id, next_type_variable_index, string_id, types, and unit_id.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::clear(), Aleph::Compiler_Line_Frontend::clear(), and TEST().
|
inline |
Dumps all known types in allocation order.
Definition at line 549 of file Compiler_Types.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::compiler_type_kind_name(), out, to_string(), and types.
Referenced by main().
|
inlineprivate |
Definition at line 219 of file Compiler_Types.H.
References append_type(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Bool, bool_id, Aleph::Builtin, Aleph::Compiler_Type::builtin, builtin_type_count, Aleph::Character, character_id, Aleph::Integer, integer_id, Aleph::Invalid, invalid_id, Aleph::Compiler_Type::kind, Aleph::String, string_id, type(), types, Aleph::Unit, and unit_id.
Referenced by Compiler_Type_Context(), and clear().
|
inlinenoexcept |
Returns the preloaded Int type id.
Definition at line 347 of file Compiler_Types.H.
References integer_id.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::analyze_expr(), Aleph::Compiler_Line_Frontend::build_hir_operand(), Aleph::Compiler_Line_Frontend::build_ir_operand(), Aleph::Compiler_Line_Frontend::lower_hir(), Aleph::Compiler_Line_Frontend::lower_ir(), main(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_builtin_type_name(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Returns the preloaded Invalid type id.
Definition at line 329 of file Compiler_Types.H.
References invalid_id.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::analyze_expr(), Aleph::Compiler_Typed_Semantic_Analyzer::analyze_stmt(), Aleph::Compiler_HIR_Lowering::invalid_type(), Aleph::Compiler_Typed_Semantic_Analyzer::is_invalid_type(), Aleph::Compiler_Typed_Semantic_Analyzer::lookup_name_type(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_alias_binding(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_builtin_type_name(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_type_annotation(), and TEST().
|
inline |
Returns whether id names a built-in type.
Definition at line 384 of file Compiler_Types.H.
References Aleph::Builtin, Aleph::Compiler_Type::kind, and type().
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::collect_free_type_variables(), and Aleph::Compiler_Typed_Semantic_Analyzer::instantiate_type().
|
inline |
Returns whether id names a nominal enum type.
Definition at line 396 of file Compiler_Types.H.
References Aleph::Enum, Aleph::Compiler_Type::kind, and type().
|
inline |
Returns whether id names a nominal struct type.
Definition at line 390 of file Compiler_Types.H.
References Aleph::Compiler_Type::kind, Aleph::Struct, and type().
|
inline |
Returns whether id names a type variable.
Definition at line 378 of file Compiler_Types.H.
References Aleph::Compiler_Type::kind, type(), and Aleph::Type_Variable.
Referenced by TEST().
|
inlinestaticprivate |
Definition at line 238 of file Compiler_Types.H.
References Aleph::DynArray< T >::append(), and Aleph::blossom_maximum_cardinality_matching().
Referenced by make_function_type(), and make_tuple_type().
|
inline |
Creates a nominal enum type placeholder.
The resulting type is opaque for unification purposes and prints as its declared name. Variant metadata can be filled later with set_enum_variants().
| name | Declared nominal type name. |
enum. Definition at line 514 of file Compiler_Types.H.
References append_type(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Enum, and Aleph::Compiler_Type::kind.
Referenced by main(), Aleph::Compiler_Typed_Semantic_Analyzer::predeclare_module_type_declarations(), and TEST().
|
inline |
Creates a function type.
| parameters | Parameter types in declaration order. |
| result | Return type. |
Definition at line 446 of file Compiler_Types.H.
References append_type(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Function, and Aleph::Compiler_Type::kind.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::analyze_expr(), Aleph::Compiler_Type_Substitution::apply(), Aleph::Compiler_Typed_Semantic_Analyzer::instantiate_type(), Aleph::Compiler_Line_Frontend::lower_hir(), Aleph::Compiler_Line_Frontend::lower_ir(), main(), make_function_type(), Aleph::Compiler_Typed_Semantic_Analyzer::predeclare_function(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_type_annotation(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Creates a function type from a small literal list.
| parameters | Parameter types in declaration order. |
| result | Return type. |
Definition at line 462 of file Compiler_Types.H.
References list_to_array(), and make_function_type().
|
inline |
Creates a nominal struct type placeholder.
The resulting type is opaque for unification purposes and prints as its declared name. Field metadata can be filled later with set_struct_fields().
| name | Declared nominal type name. |
struct. Definition at line 477 of file Compiler_Types.H.
References append_type(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Type::kind, and Aleph::Struct.
Referenced by main(), Aleph::Compiler_Typed_Semantic_Analyzer::predeclare_module_type_declarations(), and TEST().
|
inline |
Creates a tuple type.
| members | Member types in source order. |
Definition at line 422 of file Compiler_Types.H.
References append_type(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Type::kind, and Aleph::Tuple.
Referenced by Aleph::Compiler_Type_Substitution::apply(), Aleph::Compiler_Typed_Semantic_Analyzer::instantiate_type(), make_tuple_type(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_type_annotation(), TEST(), and TEST().
|
inline |
Creates a tuple type from a small literal list.
| members | Member types in source order. |
Definition at line 435 of file Compiler_Types.H.
References list_to_array(), and make_tuple_type().
|
inline |
Creates a fresh type variable.
| label | Optional display name. If empty, a fresh T<n> label is generated. |
| rigid | If true, unifiers should treat the variable as rigid. |
Definition at line 408 of file Compiler_Types.H.
References append_type(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Type::kind, next_type_variable_index, and Aleph::Type_Variable.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::fresh_type(), main(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_type_annotation(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 212 of file Compiler_Types.H.
References ah_out_of_range_error_unless, Aleph::and, and types.
Referenced by set_enum_variants(), and set_struct_fields().
|
inline |
Assigns variant metadata to a previously created enum type.
| id | Enum type id returned by make_enum_type(). |
| variant_names | Variant names in declaration order. |
| std::invalid_argument | If id is not an enum. |
Definition at line 528 of file Compiler_Types.H.
References ah_invalid_argument_unless, Aleph::blossom_maximum_cardinality_matching(), Aleph::Enum, and mutable_type().
Referenced by main(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_module_type_declaration(), and TEST().
|
inline |
Assigns field metadata to a previously created struct type.
| id | Struct type id returned by make_struct_type(). |
| field_names | Field names in declaration order. |
| field_types | Field types in declaration order. |
| std::invalid_argument | If id is not a struct or the arities differ. |
Definition at line 492 of file Compiler_Types.H.
References ah_invalid_argument_unless, Aleph::blossom_maximum_cardinality_matching(), mutable_type(), Aleph::DynArray< T >::size(), and Aleph::Struct.
Referenced by main(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_module_type_declaration(), and TEST().
|
inlinenoexcept |
Returns the total number of stored types.
Definition at line 317 of file Compiler_Types.H.
References types.
Referenced by TEST().
|
inlinenoexcept |
Returns the preloaded String type id.
Definition at line 353 of file Compiler_Types.H.
References string_id.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::analyze_expr(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_builtin_type_name(), TEST(), TEST(), and TEST().
|
inline |
Renders one type to a deterministic human-readable string.
| id | Type to render. |
Int or fn(Int) -> Bool. Definition at line 541 of file Compiler_Types.H.
References append_type_text(), and out.
Referenced by Aleph::Compiler_Type_Unifier::bind_variable(), Aleph::compiler_dump_hir_function(), Aleph::compiler_dump_hir_module(), Aleph::Compiler_Typed_Semantic_Analyzer::dump_inference(), dump_types(), main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), Aleph::Compiler_Type_Substitution::to_string(), Aleph::Compiler_C_Backend::Emitter::type_comment(), Aleph::Compiler_HIR_Detail::type_text(), and Aleph::Compiler_Type_Unifier::unify().
|
inline |
Returns type id.
| id | 1-based type identifier. |
| std::out_of_range | If id is invalid. |
Definition at line 370 of file Compiler_Types.H.
References ah_out_of_range_error_unless, Aleph::and, and types.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::analyze_function(), append_type(), append_type_text(), Aleph::Compiler_Type_Substitution::apply(), Aleph::Compiler_Type_Unifier::bind_variable(), Aleph::Compiler_Typed_Semantic_Analyzer::collect_free_type_variables(), initialize_builtins(), Aleph::Compiler_Typed_Semantic_Analyzer::instantiate_type(), is_builtin(), is_enum(), is_struct(), is_type_variable(), Aleph::Compiler_Type_Unifier::occurs_in(), TEST(), TEST(), and Aleph::Compiler_Type_Unifier::unify().
|
inlinenoexcept |
Returns the preloaded Unit type id.
Definition at line 335 of file Compiler_Types.H.
References unit_id.
Referenced by Aleph::Compiler_Typed_Semantic_Analyzer::analyze_function(), Aleph::Compiler_Typed_Semantic_Analyzer::analyze_stmt(), Aleph::Compiler_IR_Lowering::Module_Builder::emit_unit_constant(), Aleph::Compiler_Typed_Semantic_Analyzer::resolve_builtin_type_name(), and TEST().
|
private |
Definition at line 200 of file Compiler_Types.H.
Referenced by bool_type(), clear(), and initialize_builtins().
|
private |
Definition at line 195 of file Compiler_Types.H.
Referenced by builtin_count(), clear(), and initialize_builtins().
|
private |
Definition at line 203 of file Compiler_Types.H.
Referenced by character_type(), clear(), and initialize_builtins().
|
private |
Definition at line 201 of file Compiler_Types.H.
Referenced by clear(), initialize_builtins(), and integer_type().
|
private |
Definition at line 198 of file Compiler_Types.H.
Referenced by clear(), initialize_builtins(), and invalid_type().
|
private |
Definition at line 196 of file Compiler_Types.H.
Referenced by clear(), and make_type_variable().
|
private |
Definition at line 202 of file Compiler_Types.H.
Referenced by clear(), initialize_builtins(), and string_type().
|
private |
Definition at line 194 of file Compiler_Types.H.
Referenced by append_type(), clear(), dump_types(), initialize_builtins(), mutable_type(), size(), and type().
|
private |
Definition at line 199 of file Compiler_Types.H.
Referenced by clear(), initialize_builtins(), and unit_type().