forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplateArgumentHasher.cpp
411 lines (343 loc) · 11.8 KB
/
TemplateArgumentHasher.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//===- TemplateArgumentHasher.cpp - Hash Template Arguments -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "TemplateArgumentHasher.h"
#include "clang/AST/APValue.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DeclarationName.h"
#include "clang/AST/TypeVisitor.h"
#include "clang/Basic/IdentifierTable.h"
#include "llvm/ADT/FoldingSet.h"
using namespace clang;
namespace {
class TemplateArgumentHasher {
// If we bail out during the process of calculating hash values for
// template arguments for any reason. We're allowed to do it since
// TemplateArgumentHasher are only required to give the same hash value
// for the same template arguments, but not required to give different
// hash value for different template arguments.
//
// So in the worst case, it is still a valid implementation to give all
// inputs the same BailedOutValue as output.
bool BailedOut = false;
static constexpr unsigned BailedOutValue = 0x12345678;
llvm::FoldingSetNodeID ID;
public:
TemplateArgumentHasher() = default;
void AddTemplateArgument(TemplateArgument TA);
void AddInteger(unsigned V) { ID.AddInteger(V); }
unsigned getValue() {
if (BailedOut)
return BailedOutValue;
return ID.computeStableHash();
}
void setBailedOut() { BailedOut = true; }
void AddType(const Type *T);
void AddQualType(QualType T);
void AddDecl(const Decl *D);
void AddStructuralValue(const APValue &);
void AddTemplateName(TemplateName Name);
void AddDeclarationName(DeclarationName Name);
void AddIdentifierInfo(const IdentifierInfo *II);
};
void TemplateArgumentHasher::AddTemplateArgument(TemplateArgument TA) {
const auto Kind = TA.getKind();
AddInteger(Kind);
switch (Kind) {
case TemplateArgument::Null:
llvm_unreachable("Expected valid TemplateArgument");
case TemplateArgument::Type:
AddQualType(TA.getAsType());
break;
case TemplateArgument::Declaration:
AddDecl(TA.getAsDecl());
break;
case TemplateArgument::NullPtr:
ID.AddPointer(nullptr);
break;
case TemplateArgument::Integral: {
// There are integrals (e.g.: _BitInt(128)) that cannot be represented as
// any builtin integral type, so we use the hash of APSInt instead.
TA.getAsIntegral().Profile(ID);
break;
}
case TemplateArgument::StructuralValue:
AddQualType(TA.getStructuralValueType());
AddStructuralValue(TA.getAsStructuralValue());
break;
case TemplateArgument::Template:
case TemplateArgument::TemplateExpansion:
AddTemplateName(TA.getAsTemplateOrTemplatePattern());
break;
case TemplateArgument::Expression:
// If we meet expression in template argument, it implies
// that the template is still dependent. It is meaningless
// to get a stable hash for the template. Bail out simply.
BailedOut = true;
break;
case TemplateArgument::Pack:
AddInteger(TA.pack_size());
for (auto SubTA : TA.pack_elements()) {
AddTemplateArgument(SubTA);
}
break;
}
}
void TemplateArgumentHasher::AddStructuralValue(const APValue &Value) {
auto Kind = Value.getKind();
AddInteger(Kind);
// 'APValue::Profile' uses pointer values to make hash for LValue and
// MemberPointer, but they differ from one compiler invocation to another.
// It may be difficult to handle such cases. Bail out simply.
if (Kind == APValue::LValue || Kind == APValue::MemberPointer) {
BailedOut = true;
return;
}
Value.Profile(ID);
}
void TemplateArgumentHasher::AddTemplateName(TemplateName Name) {
switch (Name.getKind()) {
case TemplateName::Template:
AddDecl(Name.getAsTemplateDecl());
break;
case TemplateName::QualifiedTemplate: {
QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName();
AddTemplateName(QTN->getUnderlyingTemplate());
break;
}
case TemplateName::OverloadedTemplate:
case TemplateName::AssumedTemplate:
case TemplateName::DependentTemplate:
case TemplateName::SubstTemplateTemplateParm:
case TemplateName::SubstTemplateTemplateParmPack:
BailedOut = true;
break;
case TemplateName::UsingTemplate: {
UsingShadowDecl *USD = Name.getAsUsingShadowDecl();
if (USD)
AddDecl(USD->getTargetDecl());
else
BailedOut = true;
break;
}
case TemplateName::DeducedTemplate:
AddTemplateName(Name.getAsDeducedTemplateName()->getUnderlying());
break;
}
}
void TemplateArgumentHasher::AddIdentifierInfo(const IdentifierInfo *II) {
assert(II && "Expecting non-null pointer.");
ID.AddString(II->getName());
}
void TemplateArgumentHasher::AddDeclarationName(DeclarationName Name) {
if (Name.isEmpty())
return;
switch (Name.getNameKind()) {
case DeclarationName::Identifier:
AddIdentifierInfo(Name.getAsIdentifierInfo());
break;
case DeclarationName::ObjCZeroArgSelector:
case DeclarationName::ObjCOneArgSelector:
case DeclarationName::ObjCMultiArgSelector:
BailedOut = true;
break;
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
AddQualType(Name.getCXXNameType());
break;
case DeclarationName::CXXOperatorName:
AddInteger(Name.getCXXOverloadedOperator());
break;
case DeclarationName::CXXLiteralOperatorName:
AddIdentifierInfo(Name.getCXXLiteralIdentifier());
break;
case DeclarationName::CXXConversionFunctionName:
AddQualType(Name.getCXXNameType());
break;
case DeclarationName::CXXUsingDirective:
break;
case DeclarationName::CXXDeductionGuideName: {
if (auto *Template = Name.getCXXDeductionGuideTemplate())
AddDecl(Template);
}
}
}
void TemplateArgumentHasher::AddDecl(const Decl *D) {
const NamedDecl *ND = dyn_cast<NamedDecl>(D);
if (!ND) {
BailedOut = true;
return;
}
AddDeclarationName(ND->getDeclName());
}
void TemplateArgumentHasher::AddQualType(QualType T) {
if (T.isNull()) {
BailedOut = true;
return;
}
SplitQualType split = T.split();
AddInteger(split.Quals.getAsOpaqueValue());
AddType(split.Ty);
}
// Process a Type pointer. Add* methods call back into TemplateArgumentHasher
// while Visit* methods process the relevant parts of the Type.
// Any unhandled type will make the hash computation bail out.
class TypeVisitorHelper : public TypeVisitor<TypeVisitorHelper> {
typedef TypeVisitor<TypeVisitorHelper> Inherited;
llvm::FoldingSetNodeID &ID;
TemplateArgumentHasher &Hash;
public:
TypeVisitorHelper(llvm::FoldingSetNodeID &ID, TemplateArgumentHasher &Hash)
: ID(ID), Hash(Hash) {}
void AddDecl(const Decl *D) {
if (D)
Hash.AddDecl(D);
else
Hash.AddInteger(0);
}
void AddQualType(QualType T) { Hash.AddQualType(T); }
void AddType(const Type *T) {
if (T)
Hash.AddType(T);
else
Hash.AddInteger(0);
}
void VisitQualifiers(Qualifiers Quals) {
Hash.AddInteger(Quals.getAsOpaqueValue());
}
void Visit(const Type *T) { Inherited::Visit(T); }
// Unhandled types. Bail out simply.
void VisitType(const Type *T) { Hash.setBailedOut(); }
void VisitAdjustedType(const AdjustedType *T) {
AddQualType(T->getOriginalType());
}
void VisitDecayedType(const DecayedType *T) {
// getDecayedType and getPointeeType are derived from getAdjustedType
// and don't need to be separately processed.
VisitAdjustedType(T);
}
void VisitArrayType(const ArrayType *T) {
AddQualType(T->getElementType());
Hash.AddInteger(llvm::to_underlying(T->getSizeModifier()));
VisitQualifiers(T->getIndexTypeQualifiers());
}
void VisitConstantArrayType(const ConstantArrayType *T) {
T->getSize().Profile(ID);
VisitArrayType(T);
}
void VisitAttributedType(const AttributedType *T) {
Hash.AddInteger(T->getAttrKind());
AddQualType(T->getModifiedType());
}
void VisitBuiltinType(const BuiltinType *T) { Hash.AddInteger(T->getKind()); }
void VisitComplexType(const ComplexType *T) {
AddQualType(T->getElementType());
}
void VisitDecltypeType(const DecltypeType *T) {
AddQualType(T->getUnderlyingType());
}
void VisitDeducedType(const DeducedType *T) {
AddQualType(T->getDeducedType());
}
void VisitAutoType(const AutoType *T) { VisitDeducedType(T); }
void VisitDeducedTemplateSpecializationType(
const DeducedTemplateSpecializationType *T) {
Hash.AddTemplateName(T->getTemplateName());
VisitDeducedType(T);
}
void VisitFunctionType(const FunctionType *T) {
AddQualType(T->getReturnType());
T->getExtInfo().Profile(ID);
Hash.AddInteger(T->isConst());
Hash.AddInteger(T->isVolatile());
Hash.AddInteger(T->isRestrict());
}
void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
VisitFunctionType(T);
}
void VisitFunctionProtoType(const FunctionProtoType *T) {
Hash.AddInteger(T->getNumParams());
for (auto ParamType : T->getParamTypes())
AddQualType(ParamType);
VisitFunctionType(T);
}
void VisitMemberPointerType(const MemberPointerType *T) {
AddQualType(T->getPointeeType());
AddType(T->getQualifier()->getAsType());
if (auto *RD = T->getMostRecentCXXRecordDecl())
AddDecl(RD->getCanonicalDecl());
}
void VisitPackExpansionType(const PackExpansionType *T) {
AddQualType(T->getPattern());
}
void VisitParenType(const ParenType *T) { AddQualType(T->getInnerType()); }
void VisitPointerType(const PointerType *T) {
AddQualType(T->getPointeeType());
}
void VisitReferenceType(const ReferenceType *T) {
AddQualType(T->getPointeeTypeAsWritten());
}
void VisitLValueReferenceType(const LValueReferenceType *T) {
VisitReferenceType(T);
}
void VisitRValueReferenceType(const RValueReferenceType *T) {
VisitReferenceType(T);
}
void
VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
AddDecl(T->getAssociatedDecl());
Hash.AddTemplateArgument(T->getArgumentPack());
}
void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
AddDecl(T->getAssociatedDecl());
AddQualType(T->getReplacementType());
}
void VisitTagType(const TagType *T) { AddDecl(T->getDecl()); }
void VisitRecordType(const RecordType *T) { VisitTagType(T); }
void VisitEnumType(const EnumType *T) { VisitTagType(T); }
void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
Hash.AddInteger(T->template_arguments().size());
for (const auto &TA : T->template_arguments()) {
Hash.AddTemplateArgument(TA);
}
Hash.AddTemplateName(T->getTemplateName());
}
void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Hash.AddInteger(T->getDepth());
Hash.AddInteger(T->getIndex());
Hash.AddInteger(T->isParameterPack());
}
void VisitTypedefType(const TypedefType *T) { AddDecl(T->getDecl()); }
void VisitElaboratedType(const ElaboratedType *T) {
AddQualType(T->getNamedType());
}
void VisitUnaryTransformType(const UnaryTransformType *T) {
AddQualType(T->getUnderlyingType());
AddQualType(T->getBaseType());
}
void VisitVectorType(const VectorType *T) {
AddQualType(T->getElementType());
Hash.AddInteger(T->getNumElements());
Hash.AddInteger(llvm::to_underlying(T->getVectorKind()));
}
void VisitExtVectorType(const ExtVectorType *T) { VisitVectorType(T); }
};
void TemplateArgumentHasher::AddType(const Type *T) {
assert(T && "Expecting non-null pointer.");
TypeVisitorHelper(ID, *this).Visit(T);
}
} // namespace
unsigned clang::serialization::StableHashForTemplateArguments(
llvm::ArrayRef<TemplateArgument> Args) {
TemplateArgumentHasher Hasher;
Hasher.AddInteger(Args.size());
for (TemplateArgument Arg : Args)
Hasher.AddTemplateArgument(Arg);
return Hasher.getValue();
}