mini-llvm 0.1.0
Loading...
Searching...
No Matches
GlobalValue.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 <list>
10#include <memory>
11#include <string>
12#include <utility>
13
15#include "mini-llvm/mc/Symbol.h"
18
19namespace mini_llvm::mc {
20
22 using StatementList = std::list<std::unique_ptr<Statement>>;
23
24public:
29
31 : symbol_(std::move(symbol)), section_(std::move(section)), isGlobal_(isGlobal), alignment_(alignment) {}
32
34 : symbol_(std::move(symbol)), section_(std::move(section)), isGlobal_(isGlobal), alignment_(0) {}
35
36 GlobalValue(const GlobalValue &) = delete;
37 GlobalValue(GlobalValue &&) = default;
38 GlobalValue &operator=(const GlobalValue &) = delete;
40
41 const Symbol &symbol() const {
42 return symbol_;
43 }
44
46 symbol_ = std::move(symbol);
47 }
48
49 const std::string &section() const & {
50 return section_;
51 }
52
53 std::string &&section() && {
54 return std::move(section_);
55 }
56
57 void setSection(std::string section) {
58 section_ = std::move(section);
59 }
60
61 bool isGlobal() const {
62 return isGlobal_;
63 }
64
65 void setGlobal(bool isGlobal) {
66 isGlobal_ = isGlobal;
67 }
68
69 int alignment() const {
70 return alignment_;
71 }
72
74 alignment_ = alignment;
75 }
76
78 return iterator(stmts_.begin());
79 }
80
82 return const_iterator(stmts_.begin());
83 }
84
86 return iterator(stmts_.end());
87 }
88
90 return const_iterator(stmts_.end());
91 }
92
94 return reverse_iterator(stmts_.rbegin());
95 }
96
98 return const_reverse_iterator(stmts_.rbegin());
99 }
100
102 return reverse_iterator(stmts_.rend());
103 }
104
106 return const_reverse_iterator(stmts_.rend());
107 }
108
110 return *begin();
111 }
112
113 const Statement &front() const {
114 return *begin();
115 }
116
118 return *std::prev(end());
119 }
120
121 const Statement &back() const {
122 return *std::prev(end());
123 }
124
125 bool empty() const {
126 return stmts_.empty();
127 }
128
129 size_t size() const {
130 return stmts_.size();
131 }
132
133 Statement &add(const_iterator pos, std::unique_ptr<Statement> stmt);
134
135 Statement &prepend(std::unique_ptr<Statement> stmt) {
136 return add(begin(), std::move(stmt));
137 }
138
139 Statement &append(std::unique_ptr<Statement> stmt) {
140 return add(end(), std::move(stmt));
141 }
142
144
145 void removeFirst() {
146 remove(begin());
147 }
148
149 void removeLast() {
150 remove(std::prev(end()));
151 }
152
153 void clear();
154
155 std::string format() const;
156
157private:
158 Symbol symbol_;
159 std::string section_;
160 bool isGlobal_;
161 int alignment_;
162 StatementList stmts_;
163};
164
165} // namespace mini_llvm::mc
166
167template <typename GlobalValueT>
168 requires std::derived_from<GlobalValueT, mini_llvm::mc::GlobalValue>
169struct std::formatter<GlobalValueT> {
170 constexpr auto parse(std::format_parse_context &ctx) {
171 return ctx.begin();
172 }
173
174 template <typename FormatContext>
175 auto format(const GlobalValueT &G, FormatContext &ctx) const {
176 return std::format_to(ctx.out(), "{}", G.format());
177 }
178};
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
Definition IndirectIterator.h:16
GlobalValue(Symbol symbol, std::string section, bool isGlobal)
Definition GlobalValue.h:33
IndirectIterator< StatementList::reverse_iterator, Statement > reverse_iterator
Definition GlobalValue.h:27
int alignment() const
Definition GlobalValue.h:69
void setSymbol(Symbol symbol)
Definition GlobalValue.h:45
std::string && section() &&
Definition GlobalValue.h:53
Statement & add(const_iterator pos, std::unique_ptr< Statement > stmt)
reverse_iterator rbegin()
Definition GlobalValue.h:93
GlobalValue(GlobalValue &&)=default
const Statement & front() const
Definition GlobalValue.h:113
void removeFirst()
Definition GlobalValue.h:145
Statement & prepend(std::unique_ptr< Statement > stmt)
Definition GlobalValue.h:135
const_reverse_iterator rend() const
Definition GlobalValue.h:105
void setGlobal(bool isGlobal)
Definition GlobalValue.h:65
const Statement & back() const
Definition GlobalValue.h:121
reverse_iterator rend()
Definition GlobalValue.h:101
IndirectIterator< StatementList::iterator, Statement > iterator
Definition GlobalValue.h:25
const_reverse_iterator rbegin() const
Definition GlobalValue.h:97
Statement & back()
Definition GlobalValue.h:117
const_iterator begin() const
Definition GlobalValue.h:81
void removeLast()
Definition GlobalValue.h:149
Statement & front()
Definition GlobalValue.h:109
size_t size() const
Definition GlobalValue.h:129
void setSection(std::string section)
Definition GlobalValue.h:57
GlobalValue & operator=(GlobalValue &&)=default
GlobalValue(const GlobalValue &)=delete
std::string format() const
iterator end()
Definition GlobalValue.h:85
void remove(const_iterator pos)
const std::string & section() const &
Definition GlobalValue.h:49
bool empty() const
Definition GlobalValue.h:125
IndirectIterator< StatementList::const_reverse_iterator, const Statement > const_reverse_iterator
Definition GlobalValue.h:28
GlobalValue & operator=(const GlobalValue &)=delete
iterator begin()
Definition GlobalValue.h:77
IndirectIterator< StatementList::const_iterator, const Statement > const_iterator
Definition GlobalValue.h:26
const Symbol & symbol() const
Definition GlobalValue.h:41
void setAlignment(int alignment)
Definition GlobalValue.h:73
GlobalValue(Symbol symbol, std::string section, bool isGlobal, int alignment)
Definition GlobalValue.h:30
const_iterator end() const
Definition GlobalValue.h:89
Statement & append(std::unique_ptr< Statement > stmt)
Definition GlobalValue.h:139
bool isGlobal() const
Definition GlobalValue.h:61
Definition Statement.h:13
Definition Symbol.h:16
Definition Directive.h:8
constexpr auto parse(std::format_parse_context &ctx)
Definition GlobalValue.h:170
auto format(const GlobalValueT &G, FormatContext &ctx) const
Definition GlobalValue.h:175