clang 22.0.0git
TemplateArgumentHasher.cpp
Go to the documentation of this file.
1//===- TemplateArgumentHasher.cpp - Hash Template Arguments -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/APValue.h"
11#include "clang/AST/Decl.h"
12#include "clang/AST/DeclCXX.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/Support/TimeProfiler.h"
19
20using namespace clang;
21
22namespace {
23
24class TemplateArgumentHasher {
25 // If we bail out during the process of calculating hash values for
26 // template arguments for any reason. We're allowed to do it since
27 // TemplateArgumentHasher are only required to give the same hash value
28 // for the same template arguments, but not required to give different
29 // hash value for different template arguments.
30 //
31 // So in the worst case, it is still a valid implementation to give all
32 // inputs the same BailedOutValue as output.
33 bool BailedOut = false;
34 static constexpr unsigned BailedOutValue = 0x12345678;
35
36 llvm::FoldingSetNodeID ID;
37
38public:
39 TemplateArgumentHasher() = default;
40
41 void AddTemplateArgument(TemplateArgument TA);
42
43 void AddInteger(unsigned V) { ID.AddInteger(V); }
44
45 unsigned getValue() {
46 if (BailedOut)
47 return BailedOutValue;
48
49 return ID.computeStableHash();
50 }
51
52 void setBailedOut() { BailedOut = true; }
53
54 void AddType(const Type *T);
55 void AddQualType(QualType T);
56 void AddDecl(const Decl *D);
57 void AddStructuralValue(const APValue &);
58 void AddTemplateName(TemplateName Name);
59 void AddDeclarationName(DeclarationName Name);
60 void AddIdentifierInfo(const IdentifierInfo *II);
61};
62
63void TemplateArgumentHasher::AddTemplateArgument(TemplateArgument TA) {
64 const auto Kind = TA.getKind();
65 AddInteger(Kind);
66
67 switch (Kind) {
69 // These can occur in incomplete substitutions performed with code
70 // completion (see PartialOverloading).
71 break;
73 AddQualType(TA.getAsType());
74 break;
76 AddDecl(TA.getAsDecl());
77 break;
79 ID.AddPointer(nullptr);
80 break;
82 // There are integrals (e.g.: _BitInt(128)) that cannot be represented as
83 // any builtin integral type, so we use the hash of APSInt instead.
84 TA.getAsIntegral().Profile(ID);
85 break;
86 }
88 AddQualType(TA.getStructuralValueType());
89 AddStructuralValue(TA.getAsStructuralValue());
90 break;
93 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
94 break;
96 // If we meet expression in template argument, it implies
97 // that the template is still dependent. It is meaningless
98 // to get a stable hash for the template. Bail out simply.
99 BailedOut = true;
100 break;
102 AddInteger(TA.pack_size());
103 for (auto SubTA : TA.pack_elements()) {
104 AddTemplateArgument(SubTA);
105 }
106 break;
107 }
108}
109
110void TemplateArgumentHasher::AddStructuralValue(const APValue &Value) {
111 auto Kind = Value.getKind();
112 AddInteger(Kind);
113
114 // 'APValue::Profile' uses pointer values to make hash for LValue and
115 // MemberPointer, but they differ from one compiler invocation to another.
116 // It may be difficult to handle such cases. Bail out simply.
117
118 if (Kind == APValue::LValue || Kind == APValue::MemberPointer) {
119 BailedOut = true;
120 return;
121 }
122
123 Value.Profile(ID);
124}
125
126void TemplateArgumentHasher::AddTemplateName(TemplateName Name) {
127 switch (Name.getKind()) {
129 AddDecl(Name.getAsTemplateDecl());
130 break;
132 QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName();
133 AddTemplateName(QTN->getUnderlyingTemplate());
134 break;
135 }
141 BailedOut = true;
142 break;
144 UsingShadowDecl *USD = Name.getAsUsingShadowDecl();
145 if (USD)
146 AddDecl(USD->getTargetDecl());
147 else
148 BailedOut = true;
149 break;
150 }
152 AddTemplateName(Name.getAsDeducedTemplateName()->getUnderlying());
153 break;
154 }
155}
156
157void TemplateArgumentHasher::AddIdentifierInfo(const IdentifierInfo *II) {
158 assert(II && "Expecting non-null pointer.");
159 ID.AddString(II->getName());
160}
161
162void TemplateArgumentHasher::AddDeclarationName(DeclarationName Name) {
163 if (Name.isEmpty())
164 return;
165
166 switch (Name.getNameKind()) {
168 AddIdentifierInfo(Name.getAsIdentifierInfo());
169 break;
173 BailedOut = true;
174 break;
177 AddQualType(Name.getCXXNameType());
178 break;
180 AddInteger(Name.getCXXOverloadedOperator());
181 break;
183 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
184 break;
186 AddQualType(Name.getCXXNameType());
187 break;
189 break;
191 if (auto *Template = Name.getCXXDeductionGuideTemplate())
192 AddDecl(Template);
193 }
194 }
195}
196
197void TemplateArgumentHasher::AddDecl(const Decl *D) {
198 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
199 if (!ND) {
200 BailedOut = true;
201 return;
202 }
203
204 AddDeclarationName(ND->getDeclName());
205}
206
207void TemplateArgumentHasher::AddQualType(QualType T) {
208 if (T.isNull()) {
209 BailedOut = true;
210 return;
211 }
212 SplitQualType split = T.split();
213 AddInteger(split.Quals.getAsOpaqueValue());
214 AddType(split.Ty);
215}
216
217// Process a Type pointer. Add* methods call back into TemplateArgumentHasher
218// while Visit* methods process the relevant parts of the Type.
219// Any unhandled type will make the hash computation bail out.
220class TypeVisitorHelper : public TypeVisitor<TypeVisitorHelper> {
221 typedef TypeVisitor<TypeVisitorHelper> Inherited;
222 llvm::FoldingSetNodeID &ID;
223 TemplateArgumentHasher &Hash;
224
225public:
226 TypeVisitorHelper(llvm::FoldingSetNodeID &ID, TemplateArgumentHasher &Hash)
227 : ID(ID), Hash(Hash) {}
228
229 void AddDecl(const Decl *D) {
230 if (D)
231 Hash.AddDecl(D);
232 else
233 Hash.AddInteger(0);
234 }
235
236 void AddQualType(QualType T) { Hash.AddQualType(T); }
237
238 void AddType(const Type *T) {
239 if (T)
240 Hash.AddType(T);
241 else
242 Hash.AddInteger(0);
243 }
244
245 void VisitQualifiers(Qualifiers Quals) {
246 Hash.AddInteger(Quals.getAsOpaqueValue());
247 }
248
249 void Visit(const Type *T) { Inherited::Visit(T); }
250
251 // Unhandled types. Bail out simply.
252 void VisitType(const Type *T) { Hash.setBailedOut(); }
253
254 void VisitAdjustedType(const AdjustedType *T) {
255 AddQualType(T->getOriginalType());
256 }
257
258 void VisitDecayedType(const DecayedType *T) {
259 // getDecayedType and getPointeeType are derived from getAdjustedType
260 // and don't need to be separately processed.
261 VisitAdjustedType(T);
262 }
263
264 void VisitArrayType(const ArrayType *T) {
265 AddQualType(T->getElementType());
266 Hash.AddInteger(llvm::to_underlying(T->getSizeModifier()));
267 VisitQualifiers(T->getIndexTypeQualifiers());
268 }
269 void VisitConstantArrayType(const ConstantArrayType *T) {
270 T->getSize().Profile(ID);
271 VisitArrayType(T);
272 }
273
274 void VisitAttributedType(const AttributedType *T) {
275 Hash.AddInteger(T->getAttrKind());
276 AddQualType(T->getModifiedType());
277 }
278
279 void VisitBuiltinType(const BuiltinType *T) { Hash.AddInteger(T->getKind()); }
280
281 void VisitComplexType(const ComplexType *T) {
282 AddQualType(T->getElementType());
283 }
284
285 void VisitDecltypeType(const DecltypeType *T) {
286 AddQualType(T->getUnderlyingType());
287 }
288
289 void VisitDeducedType(const DeducedType *T) {
290 AddQualType(T->getDeducedType());
291 }
292
293 void VisitAutoType(const AutoType *T) { VisitDeducedType(T); }
294
295 void VisitDeducedTemplateSpecializationType(
297 Hash.AddTemplateName(T->getTemplateName());
298 VisitDeducedType(T);
299 }
300
301 void VisitFunctionType(const FunctionType *T) {
302 AddQualType(T->getReturnType());
303 T->getExtInfo().Profile(ID);
304 Hash.AddInteger(T->isConst());
305 Hash.AddInteger(T->isVolatile());
306 Hash.AddInteger(T->isRestrict());
307 }
308
309 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
310 VisitFunctionType(T);
311 }
312
313 void VisitFunctionProtoType(const FunctionProtoType *T) {
314 Hash.AddInteger(T->getNumParams());
315 for (auto ParamType : T->getParamTypes())
316 AddQualType(ParamType);
317
318 VisitFunctionType(T);
319 }
320
321 void VisitMemberPointerType(const MemberPointerType *T) {
322 AddQualType(T->getPointeeType());
323 AddType(T->getQualifier().getAsType());
324 if (auto *RD = T->getMostRecentCXXRecordDecl())
325 AddDecl(RD->getCanonicalDecl());
326 }
327
328 void VisitPackExpansionType(const PackExpansionType *T) {
329 AddQualType(T->getPattern());
330 }
331
332 void VisitParenType(const ParenType *T) { AddQualType(T->getInnerType()); }
333
334 void VisitPointerType(const PointerType *T) {
335 AddQualType(T->getPointeeType());
336 }
337
338 void VisitReferenceType(const ReferenceType *T) {
339 AddQualType(T->getPointeeTypeAsWritten());
340 }
341
342 void VisitLValueReferenceType(const LValueReferenceType *T) {
343 VisitReferenceType(T);
344 }
345
346 void VisitRValueReferenceType(const RValueReferenceType *T) {
347 VisitReferenceType(T);
348 }
349
350 void
351 VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
352 AddDecl(T->getAssociatedDecl());
353 Hash.AddTemplateArgument(T->getArgumentPack());
354 }
355
356 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
357 AddDecl(T->getAssociatedDecl());
358 AddQualType(T->getReplacementType());
359 }
360
361 void VisitTagType(const TagType *T) { AddDecl(T->getOriginalDecl()); }
362
363 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
364 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
365
366 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
367 Hash.AddInteger(T->template_arguments().size());
368 for (const auto &TA : T->template_arguments()) {
369 Hash.AddTemplateArgument(TA);
370 }
371 Hash.AddTemplateName(T->getTemplateName());
372 }
373
374 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
375 Hash.AddInteger(T->getDepth());
376 Hash.AddInteger(T->getIndex());
377 Hash.AddInteger(T->isParameterPack());
378 }
379
380 void VisitTypedefType(const TypedefType *T) { AddDecl(T->getDecl()); }
381
382 void VisitUnaryTransformType(const UnaryTransformType *T) {
383 AddQualType(T->getUnderlyingType());
384 AddQualType(T->getBaseType());
385 }
386
387 void VisitVectorType(const VectorType *T) {
388 AddQualType(T->getElementType());
389 Hash.AddInteger(T->getNumElements());
390 Hash.AddInteger(llvm::to_underlying(T->getVectorKind()));
391 }
392
393 void VisitExtVectorType(const ExtVectorType *T) { VisitVectorType(T); }
394};
395
396void TemplateArgumentHasher::AddType(const Type *T) {
397 assert(T && "Expecting non-null pointer.");
398 TypeVisitorHelper(ID, *this).Visit(T);
399}
400
401} // namespace
402
405 llvm::TimeTraceScope TimeScope("Stable Hash for Template Arguments");
406 TemplateArgumentHasher Hasher;
407 Hasher.AddInteger(Args.size());
408 for (TemplateArgument Arg : Args)
409 Hasher.AddTemplateArgument(Arg);
410 return Hasher.getValue();
411}
#define V(N, I)
Definition: ASTContext.h:3597
const Decl * D
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Represents a type which was implicitly adjusted by the semantic engine for arbitrary reasons.
Definition: TypeBase.h:3507
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: TypeBase.h:3738
An attributed type is a type to which a type attribute has been applied.
Definition: TypeBase.h:6585
Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained by a type-constraint.
Definition: TypeBase.h:7180
This class is used for builtin types like 'int'.
Definition: TypeBase.h:3182
Complex values, per C99 6.2.5p11.
Definition: TypeBase.h:3293
Represents the canonical version of C arrays with a specified constant size.
Definition: TypeBase.h:3776
Represents a pointer type decayed from an array or function type.
Definition: TypeBase.h:3541
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
The name of a declaration.
Represents the type decltype(expr) (C++11).
Definition: TypeBase.h:6270
Represents a C++17 deduced template specialization type.
Definition: TypeBase.h:7228
Common base class for placeholders for types that get replaced by placeholder type deduction: C++11 a...
Definition: TypeBase.h:7146
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: TypeBase.h:6522
ExtVectorType - Extended vector type.
Definition: TypeBase.h:4283
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition: TypeBase.h:4860
Represents a prototype with parameter type info, e.g.
Definition: TypeBase.h:5282
unsigned getNumParams() const
Definition: TypeBase.h:5560
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx)
Definition: Type.cpp:3991
ArrayRef< QualType > getParamTypes() const
Definition: TypeBase.h:5567
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: TypeBase.h:4705
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: TypeBase.h:4478
ExtInfo getExtInfo() const
Definition: TypeBase.h:4834
bool isConst() const
Definition: TypeBase.h:4840
bool isRestrict() const
Definition: TypeBase.h:4842
QualType getReturnType() const
Definition: TypeBase.h:4818
bool isVolatile() const
Definition: TypeBase.h:4841
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
An lvalue reference type, per C++11 [dcl.ref].
Definition: TypeBase.h:3633
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: TypeBase.h:3669
This represents a decl that may have a name.
Definition: Decl.h:273
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:339
Represents a pack expansion of types.
Definition: TypeBase.h:7524
Sugar for parentheses used when specifying types.
Definition: TypeBase.h:3320
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: TypeBase.h:3346
A (possibly-)qualified type.
Definition: TypeBase.h:937
Represents a template name as written in source code.
Definition: TemplateName.h:504
TemplateName getUnderlyingTemplate() const
Return the underlying template name.
Definition: TemplateName.h:539
The collection of all-type qualifiers we support.
Definition: TypeBase.h:331
uint64_t getAsOpaqueValue() const
Definition: TypeBase.h:455
An rvalue reference type, per C++11 [dcl.ref].
Definition: TypeBase.h:3651
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: TypeBase.h:6502
Base for LValueReferenceType and RValueReferenceType.
Definition: TypeBase.h:3589
Represents the result of substituting a set of types for a template type parameter pack.
Definition: TypeBase.h:7091
Represents the result of substituting a type for a template type parameter.
Definition: TypeBase.h:6972
Represents a template argument.
Definition: TemplateBase.h:61
QualType getStructuralValueType() const
Get the type of a StructuralValue.
Definition: TemplateBase.h:402
QualType getAsType() const
Retrieve the type for a type template argument.
Definition: TemplateBase.h:322
llvm::APSInt getAsIntegral() const
Retrieve the template argument as an integral value.
Definition: TemplateBase.h:366
unsigned pack_size() const
The number of template arguments in the given template argument pack.
Definition: TemplateBase.h:446
ValueDecl * getAsDecl() const
Retrieve the declaration for a declaration non-type template argument.
Definition: TemplateBase.h:329
ArrayRef< TemplateArgument > pack_elements() const
Iterator range referencing all of the elements of a template argument pack.
Definition: TemplateBase.h:440
@ Declaration
The template argument is a declaration that was provided for a pointer, reference,...
Definition: TemplateBase.h:74
@ Template
The template argument is a template name that was provided for a template template parameter.
Definition: TemplateBase.h:93
@ StructuralValue
The template argument is a non-type template argument that can't be represented by the special-case D...
Definition: TemplateBase.h:89
@ Pack
The template argument is actually a parameter pack.
Definition: TemplateBase.h:107
@ TemplateExpansion
The template argument is a pack expansion of a template name that was provided for a template templat...
Definition: TemplateBase.h:97
@ NullPtr
The template argument is a null pointer or null pointer to member that was provided for a non-type te...
Definition: TemplateBase.h:78
@ Type
The template argument is a type.
Definition: TemplateBase.h:70
@ Null
Represents an empty template argument, e.g., one that has not been deduced.
Definition: TemplateBase.h:67
@ Integral
The template argument is an integral value stored in an llvm::APSInt that was provided for an integra...
Definition: TemplateBase.h:82
@ Expression
The template argument is an expression, and we've not resolved it to one of the other forms yet,...
Definition: TemplateBase.h:103
ArgKind getKind() const
Return the kind of stored template argument.
Definition: TemplateBase.h:296
TemplateName getAsTemplateOrTemplatePattern() const
Retrieve the template argument as a template name; if the argument is a pack expansion,...
Definition: TemplateBase.h:353
const APValue & getAsStructuralValue() const
Get the value of a StructuralValue.
Definition: TemplateBase.h:399
Represents a C++ template name within the type system.
Definition: TemplateName.h:222
@ UsingTemplate
A template name that refers to a template declaration found through a specific using shadow declarati...
Definition: TemplateName.h:267
@ OverloadedTemplate
A set of overloaded template declarations.
Definition: TemplateName.h:242
@ Template
A single template declaration.
Definition: TemplateName.h:239
@ DependentTemplate
A dependent template name that has not been resolved to a template (or set of templates).
Definition: TemplateName.h:254
@ SubstTemplateTemplateParm
A template template parameter that has been substituted for some other template name.
Definition: TemplateName.h:258
@ SubstTemplateTemplateParmPack
A template template parameter pack that has been substituted for a template template argument pack,...
Definition: TemplateName.h:263
@ DeducedTemplate
A template name that refers to another TemplateName with deduced default arguments.
Definition: TemplateName.h:271
@ QualifiedTemplate
A qualified template name, where the qualification is kept to describe the source code as written.
Definition: TemplateName.h:250
@ AssumedTemplate
An unqualified-id that has been assumed to name a function template that will be found by ADL.
Definition: TemplateName.h:246
Represents a type template specialization; the template must be a class template, a type alias templa...
Definition: TypeBase.h:7290
An operation on a type.
Definition: TypeVisitor.h:64
RetTy Visit(const Type *T)
Performs the operation associated with this visitor object.
Definition: TypeVisitor.h:68
RetTy VisitType(const Type *)
Method called if ImpClass doesn't provide specific handler for some type class.
Definition: TypeVisitor.h:87
The base class of the type hierarchy.
Definition: TypeBase.h:1833
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:752
A unary type transform, which is a type constructed from another.
Definition: TypeBase.h:6375
Represents a shadow declaration implicitly introduced into a scope by a (resolved) using-declaration ...
Definition: DeclCXX.h:3393
NamedDecl * getTargetDecl() const
Gets the underlying declaration which has been brought into the local scope.
Definition: DeclCXX.h:3457
Kind getKind() const
Definition: Value.h:137
Represents a GCC generic vector type.
Definition: TypeBase.h:4191
unsigned StableHashForTemplateArguments(llvm::ArrayRef< TemplateArgument > Args)
Calculate a stable hash value for template arguments.
The JSON file list parser is used to communicate input to InstallAPI.
@ Template
We are parsing a template declaration.
const FunctionProtoType * T
A std::pair-like structure for storing a qualified type split into its local qualifiers and its local...
Definition: TypeBase.h:870
const Type * Ty
The locally-unqualified type.
Definition: TypeBase.h:872
Qualifiers Quals
The local qualifiers.
Definition: TypeBase.h:875