MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
tuple.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/def.h"
4
5namespace mim {
6
7/// A [dependent tuple type](https://en.wikipedia.org/wiki/Dependent_type#%CE%A3_type).
8/// @see Tuple, Arr, Pack
9class Sigma : public Def {
10private:
11 Sigma(const Def* type, Defs ops)
12 : Def(Node, type, ops, 0) {} ///< Constructor for an *immutable* Sigma.
13 Sigma(const Def* type, size_t size)
14 : Def(Node, type, size, 0) {} ///< Constructor for a *mutable* Sigma.
15
16public:
17 /// @name Setters
18 /// @see @ref set_ops "Setting Ops"
19 ///@{
20 Sigma* set(size_t i, const Def* def) { return Def::set(i, def)->as<Sigma>(); }
21 Sigma* set(Defs ops) { return Def::set(ops)->as<Sigma>(); }
22 Sigma* unset() { return Def::unset()->as<Sigma>(); }
23 ///@}
24
25 const Def* immutabilize() override;
26 Sigma* stub_(World&, Ref) override;
27 Sigma* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
28
29 /// @name Type Checking
30 ///@{
31 void check() override;
32 static Ref infer(World&, Defs);
33 ///@}
34
36};
37
38/// Data constructor for a Sigma.
39/// @see Sigma, Arr, Pack
40class Tuple : public Def {
41private:
42 Tuple(const Def* type, Defs args)
43 : Def(Node, type, args, 0) {}
44
46};
47
48/// A (possibly paramterized) Arr%ay.
49/// Arr%ays are usually homogenous but they can be *inhomogenous* as well: `«i: N; T#i»`
50/// @see Sigma, Tuple, Pack
51class Arr : public Def {
52private:
53 Arr(const Def* type, const Def* shape, const Def* body)
54 : Def(Node, type, {shape, body}, 0) {} ///< Constructor for an *immutable* Arr.
55 Arr(const Def* type)
56 : Def(Node, type, 2, 0) {} ///< Constructor for a *mut*able Arr.
57
58public:
59 /// @name ops
60 ///@{
61 const Def* shape() const { return op(0); }
62 const Def* body() const { return op(1); }
63 ///@}
64
65 /// @name Setters
66 ///@{
67 /// @see @ref set_ops "Setting Ops"
68 Arr* set_shape(const Def* shape) { return Def::set(0, shape)->as<Arr>(); }
69 Arr* set_body(const Def* body) { return Def::set(1, body)->as<Arr>(); }
70 Arr* unset() { return Def::unset()->as<Arr>(); }
71 ///@}
72
73 const Def* immutabilize() override;
74 Arr* stub_(World&, Ref) override;
75 Arr* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
76 const Def* reduce(const Def* arg) const;
77
78 /// @name Type Checking
79 ///@{
80 void check() override;
81 ///@}
82
84};
85
86/// A (possibly paramterized) Tuple.
87/// @see Sigma, Tuple, Arr
88class Pack : public Def {
89private:
90 Pack(const Def* type, const Def* body)
91 : Def(Node, type, {body}, 0) {} ///< Constructor for an *immutable* Pack.
92 Pack(const Def* type)
93 : Def(Node, type, 1, 0) {} ///< Constructor for a *mut*ablel Pack.
94
95public:
96 /// @name ops
97 ///@{
98 const Def* body() const { return op(0); }
99 const Def* shape() const;
100 ///@}
101
102 /// @name Setters
103 /// @see @ref set_ops "Setting Ops"
104 ///@{
105 Pack* set(const Def* body) { return Def::set(0, body)->as<Pack>(); }
106 Pack* reset(const Def* body) { return unset()->set(body); }
107 Pack* unset() { return Def::unset()->as<Pack>(); }
108 ///@}
109
110 const Def* immutabilize() override;
111 Pack* stub_(World&, Ref) override;
112 Pack* stub(Ref type) { return stub_(world(), type)->set(dbg()); }
113 const Def* reduce(const Def* arg) const;
114
116};
117
118/// Extracts from a Sigma or Arr%ay-typed Extract::tuple the element at position Extract::index.
119class Extract : public Def {
120private:
121 Extract(const Def* type, const Def* tuple, const Def* index)
122 : Def(Node, type, {tuple, index}, 0) {}
123
124public:
125 /// @name ops
126 ///@{
127 const Def* tuple() const { return op(0); }
128 const Def* index() const { return op(1); }
129 ///@}
130
132};
133
134/// Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
135/// @attention This is a *functional* Insert.
136/// The Insert::tuple itself remains untouched.
137/// The Insert itself is a *new* Tuple / Pack which contains the inserted Insert::value.
138class Insert : public Def {
139private:
140 Insert(const Def* tuple, const Def* index, const Def* value)
141 : Def(Node, tuple->type(), {tuple, index, value}, 0) {}
142
143public:
144 /// @name ops
145 ///@{
146 const Def* tuple() const { return op(0); }
147 const Def* index() const { return op(1); }
148 const Def* value() const { return op(2); }
149 ///@}
150
152};
153
154/// @name Helpers to work with Tulpes/Sigmas/Arrays/Packs
155///@{
156bool is_unit(const Def*);
157std::string tuple2str(const Def*);
158
159/// Flattens a sigma/array/pack/tuple.
160const Def* flatten(const Def* def);
161/// Same as unflatten, but uses the operands of a flattened Pack / Tuple directly.
162size_t flatten(DefVec& ops, const Def* def, bool flatten_sigmas = true);
163
164/// Applies the reverse transformation on a Pack / Tuple, given the original type.
165const Def* unflatten(const Def* def, const Def* type);
166/// Same as unflatten, but uses the operands of a flattened Pack / Tuple directly.
167const Def* unflatten(Defs ops, const Def* type, bool flatten_muts = true);
168
170DefVec merge(const Def* def, Defs defs);
171const Def* merge_sigma(const Def* def, Defs defs);
172const Def* merge_tuple(const Def* def, Defs defs);
173
174Ref tuple_of_types(Ref t);
175///@}
176
177} // namespace mim
A (possibly paramterized) Array.
Definition tuple.h:51
Arr * stub(Ref type)
Definition tuple.h:75
const Def * immutabilize() override
Tries to make an immutable from a mutable.
Definition def.cpp:174
void check() override
Definition check.cpp:225
const Def * body() const
Definition tuple.h:62
Arr * unset()
Definition tuple.h:70
Arr * set_body(const Def *body)
Definition tuple.h:69
Arr * stub_(World &, Ref) override
Definition def.cpp:130
Arr * set_shape(const Def *shape)
Definition tuple.h:68
const Def * shape() const
Definition tuple.h:61
const Def * reduce(const Def *arg) const
Definition def.cpp:198
Base class for all Defs.
Definition def.h:220
Def * set(size_t i, const Def *def)
Successively set from left to right.
Definition def.cpp:248
const Def * op(size_t i) const
Definition def.h:266
World & world() const
Definition def.cpp:417
auto ops() const
Definition def.h:265
const Def * type() const
Definition def.h:245
Def * unset()
Unsets all Def::ops; works even, if not set at all or partially.
Definition def.cpp:266
Dbg dbg() const
Definition def.h:463
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:119
const Def * tuple() const
Definition tuple.h:127
const Def * index() const
Definition tuple.h:128
Creates a new Tuple / Pack by inserting Insert::value at position Insert::index into Insert::tuple.
Definition tuple.h:138
const Def * tuple() const
Definition tuple.h:146
const Def * index() const
Definition tuple.h:147
const Def * value() const
Definition tuple.h:148
A (possibly paramterized) Tuple.
Definition tuple.h:88
const Def * body() const
Definition tuple.h:98
Pack * reset(const Def *body)
Definition tuple.h:106
Pack * stub_(World &, Ref) override
Definition def.cpp:134
Pack * unset()
Definition tuple.h:107
const Def * shape() const
Definition tuple.cpp:40
const Def * reduce(const Def *arg) const
Definition def.cpp:203
const Def * immutabilize() override
Tries to make an immutable from a mutable.
Definition def.cpp:184
Pack * set(const Def *body)
Definition tuple.h:105
Pack * stub(Ref type)
Definition tuple.h:112
Helper class to retrieve Infer::arg if present.
Definition def.h:85
A dependent tuple type.
Definition tuple.h:9
Sigma * unset()
Definition tuple.h:22
const Def * immutabilize() override
Tries to make an immutable from a mutable.
Definition def.cpp:168
static Ref infer(World &, Defs)
Definition check.cpp:232
Sigma * set(Defs ops)
Definition tuple.h:21
Sigma * stub(Ref type)
Definition tuple.h:27
Sigma * stub_(World &, Ref) override
Definition def.cpp:136
void check() override
Definition check.cpp:238
Sigma * set(size_t i, const Def *def)
Definition tuple.h:20
This is a thin wrapper for std::span<T, N> with the following additional features:
Definition span.h:28
Data constructor for a Sigma.
Definition tuple.h:40
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
#define MIM_DEF_MIXIN(T)
Definition def.h:196
Definition cfg.h:11
View< const Def * > Defs
Definition def.h:60
const Def * flatten(const Def *def)
Flattens a sigma/array/pack/tuple.
Definition tuple.cpp:66
Vector< const Def * > DefVec
Definition def.h:61
bool is_unit(const Def *)
Definition tuple.cpp:46
const Def * merge_sigma(const Def *def, Defs defs)
Definition tuple.cpp:93
std::string tuple2str(const Def *)
Definition tuple.cpp:48
DefVec merge(Defs, Defs)
Definition tuple.cpp:86
const Def * unflatten(const Def *def, const Def *type)
Applies the reverse transformation on a Pack / Tuple, given the original type.
Definition tuple.cpp:80
Ref tuple_of_types(Ref t)
Definition tuple.cpp:109
const Def * merge_tuple(const Def *def, Defs defs)
Definition tuple.cpp:98