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
32 static constexpr auto Node = mim::Node::Reform;
33 static constexpr size_t Num_Ops = 1;
34
35private:
36 friend class World;
37};
38
39/// A rewrite rule
40class Rule : public Def, public Setters<Rule> {
41private:
42 Rule(const Reform* type, const Def* lhs, const Def* rhs, const Def* guard)
43 : Def(Node, type, {lhs, rhs, guard}, 0) {}
44 Rule(const Reform* type)
45 : Def(Node, type, 3, 0) {}
46
47public:
48 /// @name type
49 /// @see @ref proj
50 ///@{
51 const Reform* type() const { return Def::type()->as<Reform>(); }
52 const Def* dom() const { return type()->dom(); }
53 MIM_PROJ(dom, const)
54 ///@}
55
56 /// @name ops
57 /// @see @ref proj
58 ///@{
59 const Def* lhs() const { return op(0); }
60 const Def* rhs() const { return op(1); }
61 const Def* guard() const { return op(2); }
62 MIM_PROJ(lhs, const)
63 MIM_PROJ(rhs, const)
64 MIM_PROJ(guard, const)
65 ///@}
66
67 /// @name Setters
68 /// @see @ref set_ops "Setting Ops"
69 ///@{
70 using Setters<Rule>::set;
71 Rule* set(const Def* lhs, const Def* rhs, const Def* guard) { return Def::set({lhs, rhs, guard})->as<Rule>(); }
72 Rule* set_lhs(const Def* lhs) { return Def::set(0, lhs)->as<Rule>(); }
73 Rule* set_rhs(const Def* rhs) { return Def::set(1, rhs)->as<Rule>(); }
74 Rule* set_guard(const Def* guard) { return Def::set(2, guard)->as<Rule>(); }
75 Rule* unset() { return Def::unset()->as<Rule>(); }
76 ///@}
77
78 /// @name Type checking
79 ///@{
80 ///@}
81
82 /// @name Rebuild
83 ///@{
84 const Def* reduce(const Def* arg) const { return Def::reduce(arg).front(); }
85 ///@}
86
87 /// @name Apply
88 ///@{
89 bool its_a_match(const Def* expr, Def2Def&) const;
90 const Def* replace(const Def* expr, Def2Def&) const;
91 ///@}
92
93 static bool is_in_rule(const Def*);
94
95 static constexpr auto Node = mim::Node::Rule;
96 static constexpr size_t Num_Ops = 3;
97
98private:
99 bool its_a_match_(const Def* lhs, const Def* rhs, Def2Def& seen) const;
100 friend class World;
101};
102} // namespace mim
Base class for all Defs.
Definition def.h:273
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:196
constexpr auto ops() const noexcept
Definition def.h:348
const Def * op(size_t i) const noexcept
Definition def.h:351
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1111
Def * unset()
Unsets all Def::ops; works even, if not set at all or only partially set.
Definition def.cpp:213
constexpr auto reduce(const Def *arg) const
Definition def.h:660
Type formation of a rewrite Rule.
Definition rule.h:9
Reform * set(Defs ops)
Definition rule.h:26
Reform(const Def *type, const Def *dom)
Definition rule.h:11
Reform * set(size_t i, const Def *def)
Definition rule.h:25
static const Def * infer(const Def *dom)
Definition check.cpp:432
const Def * dom() const
Definition rule.h:18
friend class World
Definition rule.h:36
static constexpr auto Node
Definition rule.h:32
static constexpr size_t Num_Ops
Definition rule.h:33
Reform * unset()
Definition rule.h:27
A rewrite rule.
Definition rule.h:40
Rule * set_rhs(const Def *rhs)
Definition rule.h:73
Rule * set_lhs(const Def *lhs)
Definition rule.h:72
const Def * dom() const
Definition rule.h:52
const Def * lhs() const
Definition rule.h:59
Rule * set_guard(const Def *guard)
Definition rule.h:74
bool its_a_match(const Def *expr, Def2Def &) const
Definition rule.cpp:49
const Def * guard() const
Definition rule.h:61
friend class World
Definition rule.h:100
static constexpr size_t Num_Ops
Definition rule.h:96
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:71
static bool is_in_rule(const Def *)
Definition rule.cpp:38
Rule * unset()
Definition rule.h:75
const Def * rhs() const
Definition rule.h:60
const Def * reduce(const Def *arg) const
Definition rule.h:84
static constexpr auto Node
Definition rule.h:95
const Reform * type() const
Definition rule.h:51
CRTP-based mixin to declare setters for Def::loc & Def::name using a covariant return type.
Definition def.h:209
#define MIM_PROJ(NAME, CONST)
Use as mixin to wrap all kind of Def::proj and Def::projs variants.
Definition def.h:175
Definition ast.h:16
DefMap< const Def * > Def2Def
Definition def.h:90
fe::View< const Def * > Defs
Definition def.h:91
@ Reform
Definition def.h:122
@ Rule
Definition def.h:122