11 for (
const auto* other : m_attach->depends) {
12 if (m_val_ty == other->m_val_ty || !other->m_val_ty)
16 m_val_ty = other->m_val_ty;
18 const auto merged = m_val_ty->coalesce(*other->m_val_ty);
20 throw std::logic_error(
"Conflicting types between AST nodes that are attached: `"s + m_val_ty->name() +
21 "` vs `" + other->m_val_ty->name() +
"`!");
31 if (m_tok.size() == 1)
34 return m_tok[0].loc + m_tok[m_tok.size() - 1].loc;
38 auto this_hash = std::hash<ast::AST>{}(*this);
39 auto other_hash = std::hash<ast::AST>{}(other);
41 return this_hash == other_hash;
45template <
typename T>
auto dup(
const vector<
AnyBase<T>>& items) {
46 auto dup = vector<AnyBase<T>>();
47 dup.reserve(items.size());
49 dup.emplace_back(unique_ptr<T>(i->clone()));
54template <
typename T>
auto dup(
const OptionalAnyBase<T>& ptr) -> OptionalAnyBase<T> {
56 return unique_ptr<T>(ptr->clone());
59template <
typename T>
auto dup(
const AnyBase<T>& ptr) -> AnyBase<T> {
return unique_ptr<T>(ptr->clone()); }
61template <std::derived_from<ast::AST> T>
auto dup(
const T& ast) -> T {
62 auto cloned = unique_ptr<T>(ast.clone());
66template <std::copy_constructible T>
auto dup(
const T& obj) -> T {
return T(obj); }
68template <visitable T>
auto dup(
const T& var) -> T {
69 return var.visit([](
auto&& x) {
return T{dup(std::forward<
decltype(x)>(x))}; });
72template <
typename T>
auto dup(
const optional<T>& opt) {
73 return opt.has_value() ? optional<T>{dup(opt.value())} : optional<T>{};
76template <
typename T>
auto dup(
const vector<T>& items) {
77 auto dup_vec = vector<T>();
78 dup_vec.reserve(items.size());
80 dup_vec.push_back(move(dup(i)));
132auto std::hash<yume::ast::AST>::operator()(
const yume::ast::AST& s)
const noexcept -> std::size_t {
virtual auto visit(const ast::AST &, string_view) -> Visitor &=0
All nodes in the AST tree of the program inherit from this class.
void unify_val_ty()
Verify the type compatibility of the depends of this node, and merge the types if possible....
auto equals_by_hash(ast::AST &other) const -> bool
auto location() const -> Loc
The union of the locations of the Tokens making up this node.
auto tok() const noexcept -> span< Token >
Represents "any" kind of ast node of type T. See AnyExpr, AnyStmt and AnyType.
Represents a location in source code, as a range starting at a line and column and ending at some oth...
auto clone() const -> AssignExpr *override
Deep copy, except for the type and attachments.
AssignExpr(span< Token > tok, AnyExpr target, AnyExpr value)
A logical operator such as || or &&. Since these aren't overloadable, they have their own AST node.
BinaryLogicExpr(span< Token > tok, Atom operation, AnyExpr lhs, AnyExpr rhs)
auto clone() const -> BinaryLogicExpr *override
Deep copy, except for the type and attachments.
Bool literals (true or false).
BoolExpr(span< Token > tok, bool val)
auto clone() const -> BoolExpr *override
Deep copy, except for the type and attachments.
A function call or operator.
CallExpr(span< Token > tok, string name, OptionalType receiver, vector< AnyExpr > args)
auto clone() const -> CallExpr *override
Deep copy, except for the type and attachments.
CharExpr(span< Token > tok, uint8_t val)
auto clone() const -> CharExpr *override
Deep copy, except for the type and attachments.
A statement consisting of multiple other statements, i.e. the body of a function.
Compound(span< Token > tok, vector< AnyStmt > body)
auto clone() const -> Compound *override
Deep copy, except for the type and attachments.
A declaration of a constant (const).
ConstDecl(span< Token > tok, string name, AnyType type, AnyExpr init)
auto clone() const -> ConstDecl *override
Deep copy, except for the type and attachments.
A constant. Currently global.
auto clone() const -> ConstExpr *override
Deep copy, except for the type and attachments.
optional< string > parent
ConstExpr(span< Token > tok, string name, optional< string > parent)
A declaration of a custom constructor (def :new).
CtorDecl(span< Token > tok, vector< TypeName > args, Compound body)
auto clone() const -> CtorDecl *override
Deep copy, except for the type and attachments.
A construction of a struct or cast of a primitive.
auto clone() const -> CtorExpr *override
Deep copy, except for the type and attachments.
CtorExpr(span< Token > tok, AnyType type, vector< AnyExpr > args)
A destruction of an object upon leaving its scope.
auto clone() const -> DtorExpr *override
Deep copy, except for the type and attachments.
DtorExpr(span< Token > tok, AnyExpr base)
Direct access of a field of a struct (::).
FieldAccessExpr(span< Token > tok, OptionalExpr base, string field)
auto clone() const -> FieldAccessExpr *override
Deep copy, except for the type and attachments.
A declaration of a function (def).
Body body
If this function declaration refers to a primitive, this field is a string representing the name of t...
auto clone() const -> FnDecl *override
Deep copy, except for the type and attachments.
FnDecl(span< Token > tok, string name, vector< TypeName > args, vector< GenericParam > type_args, OptionalType ret, Body body, std::unordered_set< string > annotations)
std::unordered_set< string > annotations
vector< GenericParam > type_args
A function type i.e. ->(Foo,Bar)Baz.
FunctionType(span< Token > tok, OptionalType ret, vector< AnyType > args, bool fn_ptr)
auto clone() const -> FunctionType *override
Deep copy, except for the type and attachments.
A generic, compile-time argument to a struct or function definition, comparable to C++ template param...
auto clone() const -> GenericParam *override
Deep copy, except for the type and attachments.
GenericParam(span< Token > tok, OptionalType type, string name)
Clauses of an if statement IfStmt.
auto clone() const -> IfClause *override
Deep copy, except for the type and attachments.
IfClause(span< Token > tok, AnyExpr cond, Compound body)
An if statement (if), with one or more IfClauses, and optionally an else clause.
auto clone() const -> IfStmt *override
Deep copy, except for the type and attachments.
optional< Compound > else_clause
vector< IfClause > clauses
IfStmt(span< Token > tok, vector< IfClause > clauses, optional< Compound > else_clause)
Represents an implicit cast to a different type, performed during semantic analysis.
ty::Conv conversion
The conversion steps performed during this cast.
auto clone() const -> ImplicitCastExpr *override
Deep copy, except for the type and attachments.
ImplicitCastExpr(span< Token > tok, AnyExpr base, ty::Conv conversion)
A local definition of an anonymous function.
auto clone() const -> LambdaExpr *override
Deep copy, except for the type and attachments.
LambdaExpr(span< Token > tok, vector< TypeName > args, OptionalType ret, Compound body, std::set< string > annotations)
std::set< string > annotations
NumberExpr(span< Token > tok, int64_t val)
auto clone() const -> NumberExpr *override
Deep copy, except for the type and attachments.
The top level structure of a file of source code.
auto clone() const -> Program *override
Deep copy, except for the type and attachments.
Program(span< Token > tok, vector< AnyStmt > body)
A type which refers to a different type, specifically that of a struct field.
ProxyType(span< Token > tok, string field)
auto clone() const -> ProxyType *override
Deep copy, except for the type and attachments.
A type with a Qualifier like mut or [] following.
QualType(span< Token > tok, AnyType base, Qualifier qualifier)
auto clone() const -> QualType *override
Deep copy, except for the type and attachments.
Return from a function body.
auto clone() const -> ReturnStmt *override
Deep copy, except for the type and attachments.
ReturnStmt(span< Token > tok, OptionalExpr expr)
SelfType(span< Token > tok)
auto clone() const -> SelfType *override
Deep copy, except for the type and attachments.
Just the name of a type, always capitalized.
SimpleType(span< Token > tok, string name)
auto clone() const -> SimpleType *override
Deep copy, except for the type and attachments.
A slice literal, i.e. an array.
SliceExpr(span< Token > tok, AnyType type, vector< AnyExpr > args)
auto clone() const -> SliceExpr *override
Deep copy, except for the type and attachments.
StringExpr(span< Token > tok, string val)
auto clone() const -> StringExpr *override
Deep copy, except for the type and attachments.
A declaration of a struct (struct) or an interface (interface).
vector< GenericParam > type_args
auto clone() const -> StructDecl *override
Deep copy, except for the type and attachments.
std::unordered_set< string > annotations
StructDecl(span< Token > tok, string name, vector< TypeName > fields, vector< GenericParam > type_args, Compound body, OptionalType implements, std::unordered_set< string > annotations, bool is_interface=false)
vector< TypeName > fields
A type with explicit type parameters i.e. Foo<Bar,Baz>.
TemplatedType(span< Token > tok, AnyType base, vector< AnyTypeOrExpr > type_args)
auto clone() const -> TemplatedType *override
Deep copy, except for the type and attachments.
vector< AnyTypeOrExpr > type_args
Represents a reference to a type.
TypeExpr(span< Token > tok, AnyType type)
auto clone() const -> TypeExpr *override
Deep copy, except for the type and attachments.
A pair of a Type and an identifier, i.e. a parameter name.
auto clone() const -> TypeName *override
Deep copy, except for the type and attachments.
TypeName(span< Token > tok, AnyType type, string name)
A declaration of a local variable (let).
VarDecl(span< Token > tok, string name, OptionalType type, AnyExpr init)
auto clone() const -> VarDecl *override
Deep copy, except for the type and attachments.
A variable, i.e. just an identifier.
auto clone() const -> VarExpr *override
Deep copy, except for the type and attachments.
VarExpr(span< Token > tok, string name)
WhileStmt(span< Token > tok, AnyExpr cond, Compound body)
auto clone() const -> WhileStmt *override
Deep copy, except for the type and attachments.