Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
ast.h
Go to the documentation of this file.
1#pragma once
2
3#include <deque>
4#include <memory>
5
6#include "thorin/def.h"
7
8#include "thorin/fe/tok.h"
9
10namespace thorin {
11
12class Infer;
13class Sigma;
14class World;
15
16class Scopes;
18
19/*
20 * Pattern
21 */
22
23class Ptrn {
24public:
25 Ptrn(Dbg dbg, bool rebind, const Def* type)
26 : dbg_(dbg)
28 , type_(type) {}
29 virtual ~Ptrn() {}
30
31 Dbg dbg() const { return dbg_; }
32 Loc loc() const { return dbg_.loc; }
33 Sym sym() const { return dbg_.sym; }
34 bool rebind() const { return rebind_; }
35 bool is_anonymous() const { return sym() == '_'; }
36 virtual void bind(Scopes&, const Def*, bool rebind = false) const = 0;
37 virtual const Def* type(World&, Def2Fields&) const = 0;
38
39protected:
41 bool rebind_;
42 mutable const Def* type_;
43};
44
45using Ptrns = std::deque<std::unique_ptr<Ptrn>>;
46
47class IdPtrn : public Ptrn {
48public:
49 IdPtrn(Dbg dbg, bool rebind, const Def* type)
50 : Ptrn(dbg, rebind, type) {}
51
52 void bind(Scopes&, const Def*, bool rebind = false) const override;
53 const Def* type(World&, Def2Fields&) const override;
54};
55
56class TuplePtrn : public Ptrn {
57public:
58 TuplePtrn(Dbg dbg, bool rebind, Ptrns&& ptrns, const Def* type, std::vector<Infer*>&& infers, Def* decl)
59 : Ptrn(dbg, rebind, type)
60 , ptrns_(std::move(ptrns))
61 , infers_(std::move(infers))
62 , decl_(decl) {}
63
64 const Ptrns& ptrns() const { return ptrns_; }
65 const Ptrn* ptrn(size_t i) const { return ptrns_[i].get(); }
66 size_t num_ptrns() const { return ptrns().size(); }
67
68 void bind(Scopes&, const Def*, bool rebind = false) const override;
69 const Def* type(World&, Def2Fields&) const override;
70
71private:
72 Ptrns ptrns_;
73 std::vector<Infer*> infers_;
74 Def* decl_ = nullptr;
75};
76
77} // namespace thorin
Base class for all Defs.
Definition def.h:222
IdPtrn(Dbg dbg, bool rebind, const Def *type)
Definition ast.h:49
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
Loc loc() const
Definition ast.h:32
virtual void bind(Scopes &, const Def *, bool rebind=false) const =0
Dbg dbg_
Definition ast.h:40
Ptrn(Dbg dbg, bool rebind, const Def *type)
Definition ast.h:25
bool rebind_
Definition ast.h:41
virtual ~Ptrn()
Definition ast.h:29
bool is_anonymous() const
Definition ast.h:35
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
const Ptrns & ptrns() const
Definition ast.h:64
size_t num_ptrns() const
Definition ast.h:66
const Def * type(World &, Def2Fields &) const override
Definition ast.cpp:37
TuplePtrn(Dbg dbg, bool rebind, Ptrns &&ptrns, const Def *type, std::vector< Infer * > &&infers, Def *decl)
Definition ast.h:58
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
The World represents the whole program and manages creation of Thorin nodes (Defs).
Definition world.h:35
Definition span.h:103
@ Infer
Definition def.h:41
@ Sigma
Definition def.h:41
Definition cfg.h:11
Loc loc
Definition dbg.h:32
std::deque< std::unique_ptr< Ptrn > > Ptrns
Definition ast.h:45
DefMap< Vector< Sym > > Def2Fields
Definition ast.h:17
GIDMap< const Def *, To > DefMap
Definition def.h:59
Sym sym
Definition dbg.h:33