1 //===-- 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 
23 using namespace clang;
24 
25 void ODRHash::AddStmt(const Stmt *S) {
26   assert(S && "Expecting non-null pointer.");
27   S->ProcessODRHash(ID, *this);
28 }
29 
30 void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
31   assert(II && "Expecting non-null pointer.");
32   ID.AddString(II->getName());
33 }
34 
35 void ODRHash::AddDeclarationName(DeclarationName Name) {
36   // Index all DeclarationName and use index numbers to refer to them.
37   auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
38   ID.AddInteger(Result.first->second);
39   if (!Result.second) {
40     // If found in map, the the DeclarationName has previously been processed.
41     return;
42   }
43 
44   // First time processing each DeclarationName, also process its details.
45   AddBoolean(Name.isEmpty());
46   if (Name.isEmpty())
47     return;
48 
49   auto Kind = Name.getNameKind();
50   ID.AddInteger(Kind);
51   switch (Kind) {
52   case DeclarationName::Identifier:
53     AddIdentifierInfo(Name.getAsIdentifierInfo());
54     break;
55   case DeclarationName::ObjCZeroArgSelector:
56   case DeclarationName::ObjCOneArgSelector:
57   case DeclarationName::ObjCMultiArgSelector: {
58     Selector S = Name.getObjCSelector();
59     AddBoolean(S.isNull());
60     AddBoolean(S.isKeywordSelector());
61     AddBoolean(S.isUnarySelector());
62     unsigned NumArgs = S.getNumArgs();
63     for (unsigned i = 0; i < NumArgs; ++i) {
64       AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
65     }
66     break;
67   }
68   case DeclarationName::CXXConstructorName:
69   case DeclarationName::CXXDestructorName:
70     AddQualType(Name.getCXXNameType());
71     break;
72   case DeclarationName::CXXOperatorName:
73     ID.AddInteger(Name.getCXXOverloadedOperator());
74     break;
75   case DeclarationName::CXXLiteralOperatorName:
76     AddIdentifierInfo(Name.getCXXLiteralIdentifier());
77     break;
78   case DeclarationName::CXXConversionFunctionName:
79     AddQualType(Name.getCXXNameType());
80     break;
81   case DeclarationName::CXXUsingDirective:
82     break;
83   case DeclarationName::CXXDeductionGuideName: {
84     auto *Template = Name.getCXXDeductionGuideTemplate();
85     AddBoolean(Template);
86     if (Template) {
87       AddDecl(Template);
88     }
89   }
90   }
91 }
92 
93 void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
94   assert(NNS && "Expecting non-null pointer.");
95   const auto *Prefix = NNS->getPrefix();
96   AddBoolean(Prefix);
97   if (Prefix) {
98     AddNestedNameSpecifier(Prefix);
99   }
100   auto Kind = NNS->getKind();
101   ID.AddInteger(Kind);
102   switch (Kind) {
103   case NestedNameSpecifier::Identifier:
104     AddIdentifierInfo(NNS->getAsIdentifier());
105     break;
106   case NestedNameSpecifier::Namespace:
107     AddDecl(NNS->getAsNamespace());
108     break;
109   case NestedNameSpecifier::NamespaceAlias:
110     AddDecl(NNS->getAsNamespaceAlias());
111     break;
112   case NestedNameSpecifier::TypeSpec:
113   case NestedNameSpecifier::TypeSpecWithTemplate:
114     AddType(NNS->getAsType());
115     break;
116   case NestedNameSpecifier::Global:
117   case NestedNameSpecifier::Super:
118     break;
119   }
120 }
121 
122 void ODRHash::AddTemplateName(TemplateName Name) {
123   auto Kind = Name.getKind();
124   ID.AddInteger(Kind);
125 
126   switch (Kind) {
127   case TemplateName::Template:
128     AddDecl(Name.getAsTemplateDecl());
129     break;
130   // TODO: Support these cases.
131   case TemplateName::OverloadedTemplate:
132   case TemplateName::QualifiedTemplate:
133   case TemplateName::DependentTemplate:
134   case TemplateName::SubstTemplateTemplateParm:
135   case TemplateName::SubstTemplateTemplateParmPack:
136     break;
137   }
138 }
139 
140 void ODRHash::AddTemplateArgument(TemplateArgument TA) {
141   const auto Kind = TA.getKind();
142   ID.AddInteger(Kind);
143 
144   switch (Kind) {
145     case TemplateArgument::Null:
146       llvm_unreachable("Expected valid TemplateArgument");
147     case TemplateArgument::Type:
148       AddQualType(TA.getAsType());
149       break;
150     case TemplateArgument::Declaration:
151       AddDecl(TA.getAsDecl());
152       break;
153     case TemplateArgument::NullPtr:
154     case TemplateArgument::Integral:
155       break;
156     case TemplateArgument::Template:
157     case TemplateArgument::TemplateExpansion:
158       AddTemplateName(TA.getAsTemplateOrTemplatePattern());
159       break;
160     case TemplateArgument::Expression:
161       AddStmt(TA.getAsExpr());
162       break;
163     case TemplateArgument::Pack:
164       ID.AddInteger(TA.pack_size());
165       for (auto SubTA : TA.pack_elements()) {
166         AddTemplateArgument(SubTA);
167       }
168       break;
169   }
170 }
171 
172 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
173   assert(TPL && "Expecting non-null pointer.");
174 
175   ID.AddInteger(TPL->size());
176   for (auto *ND : TPL->asArray()) {
177     AddSubDecl(ND);
178   }
179 }
180 
181 void ODRHash::clear() {
182   DeclNameMap.clear();
183   TypeMap.clear();
184   Bools.clear();
185   ID.clear();
186 }
187 
188 unsigned ODRHash::CalculateHash() {
189   // Append the bools to the end of the data segment backwards.  This allows
190   // for the bools data to be compressed 32 times smaller compared to using
191   // ID.AddBoolean
192   const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
193   const unsigned size = Bools.size();
194   const unsigned remainder = size % unsigned_bits;
195   const unsigned loops = size / unsigned_bits;
196   auto I = Bools.rbegin();
197   unsigned value = 0;
198   for (unsigned i = 0; i < remainder; ++i) {
199     value <<= 1;
200     value |= *I;
201     ++I;
202   }
203   ID.AddInteger(value);
204 
205   for (unsigned i = 0; i < loops; ++i) {
206     value = 0;
207     for (unsigned j = 0; j < unsigned_bits; ++j) {
208       value <<= 1;
209       value |= *I;
210       ++I;
211     }
212     ID.AddInteger(value);
213   }
214 
215   assert(I == Bools.rend());
216   Bools.clear();
217   return ID.ComputeHash();
218 }
219 
220 namespace {
221 // Process a Decl pointer.  Add* methods call back into ODRHash while Visit*
222 // methods process the relevant parts of the Decl.
223 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
224   typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
225   llvm::FoldingSetNodeID &ID;
226   ODRHash &Hash;
227 
228 public:
229   ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
230       : ID(ID), Hash(Hash) {}
231 
232   void AddStmt(const Stmt *S) {
233     Hash.AddBoolean(S);
234     if (S) {
235       Hash.AddStmt(S);
236     }
237   }
238 
239   void AddIdentifierInfo(const IdentifierInfo *II) {
240     Hash.AddBoolean(II);
241     if (II) {
242       Hash.AddIdentifierInfo(II);
243     }
244   }
245 
246   void AddQualType(QualType T) {
247     Hash.AddQualType(T);
248   }
249 
250   void AddDecl(const Decl *D) {
251     Hash.AddBoolean(D);
252     if (D) {
253       Hash.AddDecl(D);
254     }
255   }
256 
257   void AddTemplateArgument(TemplateArgument TA) {
258     Hash.AddTemplateArgument(TA);
259   }
260 
261   void Visit(const Decl *D) {
262     ID.AddInteger(D->getKind());
263     Inherited::Visit(D);
264   }
265 
266   void VisitNamedDecl(const NamedDecl *D) {
267     Hash.AddDeclarationName(D->getDeclName());
268     Inherited::VisitNamedDecl(D);
269   }
270 
271   void VisitValueDecl(const ValueDecl *D) {
272     if (!isa<FunctionDecl>(D)) {
273       AddQualType(D->getType());
274     }
275     Inherited::VisitValueDecl(D);
276   }
277 
278   void VisitVarDecl(const VarDecl *D) {
279     Hash.AddBoolean(D->isStaticLocal());
280     Hash.AddBoolean(D->isConstexpr());
281     const bool HasInit = D->hasInit();
282     Hash.AddBoolean(HasInit);
283     if (HasInit) {
284       AddStmt(D->getInit());
285     }
286     Inherited::VisitVarDecl(D);
287   }
288 
289   void VisitParmVarDecl(const ParmVarDecl *D) {
290     // TODO: Handle default arguments.
291     Inherited::VisitParmVarDecl(D);
292   }
293 
294   void VisitAccessSpecDecl(const AccessSpecDecl *D) {
295     ID.AddInteger(D->getAccess());
296     Inherited::VisitAccessSpecDecl(D);
297   }
298 
299   void VisitStaticAssertDecl(const StaticAssertDecl *D) {
300     AddStmt(D->getAssertExpr());
301     AddStmt(D->getMessage());
302 
303     Inherited::VisitStaticAssertDecl(D);
304   }
305 
306   void VisitFieldDecl(const FieldDecl *D) {
307     const bool IsBitfield = D->isBitField();
308     Hash.AddBoolean(IsBitfield);
309 
310     if (IsBitfield) {
311       AddStmt(D->getBitWidth());
312     }
313 
314     Hash.AddBoolean(D->isMutable());
315     AddStmt(D->getInClassInitializer());
316 
317     Inherited::VisitFieldDecl(D);
318   }
319 
320   void VisitFunctionDecl(const FunctionDecl *D) {
321     ID.AddInteger(D->getStorageClass());
322     Hash.AddBoolean(D->isInlineSpecified());
323     Hash.AddBoolean(D->isVirtualAsWritten());
324     Hash.AddBoolean(D->isPure());
325     Hash.AddBoolean(D->isDeletedAsWritten());
326 
327     ID.AddInteger(D->param_size());
328 
329     for (auto *Param : D->parameters()) {
330       Hash.AddSubDecl(Param);
331     }
332 
333     AddQualType(D->getReturnType());
334 
335     const auto* SpecializationArgs = D->getTemplateSpecializationArgs();
336     Hash.AddBoolean(SpecializationArgs);
337     if (SpecializationArgs) {
338       ID.AddInteger(SpecializationArgs->size());
339       for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
340         Hash.AddTemplateArgument(TA);
341       }
342     }
343 
344     Inherited::VisitFunctionDecl(D);
345   }
346 
347   void VisitCXXMethodDecl(const CXXMethodDecl *D) {
348     Hash.AddBoolean(D->isConst());
349     Hash.AddBoolean(D->isVolatile());
350 
351     Inherited::VisitCXXMethodDecl(D);
352   }
353 
354   void VisitTypedefNameDecl(const TypedefNameDecl *D) {
355     AddQualType(D->getUnderlyingType());
356 
357     Inherited::VisitTypedefNameDecl(D);
358   }
359 
360   void VisitTypedefDecl(const TypedefDecl *D) {
361     Inherited::VisitTypedefDecl(D);
362   }
363 
364   void VisitTypeAliasDecl(const TypeAliasDecl *D) {
365     Inherited::VisitTypeAliasDecl(D);
366   }
367 
368   void VisitFriendDecl(const FriendDecl *D) {
369     TypeSourceInfo *TSI = D->getFriendType();
370     Hash.AddBoolean(TSI);
371     if (TSI) {
372       AddQualType(TSI->getType());
373     } else {
374       AddDecl(D->getFriendDecl());
375     }
376   }
377 
378   void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
379     // Only care about default arguments as part of the definition.
380     const bool hasDefaultArgument =
381         D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
382     Hash.AddBoolean(hasDefaultArgument);
383     if (hasDefaultArgument) {
384       AddTemplateArgument(D->getDefaultArgument());
385     }
386 
387     Inherited::VisitTemplateTypeParmDecl(D);
388   }
389 
390   void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
391     // Only care about default arguments as part of the definition.
392     const bool hasDefaultArgument =
393         D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
394     Hash.AddBoolean(hasDefaultArgument);
395     if (hasDefaultArgument) {
396       AddStmt(D->getDefaultArgument());
397     }
398 
399     Inherited::VisitNonTypeTemplateParmDecl(D);
400   }
401 
402   void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
403     // Only care about default arguments as part of the definition.
404     const bool hasDefaultArgument =
405         D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
406     Hash.AddBoolean(hasDefaultArgument);
407     if (hasDefaultArgument) {
408       AddTemplateArgument(D->getDefaultArgument().getArgument());
409     }
410 
411     Inherited::VisitTemplateTemplateParmDecl(D);
412   }
413 };
414 } // namespace
415 
416 // Only allow a small portion of Decl's to be processed.  Remove this once
417 // all Decl's can be handled.
418 bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
419   if (D->isImplicit()) return false;
420   if (D->getDeclContext() != Parent) return false;
421 
422   switch (D->getKind()) {
423     default:
424       return false;
425     case Decl::AccessSpec:
426     case Decl::CXXConstructor:
427     case Decl::CXXDestructor:
428     case Decl::CXXMethod:
429     case Decl::Field:
430     case Decl::Friend:
431     case Decl::StaticAssert:
432     case Decl::TypeAlias:
433     case Decl::Typedef:
434     case Decl::Var:
435       return true;
436   }
437 }
438 
439 void ODRHash::AddSubDecl(const Decl *D) {
440   assert(D && "Expecting non-null pointer.");
441 
442   ODRDeclVisitor(ID, *this).Visit(D);
443 }
444 
445 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
446   assert(Record && Record->hasDefinition() &&
447          "Expected non-null record to be a definition.");
448 
449   const DeclContext *DC = Record;
450   while (DC) {
451     if (isa<ClassTemplateSpecializationDecl>(DC)) {
452       return;
453     }
454     DC = DC->getParent();
455   }
456 
457   AddDecl(Record);
458 
459   // Filter out sub-Decls which will not be processed in order to get an
460   // accurate count of Decl's.
461   llvm::SmallVector<const Decl *, 16> Decls;
462   for (const Decl *SubDecl : Record->decls()) {
463     if (isWhitelistedDecl(SubDecl, Record)) {
464       Decls.push_back(SubDecl);
465     }
466   }
467 
468   ID.AddInteger(Decls.size());
469   for (auto SubDecl : Decls) {
470     AddSubDecl(SubDecl);
471   }
472 
473   const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
474   AddBoolean(TD);
475   if (TD) {
476     AddTemplateParameterList(TD->getTemplateParameters());
477   }
478 
479   ID.AddInteger(Record->getNumBases());
480   auto Bases = Record->bases();
481   for (auto Base : Bases) {
482     AddQualType(Base.getType());
483     ID.AddInteger(Base.isVirtual());
484     ID.AddInteger(Base.getAccessSpecifierAsWritten());
485   }
486 }
487 
488 void ODRHash::AddFunctionDecl(const FunctionDecl *Function) {
489   assert(Function && "Expecting non-null pointer.");
490 
491   // Skip hashing these kinds of function.
492   if (Function->isImplicit()) return;
493   if (Function->isDefaulted()) return;
494   if (Function->isDeleted()) return;
495   if (!Function->hasBody()) return;
496   if (!Function->getBody()) return;
497 
498   // Skip functions that are specializations or in specialization context.
499   const DeclContext *DC = Function;
500   while (DC) {
501     if (isa<ClassTemplateSpecializationDecl>(DC)) return;
502     if (auto *F = dyn_cast<FunctionDecl>(DC))
503       if (F->isFunctionTemplateSpecialization()) return;
504     DC = DC->getParent();
505   }
506 
507   AddDecl(Function);
508 
509   AddQualType(Function->getReturnType());
510 
511   ID.AddInteger(Function->param_size());
512   for (auto Param : Function->parameters())
513     AddSubDecl(Param);
514 
515   AddStmt(Function->getBody());
516 }
517 
518 void ODRHash::AddDecl(const Decl *D) {
519   assert(D && "Expecting non-null pointer.");
520   D = D->getCanonicalDecl();
521 
522   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
523     AddDeclarationName(ND->getDeclName());
524     return;
525   }
526 
527   ID.AddInteger(D->getKind());
528   // TODO: Handle non-NamedDecl here.
529 }
530 
531 namespace {
532 // Process a Type pointer.  Add* methods call back into ODRHash while Visit*
533 // methods process the relevant parts of the Type.
534 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
535   typedef TypeVisitor<ODRTypeVisitor> Inherited;
536   llvm::FoldingSetNodeID &ID;
537   ODRHash &Hash;
538 
539 public:
540   ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
541       : ID(ID), Hash(Hash) {}
542 
543   void AddStmt(Stmt *S) {
544     Hash.AddBoolean(S);
545     if (S) {
546       Hash.AddStmt(S);
547     }
548   }
549 
550   void AddDecl(Decl *D) {
551     Hash.AddBoolean(D);
552     if (D) {
553       Hash.AddDecl(D);
554     }
555   }
556 
557   void AddQualType(QualType T) {
558     Hash.AddQualType(T);
559   }
560 
561   void AddType(const Type *T) {
562     Hash.AddBoolean(T);
563     if (T) {
564       Hash.AddType(T);
565     }
566   }
567 
568   void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
569     Hash.AddBoolean(NNS);
570     if (NNS) {
571       Hash.AddNestedNameSpecifier(NNS);
572     }
573   }
574 
575   void AddIdentifierInfo(const IdentifierInfo *II) {
576     Hash.AddBoolean(II);
577     if (II) {
578       Hash.AddIdentifierInfo(II);
579     }
580   }
581 
582   void VisitQualifiers(Qualifiers Quals) {
583     ID.AddInteger(Quals.getAsOpaqueValue());
584   }
585 
586   void Visit(const Type *T) {
587     ID.AddInteger(T->getTypeClass());
588     Inherited::Visit(T);
589   }
590 
591   void VisitType(const Type *T) {}
592 
593   void VisitAdjustedType(const AdjustedType *T) {
594     AddQualType(T->getOriginalType());
595     AddQualType(T->getAdjustedType());
596     VisitType(T);
597   }
598 
599   void VisitDecayedType(const DecayedType *T) {
600     AddQualType(T->getDecayedType());
601     AddQualType(T->getPointeeType());
602     VisitAdjustedType(T);
603   }
604 
605   void VisitArrayType(const ArrayType *T) {
606     AddQualType(T->getElementType());
607     ID.AddInteger(T->getSizeModifier());
608     VisitQualifiers(T->getIndexTypeQualifiers());
609     VisitType(T);
610   }
611   void VisitConstantArrayType(const ConstantArrayType *T) {
612     T->getSize().Profile(ID);
613     VisitArrayType(T);
614   }
615 
616   void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
617     AddStmt(T->getSizeExpr());
618     VisitArrayType(T);
619   }
620 
621   void VisitIncompleteArrayType(const IncompleteArrayType *T) {
622     VisitArrayType(T);
623   }
624 
625   void VisitVariableArrayType(const VariableArrayType *T) {
626     AddStmt(T->getSizeExpr());
627     VisitArrayType(T);
628   }
629 
630   void VisitBuiltinType(const BuiltinType *T) {
631     ID.AddInteger(T->getKind());
632     VisitType(T);
633   }
634 
635   void VisitFunctionType(const FunctionType *T) {
636     AddQualType(T->getReturnType());
637     T->getExtInfo().Profile(ID);
638     Hash.AddBoolean(T->isConst());
639     Hash.AddBoolean(T->isVolatile());
640     Hash.AddBoolean(T->isRestrict());
641     VisitType(T);
642   }
643 
644   void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
645     VisitFunctionType(T);
646   }
647 
648   void VisitFunctionProtoType(const FunctionProtoType *T) {
649     ID.AddInteger(T->getNumParams());
650     for (auto ParamType : T->getParamTypes())
651       AddQualType(ParamType);
652 
653     VisitFunctionType(T);
654   }
655 
656   void VisitPointerType(const PointerType *T) {
657     AddQualType(T->getPointeeType());
658     VisitType(T);
659   }
660 
661   void VisitReferenceType(const ReferenceType *T) {
662     AddQualType(T->getPointeeTypeAsWritten());
663     VisitType(T);
664   }
665 
666   void VisitLValueReferenceType(const LValueReferenceType *T) {
667     VisitReferenceType(T);
668   }
669 
670   void VisitRValueReferenceType(const RValueReferenceType *T) {
671     VisitReferenceType(T);
672   }
673 
674   void VisitTypedefType(const TypedefType *T) {
675     AddDecl(T->getDecl());
676     QualType UnderlyingType = T->getDecl()->getUnderlyingType();
677     VisitQualifiers(UnderlyingType.getQualifiers());
678     while (true) {
679       if (const TypedefType *Underlying =
680               dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
681         UnderlyingType = Underlying->getDecl()->getUnderlyingType();
682         continue;
683       }
684       if (const ElaboratedType *Underlying =
685               dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
686         UnderlyingType = Underlying->getNamedType();
687         continue;
688       }
689 
690       break;
691     }
692     AddType(UnderlyingType.getTypePtr());
693     VisitType(T);
694   }
695 
696   void VisitTagType(const TagType *T) {
697     AddDecl(T->getDecl());
698     VisitType(T);
699   }
700 
701   void VisitRecordType(const RecordType *T) { VisitTagType(T); }
702   void VisitEnumType(const EnumType *T) { VisitTagType(T); }
703 
704   void VisitTypeWithKeyword(const TypeWithKeyword *T) {
705     ID.AddInteger(T->getKeyword());
706     VisitType(T);
707   };
708 
709   void VisitDependentNameType(const DependentNameType *T) {
710     AddNestedNameSpecifier(T->getQualifier());
711     AddIdentifierInfo(T->getIdentifier());
712     VisitTypeWithKeyword(T);
713   }
714 
715   void VisitDependentTemplateSpecializationType(
716       const DependentTemplateSpecializationType *T) {
717     AddIdentifierInfo(T->getIdentifier());
718     AddNestedNameSpecifier(T->getQualifier());
719     ID.AddInteger(T->getNumArgs());
720     for (const auto &TA : T->template_arguments()) {
721       Hash.AddTemplateArgument(TA);
722     }
723     VisitTypeWithKeyword(T);
724   }
725 
726   void VisitElaboratedType(const ElaboratedType *T) {
727     AddNestedNameSpecifier(T->getQualifier());
728     AddQualType(T->getNamedType());
729     VisitTypeWithKeyword(T);
730   }
731 
732   void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
733     ID.AddInteger(T->getNumArgs());
734     for (const auto &TA : T->template_arguments()) {
735       Hash.AddTemplateArgument(TA);
736     }
737     Hash.AddTemplateName(T->getTemplateName());
738     VisitType(T);
739   }
740 
741   void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
742     ID.AddInteger(T->getDepth());
743     ID.AddInteger(T->getIndex());
744     Hash.AddBoolean(T->isParameterPack());
745     AddDecl(T->getDecl());
746   }
747 };
748 } // namespace
749 
750 void ODRHash::AddType(const Type *T) {
751   assert(T && "Expecting non-null pointer.");
752   auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
753   ID.AddInteger(Result.first->second);
754   // On first encounter of a Type pointer, process it.  Every time afterwards,
755   // only the index value is needed.
756   if (!Result.second) {
757     return;
758   }
759 
760   ODRTypeVisitor(ID, *this).Visit(T);
761 }
762 
763 void ODRHash::AddQualType(QualType T) {
764   AddBoolean(T.isNull());
765   if (T.isNull())
766     return;
767   SplitQualType split = T.split();
768   ID.AddInteger(split.Quals.getAsOpaqueValue());
769   AddType(split.Ty);
770 }
771 
772 void ODRHash::AddBoolean(bool Value) {
773   Bools.push_back(Value);
774 }
775