Yume
type_walker.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "ast/crtp_walker.hpp"
6#include "ty/type_base.hpp"
7#include "util.hpp"
8#include <map>
9#include <queue>
10#include <stdexcept>
11#include <string>
12
13namespace yume {
14class Compiler;
15struct Fn;
16namespace ast {
17class AST;
18struct CallExpr;
19struct CtorExpr;
20class Expr;
21class Stmt;
22class Type;
23} // namespace ast
24namespace ty {
25class BaseType;
26}
27} // namespace yume
28
29namespace yume::semantic {
30
31/// Determine the type information of AST nodes.
32/// This makes up most of the "semantic" phase of the compiler.
33struct TypeWalker : public CRTPWalker<TypeWalker> {
34 friend CRTPWalker;
35
36public:
37 struct ASTWithName {
39 string name;
40 };
41
46 vector<scope_t> enclosing_scopes{};
47 vector<ASTWithName> closured{};
48
49 std::queue<DeclLike> decl_queue{};
50
51 /// Whether or not to compile the bodies of methods. Initially, on the parameter types of methods are traversed and
52 /// converted, then everything else in a second pass.
53 bool in_depth = false;
54
56
59
60 void resolve_queue();
61
62 auto make_dup(ast::AnyExpr& expr) -> Fn*;
63
64private:
65 /// Convert an ast type (`ast::Type`) into a type in the type system (`ty::Type`).
66 auto convert_type(ast::Type& ast_type) -> ty::Type;
67 auto create_slice_type(const ty::Type& base_type) -> ty::Type;
68 void direct_call_operator(ast::CallExpr& expr);
69
70 auto get_or_declare_instantiation(Struct* struct_obj, Substitutions subs) -> ty::Type;
71
72 auto all_fn_overloads_by_name(ast::CallExpr& call) -> OverloadSet;
73 auto all_ctor_overloads_by_type(Struct& st, ast::CtorExpr& call) -> OverloadSet;
74
75 auto with_saved_scope(auto&& callback) {
76 // Save everything pertaining to the old context
77 auto saved_scope = scope;
78 auto saved_current_decl = current_decl;
79 auto saved_depth = in_depth;
80 auto saved_closured = closured;
81
82 callback();
83
84 // Restore again
85 closured = saved_closured;
86 in_depth = saved_depth;
87 current_decl = saved_current_decl;
88 scope = saved_scope;
89 }
90
91 template <typename T> void statement([[maybe_unused]] T& stat) {
92 throw std::runtime_error("Type walker stubbed on statement "s + stat.kind_name());
93 }
94
95 template <typename T> void expression([[maybe_unused]] T& expr) {
96 throw std::runtime_error("Type walker stubbed on expression "s + expr.kind_name());
97 }
98};
99
100void make_implicit_conversion(ast::OptionalExpr& expr, optional<ty::Type> target_ty);
101
102} // namespace yume::semantic
The Compiler the the primary top-level type during compilation. A single instance is created during t...
Definition: compiler.hpp:45
All nodes in the AST tree of the program inherit from this class.
Definition: ast.hpp:224
Expressions have an associated value and type.
Definition: ast.hpp:438
Statements make up most things in source code.
Definition: ast.hpp:297
A type annotation. This (ast::Type) is distinct from the actual type of a value (ty::Type).
Definition: ast.hpp:312
A "qualified" type, with a non-stackable qualifier, i.e. mut.
Definition: type_base.hpp:66
OptionalAnyBase< Expr > OptionalExpr
Definition: ast.hpp:450
void make_implicit_conversion(ast::OptionalExpr &expr, optional< ty::Type > target_ty)
Definition: type_walker.cpp:55
Definition: ast.cpp:8
A helper template to walk the Abstract Syntax Tree (AST), utilizing the Curiously Recurring Template ...
Definition: crtp_walker.hpp:23
A common base between declarations in the compiler: Fn, Struct and Const. Its value may also be absen...
Definition: vals.hpp:208
A function declaration in the compiler.
Definition: vals.hpp:52
A struct declaration in the compiler.
Definition: vals.hpp:136
A function call or operator.
Definition: ast.hpp:562
A construction of a struct or cast of a primitive.
Definition: ast.hpp:597
Determine the type information of AST nodes. This makes up most of the "semantic" phase of the compil...
Definition: type_walker.hpp:33
auto make_dup(ast::AnyExpr &expr) -> Fn *
std::queue< DeclLike > decl_queue
Definition: type_walker.hpp:49
void body_statement(ast::Stmt &)
void body_expression(ast::Expr &)
vector< ASTWithName > closured
Definition: type_walker.hpp:47
vector< scope_t > enclosing_scopes
Definition: type_walker.hpp:46
TypeWalker(Compiler &compiler)
Definition: type_walker.hpp:55
bool in_depth
Whether or not to compile the bodies of methods. Initially, on the parameter types of methods are tra...
Definition: type_walker.hpp:53