Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
ast.cpp
Go to the documentation of this file.
1#include "thorin/fe/ast.h"
2
3#include "thorin/check.h"
4#include "thorin/def.h"
5#include "thorin/rewrite.h"
6#include "thorin/world.h"
7
8#include "thorin/fe/scopes.h"
9
10namespace thorin {
11
12/*
13 * bind
14 */
15
16void IdPtrn::bind(Scopes& scopes, const Def* def, bool rebind) const {
17 scopes.bind(dbg(), def, rebind || this->rebind());
18}
19
20void TuplePtrn::bind(Scopes& scopes, const Def* def, bool rebind) const {
21 scopes.bind(dbg(), def, rebind || this->rebind());
22 for (size_t i = 0, e = num_ptrns(); i != e; ++i) {
23 auto proj = def->proj(e, i)->set(ptrn(i)->loc(), ptrn(i)->sym());
24 ptrn(i)->bind(scopes, proj, rebind);
25 }
26}
27
28/*
29 * type
30 */
31
32const Def* IdPtrn::type(World& world, Def2Fields&) const {
33 if (type_) return type_;
34 return type_ = world.mut_infer_type()->set(loc());
35}
36
37const Def* TuplePtrn::type(World& world, Def2Fields& def2fields) const {
38 if (type_) return type_;
39
40 auto n = num_ptrns();
41 auto ops = DefVec(n, [&](size_t i) { return ptrn(i)->type(world, def2fields); });
42
43 if (std::ranges::all_of(ptrns_, [](auto&& b) { return b->is_anonymous(); })) {
44 if (decl_) return type_ = decl_->set(ops);
45 return type_ = world.sigma(ops)->set(loc());
46 }
47
48 assert(n > 0);
49 auto type = world.umax<Sort::Type>(ops);
50
51 Sigma* sigma;
52 if (decl_) {
53 if (auto s = decl_->isa_mut<Sigma>())
54 sigma = s;
55 else {
56 sigma = world.mut_sigma(type, n)->set(loc(), sym());
57 if (auto infer = decl_->isa<Infer>()) {
58 if (infer->is_set()) {
59 if (infer->op() != sigma)
60 error(infer->loc(), "inferred different sigma '{}' for '{}'", infer->op(), sigma);
61 } else {
62 infer->set(sigma);
63 }
64 }
65 }
66 } else {
67 sigma = world.mut_sigma(type, n)->set(loc(), sym());
68 }
69
70 assert_emplace(def2fields, sigma, Vector<Sym>(n, [this](size_t i) { return ptrn(i)->sym(); }));
71
72 sigma->set(0, ops[0]);
73 for (size_t i = 1; i != n; ++i) {
74 if (auto infer = infers_[i - 1]) infer->set(sigma->var(n, i - 1)->set(ptrn(i - 1)->sym()));
75 sigma->set(i, ops[i]);
76 }
77
78 auto var = sigma->var()->as<Var>();
79 VarRewriter rw(var, var);
80 sigma->reset(0, ops[0]);
81 for (size_t i = 1; i != n; ++i) sigma->reset(i, rw.rewrite(ops[i]));
82
83 if (!decl_) {
84 if (auto imm = sigma->immutabilize()) return type_ = imm;
85 }
86
87 return type_ = sigma;
88}
89
90} // namespace thorin
Base class for all Defs.
Definition def.h:222
T * isa_mut() const
If this is *mut*able, it will cast constness away and perform a dynamic_cast to T.
Definition def.h:449
Def * set(size_t i, const Def *def)
Successively set from left to right.
Definition def.cpp:254
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:531
void bind(Scopes &, const Def *, bool rebind=false) const override
Definition ast.cpp:16
const Def * type(World &, Def2Fields &) const override
Definition ast.cpp:32
This node is a hole in the IR that is inferred by its context later on.
Definition check.h:12
Infer * set(const Def *op)
Definition check.h:21
Loc loc() const
Definition ast.h:32
virtual void bind(Scopes &, const Def *, bool rebind=false) const =0
virtual const Def * type(World &, Def2Fields &) const =0
Sym sym() const
Definition ast.h:33
const Def * type_
Definition ast.h:42
Dbg dbg() const
Definition ast.h:31
bool rebind() const
Definition ast.h:34
void bind(Scope *, Dbg, const Def *, bool rebind=false)
Definition scopes.cpp:27
A dependent tuple type.
Definition tuple.h:9
Sigma * set(size_t i, const Def *def)
Definition tuple.h:20
size_t num_ptrns() const
Definition ast.h:66
const Def * type(World &, Def2Fields &) const override
Definition ast.cpp:37
const Ptrn * ptrn(size_t i) const
Definition ast.h:65
void bind(Scopes &, const Def *, bool rebind=false) const override
Definition ast.cpp:20
This is a thin wrapper for absl::InlinedVector<T, N, A> which in turn is a drop-in replacement for st...
Definition vector.h:16
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
Infer * mut_infer_type()
Definition world.h:203
Sigma * mut_sigma(Ref type, size_t size)
Definition world.h:301
Ref sigma(Defs ops)
Definition world.cpp:218
Ref umax(Defs)
Definition world.cpp:107
Definition cfg.h:11
void error(const Def *def, const char *fmt, Args &&... args)
Definition def.h:622
auto assert_emplace(C &container, Args &&... args)
Invokes emplace on container, asserts that insertion actually happened, and returns the iterator.
Definition util.h:102
DefMap< Vector< Sym > > Def2Fields
Definition ast.h:17
Vector< const Def * > DefVec
Definition def.h:63