MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
rule.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/def.h"
4
5namespace mim {
6
7/// Type <b>form</b>ation of a <b>re</b>write Rule.
8/// Currently opaque.
9class Reform : public Def, public Setters<Reform> {
10protected:
11 Reform(const Def* type, const Def* dom)
12 : Def(Node, type, {dom}, 0) {}
13
14public:
15 /// @name dom
16 /// @see @ref proj
17 ///@{
18 const Def* dom() const { return op(0); }
19 MIM_PROJ(dom, const)
20 ///@}
21
22 /// @name Setters
23 ///@{
24 using Setters<Reform>::set;
25 Reform* set(size_t i, const Def* def) { return Def::set(i, def)->as<Reform>(); }
26 Reform* set(Defs ops) { return Def::set(ops)->as<Reform>(); }
27 Reform* unset() { return Def::unset()->as<Reform>(); }
28 ///@}
29
30 static const Def* infer(const Def* dom);
31 const Def* check() override;
32
33 static constexpr auto Node = mim::Node::Reform;
34 static constexpr size_t Num_Ops = 1;
35
36private:
37 const Def* rebuild_(World&, const Def*, Defs) const override;
38
39 friend class World;
40};
41
42/// A rewrite rule
43class Rule : public Def, public Setters<Rule> {
44private:
45 Rule(const Reform* type, const Def* lhs, const Def* rhs, const Def* guard)
46 : Def(Node, type, {lhs, rhs, guard}, 0) {}
47 Rule(const Reform* type)
48 : Def(Node, type, 3, 0) {}
49
50public:
51 /// @name type
52 /// @see @ref proj
53 ///@{
54 const Reform* type() const { return Def::type()->as<Reform>(); }
55 const Def* dom() const { return type()->dom(); }
56 MIM_PROJ(dom, const)
57 ///@}
58
59 /// @name ops
60 /// @see @ref proj
61 ///@{
62 const Def* lhs() const { return op(0); }
63 const Def* rhs() const { return op(1); }
64 const Def* guard() const { return op(2); }
65 MIM_PROJ(lhs, const)
66 MIM_PROJ(rhs, const)
67 MIM_PROJ(guard, const)
68 ///@}
69
70 /// @name Setters
71 /// @see @ref set_ops "Setting Ops"
72 ///@{
73 using Setters<Rule>::set;
74 Rule* set(const Def* lhs, const Def* rhs, const Def* guard) { return Def::set({lhs, rhs, guard})->as<Rule>(); }
75 Rule* set_lhs(const Def* lhs) { return Def::set(0, lhs)->as<Rule>(); }
76 Rule* set_rhs(const Def* rhs) { return Def::set(1, rhs)->as<Rule>(); }
77 Rule* set_guard(const Def* guard) { return Def::set(2, guard)->as<Rule>(); }
78 Rule* unset() { return Def::unset()->as<Rule>(); }
79 ///@}
80
81 /// @name Type checking
82 ///@{
83 const Def* check(size_t, const Def*) override;
84 const Def* check() override;
85 ///@}
86
87 /// @name Rebuild
88 ///@{
89 Rule* stub(const Def* type) { return stub_(world(), type)->set(dbg()); }
90 const Rule* immutabilize() override;
91 const Def* reduce(const Def* arg) const { return Def::reduce(arg).front(); }
92 constexpr size_t reduction_offset() const noexcept override { return 1; }
93 ///@}
94
95 /// @name Apply
96 ///@{
97 bool its_a_match(const Def* expr, Def2Def&) const;
98 const Def* replace(const Def* expr, Def2Def&) const;
99 ///@}
100
101 static bool is_in_rule(const Def*);
102
103 static constexpr auto Node = mim::Node::Rule;
104 static constexpr size_t Num_Ops = 3;
105
106private:
107 const Def* rebuild_(World&, const Def*, Defs) const override;
108 Rule* stub_(World&, const Def*) override;
109 bool its_a_match_(const Def* lhs, const Def* rhs, Def2Def& seen) const;
110 friend class World;
111};
112} // namespace mim
Base class for all Defs.
Definition def.h:246
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:266
World & world() const noexcept
Definition def.cpp:444
constexpr auto ops() const noexcept
Definition def.h:301
const Def * op(size_t i) const noexcept
Definition def.h:304
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.cpp:452
Def * unset()
Unsets all Def::ops; works even, if not set at all or only partially set.
Definition def.cpp:289
constexpr auto reduce(const Def *arg) const
Definition def.h:571
Dbg dbg() const
Definition def.h:513
Type formation of a rewrite Rule.
Definition rule.h:9
const Def * rebuild_(World &, const Def *, Defs) const override
Definition def.cpp:126
Reform * set(Defs ops)
Definition rule.h:26
Reform(const Def *type, const Def *dom)
Definition rule.h:11
const Def * check() override
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
Definition check.cpp:346
Reform * set(size_t i, const Def *def)
Definition rule.h:25
static const Def * infer(const Def *dom)
Definition check.cpp:353
const Def * dom() const
Definition rule.h:18
friend class World
Definition rule.h:39
static constexpr auto Node
Definition rule.h:33
static constexpr size_t Num_Ops
Definition rule.h:34
Reform * unset()
Definition rule.h:27
A rewrite rule.
Definition rule.h:43
const Def * rebuild_(World &, const Def *, Defs) const override
Definition def.cpp:125
Rule * set_rhs(const Def *rhs)
Definition rule.h:76
Rule * set_lhs(const Def *lhs)
Definition rule.h:75
const Def * dom() const
Definition rule.h:55
const Def * lhs() const
Definition rule.h:62
Rule * set_guard(const Def *guard)
Definition rule.h:77
bool its_a_match(const Def *expr, Def2Def &) const
Definition rule.cpp:49
const Def * guard() const
Definition rule.h:64
Rule * stub(const Def *type)
Definition rule.h:89
friend class World
Definition rule.h:110
const Rule * immutabilize() override
Tries to make an immutable from a mutable.
Definition def.cpp:196
constexpr size_t reduction_offset() const noexcept override
First Def::op that needs to be dealt with during reduction; e.g.
Definition rule.h:92
static constexpr size_t Num_Ops
Definition rule.h:104
Rule * stub_(World &, const Def *) override
Definition def.cpp:156
const Def * replace(const Def *expr, Def2Def &) const
Definition rule.cpp:116
Rule * set(const Def *lhs, const Def *rhs, const Def *guard)
Definition rule.h:74
static bool is_in_rule(const Def *)
Definition rule.cpp:38
Rule * unset()
Definition rule.h:78
const Def * rhs() const
Definition rule.h:63
const Def * reduce(const Def *arg) const
Definition rule.h:91
static constexpr auto Node
Definition rule.h:103
const Def * check() override
After all Def::ops have been Def::set, this method will be invoked to check the type of this mutable.
Definition check.cpp:355
const Reform * type() const
Definition rule.h:54
CRTP-based mixin to declare setters for Def::loc & Def::name using a covariant return type.
Definition def.h:190
#define MIM_PROJ(NAME, CONST)
Use as mixin to wrap all kind of Def::proj and Def::projs variants.
Definition def.h:159
Definition ast.h:14
View< const Def * > Defs
Definition def.h:78
DefMap< const Def * > Def2Def
Definition def.h:77
@ Reform
Definition def.h:109
@ Rule
Definition def.h:109