blob: 0e44a12257cb0fee8972c693ecd554b7b0ee44d2 [file] [log] [blame]
Richard Trieue7f7ed22017-02-22 01:11:25 +00001//===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// This file implements the ODRHash class, which calculates a hash based
12/// on AST nodes, which is stable across different runs.
13///
14//===----------------------------------------------------------------------===//
15
16#include "clang/AST/ODRHash.h"
17
18#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/NestedNameSpecifier.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/TypeVisitor.h"
22
23using namespace clang;
24
Richard Trieu639d7b62017-02-22 22:22:42 +000025void ODRHash::AddStmt(const Stmt *S) {
26 assert(S && "Expecting non-null pointer.");
27 S->ProcessODRHash(ID, *this);
28}
Richard Trieud0786092017-02-23 00:23:01 +000029
30void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
31 assert(II && "Expecting non-null pointer.");
32 ID.AddString(II->getName());
33}
34
Richard Trieu8459ddf2017-02-24 02:59:12 +000035void ODRHash::AddDeclarationName(DeclarationName Name) {
36 AddBoolean(Name.isEmpty());
37 if (Name.isEmpty())
38 return;
39
40 auto Kind = Name.getNameKind();
41 ID.AddInteger(Kind);
42 switch (Kind) {
43 case DeclarationName::Identifier:
44 AddIdentifierInfo(Name.getAsIdentifierInfo());
45 break;
46 case DeclarationName::ObjCZeroArgSelector:
47 case DeclarationName::ObjCOneArgSelector:
48 case DeclarationName::ObjCMultiArgSelector: {
49 Selector S = Name.getObjCSelector();
50 AddBoolean(S.isNull());
51 AddBoolean(S.isKeywordSelector());
52 AddBoolean(S.isUnarySelector());
53 unsigned NumArgs = S.getNumArgs();
54 for (unsigned i = 0; i < NumArgs; ++i) {
55 AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
56 }
57 break;
58 }
59 case DeclarationName::CXXConstructorName:
60 case DeclarationName::CXXDestructorName:
61 AddQualType(Name.getCXXNameType());
62 break;
63 case DeclarationName::CXXOperatorName:
64 ID.AddInteger(Name.getCXXOverloadedOperator());
65 break;
66 case DeclarationName::CXXLiteralOperatorName:
67 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
68 break;
69 case DeclarationName::CXXConversionFunctionName:
70 AddQualType(Name.getCXXNameType());
71 break;
72 case DeclarationName::CXXUsingDirective:
73 break;
74 case DeclarationName::CXXDeductionGuideName: {
75 auto *Template = Name.getCXXDeductionGuideTemplate();
76 AddBoolean(Template);
77 if (Template) {
78 AddDecl(Template);
79 }
80 }
81 }
82}
83
Richard Trieuce81b192017-05-17 03:23:35 +000084void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieuf21b8032017-06-09 20:11:51 +000085 // Unlike the other pointer handling functions, allow null pointers here.
86 if (!NNS) {
87 AddBoolean(false);
88 return;
Richard Trieuce81b192017-05-17 03:23:35 +000089 }
Richard Trieuf21b8032017-06-09 20:11:51 +000090
91 // Skip inlined namespaces.
Richard Trieuce81b192017-05-17 03:23:35 +000092 auto Kind = NNS->getKind();
Richard Trieuf21b8032017-06-09 20:11:51 +000093 if (Kind == NestedNameSpecifier::Namespace) {
94 if (NNS->getAsNamespace()->isInline()) {
95 return AddNestedNameSpecifier(NNS->getPrefix());
96 }
97 }
98
99 AddBoolean(true);
100
101 // Process prefix
102 AddNestedNameSpecifier(NNS->getPrefix());
103
Richard Trieuce81b192017-05-17 03:23:35 +0000104 ID.AddInteger(Kind);
105 switch (Kind) {
106 case NestedNameSpecifier::Identifier:
107 AddIdentifierInfo(NNS->getAsIdentifier());
108 break;
109 case NestedNameSpecifier::Namespace:
110 AddDecl(NNS->getAsNamespace());
111 break;
112 case NestedNameSpecifier::NamespaceAlias:
113 AddDecl(NNS->getAsNamespaceAlias());
114 break;
115 case NestedNameSpecifier::TypeSpec:
116 case NestedNameSpecifier::TypeSpecWithTemplate:
117 AddType(NNS->getAsType());
118 break;
119 case NestedNameSpecifier::Global:
120 case NestedNameSpecifier::Super:
121 break;
122 }
123}
124
Richard Trieu96b49622017-05-31 00:31:58 +0000125void ODRHash::AddTemplateName(TemplateName Name) {
126 auto Kind = Name.getKind();
127 ID.AddInteger(Kind);
128
129 switch (Kind) {
130 case TemplateName::Template:
131 AddDecl(Name.getAsTemplateDecl());
132 break;
133 // TODO: Support these cases.
134 case TemplateName::OverloadedTemplate:
135 case TemplateName::QualifiedTemplate:
136 case TemplateName::DependentTemplate:
137 case TemplateName::SubstTemplateTemplateParm:
138 case TemplateName::SubstTemplateTemplateParmPack:
139 break;
140 }
141}
142
Richard Trieu3b261bb72017-06-13 22:21:18 +0000143void ODRHash::AddTemplateArgument(TemplateArgument TA) {
144 const auto Kind = TA.getKind();
145 ID.AddInteger(Kind);
Richard Trieu1dcb4052017-06-14 01:28:00 +0000146
147 switch (Kind) {
148 case TemplateArgument::Null:
Richard Trieu8844c522017-06-30 22:40:33 +0000149 llvm_unreachable("Expected valid TemplateArgument");
Richard Trieu1dcb4052017-06-14 01:28:00 +0000150 case TemplateArgument::Type:
Richard Trieu8844c522017-06-30 22:40:33 +0000151 AddQualType(TA.getAsType());
152 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000153 case TemplateArgument::Declaration:
154 case TemplateArgument::NullPtr:
155 case TemplateArgument::Integral:
Richard Trieuee132d62017-06-14 03:17:26 +0000156 break;
Richard Trieu1dcb4052017-06-14 01:28:00 +0000157 case TemplateArgument::Template:
158 case TemplateArgument::TemplateExpansion:
Richard Trieuee132d62017-06-14 03:17:26 +0000159 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
Richard Trieu1dcb4052017-06-14 01:28:00 +0000160 break;
161 case TemplateArgument::Expression:
162 AddStmt(TA.getAsExpr());
163 break;
164 case TemplateArgument::Pack:
Richard Trieud9201d02017-06-15 01:35:06 +0000165 ID.AddInteger(TA.pack_size());
166 for (auto SubTA : TA.pack_elements()) {
167 AddTemplateArgument(SubTA);
168 }
Richard Trieu1dcb4052017-06-14 01:28:00 +0000169 break;
170 }
Richard Trieu3b261bb72017-06-13 22:21:18 +0000171}
172
Richard Trieue7f7ed22017-02-22 01:11:25 +0000173void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {}
174
175void ODRHash::clear() {
176 DeclMap.clear();
177 TypeMap.clear();
178 Bools.clear();
179 ID.clear();
180}
181
182unsigned ODRHash::CalculateHash() {
183 // Append the bools to the end of the data segment backwards. This allows
184 // for the bools data to be compressed 32 times smaller compared to using
185 // ID.AddBoolean
186 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
187 const unsigned size = Bools.size();
188 const unsigned remainder = size % unsigned_bits;
189 const unsigned loops = size / unsigned_bits;
190 auto I = Bools.rbegin();
191 unsigned value = 0;
192 for (unsigned i = 0; i < remainder; ++i) {
193 value <<= 1;
194 value |= *I;
195 ++I;
196 }
197 ID.AddInteger(value);
198
199 for (unsigned i = 0; i < loops; ++i) {
200 value = 0;
201 for (unsigned j = 0; j < unsigned_bits; ++j) {
202 value <<= 1;
203 value |= *I;
204 ++I;
205 }
206 ID.AddInteger(value);
207 }
208
209 assert(I == Bools.rend());
210 Bools.clear();
211 return ID.ComputeHash();
212}
213
214// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
215// methods process the relevant parts of the Decl.
216class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
217 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
218 llvm::FoldingSetNodeID &ID;
Richard Trieu639d7b62017-02-22 22:22:42 +0000219 ODRHash &Hash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000220
221public:
Richard Trieu639d7b62017-02-22 22:22:42 +0000222 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
223 : ID(ID), Hash(Hash) {}
224
225 void AddStmt(const Stmt *S) {
226 Hash.AddBoolean(S);
227 if (S) {
228 Hash.AddStmt(S);
229 }
230 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000231
Richard Trieud0786092017-02-23 00:23:01 +0000232 void AddIdentifierInfo(const IdentifierInfo *II) {
233 Hash.AddBoolean(II);
234 if (II) {
235 Hash.AddIdentifierInfo(II);
236 }
237 }
238
Richard Trieubcaaf962017-02-23 03:25:57 +0000239 void AddQualType(QualType T) {
240 Hash.AddQualType(T);
241 }
242
Richard Trieue7f7ed22017-02-22 01:11:25 +0000243 void Visit(const Decl *D) {
244 ID.AddInteger(D->getKind());
245 Inherited::Visit(D);
246 }
247
Richard Trieud0786092017-02-23 00:23:01 +0000248 void VisitNamedDecl(const NamedDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000249 Hash.AddDeclarationName(D->getDeclName());
Richard Trieud0786092017-02-23 00:23:01 +0000250 Inherited::VisitNamedDecl(D);
251 }
252
Richard Trieubcaaf962017-02-23 03:25:57 +0000253 void VisitValueDecl(const ValueDecl *D) {
254 AddQualType(D->getType());
255 Inherited::VisitValueDecl(D);
256 }
257
Richard Trieu6e13ff32017-06-16 02:44:29 +0000258 void VisitVarDecl(const VarDecl *D) {
259 Hash.AddBoolean(D->isStaticLocal());
260 Hash.AddBoolean(D->isConstexpr());
261 const bool HasInit = D->hasInit();
262 Hash.AddBoolean(HasInit);
263 if (HasInit) {
264 AddStmt(D->getInit());
265 }
266 Inherited::VisitVarDecl(D);
267 }
268
Richard Trieu02552272017-05-02 23:58:52 +0000269 void VisitParmVarDecl(const ParmVarDecl *D) {
270 // TODO: Handle default arguments.
271 Inherited::VisitParmVarDecl(D);
272 }
273
Richard Trieue7f7ed22017-02-22 01:11:25 +0000274 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
275 ID.AddInteger(D->getAccess());
276 Inherited::VisitAccessSpecDecl(D);
277 }
Richard Trieu639d7b62017-02-22 22:22:42 +0000278
279 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
280 AddStmt(D->getAssertExpr());
281 AddStmt(D->getMessage());
282
283 Inherited::VisitStaticAssertDecl(D);
284 }
Richard Trieud0786092017-02-23 00:23:01 +0000285
286 void VisitFieldDecl(const FieldDecl *D) {
Richard Trieu93772fc2017-02-24 20:59:28 +0000287 const bool IsBitfield = D->isBitField();
288 Hash.AddBoolean(IsBitfield);
289
290 if (IsBitfield) {
291 AddStmt(D->getBitWidth());
292 }
Richard Trieu8d543e22017-02-24 23:35:37 +0000293
294 Hash.AddBoolean(D->isMutable());
295 AddStmt(D->getInClassInitializer());
Richard Trieuff60e0f2017-02-25 01:29:34 +0000296
297 Inherited::VisitFieldDecl(D);
Richard Trieud0786092017-02-23 00:23:01 +0000298 }
Richard Trieu48143742017-02-28 21:24:38 +0000299
300 void VisitFunctionDecl(const FunctionDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000301 ID.AddInteger(D->getStorageClass());
302 Hash.AddBoolean(D->isInlineSpecified());
303 Hash.AddBoolean(D->isVirtualAsWritten());
304 Hash.AddBoolean(D->isPure());
305 Hash.AddBoolean(D->isDeletedAsWritten());
306
Richard Trieu02552272017-05-02 23:58:52 +0000307 ID.AddInteger(D->param_size());
308
309 for (auto *Param : D->parameters()) {
310 Hash.AddSubDecl(Param);
311 }
312
Richard Trieu48143742017-02-28 21:24:38 +0000313 Inherited::VisitFunctionDecl(D);
314 }
315
316 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
Richard Trieu583e2c12017-03-04 00:08:58 +0000317 Hash.AddBoolean(D->isConst());
318 Hash.AddBoolean(D->isVolatile());
319
Richard Trieu48143742017-02-28 21:24:38 +0000320 Inherited::VisitCXXMethodDecl(D);
321 }
Richard Trieu33562c22017-03-08 00:13:19 +0000322
323 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
324 AddQualType(D->getUnderlyingType());
325
326 Inherited::VisitTypedefNameDecl(D);
327 }
328
329 void VisitTypedefDecl(const TypedefDecl *D) {
330 Inherited::VisitTypedefDecl(D);
331 }
332
333 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
334 Inherited::VisitTypeAliasDecl(D);
335 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000336};
337
338// Only allow a small portion of Decl's to be processed. Remove this once
339// all Decl's can be handled.
340bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
341 if (D->isImplicit()) return false;
342 if (D->getDeclContext() != Parent) return false;
343
344 switch (D->getKind()) {
345 default:
346 return false;
347 case Decl::AccessSpec:
Richard Trieu48143742017-02-28 21:24:38 +0000348 case Decl::CXXMethod:
Richard Trieud0786092017-02-23 00:23:01 +0000349 case Decl::Field:
Richard Trieu639d7b62017-02-22 22:22:42 +0000350 case Decl::StaticAssert:
Richard Trieu33562c22017-03-08 00:13:19 +0000351 case Decl::TypeAlias:
352 case Decl::Typedef:
Richard Trieu6e13ff32017-06-16 02:44:29 +0000353 case Decl::Var:
Richard Trieue7f7ed22017-02-22 01:11:25 +0000354 return true;
355 }
356}
357
358void ODRHash::AddSubDecl(const Decl *D) {
359 assert(D && "Expecting non-null pointer.");
360 AddDecl(D);
361
Richard Trieu639d7b62017-02-22 22:22:42 +0000362 ODRDeclVisitor(ID, *this).Visit(D);
Richard Trieue7f7ed22017-02-22 01:11:25 +0000363}
364
365void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
366 assert(Record && Record->hasDefinition() &&
367 "Expected non-null record to be a definition.");
Richard Trieu02552272017-05-02 23:58:52 +0000368
369 if (isa<ClassTemplateSpecializationDecl>(Record)) {
370 return;
371 }
372
Richard Trieue7f7ed22017-02-22 01:11:25 +0000373 AddDecl(Record);
374
375 // Filter out sub-Decls which will not be processed in order to get an
376 // accurate count of Decl's.
377 llvm::SmallVector<const Decl *, 16> Decls;
378 for (const Decl *SubDecl : Record->decls()) {
379 if (isWhitelistedDecl(SubDecl, Record)) {
380 Decls.push_back(SubDecl);
381 }
382 }
383
384 ID.AddInteger(Decls.size());
385 for (auto SubDecl : Decls) {
386 AddSubDecl(SubDecl);
387 }
388}
389
390void ODRHash::AddDecl(const Decl *D) {
391 assert(D && "Expecting non-null pointer.");
392 auto Result = DeclMap.insert(std::make_pair(D, DeclMap.size()));
393 ID.AddInteger(Result.first->second);
394 // On first encounter of a Decl pointer, process it. Every time afterwards,
395 // only the index value is needed.
396 if (!Result.second) {
397 return;
398 }
399
400 ID.AddInteger(D->getKind());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000401
402 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
403 AddDeclarationName(ND->getDeclName());
404 }
Richard Trieue7f7ed22017-02-22 01:11:25 +0000405}
406
Richard Trieubcaaf962017-02-23 03:25:57 +0000407// Process a Type pointer. Add* methods call back into ODRHash while Visit*
408// methods process the relevant parts of the Type.
409class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
410 typedef TypeVisitor<ODRTypeVisitor> Inherited;
411 llvm::FoldingSetNodeID &ID;
412 ODRHash &Hash;
413
414public:
415 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
416 : ID(ID), Hash(Hash) {}
417
418 void AddStmt(Stmt *S) {
419 Hash.AddBoolean(S);
420 if (S) {
421 Hash.AddStmt(S);
422 }
423 }
424
Richard Trieu8459ddf2017-02-24 02:59:12 +0000425 void AddDecl(Decl *D) {
426 Hash.AddBoolean(D);
427 if (D) {
428 Hash.AddDecl(D);
429 }
430 }
431
Richard Trieu02552272017-05-02 23:58:52 +0000432 void AddQualType(QualType T) {
433 Hash.AddQualType(T);
434 }
435
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000436 void AddType(const Type *T) {
437 Hash.AddBoolean(T);
438 if (T) {
439 Hash.AddType(T);
440 }
441 }
442
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000443 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
Richard Trieuf21b8032017-06-09 20:11:51 +0000444 Hash.AddNestedNameSpecifier(NNS);
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000445 }
446
447 void AddIdentifierInfo(const IdentifierInfo *II) {
448 Hash.AddBoolean(II);
449 if (II) {
450 Hash.AddIdentifierInfo(II);
451 }
452 }
453
Richard Trieu02552272017-05-02 23:58:52 +0000454 void VisitQualifiers(Qualifiers Quals) {
455 ID.AddInteger(Quals.getAsOpaqueValue());
456 }
457
Richard Trieubcaaf962017-02-23 03:25:57 +0000458 void Visit(const Type *T) {
459 ID.AddInteger(T->getTypeClass());
460 Inherited::Visit(T);
461 }
462
463 void VisitType(const Type *T) {}
464
Richard Trieu02552272017-05-02 23:58:52 +0000465 void VisitAdjustedType(const AdjustedType *T) {
466 AddQualType(T->getOriginalType());
467 AddQualType(T->getAdjustedType());
468 VisitType(T);
469 }
470
471 void VisitDecayedType(const DecayedType *T) {
472 AddQualType(T->getDecayedType());
473 AddQualType(T->getPointeeType());
474 VisitAdjustedType(T);
475 }
476
477 void VisitArrayType(const ArrayType *T) {
478 AddQualType(T->getElementType());
479 ID.AddInteger(T->getSizeModifier());
480 VisitQualifiers(T->getIndexTypeQualifiers());
481 VisitType(T);
482 }
483 void VisitConstantArrayType(const ConstantArrayType *T) {
484 T->getSize().Profile(ID);
485 VisitArrayType(T);
486 }
487
488 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
489 AddStmt(T->getSizeExpr());
490 VisitArrayType(T);
491 }
492
493 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
494 VisitArrayType(T);
495 }
496
497 void VisitVariableArrayType(const VariableArrayType *T) {
498 AddStmt(T->getSizeExpr());
499 VisitArrayType(T);
500 }
501
Richard Trieubcaaf962017-02-23 03:25:57 +0000502 void VisitBuiltinType(const BuiltinType *T) {
503 ID.AddInteger(T->getKind());
504 VisitType(T);
505 }
Richard Trieu8459ddf2017-02-24 02:59:12 +0000506
Richard Trieu02552272017-05-02 23:58:52 +0000507 void VisitFunctionType(const FunctionType *T) {
508 AddQualType(T->getReturnType());
509 T->getExtInfo().Profile(ID);
510 Hash.AddBoolean(T->isConst());
511 Hash.AddBoolean(T->isVolatile());
512 Hash.AddBoolean(T->isRestrict());
513 VisitType(T);
514 }
515
516 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
517 VisitFunctionType(T);
518 }
519
520 void VisitFunctionProtoType(const FunctionProtoType *T) {
521 ID.AddInteger(T->getNumParams());
522 for (auto ParamType : T->getParamTypes())
523 AddQualType(ParamType);
524
525 VisitFunctionType(T);
526 }
527
Richard Trieu8459ddf2017-02-24 02:59:12 +0000528 void VisitTypedefType(const TypedefType *T) {
529 AddDecl(T->getDecl());
Richard Trieu3e03d3e2017-06-29 22:53:04 +0000530 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
531 VisitQualifiers(UnderlyingType.getQualifiers());
532 while (const TypedefType *Underlying =
533 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
534 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
535 }
536 AddType(UnderlyingType.getTypePtr());
Richard Trieu8459ddf2017-02-24 02:59:12 +0000537 VisitType(T);
538 }
Richard Trieu58bb7bd2017-05-17 02:29:02 +0000539
540 void VisitTagType(const TagType *T) {
541 AddDecl(T->getDecl());
542 VisitType(T);
543 }
544
545 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
546 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
547
548 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
549 ID.AddInteger(T->getKeyword());
550 VisitType(T);
551 };
552
553 void VisitDependentNameType(const DependentNameType *T) {
554 AddNestedNameSpecifier(T->getQualifier());
555 AddIdentifierInfo(T->getIdentifier());
556 VisitTypeWithKeyword(T);
557 }
558
559 void VisitDependentTemplateSpecializationType(
560 const DependentTemplateSpecializationType *T) {
561 AddIdentifierInfo(T->getIdentifier());
562 AddNestedNameSpecifier(T->getQualifier());
563 ID.AddInteger(T->getNumArgs());
564 for (const auto &TA : T->template_arguments()) {
565 Hash.AddTemplateArgument(TA);
566 }
567 VisitTypeWithKeyword(T);
568 }
569
570 void VisitElaboratedType(const ElaboratedType *T) {
571 AddNestedNameSpecifier(T->getQualifier());
572 AddQualType(T->getNamedType());
573 VisitTypeWithKeyword(T);
574 }
Richard Trieu96b49622017-05-31 00:31:58 +0000575
576 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
577 ID.AddInteger(T->getNumArgs());
578 for (const auto &TA : T->template_arguments()) {
579 Hash.AddTemplateArgument(TA);
580 }
581 Hash.AddTemplateName(T->getTemplateName());
582 VisitType(T);
583 }
Richard Trieud9201d02017-06-15 01:35:06 +0000584
585 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
586 ID.AddInteger(T->getDepth());
587 ID.AddInteger(T->getIndex());
588 Hash.AddBoolean(T->isParameterPack());
589 AddDecl(T->getDecl());
590 }
Richard Trieubcaaf962017-02-23 03:25:57 +0000591};
592
593void ODRHash::AddType(const Type *T) {
594 assert(T && "Expecting non-null pointer.");
595 auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
596 ID.AddInteger(Result.first->second);
597 // On first encounter of a Type pointer, process it. Every time afterwards,
598 // only the index value is needed.
599 if (!Result.second) {
600 return;
601 }
602
603 ODRTypeVisitor(ID, *this).Visit(T);
604}
605
606void ODRHash::AddQualType(QualType T) {
607 AddBoolean(T.isNull());
608 if (T.isNull())
609 return;
610 SplitQualType split = T.split();
611 ID.AddInteger(split.Quals.getAsOpaqueValue());
612 AddType(split.Ty);
613}
614
Richard Trieue7f7ed22017-02-22 01:11:25 +0000615void ODRHash::AddBoolean(bool Value) {
616 Bools.push_back(Value);
617}