MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
single_erasure.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/phase.h"
4
5namespace mim {
6
7/// Eliminates all types that have exactly one inhabitant, so that no Single/Wrap ever reaches a backend.
8/// Such a type carries no information, so its erasure is the unit: `«e»` becomes `[]` and `‹e›` becomes `()`.
9/// Within an aggregate the component is dropped altogether, which shifts the indices of its successors.
10/// Besides Single this also catches the unit `[]` and `Idx 1` - and any aggregate built from those.
11/// A Variant with a single case `| A` is isomorphic to `A` and erased to it.
12/// A singleton above `*` has no unit to erase to and degrades to the type of its inhabitant instead.
13/// Where an erased value meets a shape that still demands its components (e.g. an Axm's signature), it is coerce()d:
14/// dropped components are re-inserted as their sole inhabitants, and Cn%s are η-wrapped.
15class SingleErasure : public RWPhase {
16public:
18 : RWPhase(world, std::move(name)) {}
21
22private:
23 /// @name Erasure
24 /// All arguments live in old_world(), all results in new_world().
25 ///@{
26 bool is_unit(const Single* single) { return single->type() == old_world().type<0>(); }
27 const Def* erase(const Single*); ///< The erasure of the singleton *type* itself.
28 const Def* inhabitant(const Def* type); ///< The sole inhabitant of the information-free @p type.
29 Sieve sieve(const Sigma*); ///< The components of @p sigma that survive.
30 /// The literal @p index into a Sigma, adjusted for the components @p keep dropped.
31 size_t remap(const Sieve& keep, const Def* index);
32 ///@}
33
34 const Def* rewrite_imm_Single(const Single*) final;
35 const Def* rewrite_imm_Wrap(const Wrap*) final;
36 const Def* rewrite_imm_Variant(const Variant*) final;
37 const Def* rewrite_imm_Inj(const Inj*) final;
38 const Def* rewrite_imm_Match(const Match*) final;
39 const Def* rewrite_imm_Sigma(const Sigma*) final;
40 const Def* rewrite_mut_Sigma(Sigma*) final;
41 const Def* rewrite_imm_Tuple(const Tuple*) final;
42 const Def* rewrite_imm_Extract(const Extract*) final;
43 const Def* rewrite_imm_Insert(const Insert*) final;
44 const Def* rewrite_imm_Seq(const Seq*) final;
45 const Def* rewrite_mut_Seq(Seq*) final;
46};
47
48} // namespace mim
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1119
flags_t annex() const
Definition phase.h:62
std::string_view name() const
Definition phase.h:61
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:334
World & world()=delete
Hides both and forbids direct access.
World & old_world()
Get old Defs from here.
Definition phase.h:378
const Def * rewrite_imm_Tuple(const Tuple *) final
SingleErasure(World &world, flags_t annex)
const Def * rewrite_imm_Variant(const Variant *) final
const Def * rewrite_mut_Seq(Seq *) final
const Def * rewrite_mut_Sigma(Sigma *) final
const Def * rewrite_imm_Wrap(const Wrap *) final
const Def * rewrite_imm_Match(const Match *) final
const Def * rewrite_imm_Insert(const Insert *) final
const Def * rewrite_imm_Sigma(const Sigma *) final
SingleErasure(World &world, std::string name)
const Def * rewrite_imm_Extract(const Extract *) final
const Def * rewrite_imm_Inj(const Inj *) final
const Def * rewrite_imm_Single(const Single *) final
const Def * rewrite_imm_Seq(const Seq *) final
Singleton type formation.
Definition single.h:10
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
const Type * type(const Def *level)
Definition world.cpp:235
Definition ast.h:16
u64 flags_t
Definition types.h:39
bool is_unit(const Def *)
Definition tuple.cpp:113