1 //===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the ODRHash class, which calculates a hash based
11 /// on AST nodes, which is stable across different runs.
13 //===----------------------------------------------------------------------===//
15 #include "clang/AST/ODRHash.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/NestedNameSpecifier.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "clang/AST/TypeVisitor.h"
22 using namespace clang;
24 void ODRHash::AddStmt(const Stmt *S) {
25 assert(S && "Expecting non-null pointer.");
26 S->ProcessODRHash(ID, *this);
29 void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
30 assert(II && "Expecting non-null pointer.");
31 ID.AddString(II->getName());
34 void ODRHash::AddDeclarationName(DeclarationName Name, bool TreatAsDecl) {
36 // Matches the NamedDecl check in AddDecl
39 AddDeclarationNameImpl(Name);
42 // Matches the ClassTemplateSpecializationDecl check in AddDecl
46 void ODRHash::AddDeclarationNameImpl(DeclarationName Name) {
47 // Index all DeclarationName and use index numbers to refer to them.
48 auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
49 ID.AddInteger(Result.first->second);
51 // If found in map, the DeclarationName has previously been processed.
55 // First time processing each DeclarationName, also process its details.
56 AddBoolean(Name.isEmpty());
60 auto Kind = Name.getNameKind();
63 case DeclarationName::Identifier:
64 AddIdentifierInfo(Name.getAsIdentifierInfo());
66 case DeclarationName::ObjCZeroArgSelector:
67 case DeclarationName::ObjCOneArgSelector:
68 case DeclarationName::ObjCMultiArgSelector: {
69 Selector S = Name.getObjCSelector();
70 AddBoolean(S.isNull());
71 AddBoolean(S.isKeywordSelector());
72 AddBoolean(S.isUnarySelector());
73 unsigned NumArgs = S.getNumArgs();
74 ID.AddInteger(NumArgs);
75 for (unsigned i = 0; i < NumArgs; ++i) {
76 const IdentifierInfo *II = S.getIdentifierInfoForSlot(i);
79 AddIdentifierInfo(II);
84 case DeclarationName::CXXConstructorName:
85 case DeclarationName::CXXDestructorName:
86 AddQualType(Name.getCXXNameType());
88 case DeclarationName::CXXOperatorName:
89 ID.AddInteger(Name.getCXXOverloadedOperator());
91 case DeclarationName::CXXLiteralOperatorName:
92 AddIdentifierInfo(Name.getCXXLiteralIdentifier());
94 case DeclarationName::CXXConversionFunctionName:
95 AddQualType(Name.getCXXNameType());
97 case DeclarationName::CXXUsingDirective:
99 case DeclarationName::CXXDeductionGuideName: {
100 auto *Template = Name.getCXXDeductionGuideTemplate();
101 AddBoolean(Template);
109 void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
110 assert(NNS && "Expecting non-null pointer.");
111 const auto *Prefix = NNS->getPrefix();
114 AddNestedNameSpecifier(Prefix);
116 auto Kind = NNS->getKind();
119 case NestedNameSpecifier::Identifier:
120 AddIdentifierInfo(NNS->getAsIdentifier());
122 case NestedNameSpecifier::Namespace:
123 AddDecl(NNS->getAsNamespace());
125 case NestedNameSpecifier::NamespaceAlias:
126 AddDecl(NNS->getAsNamespaceAlias());
128 case NestedNameSpecifier::TypeSpec:
129 case NestedNameSpecifier::TypeSpecWithTemplate:
130 AddType(NNS->getAsType());
132 case NestedNameSpecifier::Global:
133 case NestedNameSpecifier::Super:
138 void ODRHash::AddTemplateName(TemplateName Name) {
139 auto Kind = Name.getKind();
143 case TemplateName::Template:
144 AddDecl(Name.getAsTemplateDecl());
146 // TODO: Support these cases.
147 case TemplateName::OverloadedTemplate:
148 case TemplateName::AssumedTemplate:
149 case TemplateName::QualifiedTemplate:
150 case TemplateName::DependentTemplate:
151 case TemplateName::SubstTemplateTemplateParm:
152 case TemplateName::SubstTemplateTemplateParmPack:
157 void ODRHash::AddTemplateArgument(TemplateArgument TA) {
158 const auto Kind = TA.getKind();
162 case TemplateArgument::Null:
163 llvm_unreachable("Expected valid TemplateArgument");
164 case TemplateArgument::Type:
165 AddQualType(TA.getAsType());
167 case TemplateArgument::Declaration:
168 AddDecl(TA.getAsDecl());
170 case TemplateArgument::NullPtr:
171 case TemplateArgument::Integral:
173 case TemplateArgument::Template:
174 case TemplateArgument::TemplateExpansion:
175 AddTemplateName(TA.getAsTemplateOrTemplatePattern());
177 case TemplateArgument::Expression:
178 AddStmt(TA.getAsExpr());
180 case TemplateArgument::Pack:
181 ID.AddInteger(TA.pack_size());
182 for (auto SubTA : TA.pack_elements()) {
183 AddTemplateArgument(SubTA);
189 void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
190 assert(TPL && "Expecting non-null pointer.");
192 ID.AddInteger(TPL->size());
193 for (auto *ND : TPL->asArray()) {
198 void ODRHash::clear() {
204 unsigned ODRHash::CalculateHash() {
205 // Append the bools to the end of the data segment backwards. This allows
206 // for the bools data to be compressed 32 times smaller compared to using
208 const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
209 const unsigned size = Bools.size();
210 const unsigned remainder = size % unsigned_bits;
211 const unsigned loops = size / unsigned_bits;
212 auto I = Bools.rbegin();
214 for (unsigned i = 0; i < remainder; ++i) {
219 ID.AddInteger(value);
221 for (unsigned i = 0; i < loops; ++i) {
223 for (unsigned j = 0; j < unsigned_bits; ++j) {
228 ID.AddInteger(value);
231 assert(I == Bools.rend());
233 return ID.ComputeHash();
237 // Process a Decl pointer. Add* methods call back into ODRHash while Visit*
238 // methods process the relevant parts of the Decl.
239 class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
240 typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
241 llvm::FoldingSetNodeID &ID;
245 ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
246 : ID(ID), Hash(Hash) {}
248 void AddStmt(const Stmt *S) {
255 void AddIdentifierInfo(const IdentifierInfo *II) {
258 Hash.AddIdentifierInfo(II);
262 void AddQualType(QualType T) {
266 void AddDecl(const Decl *D) {
273 void AddTemplateArgument(TemplateArgument TA) {
274 Hash.AddTemplateArgument(TA);
277 void Visit(const Decl *D) {
278 ID.AddInteger(D->getKind());
282 void VisitNamedDecl(const NamedDecl *D) {
283 Hash.AddDeclarationName(D->getDeclName());
284 Inherited::VisitNamedDecl(D);
287 void VisitValueDecl(const ValueDecl *D) {
288 if (!isa<FunctionDecl>(D)) {
289 AddQualType(D->getType());
291 Inherited::VisitValueDecl(D);
294 void VisitVarDecl(const VarDecl *D) {
295 Hash.AddBoolean(D->isStaticLocal());
296 Hash.AddBoolean(D->isConstexpr());
297 const bool HasInit = D->hasInit();
298 Hash.AddBoolean(HasInit);
300 AddStmt(D->getInit());
302 Inherited::VisitVarDecl(D);
305 void VisitParmVarDecl(const ParmVarDecl *D) {
306 // TODO: Handle default arguments.
307 Inherited::VisitParmVarDecl(D);
310 void VisitAccessSpecDecl(const AccessSpecDecl *D) {
311 ID.AddInteger(D->getAccess());
312 Inherited::VisitAccessSpecDecl(D);
315 void VisitStaticAssertDecl(const StaticAssertDecl *D) {
316 AddStmt(D->getAssertExpr());
317 AddStmt(D->getMessage());
319 Inherited::VisitStaticAssertDecl(D);
322 void VisitFieldDecl(const FieldDecl *D) {
323 const bool IsBitfield = D->isBitField();
324 Hash.AddBoolean(IsBitfield);
327 AddStmt(D->getBitWidth());
330 Hash.AddBoolean(D->isMutable());
331 AddStmt(D->getInClassInitializer());
333 Inherited::VisitFieldDecl(D);
336 void VisitFunctionDecl(const FunctionDecl *D) {
337 // Handled by the ODRHash for FunctionDecl
338 ID.AddInteger(D->getODRHash());
340 Inherited::VisitFunctionDecl(D);
343 void VisitCXXMethodDecl(const CXXMethodDecl *D) {
344 // Handled by the ODRHash for FunctionDecl
346 Inherited::VisitCXXMethodDecl(D);
349 void VisitTypedefNameDecl(const TypedefNameDecl *D) {
350 AddQualType(D->getUnderlyingType());
352 Inherited::VisitTypedefNameDecl(D);
355 void VisitTypedefDecl(const TypedefDecl *D) {
356 Inherited::VisitTypedefDecl(D);
359 void VisitTypeAliasDecl(const TypeAliasDecl *D) {
360 Inherited::VisitTypeAliasDecl(D);
363 void VisitFriendDecl(const FriendDecl *D) {
364 TypeSourceInfo *TSI = D->getFriendType();
365 Hash.AddBoolean(TSI);
367 AddQualType(TSI->getType());
369 AddDecl(D->getFriendDecl());
373 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
374 // Only care about default arguments as part of the definition.
375 const bool hasDefaultArgument =
376 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
377 Hash.AddBoolean(hasDefaultArgument);
378 if (hasDefaultArgument) {
379 AddTemplateArgument(D->getDefaultArgument());
381 Hash.AddBoolean(D->isParameterPack());
383 Inherited::VisitTemplateTypeParmDecl(D);
386 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
387 // Only care about default arguments as part of the definition.
388 const bool hasDefaultArgument =
389 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
390 Hash.AddBoolean(hasDefaultArgument);
391 if (hasDefaultArgument) {
392 AddStmt(D->getDefaultArgument());
394 Hash.AddBoolean(D->isParameterPack());
396 Inherited::VisitNonTypeTemplateParmDecl(D);
399 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
400 // Only care about default arguments as part of the definition.
401 const bool hasDefaultArgument =
402 D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
403 Hash.AddBoolean(hasDefaultArgument);
404 if (hasDefaultArgument) {
405 AddTemplateArgument(D->getDefaultArgument().getArgument());
407 Hash.AddBoolean(D->isParameterPack());
409 Inherited::VisitTemplateTemplateParmDecl(D);
412 void VisitTemplateDecl(const TemplateDecl *D) {
413 Hash.AddTemplateParameterList(D->getTemplateParameters());
415 Inherited::VisitTemplateDecl(D);
418 void VisitRedeclarableTemplateDecl(const RedeclarableTemplateDecl *D) {
419 Hash.AddBoolean(D->isMemberSpecialization());
420 Inherited::VisitRedeclarableTemplateDecl(D);
423 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
424 AddDecl(D->getTemplatedDecl());
425 ID.AddInteger(D->getTemplatedDecl()->getODRHash());
426 Inherited::VisitFunctionTemplateDecl(D);
429 void VisitEnumConstantDecl(const EnumConstantDecl *D) {
430 AddStmt(D->getInitExpr());
431 Inherited::VisitEnumConstantDecl(D);
436 // Only allow a small portion of Decl's to be processed. Remove this once
437 // all Decl's can be handled.
438 bool ODRHash::isWhitelistedDecl(const Decl *D, const DeclContext *Parent) {
439 if (D->isImplicit()) return false;
440 if (D->getDeclContext() != Parent) return false;
442 switch (D->getKind()) {
445 case Decl::AccessSpec:
446 case Decl::CXXConstructor:
447 case Decl::CXXDestructor:
448 case Decl::CXXMethod:
449 case Decl::EnumConstant: // Only found in EnumDecl's.
452 case Decl::FunctionTemplate:
453 case Decl::StaticAssert:
454 case Decl::TypeAlias:
461 void ODRHash::AddSubDecl(const Decl *D) {
462 assert(D && "Expecting non-null pointer.");
464 ODRDeclVisitor(ID, *this).Visit(D);
467 void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
468 assert(Record && Record->hasDefinition() &&
469 "Expected non-null record to be a definition.");
471 const DeclContext *DC = Record;
473 if (isa<ClassTemplateSpecializationDecl>(DC)) {
476 DC = DC->getParent();
481 // Filter out sub-Decls which will not be processed in order to get an
482 // accurate count of Decl's.
483 llvm::SmallVector<const Decl *, 16> Decls;
484 for (Decl *SubDecl : Record->decls()) {
485 if (isWhitelistedDecl(SubDecl, Record)) {
486 Decls.push_back(SubDecl);
487 if (auto *Function = dyn_cast<FunctionDecl>(SubDecl)) {
488 // Compute/Preload ODRHash into FunctionDecl.
489 Function->getODRHash();
494 ID.AddInteger(Decls.size());
495 for (auto SubDecl : Decls) {
499 const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
502 AddTemplateParameterList(TD->getTemplateParameters());
505 ID.AddInteger(Record->getNumBases());
506 auto Bases = Record->bases();
507 for (auto Base : Bases) {
508 AddQualType(Base.getType());
509 ID.AddInteger(Base.isVirtual());
510 ID.AddInteger(Base.getAccessSpecifierAsWritten());
514 void ODRHash::AddFunctionDecl(const FunctionDecl *Function,
516 assert(Function && "Expecting non-null pointer.");
518 // Skip functions that are specializations or in specialization context.
519 const DeclContext *DC = Function;
521 if (isa<ClassTemplateSpecializationDecl>(DC)) return;
522 if (auto *F = dyn_cast<FunctionDecl>(DC)) {
523 if (F->isFunctionTemplateSpecialization()) {
524 if (!isa<CXXMethodDecl>(DC)) return;
525 if (DC->getLexicalParent()->isFileContext()) return;
526 // Inline method specializations are the only supported
527 // specialization for now.
530 DC = DC->getParent();
533 ID.AddInteger(Function->getDeclKind());
535 const auto *SpecializationArgs = Function->getTemplateSpecializationArgs();
536 AddBoolean(SpecializationArgs);
537 if (SpecializationArgs) {
538 ID.AddInteger(SpecializationArgs->size());
539 for (const TemplateArgument &TA : SpecializationArgs->asArray()) {
540 AddTemplateArgument(TA);
544 if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
545 AddBoolean(Method->isConst());
546 AddBoolean(Method->isVolatile());
549 ID.AddInteger(Function->getStorageClass());
550 AddBoolean(Function->isInlineSpecified());
551 AddBoolean(Function->isVirtualAsWritten());
552 AddBoolean(Function->isPure());
553 AddBoolean(Function->isDeletedAsWritten());
554 AddBoolean(Function->isExplicitlyDefaulted());
558 AddQualType(Function->getReturnType());
560 ID.AddInteger(Function->param_size());
561 for (auto Param : Function->parameters())
569 const bool HasBody = Function->isThisDeclarationADefinition() &&
570 !Function->isDefaulted() && !Function->isDeleted() &&
571 !Function->isLateTemplateParsed();
577 auto *Body = Function->getBody();
582 // Filter out sub-Decls which will not be processed in order to get an
583 // accurate count of Decl's.
584 llvm::SmallVector<const Decl *, 16> Decls;
585 for (Decl *SubDecl : Function->decls()) {
586 if (isWhitelistedDecl(SubDecl, Function)) {
587 Decls.push_back(SubDecl);
591 ID.AddInteger(Decls.size());
592 for (auto SubDecl : Decls) {
597 void ODRHash::AddEnumDecl(const EnumDecl *Enum) {
599 AddDeclarationName(Enum->getDeclName());
601 AddBoolean(Enum->isScoped());
602 if (Enum->isScoped())
603 AddBoolean(Enum->isScopedUsingClassTag());
605 if (Enum->getIntegerTypeSourceInfo())
606 AddQualType(Enum->getIntegerType());
608 // Filter out sub-Decls which will not be processed in order to get an
609 // accurate count of Decl's.
610 llvm::SmallVector<const Decl *, 16> Decls;
611 for (Decl *SubDecl : Enum->decls()) {
612 if (isWhitelistedDecl(SubDecl, Enum)) {
613 assert(isa<EnumConstantDecl>(SubDecl) && "Unexpected Decl");
614 Decls.push_back(SubDecl);
618 ID.AddInteger(Decls.size());
619 for (auto SubDecl : Decls) {
625 void ODRHash::AddDecl(const Decl *D) {
626 assert(D && "Expecting non-null pointer.");
627 D = D->getCanonicalDecl();
629 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
632 ID.AddInteger(D->getKind());
636 AddDeclarationName(ND->getDeclName());
638 const auto *Specialization =
639 dyn_cast<ClassTemplateSpecializationDecl>(D);
640 AddBoolean(Specialization);
641 if (Specialization) {
642 const TemplateArgumentList &List = Specialization->getTemplateArgs();
643 ID.AddInteger(List.size());
644 for (const TemplateArgument &TA : List.asArray())
645 AddTemplateArgument(TA);
650 // Process a Type pointer. Add* methods call back into ODRHash while Visit*
651 // methods process the relevant parts of the Type.
652 class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
653 typedef TypeVisitor<ODRTypeVisitor> Inherited;
654 llvm::FoldingSetNodeID &ID;
658 ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
659 : ID(ID), Hash(Hash) {}
661 void AddStmt(Stmt *S) {
668 void AddDecl(Decl *D) {
675 void AddQualType(QualType T) {
679 void AddType(const Type *T) {
686 void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
687 Hash.AddBoolean(NNS);
689 Hash.AddNestedNameSpecifier(NNS);
693 void AddIdentifierInfo(const IdentifierInfo *II) {
696 Hash.AddIdentifierInfo(II);
700 void VisitQualifiers(Qualifiers Quals) {
701 ID.AddInteger(Quals.getAsOpaqueValue());
704 // Return the RecordType if the typedef only strips away a keyword.
705 // Otherwise, return the original type.
706 static const Type *RemoveTypedef(const Type *T) {
707 const auto *TypedefT = dyn_cast<TypedefType>(T);
712 const TypedefNameDecl *D = TypedefT->getDecl();
713 QualType UnderlyingType = D->getUnderlyingType();
715 if (UnderlyingType.hasLocalQualifiers()) {
719 const auto *ElaboratedT = dyn_cast<ElaboratedType>(UnderlyingType);
724 if (ElaboratedT->getQualifier() != nullptr) {
728 QualType NamedType = ElaboratedT->getNamedType();
729 if (NamedType.hasLocalQualifiers()) {
733 const auto *RecordT = dyn_cast<RecordType>(NamedType);
738 const IdentifierInfo *TypedefII = TypedefT->getDecl()->getIdentifier();
739 const IdentifierInfo *RecordII = RecordT->getDecl()->getIdentifier();
740 if (!TypedefII || !RecordII ||
741 TypedefII->getName() != RecordII->getName()) {
748 void Visit(const Type *T) {
749 T = RemoveTypedef(T);
750 ID.AddInteger(T->getTypeClass());
754 void VisitType(const Type *T) {}
756 void VisitAdjustedType(const AdjustedType *T) {
757 QualType Original = T->getOriginalType();
758 QualType Adjusted = T->getAdjustedType();
760 // The original type and pointee type can be the same, as in the case of
761 // function pointers decaying to themselves. Set a bool and only process
762 // the type once, to prevent doubling the work.
763 SplitQualType split = Adjusted.split();
764 if (auto Pointer = dyn_cast<PointerType>(split.Ty)) {
765 if (Pointer->getPointeeType() == Original) {
766 Hash.AddBoolean(true);
767 ID.AddInteger(split.Quals.getAsOpaqueValue());
768 AddQualType(Original);
774 // The original type and pointee type are different, such as in the case
775 // of a array decaying to an element pointer. Set a bool to false and
776 // process both types.
777 Hash.AddBoolean(false);
778 AddQualType(Original);
779 AddQualType(Adjusted);
784 void VisitDecayedType(const DecayedType *T) {
785 // getDecayedType and getPointeeType are derived from getAdjustedType
786 // and don't need to be separately processed.
787 VisitAdjustedType(T);
790 void VisitArrayType(const ArrayType *T) {
791 AddQualType(T->getElementType());
792 ID.AddInteger(T->getSizeModifier());
793 VisitQualifiers(T->getIndexTypeQualifiers());
796 void VisitConstantArrayType(const ConstantArrayType *T) {
797 T->getSize().Profile(ID);
801 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
802 AddStmt(T->getSizeExpr());
806 void VisitIncompleteArrayType(const IncompleteArrayType *T) {
810 void VisitVariableArrayType(const VariableArrayType *T) {
811 AddStmt(T->getSizeExpr());
815 void VisitAttributedType(const AttributedType *T) {
816 ID.AddInteger(T->getAttrKind());
817 AddQualType(T->getModifiedType());
818 AddQualType(T->getEquivalentType());
823 void VisitBlockPointerType(const BlockPointerType *T) {
824 AddQualType(T->getPointeeType());
828 void VisitBuiltinType(const BuiltinType *T) {
829 ID.AddInteger(T->getKind());
833 void VisitComplexType(const ComplexType *T) {
834 AddQualType(T->getElementType());
838 void VisitDecltypeType(const DecltypeType *T) {
839 AddStmt(T->getUnderlyingExpr());
840 AddQualType(T->getUnderlyingType());
844 void VisitDependentDecltypeType(const DependentDecltypeType *T) {
845 VisitDecltypeType(T);
848 void VisitDeducedType(const DeducedType *T) {
849 AddQualType(T->getDeducedType());
853 void VisitAutoType(const AutoType *T) {
854 ID.AddInteger((unsigned)T->getKeyword());
858 void VisitDeducedTemplateSpecializationType(
859 const DeducedTemplateSpecializationType *T) {
860 Hash.AddTemplateName(T->getTemplateName());
864 void VisitDependentAddressSpaceType(const DependentAddressSpaceType *T) {
865 AddQualType(T->getPointeeType());
866 AddStmt(T->getAddrSpaceExpr());
870 void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) {
871 AddQualType(T->getElementType());
872 AddStmt(T->getSizeExpr());
876 void VisitFunctionType(const FunctionType *T) {
877 AddQualType(T->getReturnType());
878 T->getExtInfo().Profile(ID);
879 Hash.AddBoolean(T->isConst());
880 Hash.AddBoolean(T->isVolatile());
881 Hash.AddBoolean(T->isRestrict());
885 void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
886 VisitFunctionType(T);
889 void VisitFunctionProtoType(const FunctionProtoType *T) {
890 ID.AddInteger(T->getNumParams());
891 for (auto ParamType : T->getParamTypes())
892 AddQualType(ParamType);
894 VisitFunctionType(T);
897 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
898 AddDecl(T->getDecl());
902 void VisitMemberPointerType(const MemberPointerType *T) {
903 AddQualType(T->getPointeeType());
904 AddType(T->getClass());
908 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
909 AddQualType(T->getPointeeType());
913 void VisitObjCObjectType(const ObjCObjectType *T) {
914 AddDecl(T->getInterface());
916 auto TypeArgs = T->getTypeArgsAsWritten();
917 ID.AddInteger(TypeArgs.size());
918 for (auto Arg : TypeArgs) {
922 auto Protocols = T->getProtocols();
923 ID.AddInteger(Protocols.size());
924 for (auto Protocol : Protocols) {
928 Hash.AddBoolean(T->isKindOfType());
933 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
934 // This type is handled by the parent type ObjCObjectType.
935 VisitObjCObjectType(T);
938 void VisitObjCTypeParamType(const ObjCTypeParamType *T) {
939 AddDecl(T->getDecl());
940 auto Protocols = T->getProtocols();
941 ID.AddInteger(Protocols.size());
942 for (auto Protocol : Protocols) {
949 void VisitPackExpansionType(const PackExpansionType *T) {
950 AddQualType(T->getPattern());
954 void VisitParenType(const ParenType *T) {
955 AddQualType(T->getInnerType());
959 void VisitPipeType(const PipeType *T) {
960 AddQualType(T->getElementType());
961 Hash.AddBoolean(T->isReadOnly());
965 void VisitPointerType(const PointerType *T) {
966 AddQualType(T->getPointeeType());
970 void VisitReferenceType(const ReferenceType *T) {
971 AddQualType(T->getPointeeTypeAsWritten());
975 void VisitLValueReferenceType(const LValueReferenceType *T) {
976 VisitReferenceType(T);
979 void VisitRValueReferenceType(const RValueReferenceType *T) {
980 VisitReferenceType(T);
984 VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
985 AddType(T->getReplacedParameter());
986 Hash.AddTemplateArgument(T->getArgumentPack());
990 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
991 AddType(T->getReplacedParameter());
992 AddQualType(T->getReplacementType());
996 void VisitTagType(const TagType *T) {
997 AddDecl(T->getDecl());
1001 void VisitRecordType(const RecordType *T) { VisitTagType(T); }
1002 void VisitEnumType(const EnumType *T) { VisitTagType(T); }
1004 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
1005 ID.AddInteger(T->getNumArgs());
1006 for (const auto &TA : T->template_arguments()) {
1007 Hash.AddTemplateArgument(TA);
1009 Hash.AddTemplateName(T->getTemplateName());
1013 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1014 ID.AddInteger(T->getDepth());
1015 ID.AddInteger(T->getIndex());
1016 Hash.AddBoolean(T->isParameterPack());
1017 AddDecl(T->getDecl());
1020 void VisitTypedefType(const TypedefType *T) {
1021 AddDecl(T->getDecl());
1022 QualType UnderlyingType = T->getDecl()->getUnderlyingType();
1023 VisitQualifiers(UnderlyingType.getQualifiers());
1025 if (const TypedefType *Underlying =
1026 dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
1027 UnderlyingType = Underlying->getDecl()->getUnderlyingType();
1030 if (const ElaboratedType *Underlying =
1031 dyn_cast<ElaboratedType>(UnderlyingType.getTypePtr())) {
1032 UnderlyingType = Underlying->getNamedType();
1038 AddType(UnderlyingType.getTypePtr());
1042 void VisitTypeOfExprType(const TypeOfExprType *T) {
1043 AddStmt(T->getUnderlyingExpr());
1044 Hash.AddBoolean(T->isSugared());
1046 AddQualType(T->desugar());
1050 void VisitTypeOfType(const TypeOfType *T) {
1051 AddQualType(T->getUnderlyingType());
1055 void VisitTypeWithKeyword(const TypeWithKeyword *T) {
1056 ID.AddInteger(T->getKeyword());
1060 void VisitDependentNameType(const DependentNameType *T) {
1061 AddNestedNameSpecifier(T->getQualifier());
1062 AddIdentifierInfo(T->getIdentifier());
1063 VisitTypeWithKeyword(T);
1066 void VisitDependentTemplateSpecializationType(
1067 const DependentTemplateSpecializationType *T) {
1068 AddIdentifierInfo(T->getIdentifier());
1069 AddNestedNameSpecifier(T->getQualifier());
1070 ID.AddInteger(T->getNumArgs());
1071 for (const auto &TA : T->template_arguments()) {
1072 Hash.AddTemplateArgument(TA);
1074 VisitTypeWithKeyword(T);
1077 void VisitElaboratedType(const ElaboratedType *T) {
1078 AddNestedNameSpecifier(T->getQualifier());
1079 AddQualType(T->getNamedType());
1080 VisitTypeWithKeyword(T);
1083 void VisitUnaryTransformType(const UnaryTransformType *T) {
1084 AddQualType(T->getUnderlyingType());
1085 AddQualType(T->getBaseType());
1089 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
1090 AddDecl(T->getDecl());
1094 void VisitVectorType(const VectorType *T) {
1095 AddQualType(T->getElementType());
1096 ID.AddInteger(T->getNumElements());
1097 ID.AddInteger(T->getVectorKind());
1101 void VisitExtVectorType(const ExtVectorType * T) {
1107 void ODRHash::AddType(const Type *T) {
1108 assert(T && "Expecting non-null pointer.");
1109 ODRTypeVisitor(ID, *this).Visit(T);
1112 void ODRHash::AddQualType(QualType T) {
1113 AddBoolean(T.isNull());
1116 SplitQualType split = T.split();
1117 ID.AddInteger(split.Quals.getAsOpaqueValue());
1121 void ODRHash::AddBoolean(bool Value) {
1122 Bools.push_back(Value);