mini-llvm
0.1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
Value.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
3
#pragma once
4
5
#include <concepts>
6
#include <cstddef>
7
#include <format>
8
#include <iterator>
9
#include <memory>
10
#include <ranges>
11
#include <string>
12
#include <unordered_set>
13
#include <utility>
14
15
#include "
mini-llvm/ir/Type.h
"
16
#include "
mini-llvm/utils/Compiler.h
"
17
#include "
mini-llvm/utils/IndirectIterator.h
"
18
#include "
mini-llvm/utils/Memory.h
"
19
20
namespace
mini_llvm::ir
{
21
22
class
UseBase
;
23
24
class
MINI_LLVM_EXPORT
Value
:
public
std::enable_shared_from_this<Value> {
25
using
UseSet = std::unordered_set<UseBase *>;
26
27
public
:
28
using
use_iterator
=
IndirectIterator<UseSet::iterator, UseBase>
;
29
30
virtual
~Value
() =
default
;
31
32
Value
() =
default
;
33
34
Value
(
const
Value
&) =
delete
;
35
Value
&
operator=
(
const
Value
&) =
delete
;
36
37
Value
(
Value
&&) =
delete
;
38
Value
&
operator=
(
Value
&&) =
delete
;
39
40
const
std::string &
name
() const & {
41
return
name_;
42
}
43
44
std::string &&
name
() && {
45
return
std::move(name_);
46
}
47
48
void
setName
(std::string
name
) {
49
name_ = std::move(
name
);
50
}
51
52
std::string
formatName
()
const
;
53
54
use_iterator
use_begin
()
const
{
55
return
use_iterator
(uses_.begin());
56
}
57
58
use_iterator
use_end
()
const
{
59
return
use_iterator
(uses_.end());
60
}
61
62
bool
use_empty
()
const
{
63
return
uses_.empty();
64
}
65
66
size_t
use_size
()
const
{
67
return
uses_.size();
68
}
69
70
virtual
bool
isWellFormed
()
const
{
71
return
true
;
72
}
73
74
virtual
std::unique_ptr<Type>
type
()
const
= 0;
75
76
virtual
std::string
format
()
const
= 0;
77
virtual
std::string
formatAsOperand
()
const
= 0;
78
79
virtual
std::unique_ptr<Value>
clone
()
const
= 0;
80
81
private
:
82
std::string name_;
83
mutable
UseSet uses_;
84
85
friend
class
UseBase
;
86
};
87
88
inline
auto
uses
(
const
Value
&value) {
89
return
std::ranges::subrange(value.
use_begin
(), value.
use_end
());
90
}
91
92
MINI_LLVM_EXPORT
bool
replaceAllUsesWith
(
const
Value
&value, std::shared_ptr<Value> newValue);
93
MINI_LLVM_EXPORT
bool
replaceAllUsesWith
(
const
Value
&value, std::weak_ptr<Value> newValue);
94
95
template
<
typename
ValueT>
96
requires
std::derived_from<ValueT, Value>
97
bool
replaceAllUsesWith
(
const
Value
&value, std::shared_ptr<ValueT> newValue) {
98
return
replaceAllUsesWith
(value,
cast<Value>
(std::move(newValue)));
99
}
100
101
template
<
typename
ValueT>
102
requires
std::derived_from<ValueT, Value>
103
bool
replaceAllUsesWith
(
const
Value
&value, std::weak_ptr<ValueT> newValue) {
104
return
replaceAllUsesWith
(value,
cast<Value>
(std::move(newValue)));
105
}
106
107
}
// namespace mini_llvm::ir
108
109
template
<
typename
ValueT>
110
requires
std::derived_from<ValueT, mini_llvm::ir::Value>
111
struct
std::formatter<ValueT> {
112
constexpr
auto
parse
(std::format_parse_context &ctx) {
113
if
(*ctx.begin() ==
'o'
) {
114
asOperand_ =
true
;
115
return
std::next(ctx.begin());
116
}
117
return
ctx.begin();
118
}
119
120
template
<
typename
FormatContext>
121
auto
format
(
const
ValueT &value, FormatContext &ctx)
const
{
122
if
(asOperand_) {
123
return
std::format_to(ctx.out(),
"{}"
, value.formatAsOperand());
124
}
125
return
std::format_to(ctx.out(),
"{}"
, value.format());
126
}
127
128
private
:
129
bool
asOperand_ =
false
;
130
};
Compiler.h
MINI_LLVM_EXPORT
#define MINI_LLVM_EXPORT
Definition
Compiler.h:17
IndirectIterator.h
Memory.h
Type.h
mini_llvm::IndirectIterator
Definition
IndirectIterator.h:16
mini_llvm::ir::UseBase
Definition
Use.h:17
mini_llvm::ir::Value
Definition
Value.h:24
mini_llvm::ir::Value::UseBase
friend class UseBase
Definition
Value.h:85
mini_llvm::ir::Value::use_iterator
IndirectIterator< UseSet::iterator, UseBase > use_iterator
Definition
Value.h:28
mini_llvm::ir::Value::type
virtual std::unique_ptr< Type > type() const =0
mini_llvm::ir::Value::name
const std::string & name() const &
Definition
Value.h:40
mini_llvm::ir::Value::clone
virtual std::unique_ptr< Value > clone() const =0
mini_llvm::ir::Value::format
virtual std::string format() const =0
mini_llvm::ir::Value::Value
Value(Value &&)=delete
mini_llvm::ir::Value::formatName
std::string formatName() const
mini_llvm::ir::Value::setName
void setName(std::string name)
Definition
Value.h:48
mini_llvm::ir::Value::use_begin
use_iterator use_begin() const
Definition
Value.h:54
mini_llvm::ir::Value::use_empty
bool use_empty() const
Definition
Value.h:62
mini_llvm::ir::Value::Value
Value(const Value &)=delete
mini_llvm::ir::Value::name
std::string && name() &&
Definition
Value.h:44
mini_llvm::ir::Value::operator=
Value & operator=(Value &&)=delete
mini_llvm::ir::Value::use_end
use_iterator use_end() const
Definition
Value.h:58
mini_llvm::ir::Value::operator=
Value & operator=(const Value &)=delete
mini_llvm::ir::Value::use_size
size_t use_size() const
Definition
Value.h:66
mini_llvm::ir::Value::isWellFormed
virtual bool isWellFormed() const
Definition
Value.h:70
mini_llvm::ir::Value::Value
Value()=default
mini_llvm::ir::Value::~Value
virtual ~Value()=default
mini_llvm::ir::Value::formatAsOperand
virtual std::string formatAsOperand() const =0
mini_llvm::ir
Definition
Argument.h:13
mini_llvm::ir::uses
auto uses(const Value &value)
Definition
Value.h:88
mini_llvm::ir::replaceAllUsesWith
MINI_LLVM_EXPORT bool replaceAllUsesWith(const Value &value, std::shared_ptr< Value > newValue)
mini_llvm::cast
std::unique_ptr< To > cast(std::unique_ptr< From > from) noexcept
Definition
Memory.h:10
std::formatter< ValueT >::parse
constexpr auto parse(std::format_parse_context &ctx)
Definition
Value.h:112
std::formatter< ValueT >::format
auto format(const ValueT &value, FormatContext &ctx) const
Definition
Value.h:121
include
mini-llvm
ir
Value.h
Generated by
1.17.0