MimIR 0.1
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* meta_type)
12 : Def(Node, type, {meta_type}, 0) {}
13
14public:
15 const Def* meta_type() const { return op(0); }
16
17 /// @name Setters
18 ///@{
19 using Setters<Reform>::set;
20 Reform* set(size_t i, const Def* def) { return Def::set(i, def)->as<Reform>(); }
21 Reform* set(Defs ops) { return Def::set(ops)->as<Reform>(); }
22 Reform* unset() { return Def::unset()->as<Reform>(); }
23 ///@}
24
25 static const Def* infer(const Def* meta_type);
26 const Def* check() override;
27
28 static constexpr auto Node = mim::Node::Reform;
29 static constexpr size_t Num_Ops = 1;
30
31private:
32 const Def* rebuild_(World&, const Def*, Defs) const override;
33
34 friend class World;
35};
36
37/// A rewrite rule
38class Rule : public Def, public Setters<Rule> {
39private:
40 Rule(const Reform* type, const Def* lhs, const Def* rhs, const Def* guard)
41 : Def(Node, type, {lhs, rhs, guard}, 0) {}
42 Rule(const Reform* type)
43 : Def(Node, type, 3, 0) {}
44
45public:
46 /// @name lhs & rhs
47 /// @see @ref proj
48 ///@{
49 const Reform* type() const { return Def::type()->as<Reform>(); }
50 const Def* lhs() const { return op(0); }
51 const Def* rhs() const { return op(1); }
52 const Def* guard() const { return op(2); }
53 MIM_PROJ(lhs, const)
54 MIM_PROJ(rhs, const)
55 MIM_PROJ(guard, const)
56 ///@}
57
58 /// @name Setters
59 /// @see @ref set_ops "Setting Ops"
60 ///@{
61 using Setters<Rule>::set;
62 Rule* set(const Def* lhs, const Def* rhs) { return set_lhs(lhs)->set_rhs(rhs); }
63 Rule* set(const Def* lhs, const Def* rhs, const Def* guard) { return set_lhs(lhs)->set_rhs(rhs)->set_guard(guard); }
64 Rule* set_lhs(const Def* lhs) { return Def::set(0, lhs)->as<Rule>(); }
65 Rule* set_rhs(const Def* rhs) { return Def::set(1, rhs)->as<Rule>(); }
66 Rule* set_guard(const Def* guard) { return Def::set(2, guard)->as<Rule>(); }
67 Rule* unset() { return Def::unset()->as<Rule>(); }
68 ///@}
69
70 /// @name Type checking
71 ///@{
72 const Def* check(size_t, const Def*) override;
73 const Def* check() override;
74 ///@}
75
76 /// @name Rebuild
77 ///@{
78 Rule* stub(const Def* type) { return stub_(world(), type)->set(dbg()); }
79 const Rule* immutabilize() override;
80 const Def* reduce(const Def* arg) const { return Def::reduce(arg).front(); }
81 constexpr size_t reduction_offset() const noexcept override { return 1; }
82 ///@}
83
84 /// @name Apply
85 ///@{
86 bool its_a_match(const Def* expr, Def2Def&) const;
87 const Def* replace(const Def* expr, Def2Def&) const;
88 ///@}
89
90 static bool is_in_rule(const Def*);
91
92 static constexpr auto Node = mim::Node::Rule;
93 static constexpr size_t Num_Ops = 3;
94
95private:
96 const Def* rebuild_(World&, const Def*, Defs) const override;
97 Rule* stub_(World&, const Def*) override;
98 bool its_a_match_(const Def* lhs, const Def* rhs, Def2Def& seen) const;
99 friend class World;
100};
101} // namespace mim
Base class for all Defs.
Definition def.h:251
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:266
World & world() const noexcept
Definition def.cpp:436
constexpr auto ops() const noexcept
Definition def.h:305
const Def * op(size_t i) const noexcept
Definition def.h:308
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:295
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:560
Dbg dbg() const
Definition def.h:502
Type formation of a rewrite Rule.
Definition rule.h:9
const Def * rebuild_(World &, const Def *, Defs) const override
Definition def.cpp:127
Reform * set(Defs ops)
Definition rule.h:21
const Def * meta_type() const
Definition rule.h:15
static const Def * infer(const Def *meta_type)
Definition check.cpp:402
const Def * check() override
After all Def::ops have ben Def::set, this method will be invoked to check the type of this mutable.
Definition check.cpp:395
Reform * set(size_t i, const Def *def)
Definition rule.h:20
friend class World
Definition rule.h:34
static constexpr auto Node
Definition rule.h:28
static constexpr size_t Num_Ops
Definition rule.h:29
Reform * unset()
Definition rule.h:22
Reform(const Def *type, const Def *meta_type)
Definition rule.h:11
A rewrite rule.
Definition rule.h:38
const Def * rebuild_(World &, const Def *, Defs) const override
Definition def.cpp:126
Rule * set_rhs(const Def *rhs)
Definition rule.h:65
Rule * set_lhs(const Def *lhs)
Definition rule.h:64
const Def * lhs() const
Definition rule.h:50
Rule * set_guard(const Def *guard)
Definition rule.h:66
bool its_a_match(const Def *expr, Def2Def &) const
Definition rule.cpp:49
const Def * guard() const
Definition rule.h:52
Rule * set(const Def *lhs, const Def *rhs)
Definition rule.h:62
Rule * stub(const Def *type)
Definition rule.h:78
friend class World
Definition rule.h:99
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:81
static constexpr size_t Num_Ops
Definition rule.h:93
Rule * stub_(World &, const Def *) override
Definition def.cpp:157
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:63
static bool is_in_rule(const Def *)
Definition rule.cpp:38
Rule * unset()
Definition rule.h:67
const Def * rhs() const
Definition rule.h:51
const Def * reduce(const Def *arg) const
Definition rule.h:80
static constexpr auto Node
Definition rule.h:92
const Def * check() override
After all Def::ops have ben Def::set, this method will be invoked to check the type of this mutable.
Definition check.cpp:404
const Reform * type() const
Definition rule.h:49
CRTP-based mixin to declare setters for Def::loc & Def::name using a covariant return type.
Definition def.h:195
#define MIM_PROJ(NAME, CONST)
Use as mixin to wrap all kind of Def::proj and Def::projs variants.
Definition def.h:164
Definition ast.h:14
View< const Def * > Defs
Definition def.h:76
DefMap< const Def * > Def2Def
Definition def.h:75
@ Reform
Definition def.h:114
@ Rule
Definition def.h:114