-
Notifications
You must be signed in to change notification settings - Fork 15
/
enum2string_generator.cpp
194 lines (161 loc) · 6.55 KB
/
enum2string_generator.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
#include "enum2string_generator.h"
#include "options.h"
#include "ast_reflector.h"
#include "ast_utils.h"
#include <clang/ASTMatchers/ASTMatchers.h>
#include <iostream>
using namespace clang::ast_matchers;
namespace codegen
{
namespace
{
DeclarationMatcher enumMatcher =
enumDecl().bind("enum");
}
Enum2StringGenerator::Enum2StringGenerator(const Options &opts)
: BasicGenerator(opts)
{
}
void Enum2StringGenerator::SetupMatcher(clang::ast_matchers::MatchFinder &finder, clang::ast_matchers::MatchFinder::MatchCallback *defaultCallback)
{
finder.addMatcher(enumMatcher, defaultCallback);
}
void Enum2StringGenerator::HandleMatch(const clang::ast_matchers::MatchFinder::MatchResult &matchResult)
{
if (const clang::EnumDecl* decl = matchResult.Nodes.getNodeAs<clang::EnumDecl>("enum"))
{
if (!IsFromInputFiles(decl->getLocStart(), matchResult.Context))
return;
reflection::AstReflector reflector(matchResult.Context);
reflector.ReflectEnum(decl, &m_namespaces);
}
}
void Enum2StringGenerator::WriteHeaderPreamble(CppSourceStream &hdrOs)
{
hdrOs << out::header_guard(m_options.outputHeaderName) << "\n";
// Include input files (directly, by path)
for (auto fileName : m_options.inputFiles)
{
std::replace(fileName.begin(), fileName.end(), '\\', '/');
hdrOs << "#include \"" << fileName << "\"\n";
}
WriteExtraHeaders(hdrOs);
// Necessary library files
hdrOs << "#include <flex_lib/stringized_enum.h>\n";
hdrOs << "#include <algorithm>\n";
hdrOs << "#include <utility>\n\n";
}
void Enum2StringGenerator::WriteHeaderPostamble(CppSourceStream &hdrOs)
{
hdrOs << out::scope_exit;
}
namespace
{
out::ScopedParams MakeScopedParams(CppSourceStream &os, reflection::EnumInfoPtr enumDescr)
{
std::string scopeSpec = enumDescr->scopeSpecifier;
std::string scopedName = scopeSpec + (scopeSpec.empty() ? "" : "::") + enumDescr->name;
std::string fullQualifiedName = enumDescr->GetFullQualifiedName(false);
return out::ScopedParams(os, {
{"enumName", enumDescr->name},
{"enumScopedName", scopedName},
{"scopeQual", scopeSpec},
{"namespaceQual", enumDescr->namespaceQualifier},
{"enumFullQualifiedName", fullQualifiedName},
{"prefix", enumDescr->isScoped ? scopedName + "::" : scopeSpec + (scopeSpec.empty() ? "" : "::")}
});
}
}
void Enum2StringGenerator::WriteHeaderContent(CppSourceStream &hdrOs)
{
std::vector<reflection::EnumInfoPtr> enums;
WriteNamespaceContents(hdrOs, m_namespaces.GetRootNamespace(), [this, &enums](CppSourceStream &os, reflection::NamespaceInfoPtr ns) {
for (auto& enumInfo : ns->enums)
{
WriteEnumToStringConversion(os, enumInfo);
WriteEnumFromStringConversion(os, enumInfo);
enums.push_back(enumInfo);
}
});
hdrOs << "\n\n";
{
out::BracedStreamScope flNs("\nnamespace flex_lib", "\n\n", 0);
hdrOs << out::new_line(1) << flNs;
for (reflection::EnumInfoPtr enumInfo : enums)
{
auto scopedParams = MakeScopedParams(hdrOs, enumInfo);
{
hdrOs << out::new_line(1) << "template<>";
out::BracedStreamScope body("inline const char* Enum2String($enumFullQualifiedName$ e)", "\n");
hdrOs << out::new_line(1) << body;
hdrOs << out::new_line(1) << "return $namespaceQual$::$enumName$ToString(e);";
}
{
hdrOs << out::new_line(1) << "template<>";
out::BracedStreamScope body("inline $enumFullQualifiedName$ String2Enum<$enumFullQualifiedName$>(const char* itemName)", "\n");
hdrOs << out::new_line(1) << body;
hdrOs << out::new_line(1) << "return $namespaceQual$::StringTo$enumName$(itemName);";
}
}
}
{
out::BracedStreamScope flNs("\nnamespace std", "\n\n", 0);
hdrOs << out::new_line(1) << flNs;
for (reflection::EnumInfoPtr enumInfo : enums)
{
auto scopedParams = MakeScopedParams(hdrOs, enumInfo);
out::BracedStreamScope body("inline std::string to_string($enumFullQualifiedName$ e)", "\n");
hdrOs << out::new_line(1) << body;
hdrOs << out::new_line(1) << "return $namespaceQual$::$enumName$ToString(e);";
}
}
}
// Enum item to string conversion writer
void Enum2StringGenerator::WriteEnumToStringConversion(CppSourceStream &hdrOs, const reflection::EnumInfoPtr &enumDescr)
{
auto scopedParams = MakeScopedParams(hdrOs, enumDescr);
out::BracedStreamScope fnScope("inline const char* $enumName$ToString($enumScopedName$ e)", "\n");
hdrOs << out::new_line(1) << fnScope;
{
out::BracedStreamScope switchScope("switch (e)", "\n");
hdrOs << out::new_line(1) << switchScope;
out::OutParams innerParams;
for (auto& i : enumDescr->items)
{
innerParams["itemName"] = i.itemName;
hdrOs << out::with_params(innerParams)
<< out::new_line(-1) << "case $prefix$$itemName$:"
<< out::new_line(1) << "return \"$itemName$\";";
}
}
hdrOs << out::new_line(1) << "return \"Unknown Item\";";
}
// String to enum conversion writer
void Enum2StringGenerator::WriteEnumFromStringConversion(CppSourceStream &hdrOs, const reflection::EnumInfoPtr &enumDescr)
{
auto params = MakeScopedParams(hdrOs, enumDescr);
out::BracedStreamScope fnScope("inline $enumScopedName$ StringTo$enumName$(const char* itemName)", "\n");
hdrOs << out::new_line(1) << fnScope;
{
out::BracedStreamScope itemsScope("static std::pair<const char*, $enumScopedName$> items[] = ", ";\n");
hdrOs << out::new_line(1) << itemsScope;
out::OutParams& innerParams = params.GetParams();
auto items = enumDescr->items;
std::sort(begin(items), end(items), [](auto& i1, auto& i2) {return i1.itemName < i2.itemName;});
for (auto& i : items)
{
innerParams["itemName"] = i.itemName;
hdrOs << out::with_params(innerParams) << out::new_line(1) << "{\"$itemName$\", $prefix$$itemName$},";
}
}
hdrOs << out::with_params(params.GetParams()) << R"(
$enumScopedName$ result;
if (!flex_lib::detail::String2Enum(itemName, items, result))
flex_lib::bad_enum_name::Throw(itemName, "$enumName$");
return result;)";
}
} // codegen
codegen::GeneratorPtr CreateEnum2StringGen(const codegen::Options& opts)
{
return std::make_unique<codegen::Enum2StringGenerator>(opts);
}