mini-llvm 0.1.0
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"
19
20namespace mini_llvm::ir {
21
22class UseBase;
23
24class MINI_LLVM_EXPORT Value : public std::enable_shared_from_this<Value> {
25 using UseSet = std::unordered_set<UseBase *>;
26
27public:
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
55 return use_iterator(uses_.begin());
56 }
57
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
81private:
82 std::string name_;
83 mutable UseSet uses_;
84
85 friend class UseBase;
86};
87
88inline auto uses(const Value &value) {
89 return std::ranges::subrange(value.use_begin(), value.use_end());
90}
91
92MINI_LLVM_EXPORT bool replaceAllUsesWith(const Value &value, std::shared_ptr<Value> newValue);
93MINI_LLVM_EXPORT bool replaceAllUsesWith(const Value &value, std::weak_ptr<Value> newValue);
94
95template <typename ValueT>
96 requires std::derived_from<ValueT, Value>
97bool replaceAllUsesWith(const Value &value, std::shared_ptr<ValueT> newValue) {
98 return replaceAllUsesWith(value, cast<Value>(std::move(newValue)));
99}
100
101template <typename ValueT>
102 requires std::derived_from<ValueT, Value>
103bool 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
109template <typename ValueT>
110 requires std::derived_from<ValueT, mini_llvm::ir::Value>
111struct 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
128private:
129 bool asOperand_ = false;
130};
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
Definition IndirectIterator.h:16
Definition Use.h:17
Definition Value.h:24
friend class UseBase
Definition Value.h:85
IndirectIterator< UseSet::iterator, UseBase > use_iterator
Definition Value.h:28
virtual std::unique_ptr< Type > type() const =0
const std::string & name() const &
Definition Value.h:40
virtual std::unique_ptr< Value > clone() const =0
virtual std::string format() const =0
Value(Value &&)=delete
std::string formatName() const
void setName(std::string name)
Definition Value.h:48
use_iterator use_begin() const
Definition Value.h:54
bool use_empty() const
Definition Value.h:62
Value(const Value &)=delete
std::string && name() &&
Definition Value.h:44
Value & operator=(Value &&)=delete
use_iterator use_end() const
Definition Value.h:58
Value & operator=(const Value &)=delete
size_t use_size() const
Definition Value.h:66
virtual bool isWellFormed() const
Definition Value.h:70
virtual ~Value()=default
virtual std::string formatAsOperand() const =0
Definition Argument.h:13
auto uses(const Value &value)
Definition Value.h:88
MINI_LLVM_EXPORT bool replaceAllUsesWith(const Value &value, std::shared_ptr< Value > newValue)
std::unique_ptr< To > cast(std::unique_ptr< From > from) noexcept
Definition Memory.h:10
constexpr auto parse(std::format_parse_context &ctx)
Definition Value.h:112
auto format(const ValueT &value, FormatContext &ctx) const
Definition Value.h:121