forked from andreasfertig/cppinsights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGenerator.h
291 lines (234 loc) · 10.4 KB
/
CodeGenerator.h
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/******************************************************************************
*
* C++ Insights, copyright (C) by Andreas Fertig
* Distributed under an MIT license. See LICENSE for details
*
****************************************************************************/
#ifndef INSIGHTS_CODE_GENERATOR_H
#define INSIGHTS_CODE_GENERATOR_H
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "ClangCompat.h"
#include "InsightsStaticStrings.h"
#include "InsightsStrongTypes.h"
#include "OutputFormatHelper.h"
#include "StackList.h"
//-----------------------------------------------------------------------------
namespace clang::insights {
/// \brief More or less the heart of C++ Insights.
///
/// This is the place where nearly all of the transformations happen. This class knows the needed types and how to
/// generated code from them.
class CodeGenerator
{
protected:
OutputFormatHelper& mOutputFormatHelper;
enum class LambdaCallerType
{
VarDecl,
CallExpr,
OperatorCallExpr,
MemberCallExpr,
LambdaExpr,
ReturnStmt,
BinaryOperator,
};
class LambdaHelper : public StackListEntry<LambdaHelper>
{
public:
LambdaHelper(const LambdaCallerType lambdaCallerType, OutputFormatHelper& outputFormatHelper)
: mLambdaCallerType{lambdaCallerType}
, mCurrentPos{outputFormatHelper.CurrentPos()}
, mOutputFormatHelper{outputFormatHelper}
, mLambdaOutputFormatHelper{}
, mInits{}
{
mLambdaOutputFormatHelper.SetIndent(mOutputFormatHelper);
}
void finish()
{
if(!mLambdaOutputFormatHelper.empty()) {
mOutputFormatHelper.InsertAt(mCurrentPos, mLambdaOutputFormatHelper.GetString());
}
}
OutputFormatHelper& buffer() { return mLambdaOutputFormatHelper; }
std::string& inits() { return mInits; }
void insertInits(OutputFormatHelper& outputFormatHelper)
{
if(!mInits.empty()) {
outputFormatHelper.Append(mInits);
mInits.clear();
}
}
LambdaCallerType callerType() const { return mLambdaCallerType; }
private:
const LambdaCallerType mLambdaCallerType;
const size_t mCurrentPos;
OutputFormatHelper& mOutputFormatHelper;
OutputFormatHelper mLambdaOutputFormatHelper;
std::string mInits;
};
//-----------------------------------------------------------------------------
using LambdaStackType = StackList<class LambdaHelper>;
public:
explicit CodeGenerator(OutputFormatHelper& _outputFormatHelper)
: mOutputFormatHelper{_outputFormatHelper}
, mLambdaStackThis{}
, mLambdaStack{mLambdaStackThis}
, mSkipVarDecl{SkipVarDecl::No}
, mUseCommaInsteadOfSemi{UseCommaInsteadOfSemi::No}
, mLambdaExpr{nullptr}
{
}
explicit CodeGenerator(OutputFormatHelper& _outputFormatHelper, LambdaStackType& lambdaStack)
: mOutputFormatHelper{_outputFormatHelper}
, mLambdaStackThis{}
, mLambdaStack{lambdaStack}
, mSkipVarDecl{SkipVarDecl::No}
, mUseCommaInsteadOfSemi{UseCommaInsteadOfSemi::No}
, mLambdaExpr{nullptr}
{
}
virtual ~CodeGenerator() = default;
#define IGNORED_DECL(type) \
virtual void InsertArg(const type*) {}
#define IGNORED_STMT(type) \
virtual void InsertArg(const type*) {}
#define SUPPORTED_DECL(type) virtual void InsertArg(const type* stmt);
#define SUPPORTED_STMT(type) virtual void InsertArg(const type* stmt);
#include "CodeGeneratorTypes.h"
virtual void InsertArg(const Decl* stmt);
virtual void InsertArg(const Stmt* stmt);
void InsertTemplateArgs(const DeclRefExpr& stmt);
void InsertTemplateArgs(const ClassTemplateSpecializationDecl& clsTemplateSpe);
void InsertTemplateArgs(const FunctionDecl& FD)
{
if(const auto* tmplArgs = FD.getTemplateSpecializationArgs()) {
InsertTemplateArgs(tmplArgs->asArray());
}
}
STRONG_BOOL(SkipAccess);
/// \brief Insert the code for a FunctionDecl.
///
/// This inserts the code of a FunctionDecl (and everything which is derived from one). It takes care of
/// CXXMethodDecl's access modifier as well as things like constexpr, noexcept, static and more.
///
/// @param decl The FunctionDecl to process.
/// @param skipAccess Show or hide access modifiers (public, private, protected). The default is to show them.
/// @param cxxInheritedCtorDecl If not null, the type and name of this decl is used for the parameters.
void InsertAccessModifierAndNameWithReturnType(const FunctionDecl& decl,
const SkipAccess skipAccess = SkipAccess::No,
const CXXConstructorDecl* cxxInheritedCtorDecl = nullptr);
/// Track whether we have at least one local static variable in this TU.
/// If so we need to insert the <new> header for the placement-new.
static bool NeedToInsertNewHeader() { return mHaveLocalStatic; }
protected:
virtual bool InsertVarDecl() { return true; }
virtual bool InsertComma() { return false; }
virtual bool InsertSemi() { return true; }
void HandleTemplateParameterPack(const ArrayRef<TemplateArgument>& args);
void HandleCompoundStmt(const CompoundStmt* stmt);
/// \brief Show what is behind a local static variable.
///
/// [stmt.dcl] p4: Initialization of a block-scope variable with static storage duration is thread-safe since C++11.
/// Regardless of that, as long as it is a non-trivally construct and destructable class the compiler adds code to
/// track the initialization state. Reference:
/// - www.opensource.apple.com/source/libcppabi/libcppabi-14/src/cxa_guard.cxx
void HandleLocalStaticNonTrivialClass(const VarDecl* stmt);
void
FormatCast(const std::string castName, const QualType& CastDestType, const Expr* SubExpr, const CastKind& castKind);
template<typename T, typename Lambda>
void ForEachArg(const T& arguments, Lambda&& lambda)
{
OutputFormatHelper::ForEachArg(arguments, mOutputFormatHelper, lambda);
}
void InsertArgWithParensIfNeeded(const Stmt* stmt);
void InsertSuffix(const QualType& type);
void InsertTemplateArgs(const ArrayRef<TemplateArgument>& array);
void InsertTemplateArg(const TemplateArgument& arg);
bool InsertLambdaStaticInvoker(const CXXMethodDecl* cxxMethodDecl);
void PrintNamespace(const NestedNameSpecifier* namespaceSpecifier);
void ParseDeclContext(const DeclContext* Ctx);
/// \brief Check whether or not this statement will add curlys or parentheses and add them only if required.
void InsertCurlysIfRequired(const Stmt* stmt);
STRONG_BOOL(AddSpaceAtTheEnd);
enum class BraceKind
{
Parens,
Curlys
};
template<typename T>
void WrapInParens(T&& lambda, const AddSpaceAtTheEnd addSpaceAtTheEnd = AddSpaceAtTheEnd::No);
template<typename T>
void
WrapInParensIfNeeded(bool needsParens, T&& lambda, const AddSpaceAtTheEnd addSpaceAtTheEnd = AddSpaceAtTheEnd::No);
template<typename T>
void WrapInCurlys(T&& lambda, const AddSpaceAtTheEnd addSpaceAtTheEnd = AddSpaceAtTheEnd::No);
template<typename T>
void WrapInParensOrCurlys(const BraceKind curlys,
T&& lambda,
const AddSpaceAtTheEnd addSpaceAtTheEnd = AddSpaceAtTheEnd::No);
static const char* GetKind(const UnaryExprOrTypeTraitExpr& uk);
static const char* GetBuiltinTypeSuffix(const BuiltinType::Kind& kind);
class LambdaScopeHandler
{
public:
LambdaScopeHandler(LambdaStackType& stack,
OutputFormatHelper& outputFormatHelper,
const LambdaCallerType lambdaCallerType);
~LambdaScopeHandler();
private:
LambdaStackType& mStack;
LambdaHelper mHelper;
OutputFormatHelper& GetBuffer(OutputFormatHelper& outputFormatHelper) const;
};
void HandleLambdaExpr(const LambdaExpr* stmt, LambdaHelper& lambdaHelper);
LambdaStackType mLambdaStackThis;
LambdaStackType& mLambdaStack;
STRONG_BOOL(SkipVarDecl);
STRONG_BOOL(UseCommaInsteadOfSemi);
SkipVarDecl mSkipVarDecl;
UseCommaInsteadOfSemi mUseCommaInsteadOfSemi;
const LambdaExpr* mLambdaExpr;
static inline bool
mHaveLocalStatic; // Track whether there was a thread-safe in the code. This requires adding the <new> header.
};
//-----------------------------------------------------------------------------
class LambdaCodeGenerator final : public CodeGenerator
{
public:
using CodeGenerator::CodeGenerator;
using CodeGenerator::InsertArg;
void InsertArg(const CXXThisExpr* stmt) override;
bool mCapturedThisAsCopy;
};
//-----------------------------------------------------------------------------
/*
* \brief Special case to generate the inits of e.g. a \c ForStmt.
*
* This class is a specialization to handle cases where we can have multiple init statements to the same variable and
* hence need only one time the \c VarDecl. An example a for-loops:
\code
for(int x=2, y=3, z=4; i < x; ++i) {}
\endcode
*/
class MultiStmtDeclCodeGenerator final : public CodeGenerator
{
public:
using CodeGenerator::CodeGenerator;
// Insert the semi after the last declaration. This implies that this class always requires its own scope.
~MultiStmtDeclCodeGenerator() { mOutputFormatHelper.Append("; "); }
using CodeGenerator::InsertArg;
protected:
OnceTrue mInsertVarDecl; //! Insert the \c VarDecl only once.
OnceFalse mInsertComma; //! Insert the comma after we have generated the first \c VarDecl and we are about to
//! insert another one.
bool InsertVarDecl() override { return mInsertVarDecl; }
bool InsertComma() override { return mInsertComma; }
bool InsertSemi() override { return false; }
};
//-----------------------------------------------------------------------------
} // namespace clang::insights
#endif /* INSIGHTS_CODE_GENERATOR_H */