/
pimpl_generator.cpp
191 lines (156 loc) · 5.58 KB
/
pimpl_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
#include "pimpl_generator.h"
#include "options.h"
#include "ast_reflector.h"
#include "ast_utils.h"
#include "utils.h"
#include <clang/ASTMatchers/ASTMatchers.h>
#include <iostream>
using namespace clang::ast_matchers;
namespace codegen
{
namespace
{
DeclarationMatcher pimplMatcher = cxxRecordDecl
(isDerivedFrom("flex_lib::pimpl")).bind("pimpl");
}
PimplGenerator::PimplGenerator(const codegen::Options &opts)
: BasicGenerator(opts)
{
}
void PimplGenerator::SetupMatcher(clang::ast_matchers::MatchFinder &finder, clang::ast_matchers::MatchFinder::MatchCallback *defaultCallback)
{
finder.addMatcher(pimplMatcher, defaultCallback);
}
void PimplGenerator::HandleMatch(const clang::ast_matchers::MatchFinder::MatchResult &matchResult)
{
if (const clang::CXXRecordDecl* decl = matchResult.Nodes.getNodeAs<clang::CXXRecordDecl>("pimpl"))
{
if (!IsFromInputFiles(decl->getLocStart(), matchResult.Context))
return;
reflection::AstReflector reflector(matchResult.Context, m_options.consoleWriter);
auto ci = reflector.ReflectClass(decl, &m_namespaces);
dbg() << "### Pimpl declaration found: " << ci->GetFullQualifiedName() << std::endl;
}
}
bool PimplGenerator::Validate()
{
return true;
}
void PimplGenerator::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 <utility>\n\n";
}
void PimplGenerator::WriteHeaderPostamble(CppSourceStream &hdrOs)
{
hdrOs << out::scope_exit;
}
void PimplGenerator::WriteHeaderContent(CppSourceStream &hdrOs)
{
WriteNamespaceContents(hdrOs, m_namespaces.GetRootNamespace(), [this](CppSourceStream &os, reflection::NamespaceInfoPtr ns) {
for (auto& classInfo : ns->classes)
{
WritePimplImplementation(os, classInfo);
}
});
}
void PimplGenerator::WritePimplImplementation(CppSourceStream& os, reflection::ClassInfoPtr classInfo)
{
for (reflection::MethodInfoPtr methodInfo : classInfo->methods)
{
if (methodInfo->isCtor)
WriteCtorImplementation(os, classInfo, methodInfo);
else if (methodInfo->isDtor)
{
os << out::new_line(1);
if (methodInfo->isNoExcept)
os << methodInfo->GetScopedName() << "() noexcept = default;\n";
else
os << methodInfo->GetScopedName() << "() = default;\n";
}
else
WriteMethodImplementation(os, classInfo, methodInfo);
}
}
void PimplGenerator::WriteCtorImplementation(CppSourceStream& os, reflection::ClassInfoPtr classInfo, reflection::MethodInfoPtr methodInfo)
{
if (methodInfo->isImplicit)
return;
os << out::new_line(1) << methodInfo->GetScopedName() << "(";
WriteSeq(os, methodInfo->params, ", ", [](auto&& os, const reflection::MethodParamInfo& param) {os << param.fullDecl;});
os << ")";
if (methodInfo->isNoExcept)
os << " noexcept";
os << out::new_line(+1) << ": pimpl(this";
bool isFirst = true;
WriteSeq(os, methodInfo->params, ", ", [&isFirst](auto&& os, const reflection::MethodParamInfo& param)
{
if (isFirst)
{
isFirst = false;
os << ", ";
}
reflection::TypeInfoPtr pType = param.type;
if (pType->canBeMoved())
os << "std::move(" << param.name << ")";
else
os << param.name;
});
os << ")";
os << out::new_line(1) << "{";
os << out::new_line(1) << "}\n";
}
void PimplGenerator::WriteMethodImplementation(CppSourceStream& os, reflection::ClassInfoPtr classInfo, reflection::MethodInfoPtr methodInfo)
{
if (methodInfo->isImplicit || methodInfo->isStatic)
return;
os << out::new_line(1) << methodInfo->returnType->getPrintedName() << " " << methodInfo->GetScopedName() << "(";
WriteSeq(os, methodInfo->params, ", ", [](auto&& os, const reflection::MethodParamInfo& param) {os << param.fullDecl;});
os << ")";
if (methodInfo->isConst)
os << " const";
if (methodInfo->isNoExcept)
os << " noexcept";
out::BracedStreamScope body("", "\n");
os << body;
os << out::new_line(1);
const reflection::BuiltinType* retType = methodInfo->returnType->getAsBuiltin();
if (retType == nullptr || retType->type != reflection::BuiltinType::Void)
os << "return ";
os << "m_impl->" << methodInfo->name << "(";
WriteSeq(os, methodInfo->params, ", ", [classInfo](auto&& os, const reflection::MethodParamInfo& param)
{
std::string paramName = param.name;
reflection::TypeInfoPtr pType = param.type;
const reflection::RecordType* rType = pType->getAsRecord();
if (rType && rType->decl == classInfo->decl)
paramName = "*" + param.name + (pType->getPointingLevels() != 0 ? "->" : ".") + "m_impl.get()";
if (pType->canBeMoved())
os << "std::move(" << paramName << ")";
else
os << paramName;
});
os << ");";
}
void PimplGenerator::WriteSourcePreamble(CppSourceStream &srcOs)
{
}
void PimplGenerator::WriteSourceContent(CppSourceStream &srcOs)
{
}
void PimplGenerator::WriteSourcePostamble(CppSourceStream &srcOs)
{
}
} // codegen
codegen::GeneratorPtr CreatePimplGen(const codegen::Options& opts)
{
return std::make_unique<codegen::PimplGenerator>(opts);
}