52#ifndef COMPILER_TYPES_H
53#define COMPILER_TYPES_H
55#include <initializer_list>
104 return "Type_Variable";
215 <<
"Compiler_Type_Context::mutable_type(): invalid id " << id;
216 return types.access(
id - 1);
241 for (
const auto id :
ids)
248 const auto &
ty =
type(
id);
263 for (
size_t i = 0; i <
ty.components.size(); ++i)
274 for (
size_t i = 0; i <
ty.components.size(); ++i)
373 <<
"Compiler_Type_Context::type(): invalid id " << id;
374 return types.access(
id - 1);
426 ty.components = members;
451 ty.components = parameters;
452 ty.result_type = result;
481 ty.label = std::move(name);
498 <<
"Compiler_Type_Context::set_struct_fields(): id " <<
id <<
" is not a struct";
500 <<
"Compiler_Type_Context::set_struct_fields(): mismatched field name/type arity";
502 ty.components = field_types;
518 ty.label = std::move(name);
532 <<
"Compiler_Type_Context::set_enum_variants(): id " <<
id <<
" is not an enum";
543 std::ostringstream
out;
551 std::ostringstream
out;
553 for (
size_t i = 0; i <
types.size(); ++i)
555 const auto &
ty =
types.access(i);
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_unless(C)
Throws std::out_of_range if condition does NOT hold.
#define ah_invalid_argument_unless(C)
Throws std::invalid_argument if condition does NOT hold.
size_t size_t int32_t * out
Context owning all compiler type nodes.
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.
bool is_type_variable(const Compiler_Type_Id id) const
Returns whether id names a type variable.
void append_type_text(std::ostream &out, const Compiler_Type_Id id) const
bool is_builtin(const Compiler_Type_Id id) const
Returns whether id names a built-in type.
Compiler_Type_Id string_type() const noexcept
Returns the preloaded String type id.
size_t builtin_count() const noexcept
Returns the number of eager built-in types.
Compiler_Type_Id integer_id
size_t size() const noexcept
Returns the total number of stored types.
std::string to_string(const Compiler_Type_Id id) const
Renders one type to a deterministic human-readable string.
Compiler_Type_Id string_id
Compiler_Type & mutable_type(const Compiler_Type_Id id) const
size_t builtin_type_count
bool is_struct(const Compiler_Type_Id id) const
Returns whether id names a nominal struct type.
Compiler_Type_Id make_enum_type(std::string name)
Creates a nominal enum type placeholder.
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.
const Compiler_Type & type(const Compiler_Type_Id id) const
Returns type id.
std::string dump_types() const
Dumps all known types in allocation order.
Compiler_Type_Id invalid_type() const noexcept
Returns the preloaded Invalid type id.
static DynArray< Compiler_Type_Id > list_to_array(const std::initializer_list< Compiler_Type_Id > ids)
Compiler_Type_Id append_type(Compiler_Type type)
void initialize_builtins()
Compiler_Type_Context()
Builds a fresh type context with the built-ins preloaded.
Compiler_Type_Id character_id
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.
Compiler_Type_Id invalid_id
Compiler_Type_Id make_tuple_type(const std::initializer_list< Compiler_Type_Id > members)
Creates a tuple type from a small literal list.
DynArray< Compiler_Type > types
Compiler_Type_Id make_type_variable(std::string label="", const bool rigid=false)
Creates a fresh type variable.
size_t next_type_variable_index
Compiler_Type_Id make_tuple_type(const DynArray< Compiler_Type_Id > &members)
Creates a tuple type.
Compiler_Type_Id make_function_type(const DynArray< Compiler_Type_Id > ¶meters, const Compiler_Type_Id result)
Creates a function type.
Compiler_Type_Id character_type() const noexcept
Returns the preloaded Char type id.
void clear() noexcept
Drops user-created types and keeps the built-ins.
Compiler_Type_Id unit_type() const noexcept
Returns the preloaded Unit type id.
bool is_enum(const Compiler_Type_Id id) const
Returns whether id names a nominal enum type.
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 make_struct_type(std::string name)
Creates a nominal struct type placeholder.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
const char * compiler_builtin_type_name(const Compiler_Builtin_Type builtin) noexcept
Returns a stable debug name for a built-in type.
@ String
String literal payload.
@ Character
Character literal payload.
@ Invalid
Uninitialized register/slot or explicit invalid placeholder.
Compiler_Type_Kind
Category of one stored type node.
@ Struct
Nominal struct declaration.
@ Enum
Nominal enum declaration.
Compiler_Builtin_Type
Built-in scalar types available in the MVP type graph.
and
Check uniqueness with explicit hash + equality functors.
const char * compiler_type_kind_name(const Compiler_Type_Kind kind) noexcept
Returns a stable debug name for a type kind.
@ Function
Function type such as fn(Int) -> Bool.
@ Tuple
Tuple type such as (Int, Bool).
One node inside the type graph.
DynArray< Compiler_Type_Id > components
Tuple members, function parameters, or struct field types.
Compiler_Type_Id id
Stable 1-based identifier.
bool rigid
Whether the type variable is rigid.
bool is_struct() const noexcept
Returns whether this node represents a nominal struct type.
Compiler_Type_Id result_type
Function return type, or 0.
Compiler_Builtin_Type builtin
Built-in tag.
Compiler_Type_Kind kind
Node category.
bool is_type_variable() const noexcept
Returns whether this node represents a type variable.
DynArray< std::string > member_names
Struct field names or enum variant names.
bool is_enum() const noexcept
Returns whether this node represents a nominal enum type.
std::string label
Display label for type variables or nominal type names.
bool is_builtin() const noexcept
Returns whether this node represents a built-in type.
Lazy and scalable dynamic array implementation.