mini-llvm 0.1.0
Loading...
Searching...
No Matches
Module.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <cstddef>
6#include <format>
7#include <iterator>
8#include <list>
9#include <memory>
10#include <ranges>
11#include <string>
12#include <string_view>
13#include <utility>
14
19
20namespace mini_llvm::ir {
21
23 using GlobalVarList = std::list<std::shared_ptr<GlobalVar>>;
24 using FunctionList = std::list<std::shared_ptr<Function>>;
25
26public:
29
32
34 return global_var_iterator(globalVars_.begin());
35 }
36
38 return const_global_var_iterator(globalVars_.begin());
39 }
40
42 return global_var_iterator(globalVars_.end());
43 }
44
46 return const_global_var_iterator(globalVars_.end());
47 }
48
49 bool global_var_empty() const {
50 return globalVars_.empty();
51 }
52
53 size_t global_var_size() const {
54 return globalVars_.size();
55 }
56
57 GlobalVar &addGlobalVar(const_global_var_iterator pos, std::shared_ptr<GlobalVar> G);
58
59 GlobalVar &prependGlobalVar(std::shared_ptr<GlobalVar> G) {
60 return addGlobalVar(global_var_begin(), std::move(G));
61 }
62
63 GlobalVar &appendGlobalVar(std::shared_ptr<GlobalVar> G) {
64 return addGlobalVar(global_var_end(), std::move(G));
65 }
66
68
72
74 removeGlobalVar(std::prev(global_var_end()));
75 }
76
78
80 return function_iterator(functions_.begin());
81 }
82
84 return const_function_iterator(functions_.begin());
85 }
86
88 return function_iterator(functions_.end());
89 }
90
92 return const_function_iterator(functions_.end());
93 }
94
95 bool function_empty() const {
96 return functions_.empty();
97 }
98
99 size_t function_size() const {
100 return functions_.size();
101 }
102
103 Function &addFunction(const_function_iterator pos, std::shared_ptr<Function> F);
104
105 Function &prependFunction(std::shared_ptr<Function> F) {
106 return addFunction(function_begin(), std::move(F));
107 }
108
109 Function &appendFunction(std::shared_ptr<Function> F) {
110 return addFunction(function_end(), std::move(F));
111 }
112
114
118
120 removeFunction(std::prev(function_end()));
121 }
122
124
125 bool isWellFormed() const;
126
127 std::string format() const;
128
129private:
130 GlobalVarList globalVars_;
131 FunctionList functions_;
132};
133
134inline auto globalVars(Module &M) {
135 return std::ranges::subrange(M.global_var_begin(), M.global_var_end());
136}
137
138inline auto globalVars(const Module &M) {
139 return std::ranges::subrange(M.global_var_begin(), M.global_var_end());
140}
141
142inline auto functions(Module &M) {
143 return std::ranges::subrange(M.function_begin(), M.function_end());
144}
145
146inline auto functions(const Module &M) {
147 return std::ranges::subrange(M.function_begin(), M.function_end());
148}
149
151MINI_LLVM_EXPORT const GlobalVar *getGlobalVarByName(const Module &M, std::string_view name);
153MINI_LLVM_EXPORT const Function *getFunctionByName(const Module &M, std::string_view name);
154
155} // namespace mini_llvm::ir
156
157template <>
158struct std::formatter<mini_llvm::ir::Module> {
159 constexpr auto parse(std::format_parse_context &ctx) {
160 return ctx.begin();
161 }
162
163 template <typename FormatContext>
164 auto format(const mini_llvm::ir::Module &M, FormatContext &ctx) const {
165 return std::format_to(ctx.out(), "{}", M.format());
166 }
167};
#define MINI_LLVM_EXPORT
Definition Compiler.h:17
Definition IndirectIterator.h:16
Definition Function.h:34
Definition GlobalVar.h:20
Definition Module.h:22
function_iterator function_end()
Definition Module.h:87
bool function_empty() const
Definition Module.h:95
size_t function_size() const
Definition Module.h:99
const_global_var_iterator global_var_begin() const
Definition Module.h:37
std::string format() const
size_t global_var_size() const
Definition Module.h:53
bool isWellFormed() const
function_iterator function_begin()
Definition Module.h:79
GlobalVar & addGlobalVar(const_global_var_iterator pos, std::shared_ptr< GlobalVar > G)
const_function_iterator function_end() const
Definition Module.h:91
void removeFunction(const_function_iterator pos)
GlobalVar & prependGlobalVar(std::shared_ptr< GlobalVar > G)
Definition Module.h:59
const_function_iterator function_begin() const
Definition Module.h:83
void removeGlobalVar(const_global_var_iterator pos)
const_global_var_iterator global_var_end() const
Definition Module.h:45
global_var_iterator global_var_end()
Definition Module.h:41
Function & addFunction(const_function_iterator pos, std::shared_ptr< Function > F)
global_var_iterator global_var_begin()
Definition Module.h:33
GlobalVar & appendGlobalVar(std::shared_ptr< GlobalVar > G)
Definition Module.h:63
bool global_var_empty() const
Definition Module.h:49
IndirectIterator< GlobalVarList::iterator, GlobalVar > global_var_iterator
Definition Module.h:27
void removeLastFunction()
Definition Module.h:119
Function & prependFunction(std::shared_ptr< Function > F)
Definition Module.h:105
void removeFirstGlobalVar()
Definition Module.h:69
IndirectIterator< GlobalVarList::const_iterator, const GlobalVar > const_global_var_iterator
Definition Module.h:28
void removeLastGlobalVar()
Definition Module.h:73
IndirectIterator< FunctionList::iterator, Function > function_iterator
Definition Module.h:30
IndirectIterator< FunctionList::const_iterator, const Function > const_function_iterator
Definition Module.h:31
void removeFirstFunction()
Definition Module.h:115
Function & appendFunction(std::shared_ptr< Function > F)
Definition Module.h:109
Definition Argument.h:13
MINI_LLVM_EXPORT GlobalVar * getGlobalVarByName(Module &M, std::string_view name)
auto functions(Module &M)
Definition Module.h:142
constexpr const char * name(Token::Kind kind)
Definition Token.h:31
auto globalVars(Module &M)
Definition Module.h:134
MINI_LLVM_EXPORT Function * getFunctionByName(Module &M, std::string_view name)
Definition GraphColoringAllocator.h:13
constexpr auto parse(std::format_parse_context &ctx)
Definition Module.h:159
auto format(const mini_llvm::ir::Module &M, FormatContext &ctx) const
Definition Module.h:164