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, bool TreatAsDecl) {
36   if (TreatAsDecl)
37     AddBoolean(true);
38 
39   // Index all DeclarationName and use index numbers to refer to them.
40   auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
41   ID.AddInteger(Result.first->second);
42   if (!Result.second) {
43     // If found in map, the the DeclarationName has previously been processed.
44     return;
45   }
46 
47   // First time processing each DeclarationName, also process its details.
48   AddBoolean(Name.isEmpty());
49   if (Name.isEmpty())
50     return;
51 
52   auto Kind = Name.getNameKind();
53   ID.AddInteger(Kind);
54   switch (Kind) {
55   case DeclarationName::Identifier:
56     AddIdentifierInfo(Name.getAsIdentifierInfo());
57     break;
58   case DeclarationName::ObjCZeroArgSelector:
59   case DeclarationName::ObjCOneArgSelector:
60   case DeclarationName::ObjCMultiArgSelector: {
61     Selector S = Name.getObjCSelector();
62     AddBoolean(S.isNull());
63     AddBoolean(S.isKeywordSelector());
64     AddBoolean(S.isUnarySelector());
65     unsigned NumArgs = S.getNumArgs();
66     for (unsigned i = 0; i < NumArgs; ++i) {
67       AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
68     }
69     break;
70   }
71   case DeclarationName::CXXConstructorName:
72   case DeclarationName::CXXDestructorName:
73     AddQualType(Name.getCXXNameType());
74     break;
75   case DeclarationName::CXXOperatorName:
76     ID.AddInteger(Name.getCXXOverloadedOperator());
77     break;
78   case DeclarationName::CXXLiteralOperatorName:
79     AddIdentifierInfo(Name.getCXXLiteralIdentifier());
80     break;
81   case DeclarationName::CXXConversionFunctionName:
82     AddQualType(Name.getCXXNameType());
83     break;
84   case DeclarationName::CXXUsingDirective:
85     break;
86   case DeclarationName::CXXDeductionGuideName: {
87     auto *Template = Name.getCXXDeductionGuideTemplate();
88     AddBoolean(Template);
89     if (Template) {
90       AddDecl(Template);
91     }
92   }
93   }
94 
95   if (TreatAsDecl)
96     AddBoolean(false);
97 }
98 
99 void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
100   assert(NNS && "Expecting non-null pointer.");
101   const auto *Prefix = NNS->getPrefix();
102   AddBoolean(Prefix);
103   if (Prefix) {
104     AddNestedNameSpecifier(Prefix);
105   }
106   auto Kind = NNS->getKind();
107   ID.AddInteger(Kind);
108   switch (Kind) {
109   case NestedNameSpecifier::Identifier:
110     AddIdentifierInfo(NNS->getAsIdentifier());
111     break;
112   case NestedNameSpecifier::Namespace:
113     AddDecl(NNS->getAsNamespace());
114     break;
115   case NestedNameSpecifier::NamespaceAlias:
116     AddDecl(NNS->getAsNamespaceAlias());
117     break;
118   case NestedNameSpecifier::TypeSpec:
119   case NestedNameSpecifier::TypeSpecWithTemplate:
120     AddType(NNS->getAsType());
121     break;
122   case NestedNameSpecifier::Global:
123   case NestedNameSpecifier::Super:
124     break;
125   }
126 }
127 
128 void ODRHash::AddTemplateName(TemplateName Name) {
129   auto Kind = Name.getKind();
130   ID.AddInteger(Kind);
131 
132   switch (Kind) {
133   case TemplateName::Template:
134     AddDecl(Name.getAsTemplateDecl());
135     break;
136   // TODO: Support these cases.
137   case TemplateName::OverloadedTemplate:
138   case TemplateName::QualifiedTemplate:
139   case TemplateName::DependentTemplate:
140   case TemplateName::SubstTemplateTemplateParm:
141   case TemplateName::SubstTemplateTemplateParmPack:
142     break;
143   }
144 }
145 
146 void ODRHash::AddTemplateArgument(TemplateArgument TA) {
147   const auto Kind = TA.getKind();
148   ID.AddInteger(Kind);
149 
150   switch (Kind) {
151     case TemplateArgument::Null:
152       llvm_unreachable("Expected valid TemplateArgument");
153     case TemplateArgument::Type:
154       AddQualType(TA.getAsType());
155       break;
156     case TemplateArgument::Declaration:
157       AddDecl(TA.getAsDecl());
158       break;
159     case TemplateArgument::NullPtr:
160     case TemplateArgument::Integral:
161       break;
162     case TemplateArgument::Template:
163     case TemplateArgument::TemplateExpansion:
164       AddTemplateName(TA.getAsTemplateOrTemplatePattern());
165       break;
166     case TemplateArgument::Expression:
167       AddStmt(TA.getAsExpr());
168       break;
169     case TemplateArgument::Pack:
170       ID.AddInteger(TA.pack_size());
171       for (auto SubTA : TA.pack_elements()) {
172         AddTemplateArgument(SubTA);
173       }
174       break;
175   }
176 }
177 
178 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
179   assert(TPL && "Expecting non-null pointer.");
180 
181   ID.AddInteger(TPL->size());
182   for (auto *ND : TPL->asArray()) {
183     AddSubDecl(ND);
184   }
185 }
186 
187 void ODRHash::clear() {
188   DeclNameMap.clear();
189   Bools.clear();
190   ID.clear();
191 }
192 
193 unsigned ODRHash::CalculateHash() {
194   // Append the bools to the end of the data segment backwards.  This allows
195   // for the bools data to be compressed 32 times smaller compared to using
196   // ID.AddBoolean
197   const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
198   const unsigned size = Bools.size();
199   const unsigned remainder = size % unsigned_bits;
200   const unsigned loops = size / unsigned_bits;
201   auto I = Bools.rbegin();
202   unsigned value = 0;
203   for (unsigned i = 0; i < remainder; ++i) {
204     value <<= 1;
205     value |= *I;
206     ++I;
207   }
208   ID.AddInteger(value);
209 
210   for (unsigned i = 0; i < loops; ++i) {
211     value = 0;
212     for (unsigned j = 0; j < unsigned_bits; ++j) {
213       value <<= 1;
214       value |= *I;
215       ++I;
216     }
217     ID.AddInteger(value);
218   }
219 
220   assert(I == Bools.rend());
221   Bools.clear();
222   return ID.ComputeHash();
223 }
224 
225 namespace {
226 // Process a Decl pointer.  Add* methods call back into ODRHash while Visit*
227 // methods process the relevant parts of the Decl.
228 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
229   typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
230   llvm::FoldingSetNodeID &ID;
231   ODRHash &Hash;
232 
233 public:
234   ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
235       : ID(ID), Hash(Hash) {}
236 
237   void AddStmt(const Stmt *S) {
238     Hash.AddBoolean(S);
239     if (S) {
240       Hash.AddStmt(S);
241     }
242   }
243 
244   void AddIdentifierInfo(const IdentifierInfo *II) {
245     Hash.AddBoolean(II);
246     if (II) {
247       Hash.AddIdentifierInfo(II);
248     }
249   }
250 
251   void AddQualType(QualType T) {
252     Hash.AddQualType(T);
253   }
254 
255   void AddDecl(const Decl *D) {
256     Hash.AddBoolean(D);
257     if (D) {
258       Hash.AddDecl(D);
259     }
260   }
261 
262   void AddTemplateArgument(TemplateArgument TA) {
263     Hash.AddTemplateArgument(TA);
264   }
265 
266   void Visit(const Decl *D) {
267     ID.AddInteger(D->getKind());
268     Inherited::Visit(D);
269   }
270 
271   void VisitNamedDecl(const NamedDecl *D) {
272     Hash.AddDeclarationName(D->getDeclName());
273     Inherited::VisitNamedDecl(D);
274   }
275 
276   void VisitValueDecl(const ValueDecl *D) {
277     if (!isa<FunctionDecl>(D)) {
278       AddQualType(D->getType());
279     }
280     Inherited::VisitValueDecl(D);
281   }
282 
283   void VisitVarDecl(const VarDecl *D) {
284     Hash.AddBoolean(D->isStaticLocal());
285     Hash.AddBoolean(D->isConstexpr());
286     const bool HasInit = D->hasInit();
287     Hash.AddBoolean(HasInit);
288     if (HasInit) {
289       AddStmt(D->getInit());
290     }
291     Inherited::VisitVarDecl(D);
292   }
293 
294   void VisitParmVarDecl(const ParmVarDecl *D) {
295     // TODO: Handle default arguments.
296     Inherited::VisitParmVarDecl(D);
297   }
298 
299   void VisitAccessSpecDecl(const AccessSpecDecl *D) {
300     ID.AddInteger(D->getAccess());
301     Inherited::VisitAccessSpecDecl(D);
302   }
303 
304   void VisitStaticAssertDecl(const StaticAssertDecl *D) {
305     AddStmt(D->getAssertExpr());
306     AddStmt(D->getMessage());
307 
308     Inherited::VisitStaticAssertDecl(D);
309   }
310 
311   void VisitFieldDecl(const FieldDecl *D) {
312     const bool IsBitfield = D->isBitField();
313     Hash.AddBoolean(IsBitfield);
314 
315     if (IsBitfield) {
316       AddStmt(D->getBitWidth());
317     }
318 
319     Hash.AddBoolean(D->isMutable());
320     AddStmt(D->getInClassInitializer());
321 
322     Inherited::VisitFieldDecl(D);
323   }
324 
325   void VisitFunctionDecl(const FunctionDecl *D) {
326     // Handled by the ODRHash for FunctionDecl
327     ID.AddInteger(D->getODRHash());
328 
329     Inherited::VisitFunctionDecl(D);
330   }
331 
332   void VisitCXXMethodDecl(const CXXMethodDecl *D) {
333     // Handled by the ODRHash for FunctionDecl
334 
335     Inherited::VisitCXXMethodDecl(D);
336   }
337 
338   void VisitTypedefNameDecl(const TypedefNameDecl *D) {
339     AddQualType(D->getUnderlyingType());
340 
341     Inherited::VisitTypedefNameDecl(D);
342   }
343 
344   void VisitTypedefDecl(const TypedefDecl *D) {
345     Inherited::VisitTypedefDecl(D);
346   }
347 
348   void VisitTypeAliasDecl(const TypeAliasDecl *D) {
349     Inherited::VisitTypeAliasDecl(D);
350   }
351 
352   void VisitFriendDecl(const FriendDecl *D) {
353     TypeSourceInfo *TSI = D->getFriendType();
354     Hash.AddBoolean(TSI);
355     if (TSI) {
356       AddQualType(TSI->getType());
357     } else {
358       AddDecl(D->getFriendDecl());
359     }
360   }
361 
362   void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
363     // Only care about default arguments as part of the definition.
364     const bool hasDefaultArgument =
365         D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
366     Hash.AddBoolean(hasDefaultArgument);
367     if (hasDefaultArgument) {
368       AddTemplateArgument(D->getDefaultArgument());
369     }
370     Hash.AddBoolean(D->isParameterPack());
371 
372     Inherited::VisitTemplateTypeParmDecl(D);
373   }
374 
375   void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
376     // Only care about default arguments as part of the definition.
377     const bool hasDefaultArgument =
378         D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
379     Hash.AddBoolean(hasDefaultArgument);
380     if (hasDefaultArgument) {
381       AddStmt(D->getDefaultArgument());
382     }
383     Hash.AddBoolean(D->isParameterPack());
384 
385     Inherited::VisitNonTypeTemplateParmDecl(D);
386   }
387 
388   void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
389     // Only care about default arguments as part of the definition.
390     const bool hasDefaultArgument =
391         D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
392     Hash.AddBoolean(hasDefaultArgument);
393     if (hasDefaultArgument) {
394       AddTemplateArgument(D->getDefaultArgument().getArgument());
395     }
396     Hash.AddBoolean(D->isParameterPack());
397 
398     Inherited::VisitTemplateTemplateParmDecl(D);
399   }
400 
401   void VisitTemplateDecl(const TemplateDecl *D) {
402     Hash.AddTemplateParameterList(D->getTemplateParameters());
403 
404     Inherited::VisitTemplateDecl(D);
405   }
406 
407   void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
408     Hash.AddBoolean(D->isMemberSpecialization());
409     Inherited::VisitRedeclarableTemplateDecl(D);
410   }
411 
412   void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
413     AddDecl(D->getTemplatedDecl());
414     ID.AddInteger(D->getTemplatedDecl()->getODRHash());
415     Inherited::VisitFunctionTemplateDecl(D);
416   }
417 
418   void VisitEnumConstantDecl(const EnumConstantDecl *D) {
419     AddStmt(D->getInitExpr());
420     Inherited::VisitEnumConstantDecl(D);
421   }
422 };
423 } // namespace
424 
425 // Only allow a small portion of Decl's to be processed.  Remove this once
426 // all Decl's can be handled.
427 bool ODRHash::isWhitelistedDecl(const Decl *D, const DeclContext *Parent) {
428   if (D->isImplicit()) return false;
429   if (D->getDeclContext() != Parent) return false;
430 
431   switch (D->getKind()) {
432     default:
433       return false;
434     case Decl::AccessSpec:
435     case Decl::CXXConstructor:
436     case Decl::CXXDestructor:
437     case Decl::CXXMethod:
438     case Decl::EnumConstant: // Only found in EnumDecl's.
439     case Decl::Field:
440     case Decl::Friend:
441     case Decl::FunctionTemplate:
442     case Decl::StaticAssert:
443     case Decl::TypeAlias:
444     case Decl::Typedef:
445     case Decl::Var:
446       return true;
447   }
448 }
449 
450 void ODRHash::AddSubDecl(const Decl *D) {
451   assert(D && "Expecting non-null pointer.");
452 
453   ODRDeclVisitor(ID, *this).Visit(D);
454 }
455 
456 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
457   assert(Record && Record->hasDefinition() &&
458          "Expected non-null record to be a definition.");
459 
460   const DeclContext *DC = Record;
461   while (DC) {
462     if (isa<ClassTemplateSpecializationDecl>(DC)) {
463       return;
464     }
465     DC = DC->getParent();
466   }
467 
468   AddDecl(Record);
469 
470   // Filter out sub-Decls which will not be processed in order to get an
471   // accurate count of Decl's.
472   llvm::SmallVector<const Decl *, 16> Decls;
473   for (Decl *SubDecl : Record->decls()) {
474     if (isWhitelistedDecl(SubDecl, Record)) {
475       Decls.push_back(SubDecl);
476       if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) {
477         // Compute/Preload ODRHash into FunctionDecl.
478         Function->getODRHash();
479       }
480     }
481   }
482 
483   ID.AddInteger(Decls.size());
484   for (auto SubDecl : Decls) {
485     AddSubDecl(SubDecl);
486   }
487 
488   const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
489   AddBoolean(TD);
490   if (TD) {
491     AddTemplateParameterList(TD->getTemplateParameters());
492   }
493 
494   ID.AddInteger(Record->getNumBases());
495   auto Bases = Record->bases();
496   for (auto Base : Bases) {
497     AddQualType(Base.getType());
498     ID.AddInteger(Base.isVirtual());
499     ID.AddInteger(Base.getAccessSpecifierAsWritten());
500   }
501 }
502 
503 void ODRHash::AddFunctionDecl(const FunctionDecl *Function,
504                               bool SkipBody) {
505   assert(Function && "Expecting non-null pointer.");
506 
507   // Skip functions that are specializations or in specialization context.
508   const DeclContext *DC = Function;
509   while (DC) {
510     if (isa<ClassTemplateSpecializationDecl>(DC)) return;
511     if (auto *F = dyn_cast<FunctionDecl>(DC)) {
512       if (F->isFunctionTemplateSpecialization()) {
513         if (!isa<CXXMethodDecl>(DC)) return;
514         if (DC->getLexicalParent()->isFileContext()) return;
515         // Inline method specializations are the only supported
516         // specialization for now.
517       }
518     }
519     DC = DC->getParent();
520   }
521 
522   ID.AddInteger(Function->getDeclKind());
523 
524   const auto *SpecializationArgs = Function->getTemplateSpecializationArgs();
525   AddBoolean(SpecializationArgs);
526   if (SpecializationArgs) {
527     ID.AddInteger(SpecializationArgs->size());
528     for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
529       AddTemplateArgument(TA);
530     }
531   }
532 
533   if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
534     AddBoolean(Method->isConst());
535     AddBoolean(Method->isVolatile());
536   }
537 
538   ID.AddInteger(Function->getStorageClass());
539   AddBoolean(Function->isInlineSpecified());
540   AddBoolean(Function->isVirtualAsWritten());
541   AddBoolean(Function->isPure());
542   AddBoolean(Function->isDeletedAsWritten());
543   AddBoolean(Function->isExplicitlyDefaulted());
544 
545   AddDecl(Function);
546 
547   AddQualType(Function->getReturnType());
548 
549   ID.AddInteger(Function->param_size());
550   for (auto Param : Function->parameters())
551     AddSubDecl(Param);
552 
553   if (SkipBody) {
554     AddBoolean(false);
555     return;
556   }
557 
558   const bool HasBody = Function->isThisDeclarationADefinition() &&
559                        !Function->isDefaulted() && !Function->isDeleted() &&
560                        !Function->isLateTemplateParsed();
561   AddBoolean(HasBody);
562   if (!HasBody) {
563     return;
564   }
565 
566   auto *Body = Function->getBody();
567   AddBoolean(Body);
568   if (Body)
569     AddStmt(Body);
570 
571   // Filter out sub-Decls which will not be processed in order to get an
572   // accurate count of Decl's.
573   llvm::SmallVector<const Decl *, 16> Decls;
574   for (Decl *SubDecl : Function->decls()) {
575     if (isWhitelistedDecl(SubDecl, Function)) {
576       Decls.push_back(SubDecl);
577     }
578   }
579 
580   ID.AddInteger(Decls.size());
581   for (auto SubDecl : Decls) {
582     AddSubDecl(SubDecl);
583   }
584 }
585 
586 void ODRHash::AddEnumDecl(const EnumDecl *Enum) {
587   assert(Enum);
588   AddDeclarationName(Enum->getDeclName());
589 
590   AddBoolean(Enum->isScoped());
591   if (Enum->isScoped())
592     AddBoolean(Enum->isScopedUsingClassTag());
593 
594   if (Enum->getIntegerTypeSourceInfo())
595     AddQualType(Enum->getIntegerType());
596 
597   // Filter out sub-Decls which will not be processed in order to get an
598   // accurate count of Decl's.
599   llvm::SmallVector<const Decl *, 16> Decls;
600   for (Decl *SubDecl : Enum->decls()) {
601     if (isWhitelistedDecl(SubDecl, Enum)) {
602       assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl");
603       Decls.push_back(SubDecl);
604     }
605   }
606 
607   ID.AddInteger(Decls.size());
608   for (auto SubDecl : Decls) {
609     AddSubDecl(SubDecl);
610   }
611 
612 }
613 
614 void ODRHash::AddDecl(const Decl *D) {
615   assert(D && "Expecting non-null pointer.");
616   D = D->getCanonicalDecl();
617 
618   const NamedDecl *ND = dyn_cast<NamedDecl>(D);
619   AddBoolean(ND);
620   if (!ND) {
621     ID.AddInteger(D->getKind());
622     return;
623   }
624 
625   AddDeclarationName(ND->getDeclName());
626 
627   const auto *Specialization =
628             dyn_cast<ClassTemplateSpecializationDecl>(D);
629   AddBoolean(Specialization);
630   if (Specialization) {
631     const TemplateArgumentList &List = Specialization->getTemplateArgs();
632     ID.AddInteger(List.size());
633     for (const TemplateArgument &TA : List.asArray())
634       AddTemplateArgument(TA);
635   }
636 }
637 
638 namespace {
639 // Process a Type pointer.  Add* methods call back into ODRHash while Visit*
640 // methods process the relevant parts of the Type.
641 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
642   typedef TypeVisitor<ODRTypeVisitor> Inherited;
643   llvm::FoldingSetNodeID &ID;
644   ODRHash &Hash;
645 
646 public:
647   ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
648       : ID(ID), Hash(Hash) {}
649 
650   void AddStmt(Stmt *S) {
651     Hash.AddBoolean(S);
652     if (S) {
653       Hash.AddStmt(S);
654     }
655   }
656 
657   void AddDecl(Decl *D) {
658     Hash.AddBoolean(D);
659     if (D) {
660       Hash.AddDecl(D);
661     }
662   }
663 
664   void AddQualType(QualType T) {
665     Hash.AddQualType(T);
666   }
667 
668   void AddType(const Type *T) {
669     Hash.AddBoolean(T);
670     if (T) {
671       Hash.AddType(T);
672     }
673   }
674 
675   void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
676     Hash.AddBoolean(NNS);
677     if (NNS) {
678       Hash.AddNestedNameSpecifier(NNS);
679     }
680   }
681 
682   void AddIdentifierInfo(const IdentifierInfo *II) {
683     Hash.AddBoolean(II);
684     if (II) {
685       Hash.AddIdentifierInfo(II);
686     }
687   }
688 
689   void VisitQualifiers(Qualifiers Quals) {
690     ID.AddInteger(Quals.getAsOpaqueValue());
691   }
692 
693   void Visit(const Type *T) {
694     ID.AddInteger(T->getTypeClass());
695     Inherited::Visit(T);
696   }
697 
698   void VisitType(const Type *T) {}
699 
700   void VisitAdjustedType(const AdjustedType *T) {
701     AddQualType(T->getOriginalType());
702     AddQualType(T->getAdjustedType());
703     VisitType(T);
704   }
705 
706   void VisitDecayedType(const DecayedType *T) {
707     AddQualType(T->getDecayedType());
708     AddQualType(T->getPointeeType());
709     VisitAdjustedType(T);
710   }
711 
712   void VisitArrayType(const ArrayType *T) {
713     AddQualType(T->getElementType());
714     ID.AddInteger(T->getSizeModifier());
715     VisitQualifiers(T->getIndexTypeQualifiers());
716     VisitType(T);
717   }
718   void VisitConstantArrayType(const ConstantArrayType *T) {
719     T->getSize().Profile(ID);
720     VisitArrayType(T);
721   }
722 
723   void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
724     AddStmt(T->getSizeExpr());
725     VisitArrayType(T);
726   }
727 
728   void VisitIncompleteArrayType(const IncompleteArrayType *T) {
729     VisitArrayType(T);
730   }
731 
732   void VisitVariableArrayType(const VariableArrayType *T) {
733     AddStmt(T->getSizeExpr());
734     VisitArrayType(T);
735   }
736 
737   void VisitAttributedType(const AttributedType *T) {
738     ID.AddInteger(T->getAttrKind());
739     AddQualType(T->getModifiedType());
740     AddQualType(T->getEquivalentType());
741 
742     VisitType(T);
743   }
744 
745   void VisitBlockPointerType(const BlockPointerType *T) {
746     AddQualType(T->getPointeeType());
747     VisitType(T);
748   }
749 
750   void VisitBuiltinType(const BuiltinType *T) {
751     ID.AddInteger(T->getKind());
752     VisitType(T);
753   }
754 
755   void VisitComplexType(const ComplexType *T) {
756     AddQualType(T->getElementType());
757     VisitType(T);
758   }
759 
760   void VisitDecltypeType(const DecltypeType *T) {
761     AddStmt(T->getUnderlyingExpr());
762     AddQualType(T->getUnderlyingType());
763     VisitType(T);
764   }
765 
766   void VisitDependentDecltypeType(const DependentDecltypeType *T) {
767     VisitDecltypeType(T);
768   }
769 
770   void VisitDeducedType(const DeducedType *T) {
771     AddQualType(T->getDeducedType());
772     VisitType(T);
773   }
774 
775   void VisitAutoType(const AutoType *T) {
776     ID.AddInteger((unsigned)T->getKeyword());
777     VisitDeducedType(T);
778   }
779 
780   void VisitDeducedTemplateSpecializationType(
781       const DeducedTemplateSpecializationType *T) {
782     Hash.AddTemplateName(T->getTemplateName());
783     VisitDeducedType(T);
784   }
785 
786   void VisitDependentAddressSpaceType(const DependentAddressSpaceType *T) {
787     AddQualType(T->getPointeeType());
788     AddStmt(T->getAddrSpaceExpr());
789     VisitType(T);
790   }
791 
792   void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) {
793     AddQualType(T->getElementType());
794     AddStmt(T->getSizeExpr());
795     VisitType(T);
796   }
797 
798   void VisitFunctionType(const FunctionType *T) {
799     AddQualType(T->getReturnType());
800     T->getExtInfo().Profile(ID);
801     Hash.AddBoolean(T->isConst());
802     Hash.AddBoolean(T->isVolatile());
803     Hash.AddBoolean(T->isRestrict());
804     VisitType(T);
805   }
806 
807   void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
808     VisitFunctionType(T);
809   }
810 
811   void VisitFunctionProtoType(const FunctionProtoType *T) {
812     ID.AddInteger(T->getNumParams());
813     for (auto ParamType : T->getParamTypes())
814       AddQualType(ParamType);
815 
816     VisitFunctionType(T);
817   }
818 
819   void VisitInjectedClassNameType(const InjectedClassNameType *T) {
820     AddDecl(T->getDecl());
821     VisitType(T);
822   }
823 
824   void VisitMemberPointerType(const MemberPointerType *T) {
825     AddQualType(T->getPointeeType());
826     AddType(T->getClass());
827     VisitType(T);
828   }
829 
830   void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
831     AddQualType(T->getPointeeType());
832     VisitType(T);
833   }
834 
835   void VisitObjCObjectType(const ObjCObjectType *T) {
836     AddDecl(T->getInterface());
837 
838     auto TypeArgs = T->getTypeArgsAsWritten();
839     ID.AddInteger(TypeArgs.size());
840     for (auto Arg : TypeArgs) {
841       AddQualType(Arg);
842     }
843 
844     auto Protocols = T->getProtocols();
845     ID.AddInteger(Protocols.size());
846     for (auto Protocol : Protocols) {
847       AddDecl(Protocol);
848     }
849 
850     Hash.AddBoolean(T->isKindOfType());
851 
852     VisitType(T);
853   }
854 
855   void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
856     // This type is handled by the parent type ObjCObjectType.
857     VisitObjCObjectType(T);
858   }
859 
860   void VisitObjCTypeParamType(const ObjCTypeParamType *T) {
861     AddDecl(T->getDecl());
862     auto Protocols = T->getProtocols();
863     ID.AddInteger(Protocols.size());
864     for (auto Protocol : Protocols) {
865       AddDecl(Protocol);
866     }
867 
868     VisitType(T);
869   }
870 
871   void VisitPackExpansionType(const PackExpansionType *T) {
872     AddQualType(T->getPattern());
873     VisitType(T);
874   }
875 
876   void VisitParenType(const ParenType *T) {
877     AddQualType(T->getInnerType());
878     VisitType(T);
879   }
880 
881   void VisitPipeType(const PipeType *T) {
882     AddQualType(T->getElementType());
883     Hash.AddBoolean(T->isReadOnly());
884     VisitType(T);
885   }
886 
887   void VisitPointerType(const PointerType *T) {
888     AddQualType(T->getPointeeType());
889     VisitType(T);
890   }
891 
892   void VisitReferenceType(const ReferenceType *T) {
893     AddQualType(T->getPointeeTypeAsWritten());
894     VisitType(T);
895   }
896 
897   void VisitLValueReferenceType(const LValueReferenceType *T) {
898     VisitReferenceType(T);
899   }
900 
901   void VisitRValueReferenceType(const RValueReferenceType *T) {
902     VisitReferenceType(T);
903   }
904 
905   void
906   VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
907     AddType(T->getReplacedParameter());
908     Hash.AddTemplateArgument(T->getArgumentPack());
909     VisitType(T);
910   }
911 
912   void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
913     AddType(T->getReplacedParameter());
914     AddQualType(T->getReplacementType());
915     VisitType(T);
916   }
917 
918   void VisitTagType(const TagType *T) {
919     AddDecl(T->getDecl());
920     VisitType(T);
921   }
922 
923   void VisitRecordType(const RecordType *T) { VisitTagType(T); }
924   void VisitEnumType(const EnumType *T) { VisitTagType(T); }
925 
926   void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
927     ID.AddInteger(T->getNumArgs());
928     for (const auto &TA : T->template_arguments()) {
929       Hash.AddTemplateArgument(TA);
930     }
931     Hash.AddTemplateName(T->getTemplateName());
932     VisitType(T);
933   }
934 
935   void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
936     ID.AddInteger(T->getDepth());
937     ID.AddInteger(T->getIndex());
938     Hash.AddBoolean(T->isParameterPack());
939     AddDecl(T->getDecl());
940   }
941 
942   void VisitTypedefType(const TypedefType *T) {
943     AddDecl(T->getDecl());
944     QualType UnderlyingType = T->getDecl()->getUnderlyingType();
945     VisitQualifiers(UnderlyingType.getQualifiers());
946     while (true) {
947       if (const TypedefType *Underlying =
948               dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
949         UnderlyingType = Underlying->getDecl()->getUnderlyingType();
950         continue;
951       }
952       if (const ElaboratedType *Underlying =
953               dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
954         UnderlyingType = Underlying->getNamedType();
955         continue;
956       }
957 
958       break;
959     }
960     AddType(UnderlyingType.getTypePtr());
961     VisitType(T);
962   }
963 
964   void VisitTypeOfExprType(const TypeOfExprType *T) {
965     AddStmt(T->getUnderlyingExpr());
966     Hash.AddBoolean(T->isSugared());
967     if (T->isSugared())
968       AddQualType(T->desugar());
969 
970     VisitType(T);
971   }
972   void VisitTypeOfType(const TypeOfType *T) {
973     AddQualType(T->getUnderlyingType());
974     VisitType(T);
975   }
976 
977   void VisitTypeWithKeyword(const TypeWithKeyword *T) {
978     ID.AddInteger(T->getKeyword());
979     VisitType(T);
980   };
981 
982   void VisitDependentNameType(const DependentNameType *T) {
983     AddNestedNameSpecifier(T->getQualifier());
984     AddIdentifierInfo(T->getIdentifier());
985     VisitTypeWithKeyword(T);
986   }
987 
988   void VisitDependentTemplateSpecializationType(
989       const DependentTemplateSpecializationType *T) {
990     AddIdentifierInfo(T->getIdentifier());
991     AddNestedNameSpecifier(T->getQualifier());
992     ID.AddInteger(T->getNumArgs());
993     for (const auto &TA : T->template_arguments()) {
994       Hash.AddTemplateArgument(TA);
995     }
996     VisitTypeWithKeyword(T);
997   }
998 
999   void VisitElaboratedType(const ElaboratedType *T) {
1000     AddNestedNameSpecifier(T->getQualifier());
1001     AddQualType(T->getNamedType());
1002     VisitTypeWithKeyword(T);
1003   }
1004 
1005   void VisitUnaryTransformType(const UnaryTransformType *T) {
1006     AddQualType(T->getUnderlyingType());
1007     AddQualType(T->getBaseType());
1008     VisitType(T);
1009   }
1010 
1011   void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
1012     AddDecl(T->getDecl());
1013     VisitType(T);
1014   }
1015 
1016   void VisitVectorType(const VectorType *T) {
1017     AddQualType(T->getElementType());
1018     ID.AddInteger(T->getNumElements());
1019     ID.AddInteger(T->getVectorKind());
1020     VisitType(T);
1021   }
1022 
1023   void VisitExtVectorType(const ExtVectorType * T) {
1024     VisitVectorType(T);
1025   }
1026 };
1027 } // namespace
1028 
1029 void ODRHash::AddType(const Type *T) {
1030   assert(T && "Expecting non-null pointer.");
1031   ODRTypeVisitor(ID, *this).Visit(T);
1032 }
1033 
1034 void ODRHash::AddQualType(QualType T) {
1035   AddBoolean(T.isNull());
1036   if (T.isNull())
1037     return;
1038   SplitQualType split = T.split();
1039   ID.AddInteger(split.Quals.getAsOpaqueValue());
1040   AddType(split.Ty);
1041 }
1042 
1043 void ODRHash::AddBoolean(bool Value) {
1044   Bools.push_back(Value);
1045 }
1046