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
9#include "TemplateArgumentHasher.h"
10#include "clang/AST/APValue.h"
11#include "clang/AST/Decl.h"
12#include "clang/AST/DeclCXX.h"
13#include "clang/AST/DeclTemplate.h"
14#include "clang/AST/DeclarationName.h"
15#include "clang/AST/TypeVisitor.h"
16#include "clang/Basic/IdentifierTable.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/Support/TimeProfiler.h"
19
20using namespace clang;
21
22namespace {
23
24class TemplateArgumentHasher {
25 llvm::FoldingSetNodeID ID;
26
27public:
28 TemplateArgumentHasher() = default;
29
30 void AddTemplateArgument(TemplateArgument TA);
31
32 void AddInteger(unsigned V) { ID.AddInteger(I: V); }
33
34 unsigned getValue() { return ID.computeStableHash(); }
35
36 void AddType(const Type *T);
37 void AddQualType(QualType T);
38 void AddDecl(const Decl *D);
39 void AddStructuralValue(const APValue &);
40 void AddTemplateName(TemplateName Name);
41 void AddDeclarationName(DeclarationName Name);
42 void AddIdentifierInfo(const IdentifierInfo *II);
43};
44
45void TemplateArgumentHasher::AddTemplateArgument(TemplateArgument TA) {
46 const auto Kind = TA.getKind();
47 AddInteger(V: Kind);
48
49 switch (Kind) {
50 case TemplateArgument::Null:
51 // These can occur in incomplete substitutions performed with code
52 // completion (see PartialOverloading).
53 break;
54 case TemplateArgument::Type:
55 AddQualType(T: TA.getAsType());
56 break;
57 case TemplateArgument::Declaration:
58 AddDecl(D: TA.getAsDecl());
59 break;
60 case TemplateArgument::NullPtr:
61 ID.AddPointer(Ptr: nullptr);
62 break;
63 case TemplateArgument::Integral: {
64 // There are integrals (e.g.: _BitInt(128)) that cannot be represented as
65 // any builtin integral type, so we use the hash of APSInt instead.
66 TA.getAsIntegral().Profile(ID);
67 break;
68 }
69 case TemplateArgument::StructuralValue:
70 AddQualType(T: TA.getStructuralValueType());
71 AddStructuralValue(TA.getAsStructuralValue());
72 break;
73 case TemplateArgument::Template:
74 case TemplateArgument::TemplateExpansion:
75 AddTemplateName(Name: TA.getAsTemplateOrTemplatePattern());
76 break;
77 case TemplateArgument::Expression:
78 // If we meet expression in template argument, it implies
79 // that the template is still dependent. It is meaningless
80 // to get a stable hash for the template.
81 break;
82 case TemplateArgument::Pack:
83 AddInteger(V: TA.pack_size());
84 for (auto SubTA : TA.pack_elements()) {
85 AddTemplateArgument(TA: SubTA);
86 }
87 break;
88 }
89}
90
91void TemplateArgumentHasher::AddStructuralValue(const APValue &Value) {
92 auto Kind = Value.getKind();
93 AddInteger(V: Kind);
94
95 // 'APValue::Profile' uses pointer values to make hash for LValue and
96 // MemberPointer, but they differ from one compiler invocation to another.
97 // It may be difficult to handle such cases.
98
99 if (Kind == APValue::LValue || Kind == APValue::MemberPointer) {
100 return;
101 }
102
103 Value.Profile(ID);
104}
105
106void TemplateArgumentHasher::AddTemplateName(TemplateName Name) {
107 switch (Name.getKind()) {
108 case TemplateName::Template:
109 AddDecl(D: Name.getAsTemplateDecl());
110 break;
111 case TemplateName::QualifiedTemplate: {
112 QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName();
113 AddTemplateName(Name: QTN->getUnderlyingTemplate());
114 break;
115 }
116 case TemplateName::OverloadedTemplate:
117 case TemplateName::AssumedTemplate:
118 case TemplateName::DependentTemplate:
119 case TemplateName::SubstTemplateTemplateParm:
120 case TemplateName::SubstTemplateTemplateParmPack:
121 break;
122 case TemplateName::UsingTemplate: {
123 UsingShadowDecl *USD = Name.getAsUsingShadowDecl();
124 if (USD)
125 AddDecl(D: USD->getTargetDecl());
126 break;
127 }
128 case TemplateName::DeducedTemplate:
129 AddTemplateName(Name: Name.getAsDeducedTemplateName()->getUnderlying());
130 break;
131 }
132}
133
134void TemplateArgumentHasher::AddIdentifierInfo(const IdentifierInfo *II) {
135 assert(II && "Expecting non-null pointer.");
136 ID.AddString(String: II->getName());
137}
138
139void TemplateArgumentHasher::AddDeclarationName(DeclarationName Name) {
140 if (Name.isEmpty())
141 return;
142
143 switch (Name.getNameKind()) {
144 case DeclarationName::Identifier:
145 AddIdentifierInfo(II: Name.getAsIdentifierInfo());
146 break;
147 case DeclarationName::ObjCZeroArgSelector:
148 case DeclarationName::ObjCOneArgSelector:
149 case DeclarationName::ObjCMultiArgSelector:
150 break;
151 case DeclarationName::CXXConstructorName:
152 case DeclarationName::CXXDestructorName:
153 AddQualType(T: Name.getCXXNameType());
154 break;
155 case DeclarationName::CXXOperatorName:
156 AddInteger(V: Name.getCXXOverloadedOperator());
157 break;
158 case DeclarationName::CXXLiteralOperatorName:
159 AddIdentifierInfo(II: Name.getCXXLiteralIdentifier());
160 break;
161 case DeclarationName::CXXConversionFunctionName:
162 AddQualType(T: Name.getCXXNameType());
163 break;
164 case DeclarationName::CXXUsingDirective:
165 break;
166 case DeclarationName::CXXDeductionGuideName: {
167 if (auto *Template = Name.getCXXDeductionGuideTemplate())
168 AddDecl(D: Template);
169 }
170 }
171}
172
173void TemplateArgumentHasher::AddDecl(const Decl *D) {
174 const NamedDecl *ND = dyn_cast<NamedDecl>(Val: D);
175 if (!ND) {
176 return;
177 }
178
179 AddDeclarationName(Name: ND->getDeclName());
180
181 // If this was a specialization we should take into account its template
182 // arguments. This helps to reduce collisions coming when visiting template
183 // specialization types (eg. when processing type template arguments).
184 ArrayRef<TemplateArgument> Args;
185 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Val: D))
186 Args = CTSD->getTemplateArgs().asArray();
187 else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: D))
188 Args = VTSD->getTemplateArgs().asArray();
189 else if (auto *FD = dyn_cast<FunctionDecl>(Val: D))
190 if (FD->getTemplateSpecializationArgs())
191 Args = FD->getTemplateSpecializationArgs()->asArray();
192
193 for (auto &TA : Args)
194 AddTemplateArgument(TA);
195}
196
197void TemplateArgumentHasher::AddQualType(QualType T) {
198 if (T.isNull()) {
199 return;
200 }
201 SplitQualType split = T.split();
202 AddInteger(V: split.Quals.getAsOpaqueValue());
203 AddType(T: split.Ty);
204}
205
206// Process a Type pointer. Add* methods call back into TemplateArgumentHasher
207// while Visit* methods process the relevant parts of the Type.
208class TypeVisitorHelper : public TypeVisitor<TypeVisitorHelper> {
209 typedef TypeVisitor<TypeVisitorHelper> Inherited;
210 llvm::FoldingSetNodeID &ID;
211 TemplateArgumentHasher &Hash;
212
213public:
214 TypeVisitorHelper(llvm::FoldingSetNodeID &ID, TemplateArgumentHasher &Hash)
215 : ID(ID), Hash(Hash) {}
216
217 void AddDecl(const Decl *D) {
218 if (D)
219 Hash.AddDecl(D);
220 else
221 Hash.AddInteger(V: 0);
222 }
223
224 void AddQualType(QualType T) { Hash.AddQualType(T); }
225
226 void AddType(const Type *T) {
227 if (T)
228 Hash.AddType(T);
229 else
230 Hash.AddInteger(V: 0);
231 }
232
233 void VisitQualifiers(Qualifiers Quals) {
234 Hash.AddInteger(V: Quals.getAsOpaqueValue());
235 }
236
237 void Visit(const Type *T) { Inherited::Visit(T); }
238
239 void VisitAdjustedType(const AdjustedType *T) {
240 AddQualType(T: T->getOriginalType());
241 }
242
243 void VisitDecayedType(const DecayedType *T) {
244 // getDecayedType and getPointeeType are derived from getAdjustedType
245 // and don't need to be separately processed.
246 VisitAdjustedType(T);
247 }
248
249 void VisitArrayType(const ArrayType *T) {
250 AddQualType(T: T->getElementType());
251 Hash.AddInteger(V: llvm::to_underlying(E: T->getSizeModifier()));
252 VisitQualifiers(Quals: T->getIndexTypeQualifiers());
253 }
254 void VisitConstantArrayType(const ConstantArrayType *T) {
255 T->getSize().Profile(id&: ID);
256 VisitArrayType(T);
257 }
258
259 void VisitAttributedType(const AttributedType *T) {
260 Hash.AddInteger(V: T->getAttrKind());
261 AddQualType(T: T->getModifiedType());
262 }
263
264 void VisitBuiltinType(const BuiltinType *T) { Hash.AddInteger(V: T->getKind()); }
265
266 void VisitComplexType(const ComplexType *T) {
267 AddQualType(T: T->getElementType());
268 }
269
270 void VisitDecltypeType(const DecltypeType *T) {
271 AddQualType(T: T->getUnderlyingType());
272 }
273
274 void VisitDeducedType(const DeducedType *T) {
275 AddQualType(T: T->getDeducedType());
276 }
277
278 void VisitAutoType(const AutoType *T) { VisitDeducedType(T); }
279
280 void VisitDeducedTemplateSpecializationType(
281 const DeducedTemplateSpecializationType *T) {
282 Hash.AddTemplateName(Name: T->getTemplateName());
283 VisitDeducedType(T);
284 }
285
286 void VisitFunctionType(const FunctionType *T) {
287 AddQualType(T: T->getReturnType());
288 T->getExtInfo().Profile(ID);
289 Hash.AddInteger(V: T->isConst());
290 Hash.AddInteger(V: T->isVolatile());
291 Hash.AddInteger(V: T->isRestrict());
292 }
293
294 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
295 VisitFunctionType(T);
296 }
297
298 void VisitFunctionProtoType(const FunctionProtoType *T) {
299 Hash.AddInteger(V: T->getNumParams());
300 for (auto ParamType : T->getParamTypes())
301 AddQualType(T: ParamType);
302
303 VisitFunctionType(T);
304 }
305
306 void VisitMemberPointerType(const MemberPointerType *T) {
307 AddQualType(T: T->getPointeeType());
308 AddType(T: T->getQualifier().getAsType());
309 if (auto *RD = T->getMostRecentCXXRecordDecl())
310 AddDecl(D: RD->getCanonicalDecl());
311 }
312
313 void VisitPackExpansionType(const PackExpansionType *T) {
314 AddQualType(T: T->getPattern());
315 }
316
317 void VisitParenType(const ParenType *T) { AddQualType(T: T->getInnerType()); }
318
319 void VisitPointerType(const PointerType *T) {
320 AddQualType(T: T->getPointeeType());
321 }
322
323 void VisitReferenceType(const ReferenceType *T) {
324 AddQualType(T: T->getPointeeTypeAsWritten());
325 }
326
327 void VisitLValueReferenceType(const LValueReferenceType *T) {
328 VisitReferenceType(T);
329 }
330
331 void VisitRValueReferenceType(const RValueReferenceType *T) {
332 VisitReferenceType(T);
333 }
334
335 void
336 VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
337 AddDecl(D: T->getAssociatedDecl());
338 Hash.AddTemplateArgument(TA: T->getArgumentPack());
339 }
340
341 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
342 AddDecl(D: T->getAssociatedDecl());
343 AddQualType(T: T->getReplacementType());
344 }
345
346 void VisitTagType(const TagType *T) { AddDecl(D: T->getDecl()); }
347
348 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
349 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
350
351 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
352 Hash.AddInteger(V: T->template_arguments().size());
353 for (const auto &TA : T->template_arguments()) {
354 Hash.AddTemplateArgument(TA);
355 }
356 Hash.AddTemplateName(Name: T->getTemplateName());
357 }
358
359 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
360 Hash.AddInteger(V: T->getDepth());
361 Hash.AddInteger(V: T->getIndex());
362 Hash.AddInteger(V: T->isParameterPack());
363 }
364
365 void VisitTypedefType(const TypedefType *T) { AddDecl(D: T->getDecl()); }
366
367 void VisitUnaryTransformType(const UnaryTransformType *T) {
368 AddQualType(T: T->getUnderlyingType());
369 AddQualType(T: T->getBaseType());
370 }
371
372 void VisitVectorType(const VectorType *T) {
373 AddQualType(T: T->getElementType());
374 Hash.AddInteger(V: T->getNumElements());
375 Hash.AddInteger(V: llvm::to_underlying(E: T->getVectorKind()));
376 }
377
378 void VisitExtVectorType(const ExtVectorType *T) { VisitVectorType(T); }
379};
380
381void TemplateArgumentHasher::AddType(const Type *T) {
382 assert(T && "Expecting non-null pointer.");
383 TypeVisitorHelper(ID, *this).Visit(T);
384}
385
386} // namespace
387
388unsigned clang::serialization::StableHashForTemplateArguments(
389 llvm::ArrayRef<TemplateArgument> Args) {
390 llvm::TimeTraceScope TimeScope("Stable Hash for Template Arguments");
391 TemplateArgumentHasher Hasher;
392 Hasher.AddInteger(V: Args.size());
393 for (TemplateArgument Arg : Args)
394 Hasher.AddTemplateArgument(TA: Arg);
395 return Hasher.getValue();
396}
397