forked from vgvassilev/creduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstantiateTemplateTypeParamToInt.cpp
297 lines (239 loc) · 8.32 KB
/
InstantiateTemplateTypeParamToInt.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
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
292
293
294
295
296
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2012, 2013 The University of Utah
// All rights reserved.
//
// This file is distributed under the University of Illinois Open Source
// License. See the file COPYING for details.
//
//===----------------------------------------------------------------------===//
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "InstantiateTemplateTypeParamToInt.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceManager.h"
#include "TransformationManager.h"
using namespace clang;
using namespace llvm;
// TODO: probably need to handle more cases where this transformation
// generates invalid code, e.g.:
// template <typename T> struct S {
// typedef T type;
// type &foo();
// };
// struct A {
// void foo() {};
// };
// void bar() {
// struct S<A> s;
// struct A a = s.foo();
// }
static const char *DescriptionMsg =
"This pass tries to instantiate a template type parameter of a class \
inside the class's definition. For example, \n\
template <typename T> struct A { T foo(void); } \n\
=> \n\
template <typename T> struct A { int foo(void); } \n\
\n\
Currently, just simply replace any reference of T with int.\n";
static RegisterTransformation<InstantiateTemplateTypeParamToInt>
Trans("instantiate-template-type-param-to-int", DescriptionMsg);
namespace {
typedef llvm::SmallPtrSet<const NamedDecl *, 8> TemplateParameterSet;
class TemplateParameterVisitor : public
RecursiveASTVisitor<TemplateParameterVisitor> {
public:
explicit TemplateParameterVisitor(TemplateParameterSet &Params)
: UsedParameters(Params)
{ }
~TemplateParameterVisitor() { };
bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc);
private:
TemplateParameterSet &UsedParameters;
};
// seems clang can't detect the T in T::* in the following case:
// struct B;
// template <typename T> struct C {
// C(void (T::*)()) { }
// };
// struct D { C<B> m; };
bool TemplateParameterVisitor::VisitTemplateTypeParmTypeLoc(
TemplateTypeParmTypeLoc Loc)
{
const TemplateTypeParmDecl *D = Loc.getDecl();
UsedParameters.insert(D);
return true;
}
} // end anonymous namespace
// In order to generate less uncompilable code, filter out cases such as
// template <typename T> struct S {
// T::type foo(void);
// };
class TemplateParameterFilterVisitor : public
RecursiveASTVisitor<TemplateParameterFilterVisitor> {
public:
TemplateParameterFilterVisitor(TemplateParameterSet &Params,
InstantiateTemplateTypeParamToInt *Instance)
: Parameters(Params),
ConsumerInstance(Instance)
{ }
~TemplateParameterFilterVisitor() { };
bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc);
private:
TemplateParameterSet &Parameters;
InstantiateTemplateTypeParamToInt *ConsumerInstance;
};
bool TemplateParameterFilterVisitor::VisitTemplateTypeParmTypeLoc(
TemplateTypeParmTypeLoc Loc)
{
const NamedDecl *ND = Loc.getDecl();
if (!Parameters.count(ND))
return true;
if (ConsumerInstance->isBeforeColonColon(Loc))
Parameters.erase(ND);
return true;
}
// ISSUE: maybe also try to instantiate a type parm with
// its default value, if it exists?
class InstantiateTemplateTypeParamToIntASTVisitor : public
RecursiveASTVisitor<InstantiateTemplateTypeParamToIntASTVisitor> {
public:
explicit InstantiateTemplateTypeParamToIntASTVisitor(
InstantiateTemplateTypeParamToInt *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitClassTemplateDecl(ClassTemplateDecl *D);
bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
private:
InstantiateTemplateTypeParamToInt *ConsumerInstance;
};
class InstantiateTemplateTypeParamToIntRewriteVisitor : public
RecursiveASTVisitor<InstantiateTemplateTypeParamToIntRewriteVisitor> {
public:
explicit InstantiateTemplateTypeParamToIntRewriteVisitor(
InstantiateTemplateTypeParamToInt *Instance)
: ConsumerInstance(Instance)
{ }
bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc);
private:
InstantiateTemplateTypeParamToInt *ConsumerInstance;
};
bool InstantiateTemplateTypeParamToIntASTVisitor::VisitClassTemplateDecl(
ClassTemplateDecl *D)
{
// only care about declarations
if (D->isThisDeclarationADefinition())
ConsumerInstance->handleOneTemplateDecl(D);
return true;
}
bool InstantiateTemplateTypeParamToIntASTVisitor::VisitFunctionTemplateDecl(
FunctionTemplateDecl *D)
{
// only care about declarations
if (D->isThisDeclarationADefinition())
ConsumerInstance->handleOneTemplateDecl(D);
return true;
}
bool
InstantiateTemplateTypeParamToIntRewriteVisitor::VisitTemplateTypeParmTypeLoc(
TemplateTypeParmTypeLoc Loc)
{
const TemplateTypeParmDecl *D = Loc.getDecl();
if (D != ConsumerInstance->TheParameter)
return true;
void *Ptr = Loc.getLocStart().getPtrEncoding();
if (ConsumerInstance->VisitedLocs.count(Ptr))
return true;
ConsumerInstance->VisitedLocs.insert(Ptr);
SourceRange Range = Loc.getSourceRange();
ConsumerInstance->TheRewriter.ReplaceText(Range, "int");
return true;
}
void InstantiateTemplateTypeParamToInt::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
CollectionVisitor = new InstantiateTemplateTypeParamToIntASTVisitor(this);
ParamRewriteVisitor = new InstantiateTemplateTypeParamToIntRewriteVisitor(this);
}
void InstantiateTemplateTypeParamToInt::HandleTranslationUnit(ASTContext &Ctx)
{
if (TransformationManager::isCLangOpt()) {
ValidInstanceNum = 0;
}
else {
CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
}
if (QueryInstanceOnly)
return;
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
TransAssert(TheParameter && "NULL TheParameter!");
TransAssert(ParamRewriteVisitor && "NULL ParamRewriteVisitor!");
ParamRewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
void InstantiateTemplateTypeParamToInt::handleOneTemplateDecl(const TemplateDecl *D)
{
// doesn't handle TypeAliasTemplateDecl
TransAssert((!dyn_cast<TypeAliasTemplateDecl>(D)) &&
"Doesn't support TypeAliasTemplateDecl!");
const TemplateDecl *CanonicalD =
dyn_cast<TemplateDecl>(D->getCanonicalDecl());
TransAssert(CanonicalD && "Invalid TemplateDecl!");
TransAssert((VisitedTemplateDecls.count(CanonicalD) == 0) &&
"Duplicate visitation to TemplateDecl!");
VisitedTemplateDecls.insert(CanonicalD);
NamedDecl *ND = D->getTemplatedDecl();
TemplateParameterSet ParamsSet;
TemplateParameterVisitor ParameterVisitor(ParamsSet);
ParameterVisitor.TraverseDecl(ND);
filterInvalidParams(D, ParamsSet);
if (ParamsSet.size() == 0)
return;
TemplateParameterList *TPList = D->getTemplateParameters();
for (TemplateParameterList::const_iterator I = TPList->begin(),
E = TPList->end(); I != E; ++I) {
const NamedDecl *ND = (*I);
if (!ParamsSet.count(ND))
continue;
ValidInstanceNum++;
if (ValidInstanceNum == TransformationCounter)
TheParameter = ND;
}
}
void InstantiateTemplateTypeParamToInt::filterInvalidParams(const TemplateDecl *D,
TemplateParameterSet &Params)
{
NamedDecl *ND = D->getTemplatedDecl();
TemplateParameterFilterVisitor Filter(Params, this);
Filter.TraverseDecl(ND);
const ClassTemplateDecl *CD = dyn_cast<ClassTemplateDecl>(D);
if (!CD)
return;
CXXRecordDecl *Def = CD->getTemplatedDecl()->getDefinition();
TransAssert(Def && "No Definition?");
if (!Def->isCompleteDefinition())
return;
for (CXXRecordDecl::base_class_const_iterator I = Def->bases_begin(),
E = Def->bases_end(); I != E; ++I) {
const CXXBaseSpecifier *BS = I;
const Type *Ty = BS->getType().getTypePtr();
const TemplateTypeParmType *ParmTy = dyn_cast<TemplateTypeParmType>(Ty);
if (!ParmTy)
continue;
const TemplateTypeParmDecl *ParmD = ParmTy->getDecl();
Params.erase(ParmD);
}
}
InstantiateTemplateTypeParamToInt::~InstantiateTemplateTypeParamToInt()
{
delete CollectionVisitor;
delete ParamRewriteVisitor;
}