Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Compiler_Type_Context Class Reference

Context owning all compiler type nodes. More...

#include <Compiler_Types.H>

Collaboration diagram for Aleph::Compiler_Type_Context:
[legend]

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 > &parameters, 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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Compiler_Type_Context()

Aleph::Compiler_Type_Context::Compiler_Type_Context ( )
inline

Builds a fresh type context with the built-ins preloaded.

Definition at line 293 of file Compiler_Types.H.

References initialize_builtins().

Member Function Documentation

◆ append_type()

Compiler_Type_Id Aleph::Compiler_Type_Context::append_type ( Compiler_Type  type)
inlineprivate

◆ append_type_text()

void Aleph::Compiler_Type_Context::append_type_text ( std::ostream &  out,
const Compiler_Type_Id  id 
) const
inlineprivate

◆ bool_type()

Compiler_Type_Id Aleph::Compiler_Type_Context::bool_type ( ) const
inlinenoexcept

◆ builtin_count()

size_t Aleph::Compiler_Type_Context::builtin_count ( ) const
inlinenoexcept

Returns the number of eager built-in types.

Definition at line 323 of file Compiler_Types.H.

References builtin_type_count.

Referenced by TEST(), and TEST().

◆ character_type()

Compiler_Type_Id Aleph::Compiler_Type_Context::character_type ( ) const
inlinenoexcept

◆ clear()

void Aleph::Compiler_Type_Context::clear ( )
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().

◆ dump_types()

std::string Aleph::Compiler_Type_Context::dump_types ( ) const
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().

◆ initialize_builtins()

◆ integer_type()

◆ invalid_type()

◆ is_builtin()

bool Aleph::Compiler_Type_Context::is_builtin ( const Compiler_Type_Id  id) const
inline

◆ is_enum()

bool Aleph::Compiler_Type_Context::is_enum ( const Compiler_Type_Id  id) const
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().

Referenced by TEST(), and TEST().

◆ is_struct()

bool Aleph::Compiler_Type_Context::is_struct ( const Compiler_Type_Id  id) const
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().

Referenced by TEST(), and TEST().

◆ is_type_variable()

bool Aleph::Compiler_Type_Context::is_type_variable ( const Compiler_Type_Id  id) const
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().

◆ list_to_array()

static DynArray< Compiler_Type_Id > Aleph::Compiler_Type_Context::list_to_array ( const std::initializer_list< Compiler_Type_Id >  ids)
inlinestaticprivate

◆ make_enum_type()

Compiler_Type_Id Aleph::Compiler_Type_Context::make_enum_type ( std::string  name)
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().

Parameters
nameDeclared nominal type name.
Returns
Stable type id for the 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().

◆ make_function_type() [1/2]

◆ make_function_type() [2/2]

Compiler_Type_Id Aleph::Compiler_Type_Context::make_function_type ( const std::initializer_list< Compiler_Type_Id >  parameters,
const Compiler_Type_Id  result 
)
inline

Creates a function type from a small literal list.

Parameters
parametersParameter types in declaration order.
resultReturn type.
Returns
Stable type id for the function type.

Definition at line 462 of file Compiler_Types.H.

References list_to_array(), and make_function_type().

◆ make_struct_type()

Compiler_Type_Id Aleph::Compiler_Type_Context::make_struct_type ( std::string  name)
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().

Parameters
nameDeclared nominal type name.
Returns
Stable type id for the 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().

◆ make_tuple_type() [1/2]

Compiler_Type_Id Aleph::Compiler_Type_Context::make_tuple_type ( const DynArray< Compiler_Type_Id > &  members)
inline

◆ make_tuple_type() [2/2]

Compiler_Type_Id Aleph::Compiler_Type_Context::make_tuple_type ( const std::initializer_list< Compiler_Type_Id >  members)
inline

Creates a tuple type from a small literal list.

Parameters
membersMember types in source order.
Returns
Stable type id for the tuple.

Definition at line 435 of file Compiler_Types.H.

References list_to_array(), and make_tuple_type().

◆ make_type_variable()

Compiler_Type_Id Aleph::Compiler_Type_Context::make_type_variable ( std::string  label = "",
const bool  rigid = false 
)
inline

Creates a fresh type variable.

Parameters
labelOptional display name. If empty, a fresh T<n> label is generated.
rigidIf true, unifiers should treat the variable as rigid.
Returns
Stable type id for the new variable.

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().

◆ mutable_type()

Compiler_Type & Aleph::Compiler_Type_Context::mutable_type ( const Compiler_Type_Id  id) const
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().

◆ set_enum_variants()

void Aleph::Compiler_Type_Context::set_enum_variants ( const Compiler_Type_Id  id,
const DynArray< std::string > &  variant_names 
) const
inline

Assigns variant metadata to a previously created enum type.

Parameters
idEnum type id returned by make_enum_type().
variant_namesVariant names in declaration order.
Exceptions
std::invalid_argumentIf 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().

◆ set_struct_fields()

void Aleph::Compiler_Type_Context::set_struct_fields ( const Compiler_Type_Id  id,
const DynArray< std::string > &  field_names,
const DynArray< Compiler_Type_Id > &  field_types 
) const
inline

Assigns field metadata to a previously created struct type.

Parameters
idStruct type id returned by make_struct_type().
field_namesField names in declaration order.
field_typesField types in declaration order.
Exceptions
std::invalid_argumentIf 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().

◆ size()

size_t Aleph::Compiler_Type_Context::size ( ) const
inlinenoexcept

Returns the total number of stored types.

Definition at line 317 of file Compiler_Types.H.

References types.

Referenced by TEST().

◆ string_type()

Compiler_Type_Id Aleph::Compiler_Type_Context::string_type ( ) const
inlinenoexcept

◆ to_string()

std::string Aleph::Compiler_Type_Context::to_string ( const Compiler_Type_Id  id) const
inline

◆ type()

◆ unit_type()

Member Data Documentation

◆ bool_id

Compiler_Type_Id Aleph::Compiler_Type_Context::bool_id = 0
private

Definition at line 200 of file Compiler_Types.H.

Referenced by bool_type(), clear(), and initialize_builtins().

◆ builtin_type_count

size_t Aleph::Compiler_Type_Context::builtin_type_count = 0
private

Definition at line 195 of file Compiler_Types.H.

Referenced by builtin_count(), clear(), and initialize_builtins().

◆ character_id

Compiler_Type_Id Aleph::Compiler_Type_Context::character_id = 0
private

Definition at line 203 of file Compiler_Types.H.

Referenced by character_type(), clear(), and initialize_builtins().

◆ integer_id

Compiler_Type_Id Aleph::Compiler_Type_Context::integer_id = 0
private

Definition at line 201 of file Compiler_Types.H.

Referenced by clear(), initialize_builtins(), and integer_type().

◆ invalid_id

Compiler_Type_Id Aleph::Compiler_Type_Context::invalid_id = 0
private

Definition at line 198 of file Compiler_Types.H.

Referenced by clear(), initialize_builtins(), and invalid_type().

◆ next_type_variable_index

size_t Aleph::Compiler_Type_Context::next_type_variable_index = 0
private

Definition at line 196 of file Compiler_Types.H.

Referenced by clear(), and make_type_variable().

◆ string_id

Compiler_Type_Id Aleph::Compiler_Type_Context::string_id = 0
private

Definition at line 202 of file Compiler_Types.H.

Referenced by clear(), initialize_builtins(), and string_type().

◆ types

DynArray<Compiler_Type> Aleph::Compiler_Type_Context::types
private

◆ unit_id

Compiler_Type_Id Aleph::Compiler_Type_Context::unit_id = 0
private

Definition at line 199 of file Compiler_Types.H.

Referenced by clear(), initialize_builtins(), and unit_type().


The documentation for this class was generated from the following file: