-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_generate_fundamental_types.cpp
243 lines (218 loc) · 9.44 KB
/
main_generate_fundamental_types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <string>
#include "format.hpp"
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <fmt/format.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/raw_ostream.h>
#include <string>
#include <memory>
#include <unordered_set>
#include "speculo_gen/IdGenerator.hpp"
#include "speculo_gen/idToString.hpp"
#include "speculo_gen/macros.hpp"
#include "speculo/details/array.hpp"
static llvm::cl::opt<std::string> Output(llvm::cl::Required, "output", llvm::cl::desc("Specify output path"),
llvm::cl::value_desc("filename"));
enum class InfoType {
Const,
Lvalue,
ConstLvalue,
Rvalue,
Ptr,
ConstPtr,
VolatileConst,
VolatileLvalue,
VolatileConstLvalue,
VolatileRvalue,
VolatilePtr,
VolatileConstPtr
};
[[nodiscard]] constexpr std::string wrapStructForInfoType(InfoType infoType) {
switch (infoType) {
case InfoType::Const:
return "StaticInfo_ConstWrap";
case InfoType::Lvalue:
return "StaticInfo_LRefWrap";
case InfoType::ConstLvalue:
return "StaticInfo_ConstLRefWrap";
case InfoType::Rvalue:
return "StaticInfo_RRefWrap";
case InfoType::Ptr:
return "StaticInfo_PtrWrap";
case InfoType::ConstPtr:
return "StaticInfo_ConstPtrWrap";
case InfoType::VolatileConst:
return "StaticInfo_VolatileConstWrap";
case InfoType::VolatileLvalue:
return "StaticInfo_VolatileLRefWrap";
case InfoType::VolatileConstLvalue:
return "StaticInfo_VolatileConstLRefWrap";
case InfoType::VolatileRvalue:
return "StaticInfo_VolatileRRefWrap";
case InfoType::VolatilePtr:
return "StaticInfo_VolatilePtrWrap";
case InfoType::VolatileConstPtr:
return "StaticInfo_VolatileConstPtrWrap";
}
throw "can't happen";
}
[[nodiscard]] constexpr std::string wrapNameForInfoType(InfoType infoType, const std::string &fullTypeName) {
using namespace std::string_literals;
switch (infoType) {
case InfoType::Const:
return "const " + fullTypeName;
case InfoType::Lvalue:
return fullTypeName + " &";
case InfoType::ConstLvalue:
return "const " + fullTypeName + " &";
case InfoType::Rvalue:
return fullTypeName + " &&";
case InfoType::Ptr:
return fullTypeName + " *";
case InfoType::ConstPtr:
return "const " + fullTypeName + " *";
case InfoType::VolatileConst:
return "volatile "s + "const " + fullTypeName;
case InfoType::VolatileLvalue:
return "volatile "s + fullTypeName + " &";
case InfoType::VolatileConstLvalue:
return "volatile "s + "const " + fullTypeName + " &";
case InfoType::VolatileRvalue:
return "volatile "s + fullTypeName + " &&";
case InfoType::VolatilePtr:
return "volatile "s + fullTypeName + " *";
case InfoType::VolatileConstPtr:
return "volatile "s + "const " + fullTypeName + " *";
}
throw "can't happen";
}
std::string
generateFundamentalStaticTypeInfo(speculo::gen::IdGenerator &gen, std::size_t size, std::string_view typeName,
std::string_view fullTypeName,
const std::unordered_set<InfoType> &typesToGenerate = {
InfoType::Const, InfoType::Lvalue, InfoType::ConstLvalue, InfoType::Rvalue,
InfoType::Ptr, InfoType::ConstPtr, InfoType::VolatileConst,
InfoType::VolatileLvalue,
InfoType::VolatileConstLvalue, InfoType::VolatileRvalue,
InfoType::VolatilePtr,
InfoType::VolatileConstPtr}) {
using namespace fmt::literals;
constexpr auto prologue = R"fmt(
/****************************** {full_name} START ******************************/
)fmt";
constexpr auto epilogue = R"fmt(
/****************************** {full_name} END ******************************/
)fmt";
constexpr auto typeTemplate = R"fmt(
template<>
struct StaticInfo<{type_id}> : FundamentalStaticTypeInfo<{full_type_name}, {size}, {type_id}, StringLiteral{{"{type_name}"}}, StringLiteral{{"{full_type_name}"}}> {{}};
template<>
[[nodiscard]] consteval ID getTypeId<{full_type_name}>() {{
return {type_id};
}}
)fmt";
constexpr auto variantTypeTemplate = R"fmt(
template<>
struct StaticInfo<{variant_id}> : {wrap_struct}<{variant_id}, {type_id}> {{}};
template<>
[[nodiscard]] consteval ID getTypeId<{full_wrap_type_name}>() {{
return {variant_id};
}}
)fmt";
const auto typeId = speculo::gen::idToString(gen.generateId(std::string{fullTypeName}));
std::string result{fmt::format(prologue, "full_name"_a = fullTypeName)};
result.append(fmt::format(typeTemplate, "type_id"_a = typeId, "size"_a = size, "type_name"_a = typeName,
"full_type_name"_a = fullTypeName));
for (const auto &toGen: typesToGenerate) {
const auto variantTypeName = wrapNameForInfoType(toGen, std::string{fullTypeName});
const auto variantId = speculo::gen::idToString(gen.generateId(variantTypeName));
result.append(fmt::format(variantTypeTemplate, "variant_id"_a = variantId,
"wrap_struct"_a = wrapStructForInfoType(toGen),
"type_id"_a = typeId, "full_wrap_type_name"_a = variantTypeName));
}
result.append(fmt::format(epilogue, "full_name"_a = fullTypeName));
return result;
}
int main(int argc, char **argv) {
spdlog::set_level(spdlog::level::debug);
spdlog::default_logger()->sinks().clear();
spdlog::default_logger()->sinks().emplace_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
llvm::cl::ParseCommandLineOptions(argc, argv, "speculo generate fundamental type infos");
speculo::gen::IdGenerator gen{};
#define FUND_TYPE(type) std::pair<std::string_view, std::size_t>(SPECULO_STRINGIFY(type), sizeof(type))
constexpr auto fundamentalTypes = speculo::make_array<std::pair<std::string_view, std::size_t>>(
FUND_TYPE(bool),
FUND_TYPE(char),
FUND_TYPE(signed char),
FUND_TYPE(unsigned char),
FUND_TYPE(char8_t),
FUND_TYPE(char16_t),
FUND_TYPE(char32_t),
FUND_TYPE(wchar_t),
FUND_TYPE(short),
FUND_TYPE(unsigned short),
FUND_TYPE(int),
FUND_TYPE(unsigned int),
FUND_TYPE(long),
FUND_TYPE(unsigned long),
FUND_TYPE(long long),
FUND_TYPE(unsigned long long),
FUND_TYPE(float),
FUND_TYPE(double),
FUND_TYPE(long double));
#undef FUND_TYPE
std::string output;
for (const auto [name, sz]: fundamentalTypes) {
output.append(generateFundamentalStaticTypeInfo(gen, sz, name, name)).append("\n");
}
output.append(
generateFundamentalStaticTypeInfo(gen, 0, "void", "void", {InfoType::Ptr, InfoType::ConstPtr})).append(
"\n");
output.append(generateFundamentalStaticTypeInfo(gen, 0, "nullptr_t", "std::nullptr_t", {})).append("\n");
std::error_code errorCode;
auto outStream = std::make_shared<llvm::raw_fd_ostream>(std::string{Output}, errorCode,
llvm::sys::fs::OpenFlags::OF_Text);
if (errorCode) {
spdlog::error("Failed to open output file '{}': {}", std::string{Output}, errorCode.message());
return 0;
}
*outStream << R"(#pragma once
#include <speculo/Attribute.hpp>
#include <speculo/Info.hpp>
#include <speculo/details/StaticInfo.hpp>
#include <speculo/details/StringLiteral.hpp>
#include <speculo/details/StaticInfo_Wrappers.hpp>
#include <speculo/details/array.hpp>
namespace speculo::details {
template<typename T, std::size_t TSize, ID TID, StringLiteral TypeName, StringLiteral FullTypeName = TypeName> requires(std::is_fundamental_v<T>)
struct FundamentalStaticTypeInfo {
using Type = T;
constexpr static ID Id = TID;
// FIXME: RangeOf msvc failure here
constexpr static /*RangeOf<speculo::Attribute>*/ auto Attributes = speculo::make_array<speculo::Attribute>();
constexpr static auto StaticInfoObjectType = StaticInfoType::FundamentalType;
constexpr static bool IsLvalueReference = false;
constexpr static bool IsRvalueReference = false;
constexpr static bool IsConst = false;
constexpr static bool IsPtr = false;
constexpr static bool IsVolatile = false;
constexpr static bool IsLiteral = true;
constexpr static bool IsPOD = true;
constexpr static bool IsStandardLayout = true;
constexpr static bool IsTriviallyCopyable = true;
constexpr static bool IsTrivial = true;
constexpr static bool IsEmpty = false;
constexpr static bool IsAggregate = false;
constexpr static std::size_t Size = TSize;
constexpr static std::size_t Alignment = TSize;
constexpr static auto Name = TypeName;
constexpr static auto FullName = FullTypeName;
};)";
*outStream << output;
*outStream << "}";
outStream->close();
format(std::string{Output});
return 0;
}