xref: /freebsd-src/contrib/llvm-project/clang/lib/Serialization/ASTReaderDecl.cpp (revision d686ce931cab72612a9e1ada9fe99d65e11a32a3)
10b57cec5SDimitry Andric //===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
9480093f4SDimitry Andric // This file implements the ASTReader::readDeclRecord method, which is the
100b57cec5SDimitry Andric // entrypoint for loading a decl.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "ASTCommon.h"
150b57cec5SDimitry Andric #include "ASTReaderInternals.h"
165f757f3fSDimitry Andric #include "clang/AST/ASTConcept.h"
170b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
1806c3fb27SDimitry Andric #include "clang/AST/ASTStructuralEquivalence.h"
190b57cec5SDimitry Andric #include "clang/AST/Attr.h"
200b57cec5SDimitry Andric #include "clang/AST/AttrIterator.h"
210b57cec5SDimitry Andric #include "clang/AST/Decl.h"
220b57cec5SDimitry Andric #include "clang/AST/DeclBase.h"
230b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h"
240b57cec5SDimitry Andric #include "clang/AST/DeclFriend.h"
250b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h"
260b57cec5SDimitry Andric #include "clang/AST/DeclOpenMP.h"
270b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h"
280b57cec5SDimitry Andric #include "clang/AST/DeclVisitor.h"
290b57cec5SDimitry Andric #include "clang/AST/DeclarationName.h"
300b57cec5SDimitry Andric #include "clang/AST/Expr.h"
310b57cec5SDimitry Andric #include "clang/AST/ExternalASTSource.h"
320b57cec5SDimitry Andric #include "clang/AST/LambdaCapture.h"
330b57cec5SDimitry Andric #include "clang/AST/NestedNameSpecifier.h"
340b57cec5SDimitry Andric #include "clang/AST/OpenMPClause.h"
350b57cec5SDimitry Andric #include "clang/AST/Redeclarable.h"
360b57cec5SDimitry Andric #include "clang/AST/Stmt.h"
370b57cec5SDimitry Andric #include "clang/AST/TemplateBase.h"
380b57cec5SDimitry Andric #include "clang/AST/Type.h"
390b57cec5SDimitry Andric #include "clang/AST/UnresolvedSet.h"
400b57cec5SDimitry Andric #include "clang/Basic/AttrKinds.h"
4181ad6265SDimitry Andric #include "clang/Basic/DiagnosticSema.h"
420b57cec5SDimitry Andric #include "clang/Basic/ExceptionSpecificationType.h"
430b57cec5SDimitry Andric #include "clang/Basic/IdentifierTable.h"
440b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
450b57cec5SDimitry Andric #include "clang/Basic/Lambda.h"
460b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h"
470b57cec5SDimitry Andric #include "clang/Basic/Linkage.h"
480b57cec5SDimitry Andric #include "clang/Basic/Module.h"
490b57cec5SDimitry Andric #include "clang/Basic/PragmaKinds.h"
500b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h"
510b57cec5SDimitry Andric #include "clang/Basic/Specifiers.h"
520fca6ea1SDimitry Andric #include "clang/Basic/Stack.h"
530b57cec5SDimitry Andric #include "clang/Sema/IdentifierResolver.h"
540b57cec5SDimitry Andric #include "clang/Serialization/ASTBitCodes.h"
55480093f4SDimitry Andric #include "clang/Serialization/ASTRecordReader.h"
560b57cec5SDimitry Andric #include "clang/Serialization/ContinuousRangeMap.h"
57480093f4SDimitry Andric #include "clang/Serialization/ModuleFile.h"
580b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
590b57cec5SDimitry Andric #include "llvm/ADT/FoldingSet.h"
600b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
610b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
620b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
630b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
640b57cec5SDimitry Andric #include "llvm/Bitstream/BitstreamReader.h"
650b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
660b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
670b57cec5SDimitry Andric #include "llvm/Support/SaveAndRestore.h"
680b57cec5SDimitry Andric #include <algorithm>
690b57cec5SDimitry Andric #include <cassert>
700b57cec5SDimitry Andric #include <cstdint>
710b57cec5SDimitry Andric #include <cstring>
720b57cec5SDimitry Andric #include <string>
730b57cec5SDimitry Andric #include <utility>
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric using namespace clang;
760b57cec5SDimitry Andric using namespace serialization;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
790b57cec5SDimitry Andric // Declaration deserialization
800b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric namespace clang {
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
850b57cec5SDimitry Andric     ASTReader &Reader;
860b57cec5SDimitry Andric     ASTRecordReader &Record;
870b57cec5SDimitry Andric     ASTReader::RecordLocation Loc;
880fca6ea1SDimitry Andric     const GlobalDeclID ThisDeclID;
890b57cec5SDimitry Andric     const SourceLocation ThisDeclLoc;
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric     using RecordData = ASTReader::RecordData;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric     TypeID DeferredTypeID = 0;
9406c3fb27SDimitry Andric     unsigned AnonymousDeclNumber = 0;
950fca6ea1SDimitry Andric     GlobalDeclID NamedDeclForTagDecl = GlobalDeclID();
960b57cec5SDimitry Andric     IdentifierInfo *TypedefNameForLinkage = nullptr;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric     ///A flag to carry the information for a decl from the entity is
990b57cec5SDimitry Andric     /// used. We use it to delay the marking of the canonical decl as used until
1000b57cec5SDimitry Andric     /// the entire declaration is deserialized and merged.
1010b57cec5SDimitry Andric     bool IsDeclMarkedUsed = false;
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric     uint64_t GetCurrentCursorOffset();
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric     uint64_t ReadLocalOffset() {
1060b57cec5SDimitry Andric       uint64_t LocalOffset = Record.readInt();
1070b57cec5SDimitry Andric       assert(LocalOffset < Loc.Offset && "offset point after current record");
1080b57cec5SDimitry Andric       return LocalOffset ? Loc.Offset - LocalOffset : 0;
1090b57cec5SDimitry Andric     }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric     uint64_t ReadGlobalOffset() {
1120b57cec5SDimitry Andric       uint64_t Local = ReadLocalOffset();
1130b57cec5SDimitry Andric       return Local ? Record.getGlobalBitOffset(Local) : 0;
1140b57cec5SDimitry Andric     }
1150b57cec5SDimitry Andric 
116480093f4SDimitry Andric     SourceLocation readSourceLocation() {
1170b57cec5SDimitry Andric       return Record.readSourceLocation();
1180b57cec5SDimitry Andric     }
1190b57cec5SDimitry Andric 
120480093f4SDimitry Andric     SourceRange readSourceRange() {
1210b57cec5SDimitry Andric       return Record.readSourceRange();
1220b57cec5SDimitry Andric     }
1230b57cec5SDimitry Andric 
124480093f4SDimitry Andric     TypeSourceInfo *readTypeSourceInfo() {
125480093f4SDimitry Andric       return Record.readTypeSourceInfo();
1260b57cec5SDimitry Andric     }
1270b57cec5SDimitry Andric 
1280fca6ea1SDimitry Andric     GlobalDeclID readDeclID() { return Record.readDeclID(); }
1290b57cec5SDimitry Andric 
130480093f4SDimitry Andric     std::string readString() {
1310b57cec5SDimitry Andric       return Record.readString();
1320b57cec5SDimitry Andric     }
1330b57cec5SDimitry Andric 
1340fca6ea1SDimitry Andric     void readDeclIDList(SmallVectorImpl<GlobalDeclID> &IDs) {
1350b57cec5SDimitry Andric       for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
136480093f4SDimitry Andric         IDs.push_back(readDeclID());
1370b57cec5SDimitry Andric     }
1380b57cec5SDimitry Andric 
139480093f4SDimitry Andric     Decl *readDecl() {
1400b57cec5SDimitry Andric       return Record.readDecl();
1410b57cec5SDimitry Andric     }
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric     template<typename T>
144480093f4SDimitry Andric     T *readDeclAs() {
1450b57cec5SDimitry Andric       return Record.readDeclAs<T>();
1460b57cec5SDimitry Andric     }
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric     serialization::SubmoduleID readSubmoduleID() {
1490b57cec5SDimitry Andric       if (Record.getIdx() == Record.size())
1500b57cec5SDimitry Andric         return 0;
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric       return Record.getGlobalSubmoduleID(Record.readInt());
1530b57cec5SDimitry Andric     }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric     Module *readModule() {
1560b57cec5SDimitry Andric       return Record.getSubmodule(readSubmoduleID());
1570b57cec5SDimitry Andric     }
1580b57cec5SDimitry Andric 
15906c3fb27SDimitry Andric     void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update,
16006c3fb27SDimitry Andric                                  Decl *LambdaContext = nullptr,
16106c3fb27SDimitry Andric                                  unsigned IndexInLambdaContext = 0);
1620b57cec5SDimitry Andric     void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
16306c3fb27SDimitry Andric                                const CXXRecordDecl *D, Decl *LambdaContext,
16406c3fb27SDimitry Andric                                unsigned IndexInLambdaContext);
1650b57cec5SDimitry Andric     void MergeDefinitionData(CXXRecordDecl *D,
1660b57cec5SDimitry Andric                              struct CXXRecordDecl::DefinitionData &&NewDD);
1670b57cec5SDimitry Andric     void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
1680b57cec5SDimitry Andric     void MergeDefinitionData(ObjCInterfaceDecl *D,
1690b57cec5SDimitry Andric                              struct ObjCInterfaceDecl::DefinitionData &&NewDD);
1700b57cec5SDimitry Andric     void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
1710b57cec5SDimitry Andric     void MergeDefinitionData(ObjCProtocolDecl *D,
1720b57cec5SDimitry Andric                              struct ObjCProtocolDecl::DefinitionData &&NewDD);
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric     static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric     static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
1770b57cec5SDimitry Andric                                                  DeclContext *DC,
1780b57cec5SDimitry Andric                                                  unsigned Index);
1790b57cec5SDimitry Andric     static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
1800b57cec5SDimitry Andric                                            unsigned Index, NamedDecl *D);
1810b57cec5SDimitry Andric 
1828a4dda33SDimitry Andric     /// Commit to a primary definition of the class RD, which is known to be
1838a4dda33SDimitry Andric     /// a definition of the class. We might not have read the definition data
1848a4dda33SDimitry Andric     /// for it yet. If we haven't then allocate placeholder definition data
1858a4dda33SDimitry Andric     /// now too.
1868a4dda33SDimitry Andric     static CXXRecordDecl *getOrFakePrimaryClassDefinition(ASTReader &Reader,
1878a4dda33SDimitry Andric                                                           CXXRecordDecl *RD);
1888a4dda33SDimitry Andric 
1890b57cec5SDimitry Andric     /// Results from loading a RedeclarableDecl.
1900b57cec5SDimitry Andric     class RedeclarableResult {
1910b57cec5SDimitry Andric       Decl *MergeWith;
1920b57cec5SDimitry Andric       GlobalDeclID FirstID;
1930b57cec5SDimitry Andric       bool IsKeyDecl;
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric     public:
1960b57cec5SDimitry Andric       RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
1970b57cec5SDimitry Andric           : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric       /// Retrieve the first ID.
2000b57cec5SDimitry Andric       GlobalDeclID getFirstID() const { return FirstID; }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric       /// Is this declaration a key declaration?
2030b57cec5SDimitry Andric       bool isKeyDecl() const { return IsKeyDecl; }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric       /// Get a known declaration that this should be merged with, if
2060b57cec5SDimitry Andric       /// any.
2070b57cec5SDimitry Andric       Decl *getKnownMergeTarget() const { return MergeWith; }
2080b57cec5SDimitry Andric     };
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric     /// Class used to capture the result of searching for an existing
2110b57cec5SDimitry Andric     /// declaration of a specific kind and name, along with the ability
2120b57cec5SDimitry Andric     /// to update the place where this result was found (the declaration
2130b57cec5SDimitry Andric     /// chain hanging off an identifier or the DeclContext we searched in)
2140b57cec5SDimitry Andric     /// if requested.
2150b57cec5SDimitry Andric     class FindExistingResult {
2160b57cec5SDimitry Andric       ASTReader &Reader;
2170b57cec5SDimitry Andric       NamedDecl *New = nullptr;
2180b57cec5SDimitry Andric       NamedDecl *Existing = nullptr;
2190b57cec5SDimitry Andric       bool AddResult = false;
2200b57cec5SDimitry Andric       unsigned AnonymousDeclNumber = 0;
2210b57cec5SDimitry Andric       IdentifierInfo *TypedefNameForLinkage = nullptr;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric     public:
2240b57cec5SDimitry Andric       FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric       FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
2270b57cec5SDimitry Andric                          unsigned AnonymousDeclNumber,
2280b57cec5SDimitry Andric                          IdentifierInfo *TypedefNameForLinkage)
2290b57cec5SDimitry Andric           : Reader(Reader), New(New), Existing(Existing), AddResult(true),
2300b57cec5SDimitry Andric             AnonymousDeclNumber(AnonymousDeclNumber),
2310b57cec5SDimitry Andric             TypedefNameForLinkage(TypedefNameForLinkage) {}
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric       FindExistingResult(FindExistingResult &&Other)
2340b57cec5SDimitry Andric           : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
2350b57cec5SDimitry Andric             AddResult(Other.AddResult),
2360b57cec5SDimitry Andric             AnonymousDeclNumber(Other.AnonymousDeclNumber),
2370b57cec5SDimitry Andric             TypedefNameForLinkage(Other.TypedefNameForLinkage) {
2380b57cec5SDimitry Andric         Other.AddResult = false;
2390b57cec5SDimitry Andric       }
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric       FindExistingResult &operator=(FindExistingResult &&) = delete;
2420b57cec5SDimitry Andric       ~FindExistingResult();
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric       /// Suppress the addition of this result into the known set of
2450b57cec5SDimitry Andric       /// names.
2460b57cec5SDimitry Andric       void suppress() { AddResult = false; }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric       operator NamedDecl*() const { return Existing; }
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric       template<typename T>
2510b57cec5SDimitry Andric       operator T*() const { return dyn_cast_or_null<T>(Existing); }
2520b57cec5SDimitry Andric     };
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric     static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
2550b57cec5SDimitry Andric                                                     DeclContext *DC);
2560b57cec5SDimitry Andric     FindExistingResult findExisting(NamedDecl *D);
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   public:
2590b57cec5SDimitry Andric     ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
2600fca6ea1SDimitry Andric                   ASTReader::RecordLocation Loc, GlobalDeclID thisDeclID,
2610fca6ea1SDimitry Andric                   SourceLocation ThisDeclLoc)
2620b57cec5SDimitry Andric         : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
2630b57cec5SDimitry Andric           ThisDeclLoc(ThisDeclLoc) {}
2640b57cec5SDimitry Andric 
2650fca6ea1SDimitry Andric     template <typename T>
2660fca6ea1SDimitry Andric     static void AddLazySpecializations(T *D,
2670fca6ea1SDimitry Andric                                        SmallVectorImpl<GlobalDeclID> &IDs) {
2680b57cec5SDimitry Andric       if (IDs.empty())
2690b57cec5SDimitry Andric         return;
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric       // FIXME: We should avoid this pattern of getting the ASTContext.
2720b57cec5SDimitry Andric       ASTContext &C = D->getASTContext();
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric       auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric       if (auto &Old = LazySpecializations) {
2770fca6ea1SDimitry Andric         IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0].getRawValue());
2780b57cec5SDimitry Andric         llvm::sort(IDs);
2790b57cec5SDimitry Andric         IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
2800b57cec5SDimitry Andric       }
2810b57cec5SDimitry Andric 
2820fca6ea1SDimitry Andric       auto *Result = new (C) GlobalDeclID[1 + IDs.size()];
2830fca6ea1SDimitry Andric       *Result = GlobalDeclID(IDs.size());
2840fca6ea1SDimitry Andric 
2850b57cec5SDimitry Andric       std::copy(IDs.begin(), IDs.end(), Result + 1);
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric       LazySpecializations = Result;
2880b57cec5SDimitry Andric     }
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric     template <typename DeclT>
2910b57cec5SDimitry Andric     static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
2920b57cec5SDimitry Andric     static Decl *getMostRecentDeclImpl(...);
2930b57cec5SDimitry Andric     static Decl *getMostRecentDecl(Decl *D);
2940b57cec5SDimitry Andric 
295e8d8bef9SDimitry Andric     static void mergeInheritableAttributes(ASTReader &Reader, Decl *D,
296e8d8bef9SDimitry Andric                                            Decl *Previous);
297e8d8bef9SDimitry Andric 
2980b57cec5SDimitry Andric     template <typename DeclT>
2990b57cec5SDimitry Andric     static void attachPreviousDeclImpl(ASTReader &Reader,
3000b57cec5SDimitry Andric                                        Redeclarable<DeclT> *D, Decl *Previous,
3010b57cec5SDimitry Andric                                        Decl *Canon);
3020b57cec5SDimitry Andric     static void attachPreviousDeclImpl(ASTReader &Reader, ...);
3030b57cec5SDimitry Andric     static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
3040b57cec5SDimitry Andric                                    Decl *Canon);
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric     template <typename DeclT>
3070b57cec5SDimitry Andric     static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
3080b57cec5SDimitry Andric     static void attachLatestDeclImpl(...);
3090b57cec5SDimitry Andric     static void attachLatestDecl(Decl *D, Decl *latest);
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric     template <typename DeclT>
3120b57cec5SDimitry Andric     static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
3130b57cec5SDimitry Andric     static void markIncompleteDeclChainImpl(...);
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric     void ReadFunctionDefinition(FunctionDecl *FD);
3160b57cec5SDimitry Andric     void Visit(Decl *D);
3170b57cec5SDimitry Andric 
3180fca6ea1SDimitry Andric     void UpdateDecl(Decl *D, SmallVectorImpl<GlobalDeclID> &);
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric     static void setNextObjCCategory(ObjCCategoryDecl *Cat,
3210b57cec5SDimitry Andric                                     ObjCCategoryDecl *Next) {
3220b57cec5SDimitry Andric       Cat->NextClassCategory = Next;
3230b57cec5SDimitry Andric     }
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric     void VisitDecl(Decl *D);
3260b57cec5SDimitry Andric     void VisitPragmaCommentDecl(PragmaCommentDecl *D);
3270b57cec5SDimitry Andric     void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
3280b57cec5SDimitry Andric     void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
3290b57cec5SDimitry Andric     void VisitNamedDecl(NamedDecl *ND);
3300b57cec5SDimitry Andric     void VisitLabelDecl(LabelDecl *LD);
3310b57cec5SDimitry Andric     void VisitNamespaceDecl(NamespaceDecl *D);
332bdd1243dSDimitry Andric     void VisitHLSLBufferDecl(HLSLBufferDecl *D);
3330b57cec5SDimitry Andric     void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
3340b57cec5SDimitry Andric     void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
3350b57cec5SDimitry Andric     void VisitTypeDecl(TypeDecl *TD);
3360b57cec5SDimitry Andric     RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
3370b57cec5SDimitry Andric     void VisitTypedefDecl(TypedefDecl *TD);
3380b57cec5SDimitry Andric     void VisitTypeAliasDecl(TypeAliasDecl *TD);
3390b57cec5SDimitry Andric     void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
340fe6060f1SDimitry Andric     void VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl *D);
3410b57cec5SDimitry Andric     RedeclarableResult VisitTagDecl(TagDecl *TD);
3420b57cec5SDimitry Andric     void VisitEnumDecl(EnumDecl *ED);
3430b57cec5SDimitry Andric     RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
344349cc55cSDimitry Andric     void VisitRecordDecl(RecordDecl *RD);
3450b57cec5SDimitry Andric     RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
3460b57cec5SDimitry Andric     void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
3470b57cec5SDimitry Andric     RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
3480b57cec5SDimitry Andric                                             ClassTemplateSpecializationDecl *D);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric     void VisitClassTemplateSpecializationDecl(
3510b57cec5SDimitry Andric         ClassTemplateSpecializationDecl *D) {
3520b57cec5SDimitry Andric       VisitClassTemplateSpecializationDeclImpl(D);
3530b57cec5SDimitry Andric     }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric     void VisitClassTemplatePartialSpecializationDecl(
3560b57cec5SDimitry Andric         ClassTemplatePartialSpecializationDecl *D);
3570b57cec5SDimitry Andric     RedeclarableResult
3580b57cec5SDimitry Andric     VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric     void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
3610b57cec5SDimitry Andric       VisitVarTemplateSpecializationDeclImpl(D);
3620b57cec5SDimitry Andric     }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric     void VisitVarTemplatePartialSpecializationDecl(
3650b57cec5SDimitry Andric         VarTemplatePartialSpecializationDecl *D);
3660b57cec5SDimitry Andric     void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
3670b57cec5SDimitry Andric     void VisitValueDecl(ValueDecl *VD);
3680b57cec5SDimitry Andric     void VisitEnumConstantDecl(EnumConstantDecl *ECD);
3690b57cec5SDimitry Andric     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
3700b57cec5SDimitry Andric     void VisitDeclaratorDecl(DeclaratorDecl *DD);
3710b57cec5SDimitry Andric     void VisitFunctionDecl(FunctionDecl *FD);
3720b57cec5SDimitry Andric     void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
3730b57cec5SDimitry Andric     void VisitCXXMethodDecl(CXXMethodDecl *D);
3740b57cec5SDimitry Andric     void VisitCXXConstructorDecl(CXXConstructorDecl *D);
3750b57cec5SDimitry Andric     void VisitCXXDestructorDecl(CXXDestructorDecl *D);
3760b57cec5SDimitry Andric     void VisitCXXConversionDecl(CXXConversionDecl *D);
3770b57cec5SDimitry Andric     void VisitFieldDecl(FieldDecl *FD);
3780b57cec5SDimitry Andric     void VisitMSPropertyDecl(MSPropertyDecl *FD);
3795ffd83dbSDimitry Andric     void VisitMSGuidDecl(MSGuidDecl *D);
38081ad6265SDimitry Andric     void VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl *D);
381e8d8bef9SDimitry Andric     void VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D);
3820b57cec5SDimitry Andric     void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
3830b57cec5SDimitry Andric     RedeclarableResult VisitVarDeclImpl(VarDecl *D);
38406c3fb27SDimitry Andric     void ReadVarDeclInit(VarDecl *VD);
3850b57cec5SDimitry Andric     void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
3860b57cec5SDimitry Andric     void VisitImplicitParamDecl(ImplicitParamDecl *PD);
3870b57cec5SDimitry Andric     void VisitParmVarDecl(ParmVarDecl *PD);
3880b57cec5SDimitry Andric     void VisitDecompositionDecl(DecompositionDecl *DD);
3890b57cec5SDimitry Andric     void VisitBindingDecl(BindingDecl *BD);
3900b57cec5SDimitry Andric     void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
391bdd1243dSDimitry Andric     void VisitTemplateDecl(TemplateDecl *D);
3920b57cec5SDimitry Andric     void VisitConceptDecl(ConceptDecl *D);
393bdd1243dSDimitry Andric     void VisitImplicitConceptSpecializationDecl(
394bdd1243dSDimitry Andric         ImplicitConceptSpecializationDecl *D);
39555e4f9d5SDimitry Andric     void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);
3960b57cec5SDimitry Andric     RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
3970b57cec5SDimitry Andric     void VisitClassTemplateDecl(ClassTemplateDecl *D);
3980b57cec5SDimitry Andric     void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
3990b57cec5SDimitry Andric     void VisitVarTemplateDecl(VarTemplateDecl *D);
4000b57cec5SDimitry Andric     void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
4010b57cec5SDimitry Andric     void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
4020b57cec5SDimitry Andric     void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
4030b57cec5SDimitry Andric     void VisitUsingDecl(UsingDecl *D);
404fe6060f1SDimitry Andric     void VisitUsingEnumDecl(UsingEnumDecl *D);
4050b57cec5SDimitry Andric     void VisitUsingPackDecl(UsingPackDecl *D);
4060b57cec5SDimitry Andric     void VisitUsingShadowDecl(UsingShadowDecl *D);
4070b57cec5SDimitry Andric     void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
4080b57cec5SDimitry Andric     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
4090b57cec5SDimitry Andric     void VisitExportDecl(ExportDecl *D);
4100b57cec5SDimitry Andric     void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
411bdd1243dSDimitry Andric     void VisitTopLevelStmtDecl(TopLevelStmtDecl *D);
4120b57cec5SDimitry Andric     void VisitImportDecl(ImportDecl *D);
4130b57cec5SDimitry Andric     void VisitAccessSpecDecl(AccessSpecDecl *D);
4140b57cec5SDimitry Andric     void VisitFriendDecl(FriendDecl *D);
4150b57cec5SDimitry Andric     void VisitFriendTemplateDecl(FriendTemplateDecl *D);
4160b57cec5SDimitry Andric     void VisitStaticAssertDecl(StaticAssertDecl *D);
4170b57cec5SDimitry Andric     void VisitBlockDecl(BlockDecl *BD);
4180b57cec5SDimitry Andric     void VisitCapturedDecl(CapturedDecl *CD);
4190b57cec5SDimitry Andric     void VisitEmptyDecl(EmptyDecl *D);
420480093f4SDimitry Andric     void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric     std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric     template<typename T>
4250b57cec5SDimitry Andric     RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric     template <typename T>
428bdd1243dSDimitry Andric     void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl);
429bdd1243dSDimitry Andric 
43006c3fb27SDimitry Andric     void mergeLambda(CXXRecordDecl *D, RedeclarableResult &Redecl,
43106c3fb27SDimitry Andric                      Decl *Context, unsigned Number);
43206c3fb27SDimitry Andric 
433bdd1243dSDimitry Andric     void mergeRedeclarableTemplate(RedeclarableTemplateDecl *D,
434bdd1243dSDimitry Andric                                    RedeclarableResult &Redecl);
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric     template <typename T>
4370b57cec5SDimitry Andric     void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
438bdd1243dSDimitry Andric                            RedeclarableResult &Redecl);
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric     template<typename T>
4410b57cec5SDimitry Andric     void mergeMergeable(Mergeable<T> *D);
4420b57cec5SDimitry Andric 
443480093f4SDimitry Andric     void mergeMergeable(LifetimeExtendedTemporaryDecl *D);
444480093f4SDimitry Andric 
4450b57cec5SDimitry Andric     void mergeTemplatePattern(RedeclarableTemplateDecl *D,
4460b57cec5SDimitry Andric                               RedeclarableTemplateDecl *Existing,
447bdd1243dSDimitry Andric                               bool IsKeyDecl);
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric     ObjCTypeParamList *ReadObjCTypeParamList();
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric     // FIXME: Reorder according to DeclNodes.td?
4520b57cec5SDimitry Andric     void VisitObjCMethodDecl(ObjCMethodDecl *D);
4530b57cec5SDimitry Andric     void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
4540b57cec5SDimitry Andric     void VisitObjCContainerDecl(ObjCContainerDecl *D);
4550b57cec5SDimitry Andric     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
4560b57cec5SDimitry Andric     void VisitObjCIvarDecl(ObjCIvarDecl *D);
4570b57cec5SDimitry Andric     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
4580b57cec5SDimitry Andric     void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
4590b57cec5SDimitry Andric     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
4600b57cec5SDimitry Andric     void VisitObjCImplDecl(ObjCImplDecl *D);
4610b57cec5SDimitry Andric     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
4620b57cec5SDimitry Andric     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
4630b57cec5SDimitry Andric     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
4640b57cec5SDimitry Andric     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
4650b57cec5SDimitry Andric     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
4660b57cec5SDimitry Andric     void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
4670b57cec5SDimitry Andric     void VisitOMPAllocateDecl(OMPAllocateDecl *D);
4680b57cec5SDimitry Andric     void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
4690b57cec5SDimitry Andric     void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
4700b57cec5SDimitry Andric     void VisitOMPRequiresDecl(OMPRequiresDecl *D);
4710b57cec5SDimitry Andric     void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
4720b57cec5SDimitry Andric   };
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric } // namespace clang
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric namespace {
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric /// Iterator over the redeclarations of a declaration that have already
4790b57cec5SDimitry Andric /// been merged into the same redeclaration chain.
48006c3fb27SDimitry Andric template <typename DeclT> class MergedRedeclIterator {
48106c3fb27SDimitry Andric   DeclT *Start = nullptr;
4820b57cec5SDimitry Andric   DeclT *Canonical = nullptr;
4830b57cec5SDimitry Andric   DeclT *Current = nullptr;
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric public:
4860b57cec5SDimitry Andric   MergedRedeclIterator() = default;
4870b57cec5SDimitry Andric   MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric   DeclT *operator*() { return Current; }
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   MergedRedeclIterator &operator++() {
4920b57cec5SDimitry Andric     if (Current->isFirstDecl()) {
4930b57cec5SDimitry Andric       Canonical = Current;
4940b57cec5SDimitry Andric       Current = Current->getMostRecentDecl();
4950b57cec5SDimitry Andric     } else
4960b57cec5SDimitry Andric       Current = Current->getPreviousDecl();
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric     // If we started in the merged portion, we'll reach our start position
4990b57cec5SDimitry Andric     // eventually. Otherwise, we'll never reach it, but the second declaration
5000b57cec5SDimitry Andric     // we reached was the canonical declaration, so stop when we see that one
5010b57cec5SDimitry Andric     // again.
5020b57cec5SDimitry Andric     if (Current == Start || Current == Canonical)
5030b57cec5SDimitry Andric       Current = nullptr;
5040b57cec5SDimitry Andric     return *this;
5050b57cec5SDimitry Andric   }
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric   friend bool operator!=(const MergedRedeclIterator &A,
5080b57cec5SDimitry Andric                          const MergedRedeclIterator &B) {
5090b57cec5SDimitry Andric     return A.Current != B.Current;
5100b57cec5SDimitry Andric   }
5110b57cec5SDimitry Andric };
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric } // namespace
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric template <typename DeclT>
5160b57cec5SDimitry Andric static llvm::iterator_range<MergedRedeclIterator<DeclT>>
5170b57cec5SDimitry Andric merged_redecls(DeclT *D) {
5180b57cec5SDimitry Andric   return llvm::make_range(MergedRedeclIterator<DeclT>(D),
5190b57cec5SDimitry Andric                           MergedRedeclIterator<DeclT>());
5200b57cec5SDimitry Andric }
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric uint64_t ASTDeclReader::GetCurrentCursorOffset() {
5230b57cec5SDimitry Andric   return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
5240b57cec5SDimitry Andric }
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
5275ffd83dbSDimitry Andric   if (Record.readInt()) {
528e8d8bef9SDimitry Andric     Reader.DefinitionSource[FD] =
529e8d8bef9SDimitry Andric         Loc.F->Kind == ModuleKind::MK_MainFile ||
530e8d8bef9SDimitry Andric         Reader.getContext().getLangOpts().BuildingPCHWithObjectFile;
5315ffd83dbSDimitry Andric   }
5320b57cec5SDimitry Andric   if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
5330b57cec5SDimitry Andric     CD->setNumCtorInitializers(Record.readInt());
5340b57cec5SDimitry Andric     if (CD->getNumCtorInitializers())
5350b57cec5SDimitry Andric       CD->CtorInitializers = ReadGlobalOffset();
5360b57cec5SDimitry Andric   }
5370b57cec5SDimitry Andric   // Store the offset of the body so we can lazily load it later.
5380b57cec5SDimitry Andric   Reader.PendingBodies[FD] = GetCurrentCursorOffset();
5390b57cec5SDimitry Andric }
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric void ASTDeclReader::Visit(Decl *D) {
5420b57cec5SDimitry Andric   DeclVisitor<ASTDeclReader, void>::Visit(D);
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric   // At this point we have deserialized and merged the decl and it is safe to
5450b57cec5SDimitry Andric   // update its canonical decl to signal that the entire entity is used.
5460b57cec5SDimitry Andric   D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
5470b57cec5SDimitry Andric   IsDeclMarkedUsed = false;
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric   if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
5500b57cec5SDimitry Andric     if (auto *TInfo = DD->getTypeSourceInfo())
5510b57cec5SDimitry Andric       Record.readTypeLoc(TInfo->getTypeLoc());
5520b57cec5SDimitry Andric   }
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric   if (auto *TD = dyn_cast<TypeDecl>(D)) {
5550b57cec5SDimitry Andric     // We have a fully initialized TypeDecl. Read its type now.
5560b57cec5SDimitry Andric     TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric     // If this is a tag declaration with a typedef name for linkage, it's safe
5590b57cec5SDimitry Andric     // to load that typedef now.
5600fca6ea1SDimitry Andric     if (NamedDeclForTagDecl.isValid())
5610b57cec5SDimitry Andric       cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
5620b57cec5SDimitry Andric           cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
5630b57cec5SDimitry Andric   } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5640b57cec5SDimitry Andric     // if we have a fully initialized TypeDecl, we can safely read its type now.
5650b57cec5SDimitry Andric     ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
5660b57cec5SDimitry Andric   } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
5670b57cec5SDimitry Andric     // FunctionDecl's body was written last after all other Stmts/Exprs.
5680b57cec5SDimitry Andric     if (Record.readInt())
5690b57cec5SDimitry Andric       ReadFunctionDefinition(FD);
57006c3fb27SDimitry Andric   } else if (auto *VD = dyn_cast<VarDecl>(D)) {
57106c3fb27SDimitry Andric     ReadVarDeclInit(VD);
57206c3fb27SDimitry Andric   } else if (auto *FD = dyn_cast<FieldDecl>(D)) {
57306c3fb27SDimitry Andric     if (FD->hasInClassInitializer() && Record.readInt()) {
57406c3fb27SDimitry Andric       FD->setLazyInClassInitializer(LazyDeclStmtPtr(GetCurrentCursorOffset()));
57506c3fb27SDimitry Andric     }
5760b57cec5SDimitry Andric   }
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric void ASTDeclReader::VisitDecl(Decl *D) {
5805f757f3fSDimitry Andric   BitsUnpacker DeclBits(Record.readInt());
581cb14a3feSDimitry Andric   auto ModuleOwnership =
582cb14a3feSDimitry Andric       (Decl::ModuleOwnershipKind)DeclBits.getNextBits(/*Width=*/3);
583cb14a3feSDimitry Andric   D->setReferenced(DeclBits.getNextBit());
584cb14a3feSDimitry Andric   D->Used = DeclBits.getNextBit();
585cb14a3feSDimitry Andric   IsDeclMarkedUsed |= D->Used;
586cb14a3feSDimitry Andric   D->setAccess((AccessSpecifier)DeclBits.getNextBits(/*Width=*/2));
587cb14a3feSDimitry Andric   D->setImplicit(DeclBits.getNextBit());
5885f757f3fSDimitry Andric   bool HasStandaloneLexicalDC = DeclBits.getNextBit();
589cb14a3feSDimitry Andric   bool HasAttrs = DeclBits.getNextBit();
590cb14a3feSDimitry Andric   D->setTopLevelDeclInObjCContainer(DeclBits.getNextBit());
591cb14a3feSDimitry Andric   D->InvalidDecl = DeclBits.getNextBit();
592cb14a3feSDimitry Andric   D->FromASTFile = true;
5935f757f3fSDimitry Andric 
5940b57cec5SDimitry Andric   if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
595bdd1243dSDimitry Andric       isa<ParmVarDecl, ObjCTypeParamDecl>(D)) {
5960b57cec5SDimitry Andric     // We don't want to deserialize the DeclContext of a template
5970b57cec5SDimitry Andric     // parameter or of a parameter of a function template immediately.   These
5980b57cec5SDimitry Andric     // entities might be used in the formulation of its DeclContext (for
5990b57cec5SDimitry Andric     // example, a function parameter can be used in decltype() in trailing
6000b57cec5SDimitry Andric     // return type of the function).  Use the translation unit DeclContext as a
6010b57cec5SDimitry Andric     // placeholder.
602480093f4SDimitry Andric     GlobalDeclID SemaDCIDForTemplateParmDecl = readDeclID();
6035f757f3fSDimitry Andric     GlobalDeclID LexicalDCIDForTemplateParmDecl =
6040fca6ea1SDimitry Andric         HasStandaloneLexicalDC ? readDeclID() : GlobalDeclID();
6050fca6ea1SDimitry Andric     if (LexicalDCIDForTemplateParmDecl.isInvalid())
6060b57cec5SDimitry Andric       LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
6070b57cec5SDimitry Andric     Reader.addPendingDeclContextInfo(D,
6080b57cec5SDimitry Andric                                      SemaDCIDForTemplateParmDecl,
6090b57cec5SDimitry Andric                                      LexicalDCIDForTemplateParmDecl);
6100b57cec5SDimitry Andric     D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
6110b57cec5SDimitry Andric   } else {
612480093f4SDimitry Andric     auto *SemaDC = readDeclAs<DeclContext>();
6135f757f3fSDimitry Andric     auto *LexicalDC =
6145f757f3fSDimitry Andric         HasStandaloneLexicalDC ? readDeclAs<DeclContext>() : nullptr;
6150b57cec5SDimitry Andric     if (!LexicalDC)
6160b57cec5SDimitry Andric       LexicalDC = SemaDC;
6178a4dda33SDimitry Andric     // If the context is a class, we might not have actually merged it yet, in
6188a4dda33SDimitry Andric     // the case where the definition comes from an update record.
6198a4dda33SDimitry Andric     DeclContext *MergedSemaDC;
6208a4dda33SDimitry Andric     if (auto *RD = dyn_cast<CXXRecordDecl>(SemaDC))
6218a4dda33SDimitry Andric       MergedSemaDC = getOrFakePrimaryClassDefinition(Reader, RD);
6228a4dda33SDimitry Andric     else
6238a4dda33SDimitry Andric       MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
6240b57cec5SDimitry Andric     // Avoid calling setLexicalDeclContext() directly because it uses
6250b57cec5SDimitry Andric     // Decl::getASTContext() internally which is unsafe during derialization.
6260b57cec5SDimitry Andric     D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
6270b57cec5SDimitry Andric                            Reader.getContext());
6280b57cec5SDimitry Andric   }
6290b57cec5SDimitry Andric   D->setLocation(ThisDeclLoc);
6305f757f3fSDimitry Andric 
6315f757f3fSDimitry Andric   if (HasAttrs) {
6320b57cec5SDimitry Andric     AttrVec Attrs;
6330b57cec5SDimitry Andric     Record.readAttributes(Attrs);
6340b57cec5SDimitry Andric     // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
6350b57cec5SDimitry Andric     // internally which is unsafe during derialization.
6360b57cec5SDimitry Andric     D->setAttrsImpl(Attrs, Reader.getContext());
6370b57cec5SDimitry Andric   }
6380b57cec5SDimitry Andric 
6390b57cec5SDimitry Andric   // Determine whether this declaration is part of a (sub)module. If so, it
6400b57cec5SDimitry Andric   // may not yet be visible.
641cb14a3feSDimitry Andric   bool ModulePrivate =
642cb14a3feSDimitry Andric       (ModuleOwnership == Decl::ModuleOwnershipKind::ModulePrivate);
6430b57cec5SDimitry Andric   if (unsigned SubmoduleID = readSubmoduleID()) {
64481ad6265SDimitry Andric     switch (ModuleOwnership) {
64581ad6265SDimitry Andric     case Decl::ModuleOwnershipKind::Visible:
64681ad6265SDimitry Andric       ModuleOwnership = Decl::ModuleOwnershipKind::VisibleWhenImported;
64781ad6265SDimitry Andric       break;
64881ad6265SDimitry Andric     case Decl::ModuleOwnershipKind::Unowned:
64981ad6265SDimitry Andric     case Decl::ModuleOwnershipKind::VisibleWhenImported:
65081ad6265SDimitry Andric     case Decl::ModuleOwnershipKind::ReachableWhenImported:
65181ad6265SDimitry Andric     case Decl::ModuleOwnershipKind::ModulePrivate:
65281ad6265SDimitry Andric       break;
65381ad6265SDimitry Andric     }
65481ad6265SDimitry Andric 
65581ad6265SDimitry Andric     D->setModuleOwnershipKind(ModuleOwnership);
6560b57cec5SDimitry Andric     // Store the owning submodule ID in the declaration.
6570b57cec5SDimitry Andric     D->setOwningModuleID(SubmoduleID);
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric     if (ModulePrivate) {
6600b57cec5SDimitry Andric       // Module-private declarations are never visible, so there is no work to
6610b57cec5SDimitry Andric       // do.
6620b57cec5SDimitry Andric     } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
6630b57cec5SDimitry Andric       // If local visibility is being tracked, this declaration will become
6640b57cec5SDimitry Andric       // hidden and visible as the owning module does.
6650b57cec5SDimitry Andric     } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
6660b57cec5SDimitry Andric       // Mark the declaration as visible when its owning module becomes visible.
6670b57cec5SDimitry Andric       if (Owner->NameVisibility == Module::AllVisible)
6680b57cec5SDimitry Andric         D->setVisibleDespiteOwningModule();
6690b57cec5SDimitry Andric       else
6700b57cec5SDimitry Andric         Reader.HiddenNamesMap[Owner].push_back(D);
6710b57cec5SDimitry Andric     }
6720b57cec5SDimitry Andric   } else if (ModulePrivate) {
6730b57cec5SDimitry Andric     D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
6740b57cec5SDimitry Andric   }
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
6780b57cec5SDimitry Andric   VisitDecl(D);
679480093f4SDimitry Andric   D->setLocation(readSourceLocation());
6800b57cec5SDimitry Andric   D->CommentKind = (PragmaMSCommentKind)Record.readInt();
681480093f4SDimitry Andric   std::string Arg = readString();
6820b57cec5SDimitry Andric   memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
6830b57cec5SDimitry Andric   D->getTrailingObjects<char>()[Arg.size()] = '\0';
6840b57cec5SDimitry Andric }
6850b57cec5SDimitry Andric 
6860b57cec5SDimitry Andric void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
6870b57cec5SDimitry Andric   VisitDecl(D);
688480093f4SDimitry Andric   D->setLocation(readSourceLocation());
689480093f4SDimitry Andric   std::string Name = readString();
6900b57cec5SDimitry Andric   memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
6910b57cec5SDimitry Andric   D->getTrailingObjects<char>()[Name.size()] = '\0';
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric   D->ValueStart = Name.size() + 1;
694480093f4SDimitry Andric   std::string Value = readString();
6950b57cec5SDimitry Andric   memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
6960b57cec5SDimitry Andric          Value.size());
6970b57cec5SDimitry Andric   D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
6980b57cec5SDimitry Andric }
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
7010b57cec5SDimitry Andric   llvm_unreachable("Translation units are not serialized");
7020b57cec5SDimitry Andric }
7030b57cec5SDimitry Andric 
7040b57cec5SDimitry Andric void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
7050b57cec5SDimitry Andric   VisitDecl(ND);
7060b57cec5SDimitry Andric   ND->setDeclName(Record.readDeclarationName());
7070b57cec5SDimitry Andric   AnonymousDeclNumber = Record.readInt();
7080b57cec5SDimitry Andric }
7090b57cec5SDimitry Andric 
7100b57cec5SDimitry Andric void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
7110b57cec5SDimitry Andric   VisitNamedDecl(TD);
712480093f4SDimitry Andric   TD->setLocStart(readSourceLocation());
7130b57cec5SDimitry Andric   // Delay type reading until after we have fully initialized the decl.
7140b57cec5SDimitry Andric   DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
7150b57cec5SDimitry Andric }
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult
7180b57cec5SDimitry Andric ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
7190b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(TD);
7200b57cec5SDimitry Andric   VisitTypeDecl(TD);
721480093f4SDimitry Andric   TypeSourceInfo *TInfo = readTypeSourceInfo();
7220b57cec5SDimitry Andric   if (Record.readInt()) { // isModed
7230b57cec5SDimitry Andric     QualType modedT = Record.readType();
7240b57cec5SDimitry Andric     TD->setModedTypeSourceInfo(TInfo, modedT);
7250b57cec5SDimitry Andric   } else
7260b57cec5SDimitry Andric     TD->setTypeSourceInfo(TInfo);
7270b57cec5SDimitry Andric   // Read and discard the declaration for which this is a typedef name for
7280b57cec5SDimitry Andric   // linkage, if it exists. We cannot rely on our type to pull in this decl,
7290b57cec5SDimitry Andric   // because it might have been merged with a type from another module and
7300b57cec5SDimitry Andric   // thus might not refer to our version of the declaration.
731480093f4SDimitry Andric   readDecl();
7320b57cec5SDimitry Andric   return Redecl;
7330b57cec5SDimitry Andric }
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
7360b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
7370b57cec5SDimitry Andric   mergeRedeclarable(TD, Redecl);
7380b57cec5SDimitry Andric }
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
7410b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
742480093f4SDimitry Andric   if (auto *Template = readDeclAs<TypeAliasTemplateDecl>())
7430b57cec5SDimitry Andric     // Merged when we merge the template.
7440b57cec5SDimitry Andric     TD->setDescribedAliasTemplate(Template);
7450b57cec5SDimitry Andric   else
7460b57cec5SDimitry Andric     mergeRedeclarable(TD, Redecl);
7470b57cec5SDimitry Andric }
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
7500b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(TD);
7510b57cec5SDimitry Andric   VisitTypeDecl(TD);
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric   TD->IdentifierNamespace = Record.readInt();
7545f757f3fSDimitry Andric 
7555f757f3fSDimitry Andric   BitsUnpacker TagDeclBits(Record.readInt());
7565f757f3fSDimitry Andric   TD->setTagKind(
7575f757f3fSDimitry Andric       static_cast<TagTypeKind>(TagDeclBits.getNextBits(/*Width=*/3)));
7585f757f3fSDimitry Andric   TD->setCompleteDefinition(TagDeclBits.getNextBit());
7595f757f3fSDimitry Andric   TD->setEmbeddedInDeclarator(TagDeclBits.getNextBit());
7605f757f3fSDimitry Andric   TD->setFreeStanding(TagDeclBits.getNextBit());
7615f757f3fSDimitry Andric   TD->setCompleteDefinitionRequired(TagDeclBits.getNextBit());
762480093f4SDimitry Andric   TD->setBraceRange(readSourceRange());
7630b57cec5SDimitry Andric 
7645f757f3fSDimitry Andric   switch (TagDeclBits.getNextBits(/*Width=*/2)) {
7650b57cec5SDimitry Andric   case 0:
7660b57cec5SDimitry Andric     break;
7670b57cec5SDimitry Andric   case 1: { // ExtInfo
7680b57cec5SDimitry Andric     auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
769480093f4SDimitry Andric     Record.readQualifierInfo(*Info);
7700b57cec5SDimitry Andric     TD->TypedefNameDeclOrQualifier = Info;
7710b57cec5SDimitry Andric     break;
7720b57cec5SDimitry Andric   }
7730b57cec5SDimitry Andric   case 2: // TypedefNameForAnonDecl
774480093f4SDimitry Andric     NamedDeclForTagDecl = readDeclID();
775480093f4SDimitry Andric     TypedefNameForLinkage = Record.readIdentifier();
7760b57cec5SDimitry Andric     break;
7770b57cec5SDimitry Andric   default:
7780b57cec5SDimitry Andric     llvm_unreachable("unexpected tag info kind");
7790b57cec5SDimitry Andric   }
7800b57cec5SDimitry Andric 
7810b57cec5SDimitry Andric   if (!isa<CXXRecordDecl>(TD))
7820b57cec5SDimitry Andric     mergeRedeclarable(TD, Redecl);
7830b57cec5SDimitry Andric   return Redecl;
7840b57cec5SDimitry Andric }
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
7870b57cec5SDimitry Andric   VisitTagDecl(ED);
788480093f4SDimitry Andric   if (TypeSourceInfo *TI = readTypeSourceInfo())
7890b57cec5SDimitry Andric     ED->setIntegerTypeSourceInfo(TI);
7900b57cec5SDimitry Andric   else
7910b57cec5SDimitry Andric     ED->setIntegerType(Record.readType());
7920b57cec5SDimitry Andric   ED->setPromotionType(Record.readType());
7935f757f3fSDimitry Andric 
7945f757f3fSDimitry Andric   BitsUnpacker EnumDeclBits(Record.readInt());
7955f757f3fSDimitry Andric   ED->setNumPositiveBits(EnumDeclBits.getNextBits(/*Width=*/8));
7965f757f3fSDimitry Andric   ED->setNumNegativeBits(EnumDeclBits.getNextBits(/*Width=*/8));
7975f757f3fSDimitry Andric   ED->setScoped(EnumDeclBits.getNextBit());
7985f757f3fSDimitry Andric   ED->setScopedUsingClassTag(EnumDeclBits.getNextBit());
7995f757f3fSDimitry Andric   ED->setFixed(EnumDeclBits.getNextBit());
8000b57cec5SDimitry Andric 
8010b57cec5SDimitry Andric   ED->setHasODRHash(true);
8020b57cec5SDimitry Andric   ED->ODRHash = Record.readInt();
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric   // If this is a definition subject to the ODR, and we already have a
8050b57cec5SDimitry Andric   // definition, merge this one into it.
8060fca6ea1SDimitry Andric   if (ED->isCompleteDefinition() && Reader.getContext().getLangOpts().Modules) {
8070b57cec5SDimitry Andric     EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
8080b57cec5SDimitry Andric     if (!OldDef) {
8090b57cec5SDimitry Andric       // This is the first time we've seen an imported definition. Look for a
8100b57cec5SDimitry Andric       // local definition before deciding that we are the first definition.
8110b57cec5SDimitry Andric       for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
8120b57cec5SDimitry Andric         if (!D->isFromASTFile() && D->isCompleteDefinition()) {
8130b57cec5SDimitry Andric           OldDef = D;
8140b57cec5SDimitry Andric           break;
8150b57cec5SDimitry Andric         }
8160b57cec5SDimitry Andric       }
8170b57cec5SDimitry Andric     }
8180b57cec5SDimitry Andric     if (OldDef) {
8190b57cec5SDimitry Andric       Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
82081ad6265SDimitry Andric       ED->demoteThisDefinitionToDeclaration();
8210b57cec5SDimitry Andric       Reader.mergeDefinitionVisibility(OldDef, ED);
822b3edf446SDimitry Andric       // We don't want to check the ODR hash value for declarations from global
823b3edf446SDimitry Andric       // module fragment.
8240fca6ea1SDimitry Andric       if (!shouldSkipCheckingODR(ED) && !shouldSkipCheckingODR(OldDef) &&
825b3edf446SDimitry Andric           OldDef->getODRHash() != ED->getODRHash())
8260b57cec5SDimitry Andric         Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
8270b57cec5SDimitry Andric     } else {
8280b57cec5SDimitry Andric       OldDef = ED;
8290b57cec5SDimitry Andric     }
8300b57cec5SDimitry Andric   }
8310b57cec5SDimitry Andric 
832480093f4SDimitry Andric   if (auto *InstED = readDeclAs<EnumDecl>()) {
8330b57cec5SDimitry Andric     auto TSK = (TemplateSpecializationKind)Record.readInt();
834480093f4SDimitry Andric     SourceLocation POI = readSourceLocation();
8350b57cec5SDimitry Andric     ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
8360b57cec5SDimitry Andric     ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
8370b57cec5SDimitry Andric   }
8380b57cec5SDimitry Andric }
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult
8410b57cec5SDimitry Andric ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
8420b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitTagDecl(RD);
8435f757f3fSDimitry Andric 
8445f757f3fSDimitry Andric   BitsUnpacker RecordDeclBits(Record.readInt());
8455f757f3fSDimitry Andric   RD->setHasFlexibleArrayMember(RecordDeclBits.getNextBit());
8465f757f3fSDimitry Andric   RD->setAnonymousStructOrUnion(RecordDeclBits.getNextBit());
8475f757f3fSDimitry Andric   RD->setHasObjectMember(RecordDeclBits.getNextBit());
8485f757f3fSDimitry Andric   RD->setHasVolatileMember(RecordDeclBits.getNextBit());
8495f757f3fSDimitry Andric   RD->setNonTrivialToPrimitiveDefaultInitialize(RecordDeclBits.getNextBit());
8505f757f3fSDimitry Andric   RD->setNonTrivialToPrimitiveCopy(RecordDeclBits.getNextBit());
8515f757f3fSDimitry Andric   RD->setNonTrivialToPrimitiveDestroy(RecordDeclBits.getNextBit());
8525f757f3fSDimitry Andric   RD->setHasNonTrivialToPrimitiveDefaultInitializeCUnion(
8535f757f3fSDimitry Andric       RecordDeclBits.getNextBit());
8545f757f3fSDimitry Andric   RD->setHasNonTrivialToPrimitiveDestructCUnion(RecordDeclBits.getNextBit());
8555f757f3fSDimitry Andric   RD->setHasNonTrivialToPrimitiveCopyCUnion(RecordDeclBits.getNextBit());
8565f757f3fSDimitry Andric   RD->setParamDestroyedInCallee(RecordDeclBits.getNextBit());
8575f757f3fSDimitry Andric   RD->setArgPassingRestrictions(
8585f757f3fSDimitry Andric       (RecordArgPassingKind)RecordDeclBits.getNextBits(/*Width=*/2));
8590b57cec5SDimitry Andric   return Redecl;
8600b57cec5SDimitry Andric }
8610b57cec5SDimitry Andric 
862349cc55cSDimitry Andric void ASTDeclReader::VisitRecordDecl(RecordDecl *RD) {
863349cc55cSDimitry Andric   VisitRecordDeclImpl(RD);
864bdd1243dSDimitry Andric   RD->setODRHash(Record.readInt());
865349cc55cSDimitry Andric 
866349cc55cSDimitry Andric   // Maintain the invariant of a redeclaration chain containing only
867349cc55cSDimitry Andric   // a single definition.
868349cc55cSDimitry Andric   if (RD->isCompleteDefinition()) {
869349cc55cSDimitry Andric     RecordDecl *Canon = static_cast<RecordDecl *>(RD->getCanonicalDecl());
870349cc55cSDimitry Andric     RecordDecl *&OldDef = Reader.RecordDefinitions[Canon];
871349cc55cSDimitry Andric     if (!OldDef) {
872349cc55cSDimitry Andric       // This is the first time we've seen an imported definition. Look for a
873349cc55cSDimitry Andric       // local definition before deciding that we are the first definition.
874349cc55cSDimitry Andric       for (auto *D : merged_redecls(Canon)) {
875349cc55cSDimitry Andric         if (!D->isFromASTFile() && D->isCompleteDefinition()) {
876349cc55cSDimitry Andric           OldDef = D;
877349cc55cSDimitry Andric           break;
878349cc55cSDimitry Andric         }
879349cc55cSDimitry Andric       }
880349cc55cSDimitry Andric     }
881349cc55cSDimitry Andric     if (OldDef) {
882349cc55cSDimitry Andric       Reader.MergedDeclContexts.insert(std::make_pair(RD, OldDef));
88381ad6265SDimitry Andric       RD->demoteThisDefinitionToDeclaration();
884349cc55cSDimitry Andric       Reader.mergeDefinitionVisibility(OldDef, RD);
885bdd1243dSDimitry Andric       if (OldDef->getODRHash() != RD->getODRHash())
886bdd1243dSDimitry Andric         Reader.PendingRecordOdrMergeFailures[OldDef].push_back(RD);
887349cc55cSDimitry Andric     } else {
888349cc55cSDimitry Andric       OldDef = RD;
889349cc55cSDimitry Andric     }
890349cc55cSDimitry Andric   }
891349cc55cSDimitry Andric }
892349cc55cSDimitry Andric 
8930b57cec5SDimitry Andric void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
8940b57cec5SDimitry Andric   VisitNamedDecl(VD);
89506c3fb27SDimitry Andric   // For function or variable declarations, defer reading the type in case the
89606c3fb27SDimitry Andric   // declaration has a deduced type that references an entity declared within
89706c3fb27SDimitry Andric   // the function definition or variable initializer.
89806c3fb27SDimitry Andric   if (isa<FunctionDecl, VarDecl>(VD))
8990b57cec5SDimitry Andric     DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
9000b57cec5SDimitry Andric   else
9010b57cec5SDimitry Andric     VD->setType(Record.readType());
9020b57cec5SDimitry Andric }
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
9050b57cec5SDimitry Andric   VisitValueDecl(ECD);
9060b57cec5SDimitry Andric   if (Record.readInt())
9070b57cec5SDimitry Andric     ECD->setInitExpr(Record.readExpr());
9087a6dacacSDimitry Andric   ECD->setInitVal(Reader.getContext(), Record.readAPSInt());
9090b57cec5SDimitry Andric   mergeMergeable(ECD);
9100b57cec5SDimitry Andric }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
9130b57cec5SDimitry Andric   VisitValueDecl(DD);
914480093f4SDimitry Andric   DD->setInnerLocStart(readSourceLocation());
9150b57cec5SDimitry Andric   if (Record.readInt()) { // hasExtInfo
9160b57cec5SDimitry Andric     auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
917480093f4SDimitry Andric     Record.readQualifierInfo(*Info);
918480093f4SDimitry Andric     Info->TrailingRequiresClause = Record.readExpr();
9190b57cec5SDimitry Andric     DD->DeclInfo = Info;
9200b57cec5SDimitry Andric   }
9210b57cec5SDimitry Andric   QualType TSIType = Record.readType();
9220b57cec5SDimitry Andric   DD->setTypeSourceInfo(
9230b57cec5SDimitry Andric       TSIType.isNull() ? nullptr
9240b57cec5SDimitry Andric                        : Reader.getContext().CreateTypeSourceInfo(TSIType));
9250b57cec5SDimitry Andric }
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
9280b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(FD);
9290b57cec5SDimitry Andric 
930bdd1243dSDimitry Andric   FunctionDecl *Existing = nullptr;
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
9330b57cec5SDimitry Andric   case FunctionDecl::TK_NonTemplate:
9340b57cec5SDimitry Andric     break;
935fcaf7f86SDimitry Andric   case FunctionDecl::TK_DependentNonTemplate:
936fcaf7f86SDimitry Andric     FD->setInstantiatedFromDecl(readDeclAs<FunctionDecl>());
937fcaf7f86SDimitry Andric     break;
938bdd1243dSDimitry Andric   case FunctionDecl::TK_FunctionTemplate: {
939bdd1243dSDimitry Andric     auto *Template = readDeclAs<FunctionTemplateDecl>();
940bdd1243dSDimitry Andric     Template->init(FD);
941bdd1243dSDimitry Andric     FD->setDescribedFunctionTemplate(Template);
9420b57cec5SDimitry Andric     break;
943bdd1243dSDimitry Andric   }
9440b57cec5SDimitry Andric   case FunctionDecl::TK_MemberSpecialization: {
945480093f4SDimitry Andric     auto *InstFD = readDeclAs<FunctionDecl>();
9460b57cec5SDimitry Andric     auto TSK = (TemplateSpecializationKind)Record.readInt();
947480093f4SDimitry Andric     SourceLocation POI = readSourceLocation();
9480b57cec5SDimitry Andric     FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
9490b57cec5SDimitry Andric     FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
9500b57cec5SDimitry Andric     break;
9510b57cec5SDimitry Andric   }
9520b57cec5SDimitry Andric   case FunctionDecl::TK_FunctionTemplateSpecialization: {
953480093f4SDimitry Andric     auto *Template = readDeclAs<FunctionTemplateDecl>();
9540b57cec5SDimitry Andric     auto TSK = (TemplateSpecializationKind)Record.readInt();
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric     // Template arguments.
9570b57cec5SDimitry Andric     SmallVector<TemplateArgument, 8> TemplArgs;
9580b57cec5SDimitry Andric     Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
9590b57cec5SDimitry Andric 
9600b57cec5SDimitry Andric     // Template args as written.
9615f757f3fSDimitry Andric     TemplateArgumentListInfo TemplArgsWritten;
9625f757f3fSDimitry Andric     bool HasTemplateArgumentsAsWritten = Record.readBool();
9635f757f3fSDimitry Andric     if (HasTemplateArgumentsAsWritten)
9645f757f3fSDimitry Andric       Record.readTemplateArgumentListInfo(TemplArgsWritten);
9650b57cec5SDimitry Andric 
966480093f4SDimitry Andric     SourceLocation POI = readSourceLocation();
9670b57cec5SDimitry Andric 
9680b57cec5SDimitry Andric     ASTContext &C = Reader.getContext();
9695f757f3fSDimitry Andric     TemplateArgumentList *TemplArgList =
9705f757f3fSDimitry Andric         TemplateArgumentList::CreateCopy(C, TemplArgs);
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric     MemberSpecializationInfo *MSInfo = nullptr;
9730b57cec5SDimitry Andric     if (Record.readInt()) {
974480093f4SDimitry Andric       auto *FD = readDeclAs<FunctionDecl>();
9750b57cec5SDimitry Andric       auto TSK = (TemplateSpecializationKind)Record.readInt();
976480093f4SDimitry Andric       SourceLocation POI = readSourceLocation();
9770b57cec5SDimitry Andric 
9780b57cec5SDimitry Andric       MSInfo = new (C) MemberSpecializationInfo(FD, TSK);
9790b57cec5SDimitry Andric       MSInfo->setPointOfInstantiation(POI);
9800b57cec5SDimitry Andric     }
9810b57cec5SDimitry Andric 
9820b57cec5SDimitry Andric     FunctionTemplateSpecializationInfo *FTInfo =
9830b57cec5SDimitry Andric         FunctionTemplateSpecializationInfo::Create(
9840b57cec5SDimitry Andric             C, FD, Template, TSK, TemplArgList,
9855f757f3fSDimitry Andric             HasTemplateArgumentsAsWritten ? &TemplArgsWritten : nullptr, POI,
9860b57cec5SDimitry Andric             MSInfo);
9870b57cec5SDimitry Andric     FD->TemplateOrSpecialization = FTInfo;
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric     if (FD->isCanonicalDecl()) { // if canonical add to template's set.
9900b57cec5SDimitry Andric       // The template that contains the specializations set. It's not safe to
9910b57cec5SDimitry Andric       // use getCanonicalDecl on Template since it may still be initializing.
992480093f4SDimitry Andric       auto *CanonTemplate = readDeclAs<FunctionTemplateDecl>();
9930b57cec5SDimitry Andric       // Get the InsertPos by FindNodeOrInsertPos() instead of calling
9940b57cec5SDimitry Andric       // InsertNode(FTInfo) directly to avoid the getASTContext() call in
9950b57cec5SDimitry Andric       // FunctionTemplateSpecializationInfo's Profile().
9960b57cec5SDimitry Andric       // We avoid getASTContext because a decl in the parent hierarchy may
9970b57cec5SDimitry Andric       // be initializing.
9980b57cec5SDimitry Andric       llvm::FoldingSetNodeID ID;
9990b57cec5SDimitry Andric       FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
10000b57cec5SDimitry Andric       void *InsertPos = nullptr;
10010b57cec5SDimitry Andric       FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
10020b57cec5SDimitry Andric       FunctionTemplateSpecializationInfo *ExistingInfo =
10030b57cec5SDimitry Andric           CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
10040b57cec5SDimitry Andric       if (InsertPos)
10050b57cec5SDimitry Andric         CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
10060b57cec5SDimitry Andric       else {
10070b57cec5SDimitry Andric         assert(Reader.getContext().getLangOpts().Modules &&
10080b57cec5SDimitry Andric                "already deserialized this template specialization");
1009bdd1243dSDimitry Andric         Existing = ExistingInfo->getFunction();
10100b57cec5SDimitry Andric       }
10110b57cec5SDimitry Andric     }
10120b57cec5SDimitry Andric     break;
10130b57cec5SDimitry Andric   }
10140b57cec5SDimitry Andric   case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
10150b57cec5SDimitry Andric     // Templates.
10165f757f3fSDimitry Andric     UnresolvedSet<8> Candidates;
10175f757f3fSDimitry Andric     unsigned NumCandidates = Record.readInt();
10185f757f3fSDimitry Andric     while (NumCandidates--)
10195f757f3fSDimitry Andric       Candidates.addDecl(readDeclAs<NamedDecl>());
10200b57cec5SDimitry Andric 
10210b57cec5SDimitry Andric     // Templates args.
10225f757f3fSDimitry Andric     TemplateArgumentListInfo TemplArgsWritten;
10235f757f3fSDimitry Andric     bool HasTemplateArgumentsAsWritten = Record.readBool();
10245f757f3fSDimitry Andric     if (HasTemplateArgumentsAsWritten)
10255f757f3fSDimitry Andric       Record.readTemplateArgumentListInfo(TemplArgsWritten);
10260b57cec5SDimitry Andric 
10275f757f3fSDimitry Andric     FD->setDependentTemplateSpecialization(
10285f757f3fSDimitry Andric         Reader.getContext(), Candidates,
10295f757f3fSDimitry Andric         HasTemplateArgumentsAsWritten ? &TemplArgsWritten : nullptr);
10300b57cec5SDimitry Andric     // These are not merged; we don't need to merge redeclarations of dependent
10310b57cec5SDimitry Andric     // template friends.
10320b57cec5SDimitry Andric     break;
10330b57cec5SDimitry Andric   }
10340b57cec5SDimitry Andric   }
10350b57cec5SDimitry Andric 
1036bdd1243dSDimitry Andric   VisitDeclaratorDecl(FD);
1037bdd1243dSDimitry Andric 
1038bdd1243dSDimitry Andric   // Attach a type to this function. Use the real type if possible, but fall
1039bdd1243dSDimitry Andric   // back to the type as written if it involves a deduced return type.
1040bdd1243dSDimitry Andric   if (FD->getTypeSourceInfo() && FD->getTypeSourceInfo()
1041bdd1243dSDimitry Andric                                      ->getType()
1042bdd1243dSDimitry Andric                                      ->castAs<FunctionType>()
1043bdd1243dSDimitry Andric                                      ->getReturnType()
1044bdd1243dSDimitry Andric                                      ->getContainedAutoType()) {
1045bdd1243dSDimitry Andric     // We'll set up the real type in Visit, once we've finished loading the
1046bdd1243dSDimitry Andric     // function.
1047bdd1243dSDimitry Andric     FD->setType(FD->getTypeSourceInfo()->getType());
104806c3fb27SDimitry Andric     Reader.PendingDeducedFunctionTypes.push_back({FD, DeferredTypeID});
1049bdd1243dSDimitry Andric   } else {
1050bdd1243dSDimitry Andric     FD->setType(Reader.GetType(DeferredTypeID));
1051bdd1243dSDimitry Andric   }
1052bdd1243dSDimitry Andric   DeferredTypeID = 0;
1053bdd1243dSDimitry Andric 
1054bdd1243dSDimitry Andric   FD->DNLoc = Record.readDeclarationNameLoc(FD->getDeclName());
1055bdd1243dSDimitry Andric   FD->IdentifierNamespace = Record.readInt();
1056bdd1243dSDimitry Andric 
1057bdd1243dSDimitry Andric   // FunctionDecl's body is handled last at ASTDeclReader::Visit,
1058bdd1243dSDimitry Andric   // after everything else is read.
10595f757f3fSDimitry Andric   BitsUnpacker FunctionDeclBits(Record.readInt());
1060bdd1243dSDimitry Andric 
1061cb14a3feSDimitry Andric   FD->setCachedLinkage((Linkage)FunctionDeclBits.getNextBits(/*Width=*/3));
10625f757f3fSDimitry Andric   FD->setStorageClass((StorageClass)FunctionDeclBits.getNextBits(/*Width=*/3));
10635f757f3fSDimitry Andric   FD->setInlineSpecified(FunctionDeclBits.getNextBit());
10645f757f3fSDimitry Andric   FD->setImplicitlyInline(FunctionDeclBits.getNextBit());
1065cb14a3feSDimitry Andric   FD->setHasSkippedBody(FunctionDeclBits.getNextBit());
10665f757f3fSDimitry Andric   FD->setVirtualAsWritten(FunctionDeclBits.getNextBit());
1067bdd1243dSDimitry Andric   // We defer calling `FunctionDecl::setPure()` here as for methods of
1068bdd1243dSDimitry Andric   // `CXXTemplateSpecializationDecl`s, we may not have connected up the
1069bdd1243dSDimitry Andric   // definition (which is required for `setPure`).
10705f757f3fSDimitry Andric   const bool Pure = FunctionDeclBits.getNextBit();
10715f757f3fSDimitry Andric   FD->setHasInheritedPrototype(FunctionDeclBits.getNextBit());
10725f757f3fSDimitry Andric   FD->setHasWrittenPrototype(FunctionDeclBits.getNextBit());
10735f757f3fSDimitry Andric   FD->setDeletedAsWritten(FunctionDeclBits.getNextBit());
10745f757f3fSDimitry Andric   FD->setTrivial(FunctionDeclBits.getNextBit());
10755f757f3fSDimitry Andric   FD->setTrivialForCall(FunctionDeclBits.getNextBit());
10765f757f3fSDimitry Andric   FD->setDefaulted(FunctionDeclBits.getNextBit());
10775f757f3fSDimitry Andric   FD->setExplicitlyDefaulted(FunctionDeclBits.getNextBit());
10785f757f3fSDimitry Andric   FD->setIneligibleOrNotSelected(FunctionDeclBits.getNextBit());
10795f757f3fSDimitry Andric   FD->setConstexprKind(
10805f757f3fSDimitry Andric       (ConstexprSpecKind)FunctionDeclBits.getNextBits(/*Width=*/2));
1081cb14a3feSDimitry Andric   FD->setHasImplicitReturnZero(FunctionDeclBits.getNextBit());
10825f757f3fSDimitry Andric   FD->setIsMultiVersion(FunctionDeclBits.getNextBit());
10835f757f3fSDimitry Andric   FD->setLateTemplateParsed(FunctionDeclBits.getNextBit());
10845f757f3fSDimitry Andric   FD->setFriendConstraintRefersToEnclosingTemplate(
10855f757f3fSDimitry Andric       FunctionDeclBits.getNextBit());
1086cb14a3feSDimitry Andric   FD->setUsesSEHTry(FunctionDeclBits.getNextBit());
1087bdd1243dSDimitry Andric 
1088bdd1243dSDimitry Andric   FD->EndRangeLoc = readSourceLocation();
10895f757f3fSDimitry Andric   if (FD->isExplicitlyDefaulted())
1090bdd1243dSDimitry Andric     FD->setDefaultLoc(readSourceLocation());
1091bdd1243dSDimitry Andric 
1092bdd1243dSDimitry Andric   FD->ODRHash = Record.readInt();
1093bdd1243dSDimitry Andric   FD->setHasODRHash(true);
1094bdd1243dSDimitry Andric 
10950fca6ea1SDimitry Andric   if (FD->isDefaulted() || FD->isDeletedAsWritten()) {
10960fca6ea1SDimitry Andric     // If 'Info' is nonzero, we need to read an DefaultedOrDeletedInfo; if,
10970fca6ea1SDimitry Andric     // additionally, the second bit is also set, we also need to read
10980fca6ea1SDimitry Andric     // a DeletedMessage for the DefaultedOrDeletedInfo.
10990fca6ea1SDimitry Andric     if (auto Info = Record.readInt()) {
11000fca6ea1SDimitry Andric       bool HasMessage = Info & 2;
11010fca6ea1SDimitry Andric       StringLiteral *DeletedMessage =
11020fca6ea1SDimitry Andric           HasMessage ? cast<StringLiteral>(Record.readExpr()) : nullptr;
11030fca6ea1SDimitry Andric 
11040fca6ea1SDimitry Andric       unsigned NumLookups = Record.readInt();
1105bdd1243dSDimitry Andric       SmallVector<DeclAccessPair, 8> Lookups;
1106bdd1243dSDimitry Andric       for (unsigned I = 0; I != NumLookups; ++I) {
1107bdd1243dSDimitry Andric         NamedDecl *ND = Record.readDeclAs<NamedDecl>();
1108bdd1243dSDimitry Andric         AccessSpecifier AS = (AccessSpecifier)Record.readInt();
1109bdd1243dSDimitry Andric         Lookups.push_back(DeclAccessPair::make(ND, AS));
1110bdd1243dSDimitry Andric       }
11110fca6ea1SDimitry Andric 
11120fca6ea1SDimitry Andric       FD->setDefaultedOrDeletedInfo(
11130fca6ea1SDimitry Andric           FunctionDecl::DefaultedOrDeletedFunctionInfo::Create(
11140fca6ea1SDimitry Andric               Reader.getContext(), Lookups, DeletedMessage));
1115bdd1243dSDimitry Andric     }
1116bdd1243dSDimitry Andric   }
1117bdd1243dSDimitry Andric 
1118bdd1243dSDimitry Andric   if (Existing)
1119bdd1243dSDimitry Andric     mergeRedeclarable(FD, Existing, Redecl);
1120bdd1243dSDimitry Andric   else if (auto Kind = FD->getTemplatedKind();
1121bdd1243dSDimitry Andric            Kind == FunctionDecl::TK_FunctionTemplate ||
1122bdd1243dSDimitry Andric            Kind == FunctionDecl::TK_FunctionTemplateSpecialization) {
1123bdd1243dSDimitry Andric     // Function Templates have their FunctionTemplateDecls merged instead of
1124bdd1243dSDimitry Andric     // their FunctionDecls.
1125bdd1243dSDimitry Andric     auto merge = [this, &Redecl, FD](auto &&F) {
1126bdd1243dSDimitry Andric       auto *Existing = cast_or_null<FunctionDecl>(Redecl.getKnownMergeTarget());
1127bdd1243dSDimitry Andric       RedeclarableResult NewRedecl(Existing ? F(Existing) : nullptr,
1128bdd1243dSDimitry Andric                                    Redecl.getFirstID(), Redecl.isKeyDecl());
1129bdd1243dSDimitry Andric       mergeRedeclarableTemplate(F(FD), NewRedecl);
1130bdd1243dSDimitry Andric     };
1131bdd1243dSDimitry Andric     if (Kind == FunctionDecl::TK_FunctionTemplate)
1132bdd1243dSDimitry Andric       merge(
1133bdd1243dSDimitry Andric           [](FunctionDecl *FD) { return FD->getDescribedFunctionTemplate(); });
1134bdd1243dSDimitry Andric     else
1135bdd1243dSDimitry Andric       merge([](FunctionDecl *FD) {
1136bdd1243dSDimitry Andric         return FD->getTemplateSpecializationInfo()->getTemplate();
1137bdd1243dSDimitry Andric       });
1138bdd1243dSDimitry Andric   } else
1139bdd1243dSDimitry Andric     mergeRedeclarable(FD, Redecl);
1140bdd1243dSDimitry Andric 
1141e8d8bef9SDimitry Andric   // Defer calling `setPure` until merging above has guaranteed we've set
1142e8d8bef9SDimitry Andric   // `DefinitionData` (as this will need to access it).
11437a6dacacSDimitry Andric   FD->setIsPureVirtual(Pure);
1144e8d8bef9SDimitry Andric 
11450b57cec5SDimitry Andric   // Read in the parameters.
11460b57cec5SDimitry Andric   unsigned NumParams = Record.readInt();
11470b57cec5SDimitry Andric   SmallVector<ParmVarDecl *, 16> Params;
11480b57cec5SDimitry Andric   Params.reserve(NumParams);
11490b57cec5SDimitry Andric   for (unsigned I = 0; I != NumParams; ++I)
1150480093f4SDimitry Andric     Params.push_back(readDeclAs<ParmVarDecl>());
11510b57cec5SDimitry Andric   FD->setParams(Reader.getContext(), Params);
11520b57cec5SDimitry Andric }
11530b57cec5SDimitry Andric 
11540b57cec5SDimitry Andric void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
11550b57cec5SDimitry Andric   VisitNamedDecl(MD);
11560b57cec5SDimitry Andric   if (Record.readInt()) {
11570b57cec5SDimitry Andric     // Load the body on-demand. Most clients won't care, because method
11580b57cec5SDimitry Andric     // definitions rarely show up in headers.
11590b57cec5SDimitry Andric     Reader.PendingBodies[MD] = GetCurrentCursorOffset();
11600b57cec5SDimitry Andric   }
1161480093f4SDimitry Andric   MD->setSelfDecl(readDeclAs<ImplicitParamDecl>());
1162480093f4SDimitry Andric   MD->setCmdDecl(readDeclAs<ImplicitParamDecl>());
11630b57cec5SDimitry Andric   MD->setInstanceMethod(Record.readInt());
11640b57cec5SDimitry Andric   MD->setVariadic(Record.readInt());
11650b57cec5SDimitry Andric   MD->setPropertyAccessor(Record.readInt());
1166480093f4SDimitry Andric   MD->setSynthesizedAccessorStub(Record.readInt());
11670b57cec5SDimitry Andric   MD->setDefined(Record.readInt());
11680b57cec5SDimitry Andric   MD->setOverriding(Record.readInt());
11690b57cec5SDimitry Andric   MD->setHasSkippedBody(Record.readInt());
11700b57cec5SDimitry Andric 
11710b57cec5SDimitry Andric   MD->setIsRedeclaration(Record.readInt());
11720b57cec5SDimitry Andric   MD->setHasRedeclaration(Record.readInt());
11730b57cec5SDimitry Andric   if (MD->hasRedeclaration())
11740b57cec5SDimitry Andric     Reader.getContext().setObjCMethodRedeclaration(MD,
1175480093f4SDimitry Andric                                        readDeclAs<ObjCMethodDecl>());
11760b57cec5SDimitry Andric 
11775f757f3fSDimitry Andric   MD->setDeclImplementation(
11785f757f3fSDimitry Andric       static_cast<ObjCImplementationControl>(Record.readInt()));
11790b57cec5SDimitry Andric   MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
11800b57cec5SDimitry Andric   MD->setRelatedResultType(Record.readInt());
11810b57cec5SDimitry Andric   MD->setReturnType(Record.readType());
1182480093f4SDimitry Andric   MD->setReturnTypeSourceInfo(readTypeSourceInfo());
1183480093f4SDimitry Andric   MD->DeclEndLoc = readSourceLocation();
11840b57cec5SDimitry Andric   unsigned NumParams = Record.readInt();
11850b57cec5SDimitry Andric   SmallVector<ParmVarDecl *, 16> Params;
11860b57cec5SDimitry Andric   Params.reserve(NumParams);
11870b57cec5SDimitry Andric   for (unsigned I = 0; I != NumParams; ++I)
1188480093f4SDimitry Andric     Params.push_back(readDeclAs<ParmVarDecl>());
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric   MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
11910b57cec5SDimitry Andric   unsigned NumStoredSelLocs = Record.readInt();
11920b57cec5SDimitry Andric   SmallVector<SourceLocation, 16> SelLocs;
11930b57cec5SDimitry Andric   SelLocs.reserve(NumStoredSelLocs);
11940b57cec5SDimitry Andric   for (unsigned i = 0; i != NumStoredSelLocs; ++i)
1195480093f4SDimitry Andric     SelLocs.push_back(readSourceLocation());
11960b57cec5SDimitry Andric 
11970b57cec5SDimitry Andric   MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
11980b57cec5SDimitry Andric }
11990b57cec5SDimitry Andric 
12000b57cec5SDimitry Andric void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
12010b57cec5SDimitry Andric   VisitTypedefNameDecl(D);
12020b57cec5SDimitry Andric 
12030b57cec5SDimitry Andric   D->Variance = Record.readInt();
12040b57cec5SDimitry Andric   D->Index = Record.readInt();
1205480093f4SDimitry Andric   D->VarianceLoc = readSourceLocation();
1206480093f4SDimitry Andric   D->ColonLoc = readSourceLocation();
12070b57cec5SDimitry Andric }
12080b57cec5SDimitry Andric 
12090b57cec5SDimitry Andric void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
12100b57cec5SDimitry Andric   VisitNamedDecl(CD);
1211480093f4SDimitry Andric   CD->setAtStartLoc(readSourceLocation());
1212480093f4SDimitry Andric   CD->setAtEndRange(readSourceRange());
12130b57cec5SDimitry Andric }
12140b57cec5SDimitry Andric 
12150b57cec5SDimitry Andric ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
12160b57cec5SDimitry Andric   unsigned numParams = Record.readInt();
12170b57cec5SDimitry Andric   if (numParams == 0)
12180b57cec5SDimitry Andric     return nullptr;
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric   SmallVector<ObjCTypeParamDecl *, 4> typeParams;
12210b57cec5SDimitry Andric   typeParams.reserve(numParams);
12220b57cec5SDimitry Andric   for (unsigned i = 0; i != numParams; ++i) {
1223480093f4SDimitry Andric     auto *typeParam = readDeclAs<ObjCTypeParamDecl>();
12240b57cec5SDimitry Andric     if (!typeParam)
12250b57cec5SDimitry Andric       return nullptr;
12260b57cec5SDimitry Andric 
12270b57cec5SDimitry Andric     typeParams.push_back(typeParam);
12280b57cec5SDimitry Andric   }
12290b57cec5SDimitry Andric 
1230480093f4SDimitry Andric   SourceLocation lAngleLoc = readSourceLocation();
1231480093f4SDimitry Andric   SourceLocation rAngleLoc = readSourceLocation();
12320b57cec5SDimitry Andric 
12330b57cec5SDimitry Andric   return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
12340b57cec5SDimitry Andric                                    typeParams, rAngleLoc);
12350b57cec5SDimitry Andric }
12360b57cec5SDimitry Andric 
12370b57cec5SDimitry Andric void ASTDeclReader::ReadObjCDefinitionData(
12380b57cec5SDimitry Andric          struct ObjCInterfaceDecl::DefinitionData &Data) {
12390b57cec5SDimitry Andric   // Read the superclass.
1240480093f4SDimitry Andric   Data.SuperClassTInfo = readTypeSourceInfo();
12410b57cec5SDimitry Andric 
1242480093f4SDimitry Andric   Data.EndLoc = readSourceLocation();
12430b57cec5SDimitry Andric   Data.HasDesignatedInitializers = Record.readInt();
1244bdd1243dSDimitry Andric   Data.ODRHash = Record.readInt();
1245bdd1243dSDimitry Andric   Data.HasODRHash = true;
12460b57cec5SDimitry Andric 
12470b57cec5SDimitry Andric   // Read the directly referenced protocols and their SourceLocations.
12480b57cec5SDimitry Andric   unsigned NumProtocols = Record.readInt();
12490b57cec5SDimitry Andric   SmallVector<ObjCProtocolDecl *, 16> Protocols;
12500b57cec5SDimitry Andric   Protocols.reserve(NumProtocols);
12510b57cec5SDimitry Andric   for (unsigned I = 0; I != NumProtocols; ++I)
1252480093f4SDimitry Andric     Protocols.push_back(readDeclAs<ObjCProtocolDecl>());
12530b57cec5SDimitry Andric   SmallVector<SourceLocation, 16> ProtoLocs;
12540b57cec5SDimitry Andric   ProtoLocs.reserve(NumProtocols);
12550b57cec5SDimitry Andric   for (unsigned I = 0; I != NumProtocols; ++I)
1256480093f4SDimitry Andric     ProtoLocs.push_back(readSourceLocation());
12570b57cec5SDimitry Andric   Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
12580b57cec5SDimitry Andric                                Reader.getContext());
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric   // Read the transitive closure of protocols referenced by this class.
12610b57cec5SDimitry Andric   NumProtocols = Record.readInt();
12620b57cec5SDimitry Andric   Protocols.clear();
12630b57cec5SDimitry Andric   Protocols.reserve(NumProtocols);
12640b57cec5SDimitry Andric   for (unsigned I = 0; I != NumProtocols; ++I)
1265480093f4SDimitry Andric     Protocols.push_back(readDeclAs<ObjCProtocolDecl>());
12660b57cec5SDimitry Andric   Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
12670b57cec5SDimitry Andric                                   Reader.getContext());
12680b57cec5SDimitry Andric }
12690b57cec5SDimitry Andric 
12700b57cec5SDimitry Andric void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
12710b57cec5SDimitry Andric          struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1272349cc55cSDimitry Andric   struct ObjCInterfaceDecl::DefinitionData &DD = D->data();
1273bdd1243dSDimitry Andric   if (DD.Definition == NewDD.Definition)
1274bdd1243dSDimitry Andric     return;
1275bdd1243dSDimitry Andric 
1276349cc55cSDimitry Andric   Reader.MergedDeclContexts.insert(
1277349cc55cSDimitry Andric       std::make_pair(NewDD.Definition, DD.Definition));
1278349cc55cSDimitry Andric   Reader.mergeDefinitionVisibility(DD.Definition, NewDD.Definition);
1279349cc55cSDimitry Andric 
1280bdd1243dSDimitry Andric   if (D->getODRHash() != NewDD.ODRHash)
1281bdd1243dSDimitry Andric     Reader.PendingObjCInterfaceOdrMergeFailures[DD.Definition].push_back(
1282bdd1243dSDimitry Andric         {NewDD.Definition, &NewDD});
12830b57cec5SDimitry Andric }
12840b57cec5SDimitry Andric 
12850b57cec5SDimitry Andric void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
12860b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(ID);
12870b57cec5SDimitry Andric   VisitObjCContainerDecl(ID);
12880b57cec5SDimitry Andric   DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
12890b57cec5SDimitry Andric   mergeRedeclarable(ID, Redecl);
12900b57cec5SDimitry Andric 
12910b57cec5SDimitry Andric   ID->TypeParamList = ReadObjCTypeParamList();
12920b57cec5SDimitry Andric   if (Record.readInt()) {
12930b57cec5SDimitry Andric     // Read the definition.
12940b57cec5SDimitry Andric     ID->allocateDefinitionData();
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric     ReadObjCDefinitionData(ID->data());
12970b57cec5SDimitry Andric     ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
12980b57cec5SDimitry Andric     if (Canon->Data.getPointer()) {
12990b57cec5SDimitry Andric       // If we already have a definition, keep the definition invariant and
13000b57cec5SDimitry Andric       // merge the data.
13010b57cec5SDimitry Andric       MergeDefinitionData(Canon, std::move(ID->data()));
13020b57cec5SDimitry Andric       ID->Data = Canon->Data;
13030b57cec5SDimitry Andric     } else {
13040b57cec5SDimitry Andric       // Set the definition data of the canonical declaration, so other
13050b57cec5SDimitry Andric       // redeclarations will see it.
13060b57cec5SDimitry Andric       ID->getCanonicalDecl()->Data = ID->Data;
13070b57cec5SDimitry Andric 
13080b57cec5SDimitry Andric       // We will rebuild this list lazily.
13090b57cec5SDimitry Andric       ID->setIvarList(nullptr);
13100b57cec5SDimitry Andric     }
13110b57cec5SDimitry Andric 
13120b57cec5SDimitry Andric     // Note that we have deserialized a definition.
13130b57cec5SDimitry Andric     Reader.PendingDefinitions.insert(ID);
13140b57cec5SDimitry Andric 
13150b57cec5SDimitry Andric     // Note that we've loaded this Objective-C class.
13160b57cec5SDimitry Andric     Reader.ObjCClassesLoaded.push_back(ID);
13170b57cec5SDimitry Andric   } else {
13180b57cec5SDimitry Andric     ID->Data = ID->getCanonicalDecl()->Data;
13190b57cec5SDimitry Andric   }
13200b57cec5SDimitry Andric }
13210b57cec5SDimitry Andric 
13220b57cec5SDimitry Andric void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
13230b57cec5SDimitry Andric   VisitFieldDecl(IVD);
13240b57cec5SDimitry Andric   IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
13250b57cec5SDimitry Andric   // This field will be built lazily.
13260b57cec5SDimitry Andric   IVD->setNextIvar(nullptr);
13270b57cec5SDimitry Andric   bool synth = Record.readInt();
13280b57cec5SDimitry Andric   IVD->setSynthesize(synth);
132981ad6265SDimitry Andric 
133081ad6265SDimitry Andric   // Check ivar redeclaration.
133181ad6265SDimitry Andric   if (IVD->isInvalidDecl())
133281ad6265SDimitry Andric     return;
133381ad6265SDimitry Andric   // Don't check ObjCInterfaceDecl as interfaces are named and mismatches can be
133481ad6265SDimitry Andric   // detected in VisitObjCInterfaceDecl. Here we are looking for redeclarations
133581ad6265SDimitry Andric   // in extensions.
133681ad6265SDimitry Andric   if (isa<ObjCInterfaceDecl>(IVD->getDeclContext()))
133781ad6265SDimitry Andric     return;
133881ad6265SDimitry Andric   ObjCInterfaceDecl *CanonIntf =
133981ad6265SDimitry Andric       IVD->getContainingInterface()->getCanonicalDecl();
134081ad6265SDimitry Andric   IdentifierInfo *II = IVD->getIdentifier();
134181ad6265SDimitry Andric   ObjCIvarDecl *PrevIvar = CanonIntf->lookupInstanceVariable(II);
134281ad6265SDimitry Andric   if (PrevIvar && PrevIvar != IVD) {
134381ad6265SDimitry Andric     auto *ParentExt = dyn_cast<ObjCCategoryDecl>(IVD->getDeclContext());
134481ad6265SDimitry Andric     auto *PrevParentExt =
134581ad6265SDimitry Andric         dyn_cast<ObjCCategoryDecl>(PrevIvar->getDeclContext());
134681ad6265SDimitry Andric     if (ParentExt && PrevParentExt) {
134781ad6265SDimitry Andric       // Postpone diagnostic as we should merge identical extensions from
134881ad6265SDimitry Andric       // different modules.
134981ad6265SDimitry Andric       Reader
135081ad6265SDimitry Andric           .PendingObjCExtensionIvarRedeclarations[std::make_pair(ParentExt,
135181ad6265SDimitry Andric                                                                  PrevParentExt)]
135281ad6265SDimitry Andric           .push_back(std::make_pair(IVD, PrevIvar));
135381ad6265SDimitry Andric     } else if (ParentExt || PrevParentExt) {
135481ad6265SDimitry Andric       // Duplicate ivars in extension + implementation are never compatible.
135581ad6265SDimitry Andric       // Compatibility of implementation + implementation should be handled in
135681ad6265SDimitry Andric       // VisitObjCImplementationDecl.
135781ad6265SDimitry Andric       Reader.Diag(IVD->getLocation(), diag::err_duplicate_ivar_declaration)
135881ad6265SDimitry Andric           << II;
135981ad6265SDimitry Andric       Reader.Diag(PrevIvar->getLocation(), diag::note_previous_definition);
136081ad6265SDimitry Andric     }
136181ad6265SDimitry Andric   }
13620b57cec5SDimitry Andric }
13630b57cec5SDimitry Andric 
13640b57cec5SDimitry Andric void ASTDeclReader::ReadObjCDefinitionData(
13650b57cec5SDimitry Andric          struct ObjCProtocolDecl::DefinitionData &Data) {
13660b57cec5SDimitry Andric     unsigned NumProtoRefs = Record.readInt();
13670b57cec5SDimitry Andric     SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
13680b57cec5SDimitry Andric     ProtoRefs.reserve(NumProtoRefs);
13690b57cec5SDimitry Andric     for (unsigned I = 0; I != NumProtoRefs; ++I)
1370480093f4SDimitry Andric       ProtoRefs.push_back(readDeclAs<ObjCProtocolDecl>());
13710b57cec5SDimitry Andric     SmallVector<SourceLocation, 16> ProtoLocs;
13720b57cec5SDimitry Andric     ProtoLocs.reserve(NumProtoRefs);
13730b57cec5SDimitry Andric     for (unsigned I = 0; I != NumProtoRefs; ++I)
1374480093f4SDimitry Andric       ProtoLocs.push_back(readSourceLocation());
13750b57cec5SDimitry Andric     Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
13760b57cec5SDimitry Andric                                  ProtoLocs.data(), Reader.getContext());
1377bdd1243dSDimitry Andric     Data.ODRHash = Record.readInt();
1378bdd1243dSDimitry Andric     Data.HasODRHash = true;
13790b57cec5SDimitry Andric }
13800b57cec5SDimitry Andric 
1381bdd1243dSDimitry Andric void ASTDeclReader::MergeDefinitionData(
1382bdd1243dSDimitry Andric     ObjCProtocolDecl *D, struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1383349cc55cSDimitry Andric   struct ObjCProtocolDecl::DefinitionData &DD = D->data();
1384bdd1243dSDimitry Andric   if (DD.Definition == NewDD.Definition)
1385bdd1243dSDimitry Andric     return;
1386bdd1243dSDimitry Andric 
1387349cc55cSDimitry Andric   Reader.MergedDeclContexts.insert(
1388349cc55cSDimitry Andric       std::make_pair(NewDD.Definition, DD.Definition));
1389349cc55cSDimitry Andric   Reader.mergeDefinitionVisibility(DD.Definition, NewDD.Definition);
1390349cc55cSDimitry Andric 
1391bdd1243dSDimitry Andric   if (D->getODRHash() != NewDD.ODRHash)
1392bdd1243dSDimitry Andric     Reader.PendingObjCProtocolOdrMergeFailures[DD.Definition].push_back(
1393bdd1243dSDimitry Andric         {NewDD.Definition, &NewDD});
13940b57cec5SDimitry Andric }
13950b57cec5SDimitry Andric 
13960b57cec5SDimitry Andric void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
13970b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(PD);
13980b57cec5SDimitry Andric   VisitObjCContainerDecl(PD);
13990b57cec5SDimitry Andric   mergeRedeclarable(PD, Redecl);
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric   if (Record.readInt()) {
14020b57cec5SDimitry Andric     // Read the definition.
14030b57cec5SDimitry Andric     PD->allocateDefinitionData();
14040b57cec5SDimitry Andric 
14050b57cec5SDimitry Andric     ReadObjCDefinitionData(PD->data());
14060b57cec5SDimitry Andric 
14070b57cec5SDimitry Andric     ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
14080b57cec5SDimitry Andric     if (Canon->Data.getPointer()) {
14090b57cec5SDimitry Andric       // If we already have a definition, keep the definition invariant and
14100b57cec5SDimitry Andric       // merge the data.
14110b57cec5SDimitry Andric       MergeDefinitionData(Canon, std::move(PD->data()));
14120b57cec5SDimitry Andric       PD->Data = Canon->Data;
14130b57cec5SDimitry Andric     } else {
14140b57cec5SDimitry Andric       // Set the definition data of the canonical declaration, so other
14150b57cec5SDimitry Andric       // redeclarations will see it.
14160b57cec5SDimitry Andric       PD->getCanonicalDecl()->Data = PD->Data;
14170b57cec5SDimitry Andric     }
14180b57cec5SDimitry Andric     // Note that we have deserialized a definition.
14190b57cec5SDimitry Andric     Reader.PendingDefinitions.insert(PD);
14200b57cec5SDimitry Andric   } else {
14210b57cec5SDimitry Andric     PD->Data = PD->getCanonicalDecl()->Data;
14220b57cec5SDimitry Andric   }
14230b57cec5SDimitry Andric }
14240b57cec5SDimitry Andric 
14250b57cec5SDimitry Andric void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
14260b57cec5SDimitry Andric   VisitFieldDecl(FD);
14270b57cec5SDimitry Andric }
14280b57cec5SDimitry Andric 
14290b57cec5SDimitry Andric void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
14300b57cec5SDimitry Andric   VisitObjCContainerDecl(CD);
1431480093f4SDimitry Andric   CD->setCategoryNameLoc(readSourceLocation());
1432480093f4SDimitry Andric   CD->setIvarLBraceLoc(readSourceLocation());
1433480093f4SDimitry Andric   CD->setIvarRBraceLoc(readSourceLocation());
14340b57cec5SDimitry Andric 
14350b57cec5SDimitry Andric   // Note that this category has been deserialized. We do this before
14360b57cec5SDimitry Andric   // deserializing the interface declaration, so that it will consider this
14370b57cec5SDimitry Andric   /// category.
14380b57cec5SDimitry Andric   Reader.CategoriesDeserialized.insert(CD);
14390b57cec5SDimitry Andric 
1440480093f4SDimitry Andric   CD->ClassInterface = readDeclAs<ObjCInterfaceDecl>();
14410b57cec5SDimitry Andric   CD->TypeParamList = ReadObjCTypeParamList();
14420b57cec5SDimitry Andric   unsigned NumProtoRefs = Record.readInt();
14430b57cec5SDimitry Andric   SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
14440b57cec5SDimitry Andric   ProtoRefs.reserve(NumProtoRefs);
14450b57cec5SDimitry Andric   for (unsigned I = 0; I != NumProtoRefs; ++I)
1446480093f4SDimitry Andric     ProtoRefs.push_back(readDeclAs<ObjCProtocolDecl>());
14470b57cec5SDimitry Andric   SmallVector<SourceLocation, 16> ProtoLocs;
14480b57cec5SDimitry Andric   ProtoLocs.reserve(NumProtoRefs);
14490b57cec5SDimitry Andric   for (unsigned I = 0; I != NumProtoRefs; ++I)
1450480093f4SDimitry Andric     ProtoLocs.push_back(readSourceLocation());
14510b57cec5SDimitry Andric   CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
14520b57cec5SDimitry Andric                       Reader.getContext());
14530b57cec5SDimitry Andric 
14540b57cec5SDimitry Andric   // Protocols in the class extension belong to the class.
14550b57cec5SDimitry Andric   if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
14560b57cec5SDimitry Andric     CD->ClassInterface->mergeClassExtensionProtocolList(
14570b57cec5SDimitry Andric         (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
14580b57cec5SDimitry Andric         Reader.getContext());
14590b57cec5SDimitry Andric }
14600b57cec5SDimitry Andric 
14610b57cec5SDimitry Andric void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
14620b57cec5SDimitry Andric   VisitNamedDecl(CAD);
1463480093f4SDimitry Andric   CAD->setClassInterface(readDeclAs<ObjCInterfaceDecl>());
14640b57cec5SDimitry Andric }
14650b57cec5SDimitry Andric 
14660b57cec5SDimitry Andric void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
14670b57cec5SDimitry Andric   VisitNamedDecl(D);
1468480093f4SDimitry Andric   D->setAtLoc(readSourceLocation());
1469480093f4SDimitry Andric   D->setLParenLoc(readSourceLocation());
14700b57cec5SDimitry Andric   QualType T = Record.readType();
1471480093f4SDimitry Andric   TypeSourceInfo *TSI = readTypeSourceInfo();
14720b57cec5SDimitry Andric   D->setType(T, TSI);
14735ffd83dbSDimitry Andric   D->setPropertyAttributes((ObjCPropertyAttribute::Kind)Record.readInt());
14740b57cec5SDimitry Andric   D->setPropertyAttributesAsWritten(
14755ffd83dbSDimitry Andric       (ObjCPropertyAttribute::Kind)Record.readInt());
14760b57cec5SDimitry Andric   D->setPropertyImplementation(
14770b57cec5SDimitry Andric       (ObjCPropertyDecl::PropertyControl)Record.readInt());
14780b57cec5SDimitry Andric   DeclarationName GetterName = Record.readDeclarationName();
1479480093f4SDimitry Andric   SourceLocation GetterLoc = readSourceLocation();
14800b57cec5SDimitry Andric   D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
14810b57cec5SDimitry Andric   DeclarationName SetterName = Record.readDeclarationName();
1482480093f4SDimitry Andric   SourceLocation SetterLoc = readSourceLocation();
14830b57cec5SDimitry Andric   D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
1484480093f4SDimitry Andric   D->setGetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1485480093f4SDimitry Andric   D->setSetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1486480093f4SDimitry Andric   D->setPropertyIvarDecl(readDeclAs<ObjCIvarDecl>());
14870b57cec5SDimitry Andric }
14880b57cec5SDimitry Andric 
14890b57cec5SDimitry Andric void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
14900b57cec5SDimitry Andric   VisitObjCContainerDecl(D);
1491480093f4SDimitry Andric   D->setClassInterface(readDeclAs<ObjCInterfaceDecl>());
14920b57cec5SDimitry Andric }
14930b57cec5SDimitry Andric 
14940b57cec5SDimitry Andric void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
14950b57cec5SDimitry Andric   VisitObjCImplDecl(D);
1496480093f4SDimitry Andric   D->CategoryNameLoc = readSourceLocation();
14970b57cec5SDimitry Andric }
14980b57cec5SDimitry Andric 
14990b57cec5SDimitry Andric void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
15000b57cec5SDimitry Andric   VisitObjCImplDecl(D);
1501480093f4SDimitry Andric   D->setSuperClass(readDeclAs<ObjCInterfaceDecl>());
1502480093f4SDimitry Andric   D->SuperLoc = readSourceLocation();
1503480093f4SDimitry Andric   D->setIvarLBraceLoc(readSourceLocation());
1504480093f4SDimitry Andric   D->setIvarRBraceLoc(readSourceLocation());
15050b57cec5SDimitry Andric   D->setHasNonZeroConstructors(Record.readInt());
15060b57cec5SDimitry Andric   D->setHasDestructors(Record.readInt());
15070b57cec5SDimitry Andric   D->NumIvarInitializers = Record.readInt();
15080b57cec5SDimitry Andric   if (D->NumIvarInitializers)
15090b57cec5SDimitry Andric     D->IvarInitializers = ReadGlobalOffset();
15100b57cec5SDimitry Andric }
15110b57cec5SDimitry Andric 
15120b57cec5SDimitry Andric void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
15130b57cec5SDimitry Andric   VisitDecl(D);
1514480093f4SDimitry Andric   D->setAtLoc(readSourceLocation());
1515480093f4SDimitry Andric   D->setPropertyDecl(readDeclAs<ObjCPropertyDecl>());
1516480093f4SDimitry Andric   D->PropertyIvarDecl = readDeclAs<ObjCIvarDecl>();
1517480093f4SDimitry Andric   D->IvarLoc = readSourceLocation();
1518480093f4SDimitry Andric   D->setGetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1519480093f4SDimitry Andric   D->setSetterMethodDecl(readDeclAs<ObjCMethodDecl>());
15200b57cec5SDimitry Andric   D->setGetterCXXConstructor(Record.readExpr());
15210b57cec5SDimitry Andric   D->setSetterCXXAssignment(Record.readExpr());
15220b57cec5SDimitry Andric }
15230b57cec5SDimitry Andric 
15240b57cec5SDimitry Andric void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
15250b57cec5SDimitry Andric   VisitDeclaratorDecl(FD);
15260b57cec5SDimitry Andric   FD->Mutable = Record.readInt();
15270b57cec5SDimitry Andric 
152806c3fb27SDimitry Andric   unsigned Bits = Record.readInt();
152906c3fb27SDimitry Andric   FD->StorageKind = Bits >> 1;
153006c3fb27SDimitry Andric   if (FD->StorageKind == FieldDecl::ISK_CapturedVLAType)
153106c3fb27SDimitry Andric     FD->CapturedVLAType =
153206c3fb27SDimitry Andric         cast<VariableArrayType>(Record.readType().getTypePtr());
153306c3fb27SDimitry Andric   else if (Bits & 1)
153406c3fb27SDimitry Andric     FD->setBitWidth(Record.readExpr());
15350b57cec5SDimitry Andric 
15360b57cec5SDimitry Andric   if (!FD->getDeclName()) {
1537480093f4SDimitry Andric     if (auto *Tmpl = readDeclAs<FieldDecl>())
15380b57cec5SDimitry Andric       Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
15390b57cec5SDimitry Andric   }
15400b57cec5SDimitry Andric   mergeMergeable(FD);
15410b57cec5SDimitry Andric }
15420b57cec5SDimitry Andric 
15430b57cec5SDimitry Andric void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
15440b57cec5SDimitry Andric   VisitDeclaratorDecl(PD);
1545480093f4SDimitry Andric   PD->GetterId = Record.readIdentifier();
1546480093f4SDimitry Andric   PD->SetterId = Record.readIdentifier();
15470b57cec5SDimitry Andric }
15480b57cec5SDimitry Andric 
15495ffd83dbSDimitry Andric void ASTDeclReader::VisitMSGuidDecl(MSGuidDecl *D) {
15505ffd83dbSDimitry Andric   VisitValueDecl(D);
15515ffd83dbSDimitry Andric   D->PartVal.Part1 = Record.readInt();
15525ffd83dbSDimitry Andric   D->PartVal.Part2 = Record.readInt();
15535ffd83dbSDimitry Andric   D->PartVal.Part3 = Record.readInt();
15545ffd83dbSDimitry Andric   for (auto &C : D->PartVal.Part4And5)
15555ffd83dbSDimitry Andric     C = Record.readInt();
15565ffd83dbSDimitry Andric 
15575ffd83dbSDimitry Andric   // Add this GUID to the AST context's lookup structure, and merge if needed.
15585ffd83dbSDimitry Andric   if (MSGuidDecl *Existing = Reader.getContext().MSGuidDecls.GetOrInsertNode(D))
15595ffd83dbSDimitry Andric     Reader.getContext().setPrimaryMergedDecl(D, Existing->getCanonicalDecl());
15605ffd83dbSDimitry Andric }
15615ffd83dbSDimitry Andric 
156281ad6265SDimitry Andric void ASTDeclReader::VisitUnnamedGlobalConstantDecl(
156381ad6265SDimitry Andric     UnnamedGlobalConstantDecl *D) {
156481ad6265SDimitry Andric   VisitValueDecl(D);
156581ad6265SDimitry Andric   D->Value = Record.readAPValue();
156681ad6265SDimitry Andric 
156781ad6265SDimitry Andric   // Add this to the AST context's lookup structure, and merge if needed.
156881ad6265SDimitry Andric   if (UnnamedGlobalConstantDecl *Existing =
156981ad6265SDimitry Andric           Reader.getContext().UnnamedGlobalConstantDecls.GetOrInsertNode(D))
157081ad6265SDimitry Andric     Reader.getContext().setPrimaryMergedDecl(D, Existing->getCanonicalDecl());
157181ad6265SDimitry Andric }
157281ad6265SDimitry Andric 
1573e8d8bef9SDimitry Andric void ASTDeclReader::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) {
1574e8d8bef9SDimitry Andric   VisitValueDecl(D);
1575e8d8bef9SDimitry Andric   D->Value = Record.readAPValue();
1576e8d8bef9SDimitry Andric 
1577e8d8bef9SDimitry Andric   // Add this template parameter object to the AST context's lookup structure,
1578e8d8bef9SDimitry Andric   // and merge if needed.
1579e8d8bef9SDimitry Andric   if (TemplateParamObjectDecl *Existing =
1580e8d8bef9SDimitry Andric           Reader.getContext().TemplateParamObjectDecls.GetOrInsertNode(D))
1581e8d8bef9SDimitry Andric     Reader.getContext().setPrimaryMergedDecl(D, Existing->getCanonicalDecl());
1582e8d8bef9SDimitry Andric }
1583e8d8bef9SDimitry Andric 
15840b57cec5SDimitry Andric void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
15850b57cec5SDimitry Andric   VisitValueDecl(FD);
15860b57cec5SDimitry Andric 
15870b57cec5SDimitry Andric   FD->ChainingSize = Record.readInt();
15880b57cec5SDimitry Andric   assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
15890b57cec5SDimitry Andric   FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
15900b57cec5SDimitry Andric 
15910b57cec5SDimitry Andric   for (unsigned I = 0; I != FD->ChainingSize; ++I)
1592480093f4SDimitry Andric     FD->Chaining[I] = readDeclAs<NamedDecl>();
15930b57cec5SDimitry Andric 
15940b57cec5SDimitry Andric   mergeMergeable(FD);
15950b57cec5SDimitry Andric }
15960b57cec5SDimitry Andric 
15970b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
15980b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(VD);
15990b57cec5SDimitry Andric   VisitDeclaratorDecl(VD);
16000b57cec5SDimitry Andric 
16015f757f3fSDimitry Andric   BitsUnpacker VarDeclBits(Record.readInt());
1602cb14a3feSDimitry Andric   auto VarLinkage = Linkage(VarDeclBits.getNextBits(/*Width=*/3));
1603cb14a3feSDimitry Andric   bool DefGeneratedInModule = VarDeclBits.getNextBit();
16045f757f3fSDimitry Andric   VD->VarDeclBits.SClass = (StorageClass)VarDeclBits.getNextBits(/*Width=*/3);
16055f757f3fSDimitry Andric   VD->VarDeclBits.TSCSpec = VarDeclBits.getNextBits(/*Width=*/2);
16065f757f3fSDimitry Andric   VD->VarDeclBits.InitStyle = VarDeclBits.getNextBits(/*Width=*/2);
16075f757f3fSDimitry Andric   VD->VarDeclBits.ARCPseudoStrong = VarDeclBits.getNextBit();
160806c3fb27SDimitry Andric   bool HasDeducedType = false;
16090b57cec5SDimitry Andric   if (!isa<ParmVarDecl>(VD)) {
16100b57cec5SDimitry Andric     VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
16115f757f3fSDimitry Andric         VarDeclBits.getNextBit();
16125f757f3fSDimitry Andric     VD->NonParmVarDeclBits.ExceptionVar = VarDeclBits.getNextBit();
16135f757f3fSDimitry Andric     VD->NonParmVarDeclBits.NRVOVariable = VarDeclBits.getNextBit();
16145f757f3fSDimitry Andric     VD->NonParmVarDeclBits.CXXForRangeDecl = VarDeclBits.getNextBit();
1615cb14a3feSDimitry Andric 
16165f757f3fSDimitry Andric     VD->NonParmVarDeclBits.IsInline = VarDeclBits.getNextBit();
16175f757f3fSDimitry Andric     VD->NonParmVarDeclBits.IsInlineSpecified = VarDeclBits.getNextBit();
16185f757f3fSDimitry Andric     VD->NonParmVarDeclBits.IsConstexpr = VarDeclBits.getNextBit();
16195f757f3fSDimitry Andric     VD->NonParmVarDeclBits.IsInitCapture = VarDeclBits.getNextBit();
16205f757f3fSDimitry Andric     VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope =
16215f757f3fSDimitry Andric         VarDeclBits.getNextBit();
1622cb14a3feSDimitry Andric 
16235f757f3fSDimitry Andric     VD->NonParmVarDeclBits.EscapingByref = VarDeclBits.getNextBit();
16245f757f3fSDimitry Andric     HasDeducedType = VarDeclBits.getNextBit();
1625cb14a3feSDimitry Andric     VD->NonParmVarDeclBits.ImplicitParamKind =
1626cb14a3feSDimitry Andric         VarDeclBits.getNextBits(/*Width*/ 3);
1627cb14a3feSDimitry Andric 
1628cb14a3feSDimitry Andric     VD->NonParmVarDeclBits.ObjCForDecl = VarDeclBits.getNextBit();
16290b57cec5SDimitry Andric   }
163006c3fb27SDimitry Andric 
163106c3fb27SDimitry Andric   // If this variable has a deduced type, defer reading that type until we are
163206c3fb27SDimitry Andric   // done deserializing this variable, because the type might refer back to the
163306c3fb27SDimitry Andric   // variable.
163406c3fb27SDimitry Andric   if (HasDeducedType)
163506c3fb27SDimitry Andric     Reader.PendingDeducedVarTypes.push_back({VD, DeferredTypeID});
163606c3fb27SDimitry Andric   else
163706c3fb27SDimitry Andric     VD->setType(Reader.GetType(DeferredTypeID));
163806c3fb27SDimitry Andric   DeferredTypeID = 0;
163906c3fb27SDimitry Andric 
16400b57cec5SDimitry Andric   VD->setCachedLinkage(VarLinkage);
16410b57cec5SDimitry Andric 
16420b57cec5SDimitry Andric   // Reconstruct the one piece of the IdentifierNamespace that we need.
16435f757f3fSDimitry Andric   if (VD->getStorageClass() == SC_Extern && VarLinkage != Linkage::None &&
16440b57cec5SDimitry Andric       VD->getLexicalDeclContext()->isFunctionOrMethod())
16450b57cec5SDimitry Andric     VD->setLocalExternDecl();
16460b57cec5SDimitry Andric 
1647cb14a3feSDimitry Andric   if (DefGeneratedInModule) {
16485f757f3fSDimitry Andric     Reader.DefinitionSource[VD] =
16495f757f3fSDimitry Andric         Loc.F->Kind == ModuleKind::MK_MainFile ||
16505f757f3fSDimitry Andric         Reader.getContext().getLangOpts().BuildingPCHWithObjectFile;
16515f757f3fSDimitry Andric   }
16525f757f3fSDimitry Andric 
165306c3fb27SDimitry Andric   if (VD->hasAttr<BlocksAttr>()) {
16540b57cec5SDimitry Andric     Expr *CopyExpr = Record.readExpr();
16550b57cec5SDimitry Andric     if (CopyExpr)
16560b57cec5SDimitry Andric       Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
16570b57cec5SDimitry Andric   }
16580b57cec5SDimitry Andric 
16590b57cec5SDimitry Andric   enum VarKind {
16600b57cec5SDimitry Andric     VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
16610b57cec5SDimitry Andric   };
16620b57cec5SDimitry Andric   switch ((VarKind)Record.readInt()) {
16630b57cec5SDimitry Andric   case VarNotTemplate:
16640b57cec5SDimitry Andric     // Only true variables (not parameters or implicit parameters) can be
16650b57cec5SDimitry Andric     // merged; the other kinds are not really redeclarable at all.
16660b57cec5SDimitry Andric     if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
16670b57cec5SDimitry Andric         !isa<VarTemplateSpecializationDecl>(VD))
16680b57cec5SDimitry Andric       mergeRedeclarable(VD, Redecl);
16690b57cec5SDimitry Andric     break;
16700b57cec5SDimitry Andric   case VarTemplate:
16710b57cec5SDimitry Andric     // Merged when we merge the template.
1672480093f4SDimitry Andric     VD->setDescribedVarTemplate(readDeclAs<VarTemplateDecl>());
16730b57cec5SDimitry Andric     break;
16740b57cec5SDimitry Andric   case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
1675480093f4SDimitry Andric     auto *Tmpl = readDeclAs<VarDecl>();
16760b57cec5SDimitry Andric     auto TSK = (TemplateSpecializationKind)Record.readInt();
1677480093f4SDimitry Andric     SourceLocation POI = readSourceLocation();
16780b57cec5SDimitry Andric     Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
16790b57cec5SDimitry Andric     mergeRedeclarable(VD, Redecl);
16800b57cec5SDimitry Andric     break;
16810b57cec5SDimitry Andric   }
16820b57cec5SDimitry Andric   }
16830b57cec5SDimitry Andric 
16840b57cec5SDimitry Andric   return Redecl;
16850b57cec5SDimitry Andric }
16860b57cec5SDimitry Andric 
168706c3fb27SDimitry Andric void ASTDeclReader::ReadVarDeclInit(VarDecl *VD) {
168806c3fb27SDimitry Andric   if (uint64_t Val = Record.readInt()) {
168906c3fb27SDimitry Andric     EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
169006c3fb27SDimitry Andric     Eval->HasConstantInitialization = (Val & 2) != 0;
169106c3fb27SDimitry Andric     Eval->HasConstantDestruction = (Val & 4) != 0;
169206c3fb27SDimitry Andric     Eval->WasEvaluated = (Val & 8) != 0;
169306c3fb27SDimitry Andric     if (Eval->WasEvaluated) {
169406c3fb27SDimitry Andric       Eval->Evaluated = Record.readAPValue();
169506c3fb27SDimitry Andric       if (Eval->Evaluated.needsCleanup())
169606c3fb27SDimitry Andric         Reader.getContext().addDestruction(&Eval->Evaluated);
169706c3fb27SDimitry Andric     }
169806c3fb27SDimitry Andric 
169906c3fb27SDimitry Andric     // Store the offset of the initializer. Don't deserialize it yet: it might
170006c3fb27SDimitry Andric     // not be needed, and might refer back to the variable, for example if it
170106c3fb27SDimitry Andric     // contains a lambda.
170206c3fb27SDimitry Andric     Eval->Value = GetCurrentCursorOffset();
170306c3fb27SDimitry Andric   }
170406c3fb27SDimitry Andric }
170506c3fb27SDimitry Andric 
17060b57cec5SDimitry Andric void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
17070b57cec5SDimitry Andric   VisitVarDecl(PD);
17080b57cec5SDimitry Andric }
17090b57cec5SDimitry Andric 
17100b57cec5SDimitry Andric void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
17110b57cec5SDimitry Andric   VisitVarDecl(PD);
17125f757f3fSDimitry Andric 
1713647cbc5dSDimitry Andric   unsigned scopeIndex = Record.readInt();
17145f757f3fSDimitry Andric   BitsUnpacker ParmVarDeclBits(Record.readInt());
17155f757f3fSDimitry Andric   unsigned isObjCMethodParam = ParmVarDeclBits.getNextBit();
17165f757f3fSDimitry Andric   unsigned scopeDepth = ParmVarDeclBits.getNextBits(/*Width=*/7);
17175f757f3fSDimitry Andric   unsigned declQualifier = ParmVarDeclBits.getNextBits(/*Width=*/7);
17180b57cec5SDimitry Andric   if (isObjCMethodParam) {
17190b57cec5SDimitry Andric     assert(scopeDepth == 0);
17200b57cec5SDimitry Andric     PD->setObjCMethodScopeInfo(scopeIndex);
17210b57cec5SDimitry Andric     PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
17220b57cec5SDimitry Andric   } else {
17230b57cec5SDimitry Andric     PD->setScopeInfo(scopeDepth, scopeIndex);
17240b57cec5SDimitry Andric   }
17255f757f3fSDimitry Andric   PD->ParmVarDeclBits.IsKNRPromoted = ParmVarDeclBits.getNextBit();
17265f757f3fSDimitry Andric 
17275f757f3fSDimitry Andric   PD->ParmVarDeclBits.HasInheritedDefaultArg = ParmVarDeclBits.getNextBit();
17285f757f3fSDimitry Andric   if (ParmVarDeclBits.getNextBit()) // hasUninstantiatedDefaultArg.
17290b57cec5SDimitry Andric     PD->setUninstantiatedDefaultArg(Record.readExpr());
17300b57cec5SDimitry Andric 
17315f757f3fSDimitry Andric   if (ParmVarDeclBits.getNextBit()) // Valid explicit object parameter
17325f757f3fSDimitry Andric     PD->ExplicitObjectParameterIntroducerLoc = Record.readSourceLocation();
17335f757f3fSDimitry Andric 
17340b57cec5SDimitry Andric   // FIXME: If this is a redeclaration of a function from another module, handle
17350b57cec5SDimitry Andric   // inheritance of default arguments.
17360b57cec5SDimitry Andric }
17370b57cec5SDimitry Andric 
17380b57cec5SDimitry Andric void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
17390b57cec5SDimitry Andric   VisitVarDecl(DD);
17400b57cec5SDimitry Andric   auto **BDs = DD->getTrailingObjects<BindingDecl *>();
17410b57cec5SDimitry Andric   for (unsigned I = 0; I != DD->NumBindings; ++I) {
1742480093f4SDimitry Andric     BDs[I] = readDeclAs<BindingDecl>();
17430b57cec5SDimitry Andric     BDs[I]->setDecomposedDecl(DD);
17440b57cec5SDimitry Andric   }
17450b57cec5SDimitry Andric }
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
17480b57cec5SDimitry Andric   VisitValueDecl(BD);
17490b57cec5SDimitry Andric   BD->Binding = Record.readExpr();
17500b57cec5SDimitry Andric }
17510b57cec5SDimitry Andric 
17520b57cec5SDimitry Andric void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
17530b57cec5SDimitry Andric   VisitDecl(AD);
17540b57cec5SDimitry Andric   AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
1755480093f4SDimitry Andric   AD->setRParenLoc(readSourceLocation());
17560b57cec5SDimitry Andric }
17570b57cec5SDimitry Andric 
1758bdd1243dSDimitry Andric void ASTDeclReader::VisitTopLevelStmtDecl(TopLevelStmtDecl *D) {
1759bdd1243dSDimitry Andric   VisitDecl(D);
1760bdd1243dSDimitry Andric   D->Statement = Record.readStmt();
1761bdd1243dSDimitry Andric }
1762bdd1243dSDimitry Andric 
17630b57cec5SDimitry Andric void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
17640b57cec5SDimitry Andric   VisitDecl(BD);
17650b57cec5SDimitry Andric   BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
1766480093f4SDimitry Andric   BD->setSignatureAsWritten(readTypeSourceInfo());
17670b57cec5SDimitry Andric   unsigned NumParams = Record.readInt();
17680b57cec5SDimitry Andric   SmallVector<ParmVarDecl *, 16> Params;
17690b57cec5SDimitry Andric   Params.reserve(NumParams);
17700b57cec5SDimitry Andric   for (unsigned I = 0; I != NumParams; ++I)
1771480093f4SDimitry Andric     Params.push_back(readDeclAs<ParmVarDecl>());
17720b57cec5SDimitry Andric   BD->setParams(Params);
17730b57cec5SDimitry Andric 
17740b57cec5SDimitry Andric   BD->setIsVariadic(Record.readInt());
17750b57cec5SDimitry Andric   BD->setBlockMissingReturnType(Record.readInt());
17760b57cec5SDimitry Andric   BD->setIsConversionFromLambda(Record.readInt());
17770b57cec5SDimitry Andric   BD->setDoesNotEscape(Record.readInt());
17780b57cec5SDimitry Andric   BD->setCanAvoidCopyToHeap(Record.readInt());
17790b57cec5SDimitry Andric 
17800b57cec5SDimitry Andric   bool capturesCXXThis = Record.readInt();
17810b57cec5SDimitry Andric   unsigned numCaptures = Record.readInt();
17820b57cec5SDimitry Andric   SmallVector<BlockDecl::Capture, 16> captures;
17830b57cec5SDimitry Andric   captures.reserve(numCaptures);
17840b57cec5SDimitry Andric   for (unsigned i = 0; i != numCaptures; ++i) {
1785480093f4SDimitry Andric     auto *decl = readDeclAs<VarDecl>();
17860b57cec5SDimitry Andric     unsigned flags = Record.readInt();
17870b57cec5SDimitry Andric     bool byRef = (flags & 1);
17880b57cec5SDimitry Andric     bool nested = (flags & 2);
17890b57cec5SDimitry Andric     Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
17900b57cec5SDimitry Andric 
17910b57cec5SDimitry Andric     captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
17920b57cec5SDimitry Andric   }
17930b57cec5SDimitry Andric   BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
17940b57cec5SDimitry Andric }
17950b57cec5SDimitry Andric 
17960b57cec5SDimitry Andric void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
17970b57cec5SDimitry Andric   VisitDecl(CD);
17980b57cec5SDimitry Andric   unsigned ContextParamPos = Record.readInt();
17990b57cec5SDimitry Andric   CD->setNothrow(Record.readInt() != 0);
18000b57cec5SDimitry Andric   // Body is set by VisitCapturedStmt.
18010b57cec5SDimitry Andric   for (unsigned I = 0; I < CD->NumParams; ++I) {
18020b57cec5SDimitry Andric     if (I != ContextParamPos)
1803480093f4SDimitry Andric       CD->setParam(I, readDeclAs<ImplicitParamDecl>());
18040b57cec5SDimitry Andric     else
1805480093f4SDimitry Andric       CD->setContextParam(I, readDeclAs<ImplicitParamDecl>());
18060b57cec5SDimitry Andric   }
18070b57cec5SDimitry Andric }
18080b57cec5SDimitry Andric 
18090b57cec5SDimitry Andric void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
18100b57cec5SDimitry Andric   VisitDecl(D);
18115f757f3fSDimitry Andric   D->setLanguage(static_cast<LinkageSpecLanguageIDs>(Record.readInt()));
1812480093f4SDimitry Andric   D->setExternLoc(readSourceLocation());
1813480093f4SDimitry Andric   D->setRBraceLoc(readSourceLocation());
18140b57cec5SDimitry Andric }
18150b57cec5SDimitry Andric 
18160b57cec5SDimitry Andric void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
18170b57cec5SDimitry Andric   VisitDecl(D);
1818480093f4SDimitry Andric   D->RBraceLoc = readSourceLocation();
18190b57cec5SDimitry Andric }
18200b57cec5SDimitry Andric 
18210b57cec5SDimitry Andric void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
18220b57cec5SDimitry Andric   VisitNamedDecl(D);
1823480093f4SDimitry Andric   D->setLocStart(readSourceLocation());
18240b57cec5SDimitry Andric }
18250b57cec5SDimitry Andric 
18260b57cec5SDimitry Andric void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
18270b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(D);
18280b57cec5SDimitry Andric   VisitNamedDecl(D);
18295f757f3fSDimitry Andric 
18305f757f3fSDimitry Andric   BitsUnpacker NamespaceDeclBits(Record.readInt());
18315f757f3fSDimitry Andric   D->setInline(NamespaceDeclBits.getNextBit());
18325f757f3fSDimitry Andric   D->setNested(NamespaceDeclBits.getNextBit());
1833480093f4SDimitry Andric   D->LocStart = readSourceLocation();
1834480093f4SDimitry Andric   D->RBraceLoc = readSourceLocation();
18350b57cec5SDimitry Andric 
18360b57cec5SDimitry Andric   // Defer loading the anonymous namespace until we've finished merging
18370b57cec5SDimitry Andric   // this namespace; loading it might load a later declaration of the
18380b57cec5SDimitry Andric   // same namespace, and we have an invariant that older declarations
18390b57cec5SDimitry Andric   // get merged before newer ones try to merge.
18400fca6ea1SDimitry Andric   GlobalDeclID AnonNamespace;
18410fca6ea1SDimitry Andric   if (Redecl.getFirstID() == ThisDeclID)
1842480093f4SDimitry Andric     AnonNamespace = readDeclID();
18430b57cec5SDimitry Andric 
18440b57cec5SDimitry Andric   mergeRedeclarable(D, Redecl);
18450b57cec5SDimitry Andric 
18460fca6ea1SDimitry Andric   if (AnonNamespace.isValid()) {
18470b57cec5SDimitry Andric     // Each module has its own anonymous namespace, which is disjoint from
18480b57cec5SDimitry Andric     // any other module's anonymous namespaces, so don't attach the anonymous
18490b57cec5SDimitry Andric     // namespace at all.
18500b57cec5SDimitry Andric     auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
18510b57cec5SDimitry Andric     if (!Record.isModule())
18520b57cec5SDimitry Andric       D->setAnonymousNamespace(Anon);
18530b57cec5SDimitry Andric   }
18540b57cec5SDimitry Andric }
18550b57cec5SDimitry Andric 
1856bdd1243dSDimitry Andric void ASTDeclReader::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
1857bdd1243dSDimitry Andric   VisitNamedDecl(D);
1858bdd1243dSDimitry Andric   VisitDeclContext(D);
1859bdd1243dSDimitry Andric   D->IsCBuffer = Record.readBool();
1860bdd1243dSDimitry Andric   D->KwLoc = readSourceLocation();
1861bdd1243dSDimitry Andric   D->LBraceLoc = readSourceLocation();
1862bdd1243dSDimitry Andric   D->RBraceLoc = readSourceLocation();
1863bdd1243dSDimitry Andric }
1864bdd1243dSDimitry Andric 
18650b57cec5SDimitry Andric void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
18660b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(D);
18670b57cec5SDimitry Andric   VisitNamedDecl(D);
1868480093f4SDimitry Andric   D->NamespaceLoc = readSourceLocation();
1869480093f4SDimitry Andric   D->IdentLoc = readSourceLocation();
18700b57cec5SDimitry Andric   D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1871480093f4SDimitry Andric   D->Namespace = readDeclAs<NamedDecl>();
18720b57cec5SDimitry Andric   mergeRedeclarable(D, Redecl);
18730b57cec5SDimitry Andric }
18740b57cec5SDimitry Andric 
18750b57cec5SDimitry Andric void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
18760b57cec5SDimitry Andric   VisitNamedDecl(D);
1877480093f4SDimitry Andric   D->setUsingLoc(readSourceLocation());
18780b57cec5SDimitry Andric   D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1879480093f4SDimitry Andric   D->DNLoc = Record.readDeclarationNameLoc(D->getDeclName());
1880480093f4SDimitry Andric   D->FirstUsingShadow.setPointer(readDeclAs<UsingShadowDecl>());
18810b57cec5SDimitry Andric   D->setTypename(Record.readInt());
1882480093f4SDimitry Andric   if (auto *Pattern = readDeclAs<NamedDecl>())
18830b57cec5SDimitry Andric     Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
18840b57cec5SDimitry Andric   mergeMergeable(D);
18850b57cec5SDimitry Andric }
18860b57cec5SDimitry Andric 
1887fe6060f1SDimitry Andric void ASTDeclReader::VisitUsingEnumDecl(UsingEnumDecl *D) {
1888fe6060f1SDimitry Andric   VisitNamedDecl(D);
1889fe6060f1SDimitry Andric   D->setUsingLoc(readSourceLocation());
1890fe6060f1SDimitry Andric   D->setEnumLoc(readSourceLocation());
1891bdd1243dSDimitry Andric   D->setEnumType(Record.readTypeSourceInfo());
1892fe6060f1SDimitry Andric   D->FirstUsingShadow.setPointer(readDeclAs<UsingShadowDecl>());
1893fe6060f1SDimitry Andric   if (auto *Pattern = readDeclAs<UsingEnumDecl>())
1894fe6060f1SDimitry Andric     Reader.getContext().setInstantiatedFromUsingEnumDecl(D, Pattern);
1895fe6060f1SDimitry Andric   mergeMergeable(D);
1896fe6060f1SDimitry Andric }
1897fe6060f1SDimitry Andric 
18980b57cec5SDimitry Andric void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
18990b57cec5SDimitry Andric   VisitNamedDecl(D);
1900480093f4SDimitry Andric   D->InstantiatedFrom = readDeclAs<NamedDecl>();
19010b57cec5SDimitry Andric   auto **Expansions = D->getTrailingObjects<NamedDecl *>();
19020b57cec5SDimitry Andric   for (unsigned I = 0; I != D->NumExpansions; ++I)
1903480093f4SDimitry Andric     Expansions[I] = readDeclAs<NamedDecl>();
19040b57cec5SDimitry Andric   mergeMergeable(D);
19050b57cec5SDimitry Andric }
19060b57cec5SDimitry Andric 
19070b57cec5SDimitry Andric void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
19080b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(D);
19090b57cec5SDimitry Andric   VisitNamedDecl(D);
1910480093f4SDimitry Andric   D->Underlying = readDeclAs<NamedDecl>();
19110b57cec5SDimitry Andric   D->IdentifierNamespace = Record.readInt();
1912480093f4SDimitry Andric   D->UsingOrNextShadow = readDeclAs<NamedDecl>();
1913480093f4SDimitry Andric   auto *Pattern = readDeclAs<UsingShadowDecl>();
19140b57cec5SDimitry Andric   if (Pattern)
19150b57cec5SDimitry Andric     Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
19160b57cec5SDimitry Andric   mergeRedeclarable(D, Redecl);
19170b57cec5SDimitry Andric }
19180b57cec5SDimitry Andric 
19190b57cec5SDimitry Andric void ASTDeclReader::VisitConstructorUsingShadowDecl(
19200b57cec5SDimitry Andric     ConstructorUsingShadowDecl *D) {
19210b57cec5SDimitry Andric   VisitUsingShadowDecl(D);
1922480093f4SDimitry Andric   D->NominatedBaseClassShadowDecl = readDeclAs<ConstructorUsingShadowDecl>();
1923480093f4SDimitry Andric   D->ConstructedBaseClassShadowDecl = readDeclAs<ConstructorUsingShadowDecl>();
19240b57cec5SDimitry Andric   D->IsVirtual = Record.readInt();
19250b57cec5SDimitry Andric }
19260b57cec5SDimitry Andric 
19270b57cec5SDimitry Andric void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
19280b57cec5SDimitry Andric   VisitNamedDecl(D);
1929480093f4SDimitry Andric   D->UsingLoc = readSourceLocation();
1930480093f4SDimitry Andric   D->NamespaceLoc = readSourceLocation();
19310b57cec5SDimitry Andric   D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1932480093f4SDimitry Andric   D->NominatedNamespace = readDeclAs<NamedDecl>();
1933480093f4SDimitry Andric   D->CommonAncestor = readDeclAs<DeclContext>();
19340b57cec5SDimitry Andric }
19350b57cec5SDimitry Andric 
19360b57cec5SDimitry Andric void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
19370b57cec5SDimitry Andric   VisitValueDecl(D);
1938480093f4SDimitry Andric   D->setUsingLoc(readSourceLocation());
19390b57cec5SDimitry Andric   D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1940480093f4SDimitry Andric   D->DNLoc = Record.readDeclarationNameLoc(D->getDeclName());
1941480093f4SDimitry Andric   D->EllipsisLoc = readSourceLocation();
19420b57cec5SDimitry Andric   mergeMergeable(D);
19430b57cec5SDimitry Andric }
19440b57cec5SDimitry Andric 
19450b57cec5SDimitry Andric void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
19460b57cec5SDimitry Andric                                                UnresolvedUsingTypenameDecl *D) {
19470b57cec5SDimitry Andric   VisitTypeDecl(D);
1948480093f4SDimitry Andric   D->TypenameLocation = readSourceLocation();
19490b57cec5SDimitry Andric   D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1950480093f4SDimitry Andric   D->EllipsisLoc = readSourceLocation();
19510b57cec5SDimitry Andric   mergeMergeable(D);
19520b57cec5SDimitry Andric }
19530b57cec5SDimitry Andric 
1954fe6060f1SDimitry Andric void ASTDeclReader::VisitUnresolvedUsingIfExistsDecl(
1955fe6060f1SDimitry Andric     UnresolvedUsingIfExistsDecl *D) {
1956fe6060f1SDimitry Andric   VisitNamedDecl(D);
1957fe6060f1SDimitry Andric }
1958fe6060f1SDimitry Andric 
19590b57cec5SDimitry Andric void ASTDeclReader::ReadCXXDefinitionData(
196006c3fb27SDimitry Andric     struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D,
196106c3fb27SDimitry Andric     Decl *LambdaContext, unsigned IndexInLambdaContext) {
19625f757f3fSDimitry Andric 
19635f757f3fSDimitry Andric   BitsUnpacker CXXRecordDeclBits = Record.readInt();
19645f757f3fSDimitry Andric 
19655f757f3fSDimitry Andric #define FIELD(Name, Width, Merge)                                              \
19665f757f3fSDimitry Andric   if (!CXXRecordDeclBits.canGetNextNBits(Width))                         \
19675f757f3fSDimitry Andric     CXXRecordDeclBits.updateValue(Record.readInt());                           \
19685f757f3fSDimitry Andric   Data.Name = CXXRecordDeclBits.getNextBits(Width);
19695f757f3fSDimitry Andric 
1970a7dea167SDimitry Andric #include "clang/AST/CXXRecordDeclDefinitionBits.def"
19715f757f3fSDimitry Andric #undef FIELD
1972a7dea167SDimitry Andric 
19730b57cec5SDimitry Andric   // Note: the caller has deserialized the IsLambda bit already.
19740b57cec5SDimitry Andric   Data.ODRHash = Record.readInt();
19750b57cec5SDimitry Andric   Data.HasODRHash = true;
19760b57cec5SDimitry Andric 
19775ffd83dbSDimitry Andric   if (Record.readInt()) {
1978e8d8bef9SDimitry Andric     Reader.DefinitionSource[D] =
1979e8d8bef9SDimitry Andric         Loc.F->Kind == ModuleKind::MK_MainFile ||
1980e8d8bef9SDimitry Andric         Reader.getContext().getLangOpts().BuildingPCHWithObjectFile;
19815ffd83dbSDimitry Andric   }
19820b57cec5SDimitry Andric 
19830b57cec5SDimitry Andric   Record.readUnresolvedSet(Data.Conversions);
1984a7dea167SDimitry Andric   Data.ComputedVisibleConversions = Record.readInt();
1985a7dea167SDimitry Andric   if (Data.ComputedVisibleConversions)
19860b57cec5SDimitry Andric     Record.readUnresolvedSet(Data.VisibleConversions);
19870b57cec5SDimitry Andric   assert(Data.Definition && "Data.Definition should be already set!");
19880b57cec5SDimitry Andric 
198906c3fb27SDimitry Andric   if (!Data.IsLambda) {
199006c3fb27SDimitry Andric     assert(!LambdaContext && !IndexInLambdaContext &&
199106c3fb27SDimitry Andric            "given lambda context for non-lambda");
199206c3fb27SDimitry Andric 
199306c3fb27SDimitry Andric     Data.NumBases = Record.readInt();
199406c3fb27SDimitry Andric     if (Data.NumBases)
199506c3fb27SDimitry Andric       Data.Bases = ReadGlobalOffset();
199606c3fb27SDimitry Andric 
199706c3fb27SDimitry Andric     Data.NumVBases = Record.readInt();
199806c3fb27SDimitry Andric     if (Data.NumVBases)
199906c3fb27SDimitry Andric       Data.VBases = ReadGlobalOffset();
200006c3fb27SDimitry Andric 
20010fca6ea1SDimitry Andric     Data.FirstFriend = readDeclID().getRawValue();
200206c3fb27SDimitry Andric   } else {
20030b57cec5SDimitry Andric     using Capture = LambdaCapture;
20040b57cec5SDimitry Andric 
20050b57cec5SDimitry Andric     auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
20065f757f3fSDimitry Andric 
20075f757f3fSDimitry Andric     BitsUnpacker LambdaBits(Record.readInt());
20085f757f3fSDimitry Andric     Lambda.DependencyKind = LambdaBits.getNextBits(/*Width=*/2);
20095f757f3fSDimitry Andric     Lambda.IsGenericLambda = LambdaBits.getNextBit();
20105f757f3fSDimitry Andric     Lambda.CaptureDefault = LambdaBits.getNextBits(/*Width=*/2);
20115f757f3fSDimitry Andric     Lambda.NumCaptures = LambdaBits.getNextBits(/*Width=*/15);
20125f757f3fSDimitry Andric     Lambda.HasKnownInternalLinkage = LambdaBits.getNextBit();
20135f757f3fSDimitry Andric 
20140b57cec5SDimitry Andric     Lambda.NumExplicitCaptures = Record.readInt();
20150b57cec5SDimitry Andric     Lambda.ManglingNumber = Record.readInt();
201606c3fb27SDimitry Andric     if (unsigned DeviceManglingNumber = Record.readInt())
201706c3fb27SDimitry Andric       Reader.getContext().DeviceLambdaManglingNumbers[D] = DeviceManglingNumber;
201806c3fb27SDimitry Andric     Lambda.IndexInContext = IndexInLambdaContext;
201906c3fb27SDimitry Andric     Lambda.ContextDecl = LambdaContext;
2020bdd1243dSDimitry Andric     Capture *ToCapture = nullptr;
2021bdd1243dSDimitry Andric     if (Lambda.NumCaptures) {
2022bdd1243dSDimitry Andric       ToCapture = (Capture *)Reader.getContext().Allocate(sizeof(Capture) *
2023bdd1243dSDimitry Andric                                                           Lambda.NumCaptures);
2024bdd1243dSDimitry Andric       Lambda.AddCaptureList(Reader.getContext(), ToCapture);
2025bdd1243dSDimitry Andric     }
2026480093f4SDimitry Andric     Lambda.MethodTyInfo = readTypeSourceInfo();
20270b57cec5SDimitry Andric     for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
2028480093f4SDimitry Andric       SourceLocation Loc = readSourceLocation();
20295f757f3fSDimitry Andric       BitsUnpacker CaptureBits(Record.readInt());
20305f757f3fSDimitry Andric       bool IsImplicit = CaptureBits.getNextBit();
20315f757f3fSDimitry Andric       auto Kind =
20325f757f3fSDimitry Andric           static_cast<LambdaCaptureKind>(CaptureBits.getNextBits(/*Width=*/3));
20330b57cec5SDimitry Andric       switch (Kind) {
20340b57cec5SDimitry Andric       case LCK_StarThis:
20350b57cec5SDimitry Andric       case LCK_This:
20360b57cec5SDimitry Andric       case LCK_VLAType:
20375f757f3fSDimitry Andric         new (ToCapture)
20385f757f3fSDimitry Andric             Capture(Loc, IsImplicit, Kind, nullptr, SourceLocation());
20395f757f3fSDimitry Andric         ToCapture++;
20400b57cec5SDimitry Andric         break;
20410b57cec5SDimitry Andric       case LCK_ByCopy:
20420b57cec5SDimitry Andric       case LCK_ByRef:
20435f757f3fSDimitry Andric         auto *Var = readDeclAs<ValueDecl>();
2044480093f4SDimitry Andric         SourceLocation EllipsisLoc = readSourceLocation();
20455f757f3fSDimitry Andric         new (ToCapture) Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
20465f757f3fSDimitry Andric         ToCapture++;
20470b57cec5SDimitry Andric         break;
20480b57cec5SDimitry Andric       }
20490b57cec5SDimitry Andric     }
20500b57cec5SDimitry Andric   }
20510b57cec5SDimitry Andric }
20520b57cec5SDimitry Andric 
20530b57cec5SDimitry Andric void ASTDeclReader::MergeDefinitionData(
20540b57cec5SDimitry Andric     CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
20550b57cec5SDimitry Andric   assert(D->DefinitionData &&
20560b57cec5SDimitry Andric          "merging class definition into non-definition");
20570b57cec5SDimitry Andric   auto &DD = *D->DefinitionData;
20580b57cec5SDimitry Andric 
20590b57cec5SDimitry Andric   if (DD.Definition != MergeDD.Definition) {
20600b57cec5SDimitry Andric     // Track that we merged the definitions.
20610b57cec5SDimitry Andric     Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
20620b57cec5SDimitry Andric                                                     DD.Definition));
20630b57cec5SDimitry Andric     Reader.PendingDefinitions.erase(MergeDD.Definition);
20640b57cec5SDimitry Andric     MergeDD.Definition->setCompleteDefinition(false);
20650b57cec5SDimitry Andric     Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
206606c3fb27SDimitry Andric     assert(!Reader.Lookups.contains(MergeDD.Definition) &&
20670b57cec5SDimitry Andric            "already loaded pending lookups for merged definition");
20680b57cec5SDimitry Andric   }
20690b57cec5SDimitry Andric 
20700b57cec5SDimitry Andric   auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
20710b57cec5SDimitry Andric   if (PFDI != Reader.PendingFakeDefinitionData.end() &&
20720b57cec5SDimitry Andric       PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
20730b57cec5SDimitry Andric     // We faked up this definition data because we found a class for which we'd
20740b57cec5SDimitry Andric     // not yet loaded the definition. Replace it with the real thing now.
20750b57cec5SDimitry Andric     assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
20760b57cec5SDimitry Andric     PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
20770b57cec5SDimitry Andric 
20780b57cec5SDimitry Andric     // Don't change which declaration is the definition; that is required
20790b57cec5SDimitry Andric     // to be invariant once we select it.
20800b57cec5SDimitry Andric     auto *Def = DD.Definition;
20810b57cec5SDimitry Andric     DD = std::move(MergeDD);
20820b57cec5SDimitry Andric     DD.Definition = Def;
20830b57cec5SDimitry Andric     return;
20840b57cec5SDimitry Andric   }
20850b57cec5SDimitry Andric 
20860b57cec5SDimitry Andric   bool DetectedOdrViolation = false;
2087a7dea167SDimitry Andric 
2088a7dea167SDimitry Andric   #define FIELD(Name, Width, Merge) Merge(Name)
2089a7dea167SDimitry Andric   #define MERGE_OR(Field) DD.Field |= MergeDD.Field;
2090a7dea167SDimitry Andric   #define NO_MERGE(Field) \
20910b57cec5SDimitry Andric     DetectedOdrViolation |= DD.Field != MergeDD.Field; \
2092a7dea167SDimitry Andric     MERGE_OR(Field)
2093a7dea167SDimitry Andric   #include "clang/AST/CXXRecordDeclDefinitionBits.def"
2094a7dea167SDimitry Andric   NO_MERGE(IsLambda)
2095a7dea167SDimitry Andric   #undef NO_MERGE
2096a7dea167SDimitry Andric   #undef MERGE_OR
20970b57cec5SDimitry Andric 
20980b57cec5SDimitry Andric   if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
20990b57cec5SDimitry Andric     DetectedOdrViolation = true;
21000b57cec5SDimitry Andric   // FIXME: Issue a diagnostic if the base classes don't match when we come
21010b57cec5SDimitry Andric   // to lazily load them.
21020b57cec5SDimitry Andric 
21030b57cec5SDimitry Andric   // FIXME: Issue a diagnostic if the list of conversion functions doesn't
21040b57cec5SDimitry Andric   // match when we come to lazily load them.
21050b57cec5SDimitry Andric   if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
21060b57cec5SDimitry Andric     DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
21070b57cec5SDimitry Andric     DD.ComputedVisibleConversions = true;
21080b57cec5SDimitry Andric   }
21090b57cec5SDimitry Andric 
21100b57cec5SDimitry Andric   // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
21110b57cec5SDimitry Andric   // lazily load it.
21120b57cec5SDimitry Andric 
21130b57cec5SDimitry Andric   if (DD.IsLambda) {
2114bdd1243dSDimitry Andric     auto &Lambda1 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(DD);
2115bdd1243dSDimitry Andric     auto &Lambda2 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(MergeDD);
2116bdd1243dSDimitry Andric     DetectedOdrViolation |= Lambda1.DependencyKind != Lambda2.DependencyKind;
2117bdd1243dSDimitry Andric     DetectedOdrViolation |= Lambda1.IsGenericLambda != Lambda2.IsGenericLambda;
2118bdd1243dSDimitry Andric     DetectedOdrViolation |= Lambda1.CaptureDefault != Lambda2.CaptureDefault;
2119bdd1243dSDimitry Andric     DetectedOdrViolation |= Lambda1.NumCaptures != Lambda2.NumCaptures;
2120bdd1243dSDimitry Andric     DetectedOdrViolation |=
2121bdd1243dSDimitry Andric         Lambda1.NumExplicitCaptures != Lambda2.NumExplicitCaptures;
2122bdd1243dSDimitry Andric     DetectedOdrViolation |=
2123bdd1243dSDimitry Andric         Lambda1.HasKnownInternalLinkage != Lambda2.HasKnownInternalLinkage;
2124bdd1243dSDimitry Andric     DetectedOdrViolation |= Lambda1.ManglingNumber != Lambda2.ManglingNumber;
2125bdd1243dSDimitry Andric 
2126bdd1243dSDimitry Andric     if (Lambda1.NumCaptures && Lambda1.NumCaptures == Lambda2.NumCaptures) {
2127bdd1243dSDimitry Andric       for (unsigned I = 0, N = Lambda1.NumCaptures; I != N; ++I) {
2128bdd1243dSDimitry Andric         LambdaCapture &Cap1 = Lambda1.Captures.front()[I];
2129bdd1243dSDimitry Andric         LambdaCapture &Cap2 = Lambda2.Captures.front()[I];
2130bdd1243dSDimitry Andric         DetectedOdrViolation |= Cap1.getCaptureKind() != Cap2.getCaptureKind();
2131bdd1243dSDimitry Andric       }
2132bdd1243dSDimitry Andric       Lambda1.AddCaptureList(Reader.getContext(), Lambda2.Captures.front());
2133bdd1243dSDimitry Andric     }
21340b57cec5SDimitry Andric   }
21350b57cec5SDimitry Andric 
2136b3edf446SDimitry Andric   // We don't want to check ODR for decls in the global module fragment.
21370fca6ea1SDimitry Andric   if (shouldSkipCheckingODR(MergeDD.Definition) || shouldSkipCheckingODR(D))
2138b3edf446SDimitry Andric     return;
2139b3edf446SDimitry Andric 
21400b57cec5SDimitry Andric   if (D->getODRHash() != MergeDD.ODRHash) {
21410b57cec5SDimitry Andric     DetectedOdrViolation = true;
21420b57cec5SDimitry Andric   }
21430b57cec5SDimitry Andric 
21440b57cec5SDimitry Andric   if (DetectedOdrViolation)
21450b57cec5SDimitry Andric     Reader.PendingOdrMergeFailures[DD.Definition].push_back(
21460b57cec5SDimitry Andric         {MergeDD.Definition, &MergeDD});
21470b57cec5SDimitry Andric }
21480b57cec5SDimitry Andric 
214906c3fb27SDimitry Andric void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update,
215006c3fb27SDimitry Andric                                             Decl *LambdaContext,
215106c3fb27SDimitry Andric                                             unsigned IndexInLambdaContext) {
21520b57cec5SDimitry Andric   struct CXXRecordDecl::DefinitionData *DD;
21530b57cec5SDimitry Andric   ASTContext &C = Reader.getContext();
21540b57cec5SDimitry Andric 
21550b57cec5SDimitry Andric   // Determine whether this is a lambda closure type, so that we can
21560b57cec5SDimitry Andric   // allocate the appropriate DefinitionData structure.
21570b57cec5SDimitry Andric   bool IsLambda = Record.readInt();
215806c3fb27SDimitry Andric   assert(!(IsLambda && Update) &&
215906c3fb27SDimitry Andric          "lambda definition should not be added by update record");
21600b57cec5SDimitry Andric   if (IsLambda)
216181ad6265SDimitry Andric     DD = new (C) CXXRecordDecl::LambdaDefinitionData(
216281ad6265SDimitry Andric         D, nullptr, CXXRecordDecl::LDK_Unknown, false, LCD_None);
21630b57cec5SDimitry Andric   else
21640b57cec5SDimitry Andric     DD = new (C) struct CXXRecordDecl::DefinitionData(D);
21650b57cec5SDimitry Andric 
21660b57cec5SDimitry Andric   CXXRecordDecl *Canon = D->getCanonicalDecl();
21670b57cec5SDimitry Andric   // Set decl definition data before reading it, so that during deserialization
21680b57cec5SDimitry Andric   // when we read CXXRecordDecl, it already has definition data and we don't
21690b57cec5SDimitry Andric   // set fake one.
21700b57cec5SDimitry Andric   if (!Canon->DefinitionData)
21710b57cec5SDimitry Andric     Canon->DefinitionData = DD;
21720b57cec5SDimitry Andric   D->DefinitionData = Canon->DefinitionData;
217306c3fb27SDimitry Andric   ReadCXXDefinitionData(*DD, D, LambdaContext, IndexInLambdaContext);
21740b57cec5SDimitry Andric 
21750b57cec5SDimitry Andric   // We might already have a different definition for this record. This can
21760b57cec5SDimitry Andric   // happen either because we're reading an update record, or because we've
21770b57cec5SDimitry Andric   // already done some merging. Either way, just merge into it.
21780b57cec5SDimitry Andric   if (Canon->DefinitionData != DD) {
21790b57cec5SDimitry Andric     MergeDefinitionData(Canon, std::move(*DD));
21800b57cec5SDimitry Andric     return;
21810b57cec5SDimitry Andric   }
21820b57cec5SDimitry Andric 
21830b57cec5SDimitry Andric   // Mark this declaration as being a definition.
21840b57cec5SDimitry Andric   D->setCompleteDefinition(true);
21850b57cec5SDimitry Andric 
21860b57cec5SDimitry Andric   // If this is not the first declaration or is an update record, we can have
21870b57cec5SDimitry Andric   // other redeclarations already. Make a note that we need to propagate the
21880b57cec5SDimitry Andric   // DefinitionData pointer onto them.
21890b57cec5SDimitry Andric   if (Update || Canon != D)
21900b57cec5SDimitry Andric     Reader.PendingDefinitions.insert(D);
21910b57cec5SDimitry Andric }
21920b57cec5SDimitry Andric 
21930b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult
21940b57cec5SDimitry Andric ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
21950b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRecordDeclImpl(D);
21960b57cec5SDimitry Andric 
21970b57cec5SDimitry Andric   ASTContext &C = Reader.getContext();
21980b57cec5SDimitry Andric 
21990b57cec5SDimitry Andric   enum CXXRecKind {
220006c3fb27SDimitry Andric     CXXRecNotTemplate = 0,
220106c3fb27SDimitry Andric     CXXRecTemplate,
220206c3fb27SDimitry Andric     CXXRecMemberSpecialization,
220306c3fb27SDimitry Andric     CXXLambda
22040b57cec5SDimitry Andric   };
220506c3fb27SDimitry Andric 
220606c3fb27SDimitry Andric   Decl *LambdaContext = nullptr;
220706c3fb27SDimitry Andric   unsigned IndexInLambdaContext = 0;
220806c3fb27SDimitry Andric 
22090b57cec5SDimitry Andric   switch ((CXXRecKind)Record.readInt()) {
22100b57cec5SDimitry Andric   case CXXRecNotTemplate:
22110b57cec5SDimitry Andric     // Merged when we merge the folding set entry in the primary template.
22120b57cec5SDimitry Andric     if (!isa<ClassTemplateSpecializationDecl>(D))
22130b57cec5SDimitry Andric       mergeRedeclarable(D, Redecl);
22140b57cec5SDimitry Andric     break;
22150b57cec5SDimitry Andric   case CXXRecTemplate: {
22160b57cec5SDimitry Andric     // Merged when we merge the template.
2217480093f4SDimitry Andric     auto *Template = readDeclAs<ClassTemplateDecl>();
22180b57cec5SDimitry Andric     D->TemplateOrInstantiation = Template;
22190b57cec5SDimitry Andric     if (!Template->getTemplatedDecl()) {
22200b57cec5SDimitry Andric       // We've not actually loaded the ClassTemplateDecl yet, because we're
22210b57cec5SDimitry Andric       // currently being loaded as its pattern. Rely on it to set up our
22220b57cec5SDimitry Andric       // TypeForDecl (see VisitClassTemplateDecl).
22230b57cec5SDimitry Andric       //
22240b57cec5SDimitry Andric       // Beware: we do not yet know our canonical declaration, and may still
22250b57cec5SDimitry Andric       // get merged once the surrounding class template has got off the ground.
22260b57cec5SDimitry Andric       DeferredTypeID = 0;
22270b57cec5SDimitry Andric     }
22280b57cec5SDimitry Andric     break;
22290b57cec5SDimitry Andric   }
22300b57cec5SDimitry Andric   case CXXRecMemberSpecialization: {
2231480093f4SDimitry Andric     auto *RD = readDeclAs<CXXRecordDecl>();
22320b57cec5SDimitry Andric     auto TSK = (TemplateSpecializationKind)Record.readInt();
2233480093f4SDimitry Andric     SourceLocation POI = readSourceLocation();
22340b57cec5SDimitry Andric     MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
22350b57cec5SDimitry Andric     MSI->setPointOfInstantiation(POI);
22360b57cec5SDimitry Andric     D->TemplateOrInstantiation = MSI;
22370b57cec5SDimitry Andric     mergeRedeclarable(D, Redecl);
22380b57cec5SDimitry Andric     break;
22390b57cec5SDimitry Andric   }
224006c3fb27SDimitry Andric   case CXXLambda: {
224106c3fb27SDimitry Andric     LambdaContext = readDecl();
224206c3fb27SDimitry Andric     if (LambdaContext)
224306c3fb27SDimitry Andric       IndexInLambdaContext = Record.readInt();
224406c3fb27SDimitry Andric     mergeLambda(D, Redecl, LambdaContext, IndexInLambdaContext);
224506c3fb27SDimitry Andric     break;
224606c3fb27SDimitry Andric   }
22470b57cec5SDimitry Andric   }
22480b57cec5SDimitry Andric 
22490b57cec5SDimitry Andric   bool WasDefinition = Record.readInt();
22500b57cec5SDimitry Andric   if (WasDefinition)
225106c3fb27SDimitry Andric     ReadCXXRecordDefinition(D, /*Update=*/false, LambdaContext,
225206c3fb27SDimitry Andric                             IndexInLambdaContext);
22530b57cec5SDimitry Andric   else
22540b57cec5SDimitry Andric     // Propagate DefinitionData pointer from the canonical declaration.
22550b57cec5SDimitry Andric     D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
22560b57cec5SDimitry Andric 
22570b57cec5SDimitry Andric   // Lazily load the key function to avoid deserializing every method so we can
22580b57cec5SDimitry Andric   // compute it.
22590b57cec5SDimitry Andric   if (WasDefinition) {
22600fca6ea1SDimitry Andric     GlobalDeclID KeyFn = readDeclID();
22610fca6ea1SDimitry Andric     if (KeyFn.isValid() && D->isCompleteDefinition())
22620b57cec5SDimitry Andric       // FIXME: This is wrong for the ARM ABI, where some other module may have
22630b57cec5SDimitry Andric       // made this function no longer be a key function. We need an update
22640b57cec5SDimitry Andric       // record or similar for that case.
22650fca6ea1SDimitry Andric       C.KeyFunctions[D] = KeyFn.getRawValue();
22660b57cec5SDimitry Andric   }
22670b57cec5SDimitry Andric 
22680b57cec5SDimitry Andric   return Redecl;
22690b57cec5SDimitry Andric }
22700b57cec5SDimitry Andric 
22710b57cec5SDimitry Andric void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
22720b57cec5SDimitry Andric   D->setExplicitSpecifier(Record.readExplicitSpec());
2273fe6060f1SDimitry Andric   D->Ctor = readDeclAs<CXXConstructorDecl>();
22740b57cec5SDimitry Andric   VisitFunctionDecl(D);
227506c3fb27SDimitry Andric   D->setDeductionCandidateKind(
227606c3fb27SDimitry Andric       static_cast<DeductionCandidate>(Record.readInt()));
22770b57cec5SDimitry Andric }
22780b57cec5SDimitry Andric 
22790b57cec5SDimitry Andric void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
22800b57cec5SDimitry Andric   VisitFunctionDecl(D);
22810b57cec5SDimitry Andric 
22820b57cec5SDimitry Andric   unsigned NumOverridenMethods = Record.readInt();
22830b57cec5SDimitry Andric   if (D->isCanonicalDecl()) {
22840b57cec5SDimitry Andric     while (NumOverridenMethods--) {
22850b57cec5SDimitry Andric       // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
22860b57cec5SDimitry Andric       // MD may be initializing.
2287480093f4SDimitry Andric       if (auto *MD = readDeclAs<CXXMethodDecl>())
22880b57cec5SDimitry Andric         Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
22890b57cec5SDimitry Andric     }
22900b57cec5SDimitry Andric   } else {
22910b57cec5SDimitry Andric     // We don't care about which declarations this used to override; we get
22920b57cec5SDimitry Andric     // the relevant information from the canonical declaration.
22930b57cec5SDimitry Andric     Record.skipInts(NumOverridenMethods);
22940b57cec5SDimitry Andric   }
22950b57cec5SDimitry Andric }
22960b57cec5SDimitry Andric 
22970b57cec5SDimitry Andric void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
22980b57cec5SDimitry Andric   // We need the inherited constructor information to merge the declaration,
22990b57cec5SDimitry Andric   // so we have to read it before we call VisitCXXMethodDecl.
23000b57cec5SDimitry Andric   D->setExplicitSpecifier(Record.readExplicitSpec());
23010b57cec5SDimitry Andric   if (D->isInheritingConstructor()) {
2302480093f4SDimitry Andric     auto *Shadow = readDeclAs<ConstructorUsingShadowDecl>();
2303480093f4SDimitry Andric     auto *Ctor = readDeclAs<CXXConstructorDecl>();
23040b57cec5SDimitry Andric     *D->getTrailingObjects<InheritedConstructor>() =
23050b57cec5SDimitry Andric         InheritedConstructor(Shadow, Ctor);
23060b57cec5SDimitry Andric   }
23070b57cec5SDimitry Andric 
23080b57cec5SDimitry Andric   VisitCXXMethodDecl(D);
23090b57cec5SDimitry Andric }
23100b57cec5SDimitry Andric 
23110b57cec5SDimitry Andric void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
23120b57cec5SDimitry Andric   VisitCXXMethodDecl(D);
23130b57cec5SDimitry Andric 
2314480093f4SDimitry Andric   if (auto *OperatorDelete = readDeclAs<FunctionDecl>()) {
23150b57cec5SDimitry Andric     CXXDestructorDecl *Canon = D->getCanonicalDecl();
23160b57cec5SDimitry Andric     auto *ThisArg = Record.readExpr();
23170b57cec5SDimitry Andric     // FIXME: Check consistency if we have an old and new operator delete.
23180b57cec5SDimitry Andric     if (!Canon->OperatorDelete) {
23190b57cec5SDimitry Andric       Canon->OperatorDelete = OperatorDelete;
23200b57cec5SDimitry Andric       Canon->OperatorDeleteThisArg = ThisArg;
23210b57cec5SDimitry Andric     }
23220b57cec5SDimitry Andric   }
23230b57cec5SDimitry Andric }
23240b57cec5SDimitry Andric 
23250b57cec5SDimitry Andric void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
23260b57cec5SDimitry Andric   D->setExplicitSpecifier(Record.readExplicitSpec());
23270b57cec5SDimitry Andric   VisitCXXMethodDecl(D);
23280b57cec5SDimitry Andric }
23290b57cec5SDimitry Andric 
23300b57cec5SDimitry Andric void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
23310b57cec5SDimitry Andric   VisitDecl(D);
23325ffd83dbSDimitry Andric   D->ImportedModule = readModule();
23335ffd83dbSDimitry Andric   D->setImportComplete(Record.readInt());
23340b57cec5SDimitry Andric   auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
23350b57cec5SDimitry Andric   for (unsigned I = 0, N = Record.back(); I != N; ++I)
2336480093f4SDimitry Andric     StoredLocs[I] = readSourceLocation();
23370b57cec5SDimitry Andric   Record.skipInts(1); // The number of stored source locations.
23380b57cec5SDimitry Andric }
23390b57cec5SDimitry Andric 
23400b57cec5SDimitry Andric void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
23410b57cec5SDimitry Andric   VisitDecl(D);
2342480093f4SDimitry Andric   D->setColonLoc(readSourceLocation());
23430b57cec5SDimitry Andric }
23440b57cec5SDimitry Andric 
23450b57cec5SDimitry Andric void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
23460b57cec5SDimitry Andric   VisitDecl(D);
23470b57cec5SDimitry Andric   if (Record.readInt()) // hasFriendDecl
2348480093f4SDimitry Andric     D->Friend = readDeclAs<NamedDecl>();
23490b57cec5SDimitry Andric   else
2350480093f4SDimitry Andric     D->Friend = readTypeSourceInfo();
23510b57cec5SDimitry Andric   for (unsigned i = 0; i != D->NumTPLists; ++i)
23520b57cec5SDimitry Andric     D->getTrailingObjects<TemplateParameterList *>()[i] =
23530b57cec5SDimitry Andric         Record.readTemplateParameterList();
23540fca6ea1SDimitry Andric   D->NextFriend = readDeclID().getRawValue();
23550b57cec5SDimitry Andric   D->UnsupportedFriend = (Record.readInt() != 0);
2356480093f4SDimitry Andric   D->FriendLoc = readSourceLocation();
23570b57cec5SDimitry Andric }
23580b57cec5SDimitry Andric 
23590b57cec5SDimitry Andric void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
23600b57cec5SDimitry Andric   VisitDecl(D);
23610b57cec5SDimitry Andric   unsigned NumParams = Record.readInt();
23620b57cec5SDimitry Andric   D->NumParams = NumParams;
2363d781ede6SDimitry Andric   D->Params = new (Reader.getContext()) TemplateParameterList *[NumParams];
23640b57cec5SDimitry Andric   for (unsigned i = 0; i != NumParams; ++i)
23650b57cec5SDimitry Andric     D->Params[i] = Record.readTemplateParameterList();
23660b57cec5SDimitry Andric   if (Record.readInt()) // HasFriendDecl
2367480093f4SDimitry Andric     D->Friend = readDeclAs<NamedDecl>();
23680b57cec5SDimitry Andric   else
2369480093f4SDimitry Andric     D->Friend = readTypeSourceInfo();
2370480093f4SDimitry Andric   D->FriendLoc = readSourceLocation();
23710b57cec5SDimitry Andric }
23720b57cec5SDimitry Andric 
2373bdd1243dSDimitry Andric void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
23740b57cec5SDimitry Andric   VisitNamedDecl(D);
23750b57cec5SDimitry Andric 
2376bdd1243dSDimitry Andric   assert(!D->TemplateParams && "TemplateParams already set!");
2377bdd1243dSDimitry Andric   D->TemplateParams = Record.readTemplateParameterList();
2378bdd1243dSDimitry Andric   D->init(readDeclAs<NamedDecl>());
23790b57cec5SDimitry Andric }
23800b57cec5SDimitry Andric 
23810b57cec5SDimitry Andric void ASTDeclReader::VisitConceptDecl(ConceptDecl *D) {
23820b57cec5SDimitry Andric   VisitTemplateDecl(D);
23830b57cec5SDimitry Andric   D->ConstraintExpr = Record.readExpr();
23840b57cec5SDimitry Andric   mergeMergeable(D);
23850b57cec5SDimitry Andric }
23860b57cec5SDimitry Andric 
2387bdd1243dSDimitry Andric void ASTDeclReader::VisitImplicitConceptSpecializationDecl(
2388bdd1243dSDimitry Andric     ImplicitConceptSpecializationDecl *D) {
2389bdd1243dSDimitry Andric   // The size of the template list was read during creation of the Decl, so we
2390bdd1243dSDimitry Andric   // don't have to re-read it here.
2391bdd1243dSDimitry Andric   VisitDecl(D);
2392bdd1243dSDimitry Andric   llvm::SmallVector<TemplateArgument, 4> Args;
2393bdd1243dSDimitry Andric   for (unsigned I = 0; I < D->NumTemplateArgs; ++I)
2394bdd1243dSDimitry Andric     Args.push_back(Record.readTemplateArgument(/*Canonicalize=*/true));
2395bdd1243dSDimitry Andric   D->setTemplateArguments(Args);
2396bdd1243dSDimitry Andric }
2397bdd1243dSDimitry Andric 
239855e4f9d5SDimitry Andric void ASTDeclReader::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {
239955e4f9d5SDimitry Andric }
240055e4f9d5SDimitry Andric 
24010b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult
24020b57cec5SDimitry Andric ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
24030b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarable(D);
24040b57cec5SDimitry Andric 
24050b57cec5SDimitry Andric   // Make sure we've allocated the Common pointer first. We do this before
24060b57cec5SDimitry Andric   // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
24070b57cec5SDimitry Andric   RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
24080b57cec5SDimitry Andric   if (!CanonD->Common) {
24090b57cec5SDimitry Andric     CanonD->Common = CanonD->newCommon(Reader.getContext());
24100b57cec5SDimitry Andric     Reader.PendingDefinitions.insert(CanonD);
24110b57cec5SDimitry Andric   }
24120b57cec5SDimitry Andric   D->Common = CanonD->Common;
24130b57cec5SDimitry Andric 
24140b57cec5SDimitry Andric   // If this is the first declaration of the template, fill in the information
24150b57cec5SDimitry Andric   // for the 'common' pointer.
24160b57cec5SDimitry Andric   if (ThisDeclID == Redecl.getFirstID()) {
2417480093f4SDimitry Andric     if (auto *RTD = readDeclAs<RedeclarableTemplateDecl>()) {
24180b57cec5SDimitry Andric       assert(RTD->getKind() == D->getKind() &&
24190b57cec5SDimitry Andric              "InstantiatedFromMemberTemplate kind mismatch");
24200b57cec5SDimitry Andric       D->setInstantiatedFromMemberTemplate(RTD);
24210b57cec5SDimitry Andric       if (Record.readInt())
24220b57cec5SDimitry Andric         D->setMemberSpecialization();
24230b57cec5SDimitry Andric     }
24240b57cec5SDimitry Andric   }
24250b57cec5SDimitry Andric 
2426bdd1243dSDimitry Andric   VisitTemplateDecl(D);
24270b57cec5SDimitry Andric   D->IdentifierNamespace = Record.readInt();
24280b57cec5SDimitry Andric 
24290b57cec5SDimitry Andric   return Redecl;
24300b57cec5SDimitry Andric }
24310b57cec5SDimitry Andric 
24320b57cec5SDimitry Andric void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
24330b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2434bdd1243dSDimitry Andric   mergeRedeclarableTemplate(D, Redecl);
24350b57cec5SDimitry Andric 
24360b57cec5SDimitry Andric   if (ThisDeclID == Redecl.getFirstID()) {
24370b57cec5SDimitry Andric     // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
24380b57cec5SDimitry Andric     // the specializations.
24390fca6ea1SDimitry Andric     SmallVector<GlobalDeclID, 32> SpecIDs;
2440480093f4SDimitry Andric     readDeclIDList(SpecIDs);
24410b57cec5SDimitry Andric     ASTDeclReader::AddLazySpecializations(D, SpecIDs);
24420b57cec5SDimitry Andric   }
24430b57cec5SDimitry Andric 
24440b57cec5SDimitry Andric   if (D->getTemplatedDecl()->TemplateOrInstantiation) {
24450b57cec5SDimitry Andric     // We were loaded before our templated declaration was. We've not set up
24460b57cec5SDimitry Andric     // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
24470b57cec5SDimitry Andric     // it now.
24480b57cec5SDimitry Andric     Reader.getContext().getInjectedClassNameType(
24490b57cec5SDimitry Andric         D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
24500b57cec5SDimitry Andric   }
24510b57cec5SDimitry Andric }
24520b57cec5SDimitry Andric 
24530b57cec5SDimitry Andric void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
24540b57cec5SDimitry Andric   llvm_unreachable("BuiltinTemplates are not serialized");
24550b57cec5SDimitry Andric }
24560b57cec5SDimitry Andric 
24570b57cec5SDimitry Andric /// TODO: Unify with ClassTemplateDecl version?
24580b57cec5SDimitry Andric ///       May require unifying ClassTemplateDecl and
24590b57cec5SDimitry Andric ///        VarTemplateDecl beyond TemplateDecl...
24600b57cec5SDimitry Andric void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
24610b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2462bdd1243dSDimitry Andric   mergeRedeclarableTemplate(D, Redecl);
24630b57cec5SDimitry Andric 
24640b57cec5SDimitry Andric   if (ThisDeclID == Redecl.getFirstID()) {
24650b57cec5SDimitry Andric     // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
24660b57cec5SDimitry Andric     // the specializations.
24670fca6ea1SDimitry Andric     SmallVector<GlobalDeclID, 32> SpecIDs;
2468480093f4SDimitry Andric     readDeclIDList(SpecIDs);
24690b57cec5SDimitry Andric     ASTDeclReader::AddLazySpecializations(D, SpecIDs);
24700b57cec5SDimitry Andric   }
24710b57cec5SDimitry Andric }
24720b57cec5SDimitry Andric 
24730b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult
24740b57cec5SDimitry Andric ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
24750b57cec5SDimitry Andric     ClassTemplateSpecializationDecl *D) {
24760b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
24770b57cec5SDimitry Andric 
24780b57cec5SDimitry Andric   ASTContext &C = Reader.getContext();
2479480093f4SDimitry Andric   if (Decl *InstD = readDecl()) {
24800b57cec5SDimitry Andric     if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
24810b57cec5SDimitry Andric       D->SpecializedTemplate = CTD;
24820b57cec5SDimitry Andric     } else {
24830b57cec5SDimitry Andric       SmallVector<TemplateArgument, 8> TemplArgs;
24840b57cec5SDimitry Andric       Record.readTemplateArgumentList(TemplArgs);
24850b57cec5SDimitry Andric       TemplateArgumentList *ArgList
24860b57cec5SDimitry Andric         = TemplateArgumentList::CreateCopy(C, TemplArgs);
24870b57cec5SDimitry Andric       auto *PS =
24880b57cec5SDimitry Andric           new (C) ClassTemplateSpecializationDecl::
24890b57cec5SDimitry Andric                                              SpecializedPartialSpecialization();
24900b57cec5SDimitry Andric       PS->PartialSpecialization
24910b57cec5SDimitry Andric           = cast<ClassTemplatePartialSpecializationDecl>(InstD);
24920b57cec5SDimitry Andric       PS->TemplateArgs = ArgList;
24930b57cec5SDimitry Andric       D->SpecializedTemplate = PS;
24940b57cec5SDimitry Andric     }
24950b57cec5SDimitry Andric   }
24960b57cec5SDimitry Andric 
24970b57cec5SDimitry Andric   SmallVector<TemplateArgument, 8> TemplArgs;
24980b57cec5SDimitry Andric   Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
24990b57cec5SDimitry Andric   D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
2500480093f4SDimitry Andric   D->PointOfInstantiation = readSourceLocation();
25010b57cec5SDimitry Andric   D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
25020b57cec5SDimitry Andric 
25030b57cec5SDimitry Andric   bool writtenAsCanonicalDecl = Record.readInt();
25040b57cec5SDimitry Andric   if (writtenAsCanonicalDecl) {
2505480093f4SDimitry Andric     auto *CanonPattern = readDeclAs<ClassTemplateDecl>();
25060b57cec5SDimitry Andric     if (D->isCanonicalDecl()) { // It's kept in the folding set.
25070b57cec5SDimitry Andric       // Set this as, or find, the canonical declaration for this specialization
25080b57cec5SDimitry Andric       ClassTemplateSpecializationDecl *CanonSpec;
25090b57cec5SDimitry Andric       if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
25100b57cec5SDimitry Andric         CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
25110b57cec5SDimitry Andric             .GetOrInsertNode(Partial);
25120b57cec5SDimitry Andric       } else {
25130b57cec5SDimitry Andric         CanonSpec =
25140b57cec5SDimitry Andric             CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
25150b57cec5SDimitry Andric       }
25160b57cec5SDimitry Andric       // If there was already a canonical specialization, merge into it.
25170b57cec5SDimitry Andric       if (CanonSpec != D) {
25180b57cec5SDimitry Andric         mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
25190b57cec5SDimitry Andric 
25200b57cec5SDimitry Andric         // This declaration might be a definition. Merge with any existing
25210b57cec5SDimitry Andric         // definition.
25220b57cec5SDimitry Andric         if (auto *DDD = D->DefinitionData) {
25230b57cec5SDimitry Andric           if (CanonSpec->DefinitionData)
25240b57cec5SDimitry Andric             MergeDefinitionData(CanonSpec, std::move(*DDD));
25250b57cec5SDimitry Andric           else
25260b57cec5SDimitry Andric             CanonSpec->DefinitionData = D->DefinitionData;
25270b57cec5SDimitry Andric         }
25280b57cec5SDimitry Andric         D->DefinitionData = CanonSpec->DefinitionData;
25290b57cec5SDimitry Andric       }
25300b57cec5SDimitry Andric     }
25310b57cec5SDimitry Andric   }
25320b57cec5SDimitry Andric 
25330fca6ea1SDimitry Andric   // extern/template keyword locations for explicit instantiations
25340fca6ea1SDimitry Andric   if (Record.readBool()) {
25350fca6ea1SDimitry Andric     auto *ExplicitInfo = new (C) ExplicitInstantiationInfo;
25360fca6ea1SDimitry Andric     ExplicitInfo->ExternKeywordLoc = readSourceLocation();
2537480093f4SDimitry Andric     ExplicitInfo->TemplateKeywordLoc = readSourceLocation();
25380b57cec5SDimitry Andric     D->ExplicitInfo = ExplicitInfo;
25390b57cec5SDimitry Andric   }
25400b57cec5SDimitry Andric 
25410fca6ea1SDimitry Andric   if (Record.readBool())
25420fca6ea1SDimitry Andric     D->setTemplateArgsAsWritten(Record.readASTTemplateArgumentListInfo());
25430fca6ea1SDimitry Andric 
25440b57cec5SDimitry Andric   return Redecl;
25450b57cec5SDimitry Andric }
25460b57cec5SDimitry Andric 
25470b57cec5SDimitry Andric void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
25480b57cec5SDimitry Andric                                     ClassTemplatePartialSpecializationDecl *D) {
2549480093f4SDimitry Andric   // We need to read the template params first because redeclarable is going to
2550480093f4SDimitry Andric   // need them for profiling
2551a7dea167SDimitry Andric   TemplateParameterList *Params = Record.readTemplateParameterList();
2552a7dea167SDimitry Andric   D->TemplateParams = Params;
25530b57cec5SDimitry Andric 
2554480093f4SDimitry Andric   RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
2555480093f4SDimitry Andric 
25560b57cec5SDimitry Andric   // These are read/set from/to the first declaration.
25570b57cec5SDimitry Andric   if (ThisDeclID == Redecl.getFirstID()) {
25580b57cec5SDimitry Andric     D->InstantiatedFromMember.setPointer(
2559480093f4SDimitry Andric       readDeclAs<ClassTemplatePartialSpecializationDecl>());
25600b57cec5SDimitry Andric     D->InstantiatedFromMember.setInt(Record.readInt());
25610b57cec5SDimitry Andric   }
25620b57cec5SDimitry Andric }
25630b57cec5SDimitry Andric 
25640b57cec5SDimitry Andric void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
25650b57cec5SDimitry Andric   RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
25660b57cec5SDimitry Andric 
25670b57cec5SDimitry Andric   if (ThisDeclID == Redecl.getFirstID()) {
25680b57cec5SDimitry Andric     // This FunctionTemplateDecl owns a CommonPtr; read it.
25690fca6ea1SDimitry Andric     SmallVector<GlobalDeclID, 32> SpecIDs;
2570480093f4SDimitry Andric     readDeclIDList(SpecIDs);
25710b57cec5SDimitry Andric     ASTDeclReader::AddLazySpecializations(D, SpecIDs);
25720b57cec5SDimitry Andric   }
25730b57cec5SDimitry Andric }
25740b57cec5SDimitry Andric 
25750b57cec5SDimitry Andric /// TODO: Unify with ClassTemplateSpecializationDecl version?
25760b57cec5SDimitry Andric ///       May require unifying ClassTemplate(Partial)SpecializationDecl and
25770b57cec5SDimitry Andric ///        VarTemplate(Partial)SpecializationDecl with a new data
25780b57cec5SDimitry Andric ///        structure Template(Partial)SpecializationDecl, and
25790b57cec5SDimitry Andric ///        using Template(Partial)SpecializationDecl as input type.
25800b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult
25810b57cec5SDimitry Andric ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
25820b57cec5SDimitry Andric     VarTemplateSpecializationDecl *D) {
25830b57cec5SDimitry Andric   ASTContext &C = Reader.getContext();
2584480093f4SDimitry Andric   if (Decl *InstD = readDecl()) {
25850b57cec5SDimitry Andric     if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
25860b57cec5SDimitry Andric       D->SpecializedTemplate = VTD;
25870b57cec5SDimitry Andric     } else {
25880b57cec5SDimitry Andric       SmallVector<TemplateArgument, 8> TemplArgs;
25890b57cec5SDimitry Andric       Record.readTemplateArgumentList(TemplArgs);
25900b57cec5SDimitry Andric       TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
25910b57cec5SDimitry Andric           C, TemplArgs);
25920b57cec5SDimitry Andric       auto *PS =
25930b57cec5SDimitry Andric           new (C)
25940b57cec5SDimitry Andric           VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
25950b57cec5SDimitry Andric       PS->PartialSpecialization =
25960b57cec5SDimitry Andric           cast<VarTemplatePartialSpecializationDecl>(InstD);
25970b57cec5SDimitry Andric       PS->TemplateArgs = ArgList;
25980b57cec5SDimitry Andric       D->SpecializedTemplate = PS;
25990b57cec5SDimitry Andric     }
26000b57cec5SDimitry Andric   }
26010b57cec5SDimitry Andric 
26020fca6ea1SDimitry Andric   // extern/template keyword locations for explicit instantiations
26030fca6ea1SDimitry Andric   if (Record.readBool()) {
26040fca6ea1SDimitry Andric     auto *ExplicitInfo = new (C) ExplicitInstantiationInfo;
26050fca6ea1SDimitry Andric     ExplicitInfo->ExternKeywordLoc = readSourceLocation();
2606480093f4SDimitry Andric     ExplicitInfo->TemplateKeywordLoc = readSourceLocation();
26070b57cec5SDimitry Andric     D->ExplicitInfo = ExplicitInfo;
26080b57cec5SDimitry Andric   }
26090b57cec5SDimitry Andric 
26100fca6ea1SDimitry Andric   if (Record.readBool())
26110fca6ea1SDimitry Andric     D->setTemplateArgsAsWritten(Record.readASTTemplateArgumentListInfo());
26120fca6ea1SDimitry Andric 
26130b57cec5SDimitry Andric   SmallVector<TemplateArgument, 8> TemplArgs;
26140b57cec5SDimitry Andric   Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
26150b57cec5SDimitry Andric   D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
2616480093f4SDimitry Andric   D->PointOfInstantiation = readSourceLocation();
26170b57cec5SDimitry Andric   D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
26180b57cec5SDimitry Andric   D->IsCompleteDefinition = Record.readInt();
26190b57cec5SDimitry Andric 
2620bdd1243dSDimitry Andric   RedeclarableResult Redecl = VisitVarDeclImpl(D);
2621bdd1243dSDimitry Andric 
26220b57cec5SDimitry Andric   bool writtenAsCanonicalDecl = Record.readInt();
26230b57cec5SDimitry Andric   if (writtenAsCanonicalDecl) {
2624480093f4SDimitry Andric     auto *CanonPattern = readDeclAs<VarTemplateDecl>();
26250b57cec5SDimitry Andric     if (D->isCanonicalDecl()) { // It's kept in the folding set.
262681ad6265SDimitry Andric       VarTemplateSpecializationDecl *CanonSpec;
26270b57cec5SDimitry Andric       if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
262881ad6265SDimitry Andric         CanonSpec = CanonPattern->getCommonPtr()
262981ad6265SDimitry Andric                         ->PartialSpecializations.GetOrInsertNode(Partial);
26300b57cec5SDimitry Andric       } else {
263181ad6265SDimitry Andric         CanonSpec =
26320b57cec5SDimitry Andric             CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
26330b57cec5SDimitry Andric       }
263481ad6265SDimitry Andric       // If we already have a matching specialization, merge it.
263581ad6265SDimitry Andric       if (CanonSpec != D)
263681ad6265SDimitry Andric         mergeRedeclarable<VarDecl>(D, CanonSpec, Redecl);
26370b57cec5SDimitry Andric     }
26380b57cec5SDimitry Andric   }
26390b57cec5SDimitry Andric 
26400b57cec5SDimitry Andric   return Redecl;
26410b57cec5SDimitry Andric }
26420b57cec5SDimitry Andric 
26430b57cec5SDimitry Andric /// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
26440b57cec5SDimitry Andric ///       May require unifying ClassTemplate(Partial)SpecializationDecl and
26450b57cec5SDimitry Andric ///        VarTemplate(Partial)SpecializationDecl with a new data
26460b57cec5SDimitry Andric ///        structure Template(Partial)SpecializationDecl, and
26470b57cec5SDimitry Andric ///        using Template(Partial)SpecializationDecl as input type.
26480b57cec5SDimitry Andric void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
26490b57cec5SDimitry Andric     VarTemplatePartialSpecializationDecl *D) {
2650a7dea167SDimitry Andric   TemplateParameterList *Params = Record.readTemplateParameterList();
2651a7dea167SDimitry Andric   D->TemplateParams = Params;
26520b57cec5SDimitry Andric 
2653480093f4SDimitry Andric   RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2654480093f4SDimitry Andric 
26550b57cec5SDimitry Andric   // These are read/set from/to the first declaration.
26560b57cec5SDimitry Andric   if (ThisDeclID == Redecl.getFirstID()) {
26570b57cec5SDimitry Andric     D->InstantiatedFromMember.setPointer(
2658480093f4SDimitry Andric         readDeclAs<VarTemplatePartialSpecializationDecl>());
26590b57cec5SDimitry Andric     D->InstantiatedFromMember.setInt(Record.readInt());
26600b57cec5SDimitry Andric   }
26610b57cec5SDimitry Andric }
26620b57cec5SDimitry Andric 
26630b57cec5SDimitry Andric void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
26640b57cec5SDimitry Andric   VisitTypeDecl(D);
26650b57cec5SDimitry Andric 
26660b57cec5SDimitry Andric   D->setDeclaredWithTypename(Record.readInt());
26670b57cec5SDimitry Andric 
2668*d686ce93SDimitry Andric   const bool TypeConstraintInitialized = Record.readBool();
2669*d686ce93SDimitry Andric   if (TypeConstraintInitialized && D->hasTypeConstraint()) {
26705f757f3fSDimitry Andric     ConceptReference *CR = nullptr;
267155e4f9d5SDimitry Andric     if (Record.readBool())
26725f757f3fSDimitry Andric       CR = Record.readConceptReference();
2673480093f4SDimitry Andric     Expr *ImmediatelyDeclaredConstraint = Record.readExpr();
26745f757f3fSDimitry Andric 
26755f757f3fSDimitry Andric     D->setTypeConstraint(CR, ImmediatelyDeclaredConstraint);
2676480093f4SDimitry Andric     if ((D->ExpandedParameterPack = Record.readInt()))
2677480093f4SDimitry Andric       D->NumExpanded = Record.readInt();
2678480093f4SDimitry Andric   }
2679480093f4SDimitry Andric 
2680480093f4SDimitry Andric   if (Record.readInt())
26810fca6ea1SDimitry Andric     D->setDefaultArgument(Reader.getContext(),
26820fca6ea1SDimitry Andric                           Record.readTemplateArgumentLoc());
26830b57cec5SDimitry Andric }
26840b57cec5SDimitry Andric 
26850b57cec5SDimitry Andric void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
26860b57cec5SDimitry Andric   VisitDeclaratorDecl(D);
26870b57cec5SDimitry Andric   // TemplateParmPosition.
26880b57cec5SDimitry Andric   D->setDepth(Record.readInt());
26890b57cec5SDimitry Andric   D->setPosition(Record.readInt());
269055e4f9d5SDimitry Andric   if (D->hasPlaceholderTypeConstraint())
269155e4f9d5SDimitry Andric     D->setPlaceholderTypeConstraint(Record.readExpr());
26920b57cec5SDimitry Andric   if (D->isExpandedParameterPack()) {
26930b57cec5SDimitry Andric     auto TypesAndInfos =
26940b57cec5SDimitry Andric         D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
26950b57cec5SDimitry Andric     for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
26960b57cec5SDimitry Andric       new (&TypesAndInfos[I].first) QualType(Record.readType());
2697480093f4SDimitry Andric       TypesAndInfos[I].second = readTypeSourceInfo();
26980b57cec5SDimitry Andric     }
26990b57cec5SDimitry Andric   } else {
27000b57cec5SDimitry Andric     // Rest of NonTypeTemplateParmDecl.
27010b57cec5SDimitry Andric     D->ParameterPack = Record.readInt();
27020b57cec5SDimitry Andric     if (Record.readInt())
27030fca6ea1SDimitry Andric       D->setDefaultArgument(Reader.getContext(),
27040fca6ea1SDimitry Andric                             Record.readTemplateArgumentLoc());
27050b57cec5SDimitry Andric   }
27060b57cec5SDimitry Andric }
27070b57cec5SDimitry Andric 
27080b57cec5SDimitry Andric void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
27090b57cec5SDimitry Andric   VisitTemplateDecl(D);
27100fca6ea1SDimitry Andric   D->setDeclaredWithTypename(Record.readBool());
27110b57cec5SDimitry Andric   // TemplateParmPosition.
27120b57cec5SDimitry Andric   D->setDepth(Record.readInt());
27130b57cec5SDimitry Andric   D->setPosition(Record.readInt());
27140b57cec5SDimitry Andric   if (D->isExpandedParameterPack()) {
27150b57cec5SDimitry Andric     auto **Data = D->getTrailingObjects<TemplateParameterList *>();
27160b57cec5SDimitry Andric     for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
27170b57cec5SDimitry Andric          I != N; ++I)
27180b57cec5SDimitry Andric       Data[I] = Record.readTemplateParameterList();
27190b57cec5SDimitry Andric   } else {
27200b57cec5SDimitry Andric     // Rest of TemplateTemplateParmDecl.
27210b57cec5SDimitry Andric     D->ParameterPack = Record.readInt();
27220b57cec5SDimitry Andric     if (Record.readInt())
27230b57cec5SDimitry Andric       D->setDefaultArgument(Reader.getContext(),
27240b57cec5SDimitry Andric                             Record.readTemplateArgumentLoc());
27250b57cec5SDimitry Andric   }
27260b57cec5SDimitry Andric }
27270b57cec5SDimitry Andric 
27280b57cec5SDimitry Andric void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2729bdd1243dSDimitry Andric   RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2730bdd1243dSDimitry Andric   mergeRedeclarableTemplate(D, Redecl);
27310b57cec5SDimitry Andric }
27320b57cec5SDimitry Andric 
27330b57cec5SDimitry Andric void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
27340b57cec5SDimitry Andric   VisitDecl(D);
27350b57cec5SDimitry Andric   D->AssertExprAndFailed.setPointer(Record.readExpr());
27360b57cec5SDimitry Andric   D->AssertExprAndFailed.setInt(Record.readInt());
27370b57cec5SDimitry Andric   D->Message = cast_or_null<StringLiteral>(Record.readExpr());
2738480093f4SDimitry Andric   D->RParenLoc = readSourceLocation();
27390b57cec5SDimitry Andric }
27400b57cec5SDimitry Andric 
27410b57cec5SDimitry Andric void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
27420b57cec5SDimitry Andric   VisitDecl(D);
27430b57cec5SDimitry Andric }
27440b57cec5SDimitry Andric 
2745480093f4SDimitry Andric void ASTDeclReader::VisitLifetimeExtendedTemporaryDecl(
2746480093f4SDimitry Andric     LifetimeExtendedTemporaryDecl *D) {
2747480093f4SDimitry Andric   VisitDecl(D);
2748480093f4SDimitry Andric   D->ExtendingDecl = readDeclAs<ValueDecl>();
2749480093f4SDimitry Andric   D->ExprWithTemporary = Record.readStmt();
2750e8d8bef9SDimitry Andric   if (Record.readInt()) {
2751480093f4SDimitry Andric     D->Value = new (D->getASTContext()) APValue(Record.readAPValue());
2752e8d8bef9SDimitry Andric     D->getASTContext().addDestruction(D->Value);
2753e8d8bef9SDimitry Andric   }
2754480093f4SDimitry Andric   D->ManglingNumber = Record.readInt();
2755480093f4SDimitry Andric   mergeMergeable(D);
2756480093f4SDimitry Andric }
2757480093f4SDimitry Andric 
27580b57cec5SDimitry Andric std::pair<uint64_t, uint64_t>
27590b57cec5SDimitry Andric ASTDeclReader::VisitDeclContext(DeclContext *DC) {
27600b57cec5SDimitry Andric   uint64_t LexicalOffset = ReadLocalOffset();
27610b57cec5SDimitry Andric   uint64_t VisibleOffset = ReadLocalOffset();
27620b57cec5SDimitry Andric   return std::make_pair(LexicalOffset, VisibleOffset);
27630b57cec5SDimitry Andric }
27640b57cec5SDimitry Andric 
27650b57cec5SDimitry Andric template <typename T>
27660b57cec5SDimitry Andric ASTDeclReader::RedeclarableResult
27670b57cec5SDimitry Andric ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
27680fca6ea1SDimitry Andric   GlobalDeclID FirstDeclID = readDeclID();
27690b57cec5SDimitry Andric   Decl *MergeWith = nullptr;
27700b57cec5SDimitry Andric 
27710b57cec5SDimitry Andric   bool IsKeyDecl = ThisDeclID == FirstDeclID;
27720b57cec5SDimitry Andric   bool IsFirstLocalDecl = false;
27730b57cec5SDimitry Andric 
27740b57cec5SDimitry Andric   uint64_t RedeclOffset = 0;
27750b57cec5SDimitry Andric 
27760fca6ea1SDimitry Andric   // invalid FirstDeclID  indicates that this declaration was the only
27770fca6ea1SDimitry Andric   // declaration of its entity, and is used for space optimization.
27780fca6ea1SDimitry Andric   if (FirstDeclID.isInvalid()) {
27790b57cec5SDimitry Andric     FirstDeclID = ThisDeclID;
27800b57cec5SDimitry Andric     IsKeyDecl = true;
27810b57cec5SDimitry Andric     IsFirstLocalDecl = true;
27820b57cec5SDimitry Andric   } else if (unsigned N = Record.readInt()) {
27830b57cec5SDimitry Andric     // This declaration was the first local declaration, but may have imported
27840b57cec5SDimitry Andric     // other declarations.
27850b57cec5SDimitry Andric     IsKeyDecl = N == 1;
27860b57cec5SDimitry Andric     IsFirstLocalDecl = true;
27870b57cec5SDimitry Andric 
27880b57cec5SDimitry Andric     // We have some declarations that must be before us in our redeclaration
27890b57cec5SDimitry Andric     // chain. Read them now, and remember that we ought to merge with one of
27900b57cec5SDimitry Andric     // them.
27910b57cec5SDimitry Andric     // FIXME: Provide a known merge target to the second and subsequent such
27920b57cec5SDimitry Andric     // declaration.
27930b57cec5SDimitry Andric     for (unsigned I = 0; I != N - 1; ++I)
2794480093f4SDimitry Andric       MergeWith = readDecl();
27950b57cec5SDimitry Andric 
27960b57cec5SDimitry Andric     RedeclOffset = ReadLocalOffset();
27970b57cec5SDimitry Andric   } else {
27980b57cec5SDimitry Andric     // This declaration was not the first local declaration. Read the first
27990b57cec5SDimitry Andric     // local declaration now, to trigger the import of other redeclarations.
2800480093f4SDimitry Andric     (void)readDecl();
28010b57cec5SDimitry Andric   }
28020b57cec5SDimitry Andric 
28030b57cec5SDimitry Andric   auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
28040b57cec5SDimitry Andric   if (FirstDecl != D) {
28050b57cec5SDimitry Andric     // We delay loading of the redeclaration chain to avoid deeply nested calls.
28060b57cec5SDimitry Andric     // We temporarily set the first (canonical) declaration as the previous one
28070b57cec5SDimitry Andric     // which is the one that matters and mark the real previous DeclID to be
28080b57cec5SDimitry Andric     // loaded & attached later on.
28090b57cec5SDimitry Andric     D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
28100b57cec5SDimitry Andric     D->First = FirstDecl->getCanonicalDecl();
28110b57cec5SDimitry Andric   }
28120b57cec5SDimitry Andric 
28130b57cec5SDimitry Andric   auto *DAsT = static_cast<T *>(D);
28140b57cec5SDimitry Andric 
28150b57cec5SDimitry Andric   // Note that we need to load local redeclarations of this decl and build a
28160b57cec5SDimitry Andric   // decl chain for them. This must happen *after* we perform the preloading
28170b57cec5SDimitry Andric   // above; this ensures that the redeclaration chain is built in the correct
28180b57cec5SDimitry Andric   // order.
28190b57cec5SDimitry Andric   if (IsFirstLocalDecl)
28200b57cec5SDimitry Andric     Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
28210b57cec5SDimitry Andric 
28220b57cec5SDimitry Andric   return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
28230b57cec5SDimitry Andric }
28240b57cec5SDimitry Andric 
28250b57cec5SDimitry Andric /// Attempts to merge the given declaration (D) with another declaration
28260b57cec5SDimitry Andric /// of the same entity.
28270b57cec5SDimitry Andric template <typename T>
28280b57cec5SDimitry Andric void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
2829bdd1243dSDimitry Andric                                       RedeclarableResult &Redecl) {
28300b57cec5SDimitry Andric   // If modules are not available, there is no reason to perform this merge.
28310b57cec5SDimitry Andric   if (!Reader.getContext().getLangOpts().Modules)
28320b57cec5SDimitry Andric     return;
28330b57cec5SDimitry Andric 
28340b57cec5SDimitry Andric   // If we're not the canonical declaration, we don't need to merge.
28350b57cec5SDimitry Andric   if (!DBase->isFirstDecl())
28360b57cec5SDimitry Andric     return;
28370b57cec5SDimitry Andric 
28380b57cec5SDimitry Andric   auto *D = static_cast<T *>(DBase);
28390b57cec5SDimitry Andric 
28400b57cec5SDimitry Andric   if (auto *Existing = Redecl.getKnownMergeTarget())
28410b57cec5SDimitry Andric     // We already know of an existing declaration we should merge with.
2842bdd1243dSDimitry Andric     mergeRedeclarable(D, cast<T>(Existing), Redecl);
28430b57cec5SDimitry Andric   else if (FindExistingResult ExistingRes = findExisting(D))
28440b57cec5SDimitry Andric     if (T *Existing = ExistingRes)
2845bdd1243dSDimitry Andric       mergeRedeclarable(D, Existing, Redecl);
2846bdd1243dSDimitry Andric }
2847bdd1243dSDimitry Andric 
284806c3fb27SDimitry Andric /// Attempt to merge D with a previous declaration of the same lambda, which is
284906c3fb27SDimitry Andric /// found by its index within its context declaration, if it has one.
285006c3fb27SDimitry Andric ///
285106c3fb27SDimitry Andric /// We can't look up lambdas in their enclosing lexical or semantic context in
285206c3fb27SDimitry Andric /// general, because for lambdas in variables, both of those might be a
285306c3fb27SDimitry Andric /// namespace or the translation unit.
285406c3fb27SDimitry Andric void ASTDeclReader::mergeLambda(CXXRecordDecl *D, RedeclarableResult &Redecl,
285506c3fb27SDimitry Andric                                 Decl *Context, unsigned IndexInContext) {
285606c3fb27SDimitry Andric   // If we don't have a mangling context, treat this like any other
285706c3fb27SDimitry Andric   // declaration.
285806c3fb27SDimitry Andric   if (!Context)
285906c3fb27SDimitry Andric     return mergeRedeclarable(D, Redecl);
286006c3fb27SDimitry Andric 
286106c3fb27SDimitry Andric   // If modules are not available, there is no reason to perform this merge.
286206c3fb27SDimitry Andric   if (!Reader.getContext().getLangOpts().Modules)
286306c3fb27SDimitry Andric     return;
286406c3fb27SDimitry Andric 
286506c3fb27SDimitry Andric   // If we're not the canonical declaration, we don't need to merge.
286606c3fb27SDimitry Andric   if (!D->isFirstDecl())
286706c3fb27SDimitry Andric     return;
286806c3fb27SDimitry Andric 
286906c3fb27SDimitry Andric   if (auto *Existing = Redecl.getKnownMergeTarget())
287006c3fb27SDimitry Andric     // We already know of an existing declaration we should merge with.
287106c3fb27SDimitry Andric     mergeRedeclarable(D, cast<TagDecl>(Existing), Redecl);
287206c3fb27SDimitry Andric 
287306c3fb27SDimitry Andric   // Look up this lambda to see if we've seen it before. If so, merge with the
287406c3fb27SDimitry Andric   // one we already loaded.
287506c3fb27SDimitry Andric   NamedDecl *&Slot = Reader.LambdaDeclarationsForMerging[{
287606c3fb27SDimitry Andric       Context->getCanonicalDecl(), IndexInContext}];
287706c3fb27SDimitry Andric   if (Slot)
287806c3fb27SDimitry Andric     mergeRedeclarable(D, cast<TagDecl>(Slot), Redecl);
287906c3fb27SDimitry Andric   else
288006c3fb27SDimitry Andric     Slot = D;
288106c3fb27SDimitry Andric }
288206c3fb27SDimitry Andric 
2883bdd1243dSDimitry Andric void ASTDeclReader::mergeRedeclarableTemplate(RedeclarableTemplateDecl *D,
2884bdd1243dSDimitry Andric                                               RedeclarableResult &Redecl) {
2885bdd1243dSDimitry Andric   mergeRedeclarable(D, Redecl);
2886bdd1243dSDimitry Andric   // If we merged the template with a prior declaration chain, merge the
2887bdd1243dSDimitry Andric   // common pointer.
2888bdd1243dSDimitry Andric   // FIXME: Actually merge here, don't just overwrite.
2889bdd1243dSDimitry Andric   D->Common = D->getCanonicalDecl()->Common;
28900b57cec5SDimitry Andric }
28910b57cec5SDimitry Andric 
28920b57cec5SDimitry Andric /// "Cast" to type T, asserting if we don't have an implicit conversion.
28930b57cec5SDimitry Andric /// We use this to put code in a template that will only be valid for certain
28940b57cec5SDimitry Andric /// instantiations.
28950b57cec5SDimitry Andric template<typename T> static T assert_cast(T t) { return t; }
28960b57cec5SDimitry Andric template<typename T> static T assert_cast(...) {
28970b57cec5SDimitry Andric   llvm_unreachable("bad assert_cast");
28980b57cec5SDimitry Andric }
28990b57cec5SDimitry Andric 
29000b57cec5SDimitry Andric /// Merge together the pattern declarations from two template
29010b57cec5SDimitry Andric /// declarations.
29020b57cec5SDimitry Andric void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
29030b57cec5SDimitry Andric                                          RedeclarableTemplateDecl *Existing,
2904bdd1243dSDimitry Andric                                          bool IsKeyDecl) {
29050b57cec5SDimitry Andric   auto *DPattern = D->getTemplatedDecl();
29060b57cec5SDimitry Andric   auto *ExistingPattern = Existing->getTemplatedDecl();
29070fca6ea1SDimitry Andric   RedeclarableResult Result(
29080fca6ea1SDimitry Andric       /*MergeWith*/ ExistingPattern,
29090fca6ea1SDimitry Andric       DPattern->getCanonicalDecl()->getGlobalID(), IsKeyDecl);
29100b57cec5SDimitry Andric 
29110b57cec5SDimitry Andric   if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
29120b57cec5SDimitry Andric     // Merge with any existing definition.
29130b57cec5SDimitry Andric     // FIXME: This is duplicated in several places. Refactor.
29140b57cec5SDimitry Andric     auto *ExistingClass =
29150b57cec5SDimitry Andric         cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
29160b57cec5SDimitry Andric     if (auto *DDD = DClass->DefinitionData) {
29170b57cec5SDimitry Andric       if (ExistingClass->DefinitionData) {
29180b57cec5SDimitry Andric         MergeDefinitionData(ExistingClass, std::move(*DDD));
29190b57cec5SDimitry Andric       } else {
29200b57cec5SDimitry Andric         ExistingClass->DefinitionData = DClass->DefinitionData;
29210b57cec5SDimitry Andric         // We may have skipped this before because we thought that DClass
29220b57cec5SDimitry Andric         // was the canonical declaration.
29230b57cec5SDimitry Andric         Reader.PendingDefinitions.insert(DClass);
29240b57cec5SDimitry Andric       }
29250b57cec5SDimitry Andric     }
29260b57cec5SDimitry Andric     DClass->DefinitionData = ExistingClass->DefinitionData;
29270b57cec5SDimitry Andric 
29280b57cec5SDimitry Andric     return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
29290b57cec5SDimitry Andric                              Result);
29300b57cec5SDimitry Andric   }
29310b57cec5SDimitry Andric   if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
29320b57cec5SDimitry Andric     return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
29330b57cec5SDimitry Andric                              Result);
29340b57cec5SDimitry Andric   if (auto *DVar = dyn_cast<VarDecl>(DPattern))
29350b57cec5SDimitry Andric     return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
29360b57cec5SDimitry Andric   if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
29370b57cec5SDimitry Andric     return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
29380b57cec5SDimitry Andric                              Result);
29390b57cec5SDimitry Andric   llvm_unreachable("merged an unknown kind of redeclarable template");
29400b57cec5SDimitry Andric }
29410b57cec5SDimitry Andric 
29420b57cec5SDimitry Andric /// Attempts to merge the given declaration (D) with another declaration
29430b57cec5SDimitry Andric /// of the same entity.
29440b57cec5SDimitry Andric template <typename T>
29450b57cec5SDimitry Andric void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2946bdd1243dSDimitry Andric                                       RedeclarableResult &Redecl) {
29470b57cec5SDimitry Andric   auto *D = static_cast<T *>(DBase);
29480b57cec5SDimitry Andric   T *ExistingCanon = Existing->getCanonicalDecl();
29490b57cec5SDimitry Andric   T *DCanon = D->getCanonicalDecl();
29500b57cec5SDimitry Andric   if (ExistingCanon != DCanon) {
29510b57cec5SDimitry Andric     // Have our redeclaration link point back at the canonical declaration
29520b57cec5SDimitry Andric     // of the existing declaration, so that this declaration has the
29530b57cec5SDimitry Andric     // appropriate canonical declaration.
29540b57cec5SDimitry Andric     D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
29550b57cec5SDimitry Andric     D->First = ExistingCanon;
29560b57cec5SDimitry Andric     ExistingCanon->Used |= D->Used;
29570b57cec5SDimitry Andric     D->Used = false;
29580b57cec5SDimitry Andric 
29590b57cec5SDimitry Andric     // When we merge a template, merge its pattern.
29600b57cec5SDimitry Andric     if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
29610b57cec5SDimitry Andric       mergeTemplatePattern(
29620b57cec5SDimitry Andric           DTemplate, assert_cast<RedeclarableTemplateDecl *>(ExistingCanon),
2963bdd1243dSDimitry Andric           Redecl.isKeyDecl());
29640b57cec5SDimitry Andric 
29650b57cec5SDimitry Andric     // If this declaration is a key declaration, make a note of that.
29660b57cec5SDimitry Andric     if (Redecl.isKeyDecl())
29670b57cec5SDimitry Andric       Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
29680b57cec5SDimitry Andric   }
29690b57cec5SDimitry Andric }
29700b57cec5SDimitry Andric 
29710b57cec5SDimitry Andric /// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
29720b57cec5SDimitry Andric /// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
29730b57cec5SDimitry Andric /// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
29740b57cec5SDimitry Andric /// that some types are mergeable during deserialization, otherwise name
29750b57cec5SDimitry Andric /// lookup fails. This is the case for EnumConstantDecl.
29760b57cec5SDimitry Andric static bool allowODRLikeMergeInC(NamedDecl *ND) {
29770b57cec5SDimitry Andric   if (!ND)
29780b57cec5SDimitry Andric     return false;
29790b57cec5SDimitry Andric   // TODO: implement merge for other necessary decls.
2980349cc55cSDimitry Andric   if (isa<EnumConstantDecl, FieldDecl, IndirectFieldDecl>(ND))
29810b57cec5SDimitry Andric     return true;
29820b57cec5SDimitry Andric   return false;
29830b57cec5SDimitry Andric }
29840b57cec5SDimitry Andric 
2985480093f4SDimitry Andric /// Attempts to merge LifetimeExtendedTemporaryDecl with
2986480093f4SDimitry Andric /// identical class definitions from two different modules.
2987480093f4SDimitry Andric void ASTDeclReader::mergeMergeable(LifetimeExtendedTemporaryDecl *D) {
2988480093f4SDimitry Andric   // If modules are not available, there is no reason to perform this merge.
2989480093f4SDimitry Andric   if (!Reader.getContext().getLangOpts().Modules)
2990480093f4SDimitry Andric     return;
2991480093f4SDimitry Andric 
2992480093f4SDimitry Andric   LifetimeExtendedTemporaryDecl *LETDecl = D;
2993480093f4SDimitry Andric 
2994480093f4SDimitry Andric   LifetimeExtendedTemporaryDecl *&LookupResult =
2995480093f4SDimitry Andric       Reader.LETemporaryForMerging[std::make_pair(
2996480093f4SDimitry Andric           LETDecl->getExtendingDecl(), LETDecl->getManglingNumber())];
2997480093f4SDimitry Andric   if (LookupResult)
2998480093f4SDimitry Andric     Reader.getContext().setPrimaryMergedDecl(LETDecl,
2999480093f4SDimitry Andric                                              LookupResult->getCanonicalDecl());
3000480093f4SDimitry Andric   else
3001480093f4SDimitry Andric     LookupResult = LETDecl;
3002480093f4SDimitry Andric }
3003480093f4SDimitry Andric 
30040b57cec5SDimitry Andric /// Attempts to merge the given declaration (D) with another declaration
30050b57cec5SDimitry Andric /// of the same entity, for the case where the entity is not actually
30060b57cec5SDimitry Andric /// redeclarable. This happens, for instance, when merging the fields of
30070b57cec5SDimitry Andric /// identical class definitions from two different modules.
30080b57cec5SDimitry Andric template<typename T>
30090b57cec5SDimitry Andric void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
30100b57cec5SDimitry Andric   // If modules are not available, there is no reason to perform this merge.
30110b57cec5SDimitry Andric   if (!Reader.getContext().getLangOpts().Modules)
30120b57cec5SDimitry Andric     return;
30130b57cec5SDimitry Andric 
30140b57cec5SDimitry Andric   // ODR-based merging is performed in C++ and in some cases (tag types) in C.
30150b57cec5SDimitry Andric   // Note that C identically-named things in different translation units are
30160b57cec5SDimitry Andric   // not redeclarations, but may still have compatible types, where ODR-like
30170b57cec5SDimitry Andric   // semantics may apply.
30180b57cec5SDimitry Andric   if (!Reader.getContext().getLangOpts().CPlusPlus &&
30190b57cec5SDimitry Andric       !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
30200b57cec5SDimitry Andric     return;
30210b57cec5SDimitry Andric 
30220b57cec5SDimitry Andric   if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
30230b57cec5SDimitry Andric     if (T *Existing = ExistingRes)
30240b57cec5SDimitry Andric       Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
30250b57cec5SDimitry Andric                                                Existing->getCanonicalDecl());
30260b57cec5SDimitry Andric }
30270b57cec5SDimitry Andric 
30280b57cec5SDimitry Andric void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
3029e8d8bef9SDimitry Andric   Record.readOMPChildren(D->Data);
30300b57cec5SDimitry Andric   VisitDecl(D);
30310b57cec5SDimitry Andric }
30320b57cec5SDimitry Andric 
30330b57cec5SDimitry Andric void ASTDeclReader::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
3034e8d8bef9SDimitry Andric   Record.readOMPChildren(D->Data);
30350b57cec5SDimitry Andric   VisitDecl(D);
30360b57cec5SDimitry Andric }
30370b57cec5SDimitry Andric 
30380b57cec5SDimitry Andric void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
3039e8d8bef9SDimitry Andric   Record.readOMPChildren(D->Data);
30400b57cec5SDimitry Andric   VisitDecl(D);
30410b57cec5SDimitry Andric }
30420b57cec5SDimitry Andric 
30430b57cec5SDimitry Andric void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
30440b57cec5SDimitry Andric   VisitValueDecl(D);
3045480093f4SDimitry Andric   D->setLocation(readSourceLocation());
30460b57cec5SDimitry Andric   Expr *In = Record.readExpr();
30470b57cec5SDimitry Andric   Expr *Out = Record.readExpr();
30480b57cec5SDimitry Andric   D->setCombinerData(In, Out);
30490b57cec5SDimitry Andric   Expr *Combiner = Record.readExpr();
30500b57cec5SDimitry Andric   D->setCombiner(Combiner);
30510b57cec5SDimitry Andric   Expr *Orig = Record.readExpr();
30520b57cec5SDimitry Andric   Expr *Priv = Record.readExpr();
30530b57cec5SDimitry Andric   D->setInitializerData(Orig, Priv);
30540b57cec5SDimitry Andric   Expr *Init = Record.readExpr();
30555f757f3fSDimitry Andric   auto IK = static_cast<OMPDeclareReductionInitKind>(Record.readInt());
30560b57cec5SDimitry Andric   D->setInitializer(Init, IK);
30570fca6ea1SDimitry Andric   D->PrevDeclInScope = readDeclID().getRawValue();
30580b57cec5SDimitry Andric }
30590b57cec5SDimitry Andric 
30600b57cec5SDimitry Andric void ASTDeclReader::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
3061e8d8bef9SDimitry Andric   Record.readOMPChildren(D->Data);
30620b57cec5SDimitry Andric   VisitValueDecl(D);
30630b57cec5SDimitry Andric   D->VarName = Record.readDeclarationName();
30640fca6ea1SDimitry Andric   D->PrevDeclInScope = readDeclID().getRawValue();
30650b57cec5SDimitry Andric }
30660b57cec5SDimitry Andric 
30670b57cec5SDimitry Andric void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
30680b57cec5SDimitry Andric   VisitVarDecl(D);
30690b57cec5SDimitry Andric }
30700b57cec5SDimitry Andric 
30710b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
30720b57cec5SDimitry Andric // Attribute Reading
30730b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
30740b57cec5SDimitry Andric 
30750b57cec5SDimitry Andric namespace {
30760b57cec5SDimitry Andric class AttrReader {
3077480093f4SDimitry Andric   ASTRecordReader &Reader;
30780b57cec5SDimitry Andric 
30790b57cec5SDimitry Andric public:
3080480093f4SDimitry Andric   AttrReader(ASTRecordReader &Reader) : Reader(Reader) {}
30810b57cec5SDimitry Andric 
3082480093f4SDimitry Andric   uint64_t readInt() {
3083480093f4SDimitry Andric     return Reader.readInt();
3084480093f4SDimitry Andric   }
30850b57cec5SDimitry Andric 
3086bdd1243dSDimitry Andric   bool readBool() { return Reader.readBool(); }
3087bdd1243dSDimitry Andric 
30880b57cec5SDimitry Andric   SourceRange readSourceRange() {
3089480093f4SDimitry Andric     return Reader.readSourceRange();
30900b57cec5SDimitry Andric   }
30910b57cec5SDimitry Andric 
3092a7dea167SDimitry Andric   SourceLocation readSourceLocation() {
3093480093f4SDimitry Andric     return Reader.readSourceLocation();
3094a7dea167SDimitry Andric   }
3095a7dea167SDimitry Andric 
3096480093f4SDimitry Andric   Expr *readExpr() { return Reader.readExpr(); }
30970b57cec5SDimitry Andric 
30987a6dacacSDimitry Andric   Attr *readAttr() { return Reader.readAttr(); }
30997a6dacacSDimitry Andric 
31000b57cec5SDimitry Andric   std::string readString() {
3101480093f4SDimitry Andric     return Reader.readString();
31020b57cec5SDimitry Andric   }
31030b57cec5SDimitry Andric 
3104480093f4SDimitry Andric   TypeSourceInfo *readTypeSourceInfo() {
3105480093f4SDimitry Andric     return Reader.readTypeSourceInfo();
31060b57cec5SDimitry Andric   }
31070b57cec5SDimitry Andric 
3108480093f4SDimitry Andric   IdentifierInfo *readIdentifier() {
3109480093f4SDimitry Andric     return Reader.readIdentifier();
31100b57cec5SDimitry Andric   }
31110b57cec5SDimitry Andric 
31120b57cec5SDimitry Andric   VersionTuple readVersionTuple() {
3113480093f4SDimitry Andric     return Reader.readVersionTuple();
31140b57cec5SDimitry Andric   }
31150b57cec5SDimitry Andric 
31165ffd83dbSDimitry Andric   OMPTraitInfo *readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
31175ffd83dbSDimitry Andric 
31180fca6ea1SDimitry Andric   template <typename T> T *readDeclAs() { return Reader.readDeclAs<T>(); }
31190b57cec5SDimitry Andric };
31200b57cec5SDimitry Andric }
31210b57cec5SDimitry Andric 
3122480093f4SDimitry Andric Attr *ASTRecordReader::readAttr() {
3123480093f4SDimitry Andric   AttrReader Record(*this);
31240b57cec5SDimitry Andric   auto V = Record.readInt();
31250b57cec5SDimitry Andric   if (!V)
31260b57cec5SDimitry Andric     return nullptr;
31270b57cec5SDimitry Andric 
31280b57cec5SDimitry Andric   Attr *New = nullptr;
31290b57cec5SDimitry Andric   // Kind is stored as a 1-based integer because 0 is used to indicate a null
31300b57cec5SDimitry Andric   // Attr pointer.
31310b57cec5SDimitry Andric   auto Kind = static_cast<attr::Kind>(V - 1);
31320b57cec5SDimitry Andric   ASTContext &Context = getContext();
31330b57cec5SDimitry Andric 
3134480093f4SDimitry Andric   IdentifierInfo *AttrName = Record.readIdentifier();
3135480093f4SDimitry Andric   IdentifierInfo *ScopeName = Record.readIdentifier();
3136a7dea167SDimitry Andric   SourceRange AttrRange = Record.readSourceRange();
3137a7dea167SDimitry Andric   SourceLocation ScopeLoc = Record.readSourceLocation();
3138a7dea167SDimitry Andric   unsigned ParsedKind = Record.readInt();
3139a7dea167SDimitry Andric   unsigned Syntax = Record.readInt();
3140a7dea167SDimitry Andric   unsigned SpellingIndex = Record.readInt();
314106c3fb27SDimitry Andric   bool IsAlignas = (ParsedKind == AttributeCommonInfo::AT_Aligned &&
314206c3fb27SDimitry Andric                     Syntax == AttributeCommonInfo::AS_Keyword &&
314306c3fb27SDimitry Andric                     SpellingIndex == AlignedAttr::Keyword_alignas);
314406c3fb27SDimitry Andric   bool IsRegularKeywordAttribute = Record.readBool();
3145a7dea167SDimitry Andric 
3146a7dea167SDimitry Andric   AttributeCommonInfo Info(AttrName, ScopeName, AttrRange, ScopeLoc,
3147a7dea167SDimitry Andric                            AttributeCommonInfo::Kind(ParsedKind),
314806c3fb27SDimitry Andric                            {AttributeCommonInfo::Syntax(Syntax), SpellingIndex,
314906c3fb27SDimitry Andric                             IsAlignas, IsRegularKeywordAttribute});
3150a7dea167SDimitry Andric 
31510b57cec5SDimitry Andric #include "clang/Serialization/AttrPCHRead.inc"
31520b57cec5SDimitry Andric 
31530b57cec5SDimitry Andric   assert(New && "Unable to decode attribute?");
31540b57cec5SDimitry Andric   return New;
31550b57cec5SDimitry Andric }
31560b57cec5SDimitry Andric 
31570b57cec5SDimitry Andric /// Reads attributes from the current stream position.
3158480093f4SDimitry Andric void ASTRecordReader::readAttributes(AttrVec &Attrs) {
3159480093f4SDimitry Andric   for (unsigned I = 0, E = readInt(); I != E; ++I)
3160972a253aSDimitry Andric     if (auto *A = readAttr())
3161972a253aSDimitry Andric       Attrs.push_back(A);
31620b57cec5SDimitry Andric }
31630b57cec5SDimitry Andric 
31640b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
31650b57cec5SDimitry Andric // ASTReader Implementation
31660b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
31670b57cec5SDimitry Andric 
31680b57cec5SDimitry Andric /// Note that we have loaded the declaration with the given
31690b57cec5SDimitry Andric /// Index.
31700b57cec5SDimitry Andric ///
31710b57cec5SDimitry Andric /// This routine notes that this declaration has already been loaded,
31720b57cec5SDimitry Andric /// so that future GetDecl calls will return this declaration rather
31730b57cec5SDimitry Andric /// than trying to load a new declaration.
31740b57cec5SDimitry Andric inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
31750b57cec5SDimitry Andric   assert(!DeclsLoaded[Index] && "Decl loaded twice?");
31760b57cec5SDimitry Andric   DeclsLoaded[Index] = D;
31770b57cec5SDimitry Andric }
31780b57cec5SDimitry Andric 
31790b57cec5SDimitry Andric /// Determine whether the consumer will be interested in seeing
31800b57cec5SDimitry Andric /// this declaration (via HandleTopLevelDecl).
31810b57cec5SDimitry Andric ///
31820b57cec5SDimitry Andric /// This routine should return true for anything that might affect
31830b57cec5SDimitry Andric /// code generation, e.g., inline function definitions, Objective-C
31840b57cec5SDimitry Andric /// declarations with metadata, etc.
31850fca6ea1SDimitry Andric bool ASTReader::isConsumerInterestedIn(Decl *D) {
31860b57cec5SDimitry Andric   // An ObjCMethodDecl is never considered as "interesting" because its
31870b57cec5SDimitry Andric   // implementation container always is.
31880b57cec5SDimitry Andric 
31890b57cec5SDimitry Andric   // An ImportDecl or VarDecl imported from a module map module will get
31900b57cec5SDimitry Andric   // emitted when we import the relevant module.
31910b57cec5SDimitry Andric   if (isPartOfPerModuleInitializer(D)) {
31920b57cec5SDimitry Andric     auto *M = D->getImportedOwningModule();
31930b57cec5SDimitry Andric     if (M && M->Kind == Module::ModuleMapModule &&
31940fca6ea1SDimitry Andric         getContext().DeclMustBeEmitted(D))
31950b57cec5SDimitry Andric       return false;
31960b57cec5SDimitry Andric   }
31970b57cec5SDimitry Andric 
3198bdd1243dSDimitry Andric   if (isa<FileScopeAsmDecl, TopLevelStmtDecl, ObjCProtocolDecl, ObjCImplDecl,
3199bdd1243dSDimitry Andric           ImportDecl, PragmaCommentDecl, PragmaDetectMismatchDecl>(D))
32000b57cec5SDimitry Andric     return true;
3201bdd1243dSDimitry Andric   if (isa<OMPThreadPrivateDecl, OMPDeclareReductionDecl, OMPDeclareMapperDecl,
3202bdd1243dSDimitry Andric           OMPAllocateDecl, OMPRequiresDecl>(D))
32030b57cec5SDimitry Andric     return !D->getDeclContext()->isFunctionOrMethod();
32040b57cec5SDimitry Andric   if (const auto *Var = dyn_cast<VarDecl>(D))
32050b57cec5SDimitry Andric     return Var->isFileVarDecl() &&
32060b57cec5SDimitry Andric            (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
32070b57cec5SDimitry Andric             OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
32080b57cec5SDimitry Andric   if (const auto *Func = dyn_cast<FunctionDecl>(D))
32090fca6ea1SDimitry Andric     return Func->doesThisDeclarationHaveABody() || PendingBodies.count(D);
32100b57cec5SDimitry Andric 
32110b57cec5SDimitry Andric   if (auto *ES = D->getASTContext().getExternalSource())
32120b57cec5SDimitry Andric     if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
32130b57cec5SDimitry Andric       return true;
32140b57cec5SDimitry Andric 
32150b57cec5SDimitry Andric   return false;
32160b57cec5SDimitry Andric }
32170b57cec5SDimitry Andric 
32180b57cec5SDimitry Andric /// Get the correct cursor and offset for loading a declaration.
32190fca6ea1SDimitry Andric ASTReader::RecordLocation ASTReader::DeclCursorForID(GlobalDeclID ID,
32200fca6ea1SDimitry Andric                                                      SourceLocation &Loc) {
32210fca6ea1SDimitry Andric   ModuleFile *M = getOwningModuleFile(ID);
32220fca6ea1SDimitry Andric   assert(M);
32230fca6ea1SDimitry Andric   unsigned LocalDeclIndex = ID.getLocalDeclIndex();
32240fca6ea1SDimitry Andric   const DeclOffset &DOffs = M->DeclOffsets[LocalDeclIndex];
32250fca6ea1SDimitry Andric   Loc = ReadSourceLocation(*M, DOffs.getRawLoc());
32265ffd83dbSDimitry Andric   return RecordLocation(M, DOffs.getBitOffset(M->DeclsBlockStartOffset));
32270b57cec5SDimitry Andric }
32280b57cec5SDimitry Andric 
32290b57cec5SDimitry Andric ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
32300b57cec5SDimitry Andric   auto I = GlobalBitOffsetsMap.find(GlobalOffset);
32310b57cec5SDimitry Andric 
32320b57cec5SDimitry Andric   assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
32330b57cec5SDimitry Andric   return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
32340b57cec5SDimitry Andric }
32350b57cec5SDimitry Andric 
32365ffd83dbSDimitry Andric uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint64_t LocalOffset) {
32370b57cec5SDimitry Andric   return LocalOffset + M.GlobalBitOffset;
32380b57cec5SDimitry Andric }
32390b57cec5SDimitry Andric 
32408a4dda33SDimitry Andric CXXRecordDecl *
32418a4dda33SDimitry Andric ASTDeclReader::getOrFakePrimaryClassDefinition(ASTReader &Reader,
32428a4dda33SDimitry Andric                                                CXXRecordDecl *RD) {
32430b57cec5SDimitry Andric   // Try to dig out the definition.
32440b57cec5SDimitry Andric   auto *DD = RD->DefinitionData;
32450b57cec5SDimitry Andric   if (!DD)
32460b57cec5SDimitry Andric     DD = RD->getCanonicalDecl()->DefinitionData;
32470b57cec5SDimitry Andric 
32480b57cec5SDimitry Andric   // If there's no definition yet, then DC's definition is added by an update
32490b57cec5SDimitry Andric   // record, but we've not yet loaded that update record. In this case, we
32500b57cec5SDimitry Andric   // commit to DC being the canonical definition now, and will fix this when
32510b57cec5SDimitry Andric   // we load the update record.
32520b57cec5SDimitry Andric   if (!DD) {
32530b57cec5SDimitry Andric     DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
32540b57cec5SDimitry Andric     RD->setCompleteDefinition(true);
32550b57cec5SDimitry Andric     RD->DefinitionData = DD;
32560b57cec5SDimitry Andric     RD->getCanonicalDecl()->DefinitionData = DD;
32570b57cec5SDimitry Andric 
32580b57cec5SDimitry Andric     // Track that we did this horrible thing so that we can fix it later.
32590b57cec5SDimitry Andric     Reader.PendingFakeDefinitionData.insert(
32600b57cec5SDimitry Andric         std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
32610b57cec5SDimitry Andric   }
32620b57cec5SDimitry Andric 
32630b57cec5SDimitry Andric   return DD->Definition;
32640b57cec5SDimitry Andric }
32650b57cec5SDimitry Andric 
32668a4dda33SDimitry Andric /// Find the context in which we should search for previous declarations when
32678a4dda33SDimitry Andric /// looking for declarations to merge.
32688a4dda33SDimitry Andric DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
32698a4dda33SDimitry Andric                                                         DeclContext *DC) {
32708a4dda33SDimitry Andric   if (auto *ND = dyn_cast<NamespaceDecl>(DC))
32710fca6ea1SDimitry Andric     return ND->getFirstDecl();
32728a4dda33SDimitry Andric 
32738a4dda33SDimitry Andric   if (auto *RD = dyn_cast<CXXRecordDecl>(DC))
32748a4dda33SDimitry Andric     return getOrFakePrimaryClassDefinition(Reader, RD);
32758a4dda33SDimitry Andric 
3276349cc55cSDimitry Andric   if (auto *RD = dyn_cast<RecordDecl>(DC))
3277349cc55cSDimitry Andric     return RD->getDefinition();
3278349cc55cSDimitry Andric 
32790b57cec5SDimitry Andric   if (auto *ED = dyn_cast<EnumDecl>(DC))
32800fca6ea1SDimitry Andric     return ED->getDefinition();
32810b57cec5SDimitry Andric 
3282349cc55cSDimitry Andric   if (auto *OID = dyn_cast<ObjCInterfaceDecl>(DC))
3283349cc55cSDimitry Andric     return OID->getDefinition();
3284349cc55cSDimitry Andric 
32850fca6ea1SDimitry Andric   // We can see the TU here only if we have no Sema object. It is possible
32860fca6ea1SDimitry Andric   // we're in clang-repl so we still need to get the primary context.
32870b57cec5SDimitry Andric   if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
32880fca6ea1SDimitry Andric     return TU->getPrimaryContext();
32890b57cec5SDimitry Andric 
32900b57cec5SDimitry Andric   return nullptr;
32910b57cec5SDimitry Andric }
32920b57cec5SDimitry Andric 
32930b57cec5SDimitry Andric ASTDeclReader::FindExistingResult::~FindExistingResult() {
32940b57cec5SDimitry Andric   // Record that we had a typedef name for linkage whether or not we merge
32950b57cec5SDimitry Andric   // with that declaration.
32960b57cec5SDimitry Andric   if (TypedefNameForLinkage) {
32970b57cec5SDimitry Andric     DeclContext *DC = New->getDeclContext()->getRedeclContext();
32980b57cec5SDimitry Andric     Reader.ImportedTypedefNamesForLinkage.insert(
32990b57cec5SDimitry Andric         std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
33000b57cec5SDimitry Andric     return;
33010b57cec5SDimitry Andric   }
33020b57cec5SDimitry Andric 
33030b57cec5SDimitry Andric   if (!AddResult || Existing)
33040b57cec5SDimitry Andric     return;
33050b57cec5SDimitry Andric 
33060b57cec5SDimitry Andric   DeclarationName Name = New->getDeclName();
33070b57cec5SDimitry Andric   DeclContext *DC = New->getDeclContext()->getRedeclContext();
33080b57cec5SDimitry Andric   if (needsAnonymousDeclarationNumber(New)) {
33090b57cec5SDimitry Andric     setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
33100b57cec5SDimitry Andric                                AnonymousDeclNumber, New);
33110b57cec5SDimitry Andric   } else if (DC->isTranslationUnit() &&
33120b57cec5SDimitry Andric              !Reader.getContext().getLangOpts().CPlusPlus) {
33130b57cec5SDimitry Andric     if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
33140b57cec5SDimitry Andric       Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
33150b57cec5SDimitry Andric             .push_back(New);
33160b57cec5SDimitry Andric   } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
33170b57cec5SDimitry Andric     // Add the declaration to its redeclaration context so later merging
33180b57cec5SDimitry Andric     // lookups will find it.
33190b57cec5SDimitry Andric     MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
33200b57cec5SDimitry Andric   }
33210b57cec5SDimitry Andric }
33220b57cec5SDimitry Andric 
33230b57cec5SDimitry Andric /// Find the declaration that should be merged into, given the declaration found
33240b57cec5SDimitry Andric /// by name lookup. If we're merging an anonymous declaration within a typedef,
33250b57cec5SDimitry Andric /// we need a matching typedef, and we merge with the type inside it.
33260b57cec5SDimitry Andric static NamedDecl *getDeclForMerging(NamedDecl *Found,
33270b57cec5SDimitry Andric                                     bool IsTypedefNameForLinkage) {
33280b57cec5SDimitry Andric   if (!IsTypedefNameForLinkage)
33290b57cec5SDimitry Andric     return Found;
33300b57cec5SDimitry Andric 
33310b57cec5SDimitry Andric   // If we found a typedef declaration that gives a name to some other
33320b57cec5SDimitry Andric   // declaration, then we want that inner declaration. Declarations from
33330b57cec5SDimitry Andric   // AST files are handled via ImportedTypedefNamesForLinkage.
33340b57cec5SDimitry Andric   if (Found->isFromASTFile())
33350b57cec5SDimitry Andric     return nullptr;
33360b57cec5SDimitry Andric 
33370b57cec5SDimitry Andric   if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
33380b57cec5SDimitry Andric     return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
33390b57cec5SDimitry Andric 
33400b57cec5SDimitry Andric   return nullptr;
33410b57cec5SDimitry Andric }
33420b57cec5SDimitry Andric 
33430b57cec5SDimitry Andric /// Find the declaration to use to populate the anonymous declaration table
33440b57cec5SDimitry Andric /// for the given lexical DeclContext. We only care about finding local
33450b57cec5SDimitry Andric /// definitions of the context; we'll merge imported ones as we go.
33460b57cec5SDimitry Andric DeclContext *
33470b57cec5SDimitry Andric ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
33480b57cec5SDimitry Andric   // For classes, we track the definition as we merge.
33490b57cec5SDimitry Andric   if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
33500b57cec5SDimitry Andric     auto *DD = RD->getCanonicalDecl()->DefinitionData;
33510b57cec5SDimitry Andric     return DD ? DD->Definition : nullptr;
335281ad6265SDimitry Andric   } else if (auto *OID = dyn_cast<ObjCInterfaceDecl>(LexicalDC)) {
335381ad6265SDimitry Andric     return OID->getCanonicalDecl()->getDefinition();
33540b57cec5SDimitry Andric   }
33550b57cec5SDimitry Andric 
33560b57cec5SDimitry Andric   // For anything else, walk its merged redeclarations looking for a definition.
33570b57cec5SDimitry Andric   // Note that we can't just call getDefinition here because the redeclaration
33580b57cec5SDimitry Andric   // chain isn't wired up.
33590b57cec5SDimitry Andric   for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
33600b57cec5SDimitry Andric     if (auto *FD = dyn_cast<FunctionDecl>(D))
33610b57cec5SDimitry Andric       if (FD->isThisDeclarationADefinition())
33620b57cec5SDimitry Andric         return FD;
33630b57cec5SDimitry Andric     if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
33640b57cec5SDimitry Andric       if (MD->isThisDeclarationADefinition())
33650b57cec5SDimitry Andric         return MD;
3366349cc55cSDimitry Andric     if (auto *RD = dyn_cast<RecordDecl>(D))
3367349cc55cSDimitry Andric       if (RD->isThisDeclarationADefinition())
3368349cc55cSDimitry Andric         return RD;
33690b57cec5SDimitry Andric   }
33700b57cec5SDimitry Andric 
33710b57cec5SDimitry Andric   // No merged definition yet.
33720b57cec5SDimitry Andric   return nullptr;
33730b57cec5SDimitry Andric }
33740b57cec5SDimitry Andric 
33750b57cec5SDimitry Andric NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
33760b57cec5SDimitry Andric                                                      DeclContext *DC,
33770b57cec5SDimitry Andric                                                      unsigned Index) {
33780b57cec5SDimitry Andric   // If the lexical context has been merged, look into the now-canonical
33790b57cec5SDimitry Andric   // definition.
33800b57cec5SDimitry Andric   auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
33810b57cec5SDimitry Andric 
33820b57cec5SDimitry Andric   // If we've seen this before, return the canonical declaration.
33830b57cec5SDimitry Andric   auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
33840b57cec5SDimitry Andric   if (Index < Previous.size() && Previous[Index])
33850b57cec5SDimitry Andric     return Previous[Index];
33860b57cec5SDimitry Andric 
33870b57cec5SDimitry Andric   // If this is the first time, but we have parsed a declaration of the context,
33880b57cec5SDimitry Andric   // build the anonymous declaration list from the parsed declaration.
33890b57cec5SDimitry Andric   auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
33900b57cec5SDimitry Andric   if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
33910b57cec5SDimitry Andric     numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
33920b57cec5SDimitry Andric       if (Previous.size() == Number)
33930b57cec5SDimitry Andric         Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
33940b57cec5SDimitry Andric       else
33950b57cec5SDimitry Andric         Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
33960b57cec5SDimitry Andric     });
33970b57cec5SDimitry Andric   }
33980b57cec5SDimitry Andric 
33990b57cec5SDimitry Andric   return Index < Previous.size() ? Previous[Index] : nullptr;
34000b57cec5SDimitry Andric }
34010b57cec5SDimitry Andric 
34020b57cec5SDimitry Andric void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
34030b57cec5SDimitry Andric                                                DeclContext *DC, unsigned Index,
34040b57cec5SDimitry Andric                                                NamedDecl *D) {
34050b57cec5SDimitry Andric   auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
34060b57cec5SDimitry Andric 
34070b57cec5SDimitry Andric   auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
34080b57cec5SDimitry Andric   if (Index >= Previous.size())
34090b57cec5SDimitry Andric     Previous.resize(Index + 1);
34100b57cec5SDimitry Andric   if (!Previous[Index])
34110b57cec5SDimitry Andric     Previous[Index] = D;
34120b57cec5SDimitry Andric }
34130b57cec5SDimitry Andric 
34140b57cec5SDimitry Andric ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
34150b57cec5SDimitry Andric   DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
34160b57cec5SDimitry Andric                                                : D->getDeclName();
34170b57cec5SDimitry Andric 
34180b57cec5SDimitry Andric   if (!Name && !needsAnonymousDeclarationNumber(D)) {
34190b57cec5SDimitry Andric     // Don't bother trying to find unnamed declarations that are in
34200b57cec5SDimitry Andric     // unmergeable contexts.
34210b57cec5SDimitry Andric     FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
34220b57cec5SDimitry Andric                               AnonymousDeclNumber, TypedefNameForLinkage);
34230b57cec5SDimitry Andric     Result.suppress();
34240b57cec5SDimitry Andric     return Result;
34250b57cec5SDimitry Andric   }
34260b57cec5SDimitry Andric 
342704eeddc0SDimitry Andric   ASTContext &C = Reader.getContext();
34280b57cec5SDimitry Andric   DeclContext *DC = D->getDeclContext()->getRedeclContext();
34290b57cec5SDimitry Andric   if (TypedefNameForLinkage) {
34300b57cec5SDimitry Andric     auto It = Reader.ImportedTypedefNamesForLinkage.find(
34310b57cec5SDimitry Andric         std::make_pair(DC, TypedefNameForLinkage));
34320b57cec5SDimitry Andric     if (It != Reader.ImportedTypedefNamesForLinkage.end())
343304eeddc0SDimitry Andric       if (C.isSameEntity(It->second, D))
34340b57cec5SDimitry Andric         return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
34350b57cec5SDimitry Andric                                   TypedefNameForLinkage);
34360b57cec5SDimitry Andric     // Go on to check in other places in case an existing typedef name
34370b57cec5SDimitry Andric     // was not imported.
34380b57cec5SDimitry Andric   }
34390b57cec5SDimitry Andric 
34400b57cec5SDimitry Andric   if (needsAnonymousDeclarationNumber(D)) {
34410b57cec5SDimitry Andric     // This is an anonymous declaration that we may need to merge. Look it up
34420b57cec5SDimitry Andric     // in its context by number.
34430b57cec5SDimitry Andric     if (auto *Existing = getAnonymousDeclForMerging(
34440b57cec5SDimitry Andric             Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
344504eeddc0SDimitry Andric       if (C.isSameEntity(Existing, D))
34460b57cec5SDimitry Andric         return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
34470b57cec5SDimitry Andric                                   TypedefNameForLinkage);
34480b57cec5SDimitry Andric   } else if (DC->isTranslationUnit() &&
34490b57cec5SDimitry Andric              !Reader.getContext().getLangOpts().CPlusPlus) {
34500b57cec5SDimitry Andric     IdentifierResolver &IdResolver = Reader.getIdResolver();
34510b57cec5SDimitry Andric 
34520b57cec5SDimitry Andric     // Temporarily consider the identifier to be up-to-date. We don't want to
34530b57cec5SDimitry Andric     // cause additional lookups here.
34540b57cec5SDimitry Andric     class UpToDateIdentifierRAII {
34550b57cec5SDimitry Andric       IdentifierInfo *II;
34560b57cec5SDimitry Andric       bool WasOutToDate = false;
34570b57cec5SDimitry Andric 
34580b57cec5SDimitry Andric     public:
34590b57cec5SDimitry Andric       explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
34600b57cec5SDimitry Andric         if (II) {
34610b57cec5SDimitry Andric           WasOutToDate = II->isOutOfDate();
34620b57cec5SDimitry Andric           if (WasOutToDate)
34630b57cec5SDimitry Andric             II->setOutOfDate(false);
34640b57cec5SDimitry Andric         }
34650b57cec5SDimitry Andric       }
34660b57cec5SDimitry Andric 
34670b57cec5SDimitry Andric       ~UpToDateIdentifierRAII() {
34680b57cec5SDimitry Andric         if (WasOutToDate)
34690b57cec5SDimitry Andric           II->setOutOfDate(true);
34700b57cec5SDimitry Andric       }
34710b57cec5SDimitry Andric     } UpToDate(Name.getAsIdentifierInfo());
34720b57cec5SDimitry Andric 
34730b57cec5SDimitry Andric     for (IdentifierResolver::iterator I = IdResolver.begin(Name),
34740b57cec5SDimitry Andric                                    IEnd = IdResolver.end();
34750b57cec5SDimitry Andric          I != IEnd; ++I) {
34760b57cec5SDimitry Andric       if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
347704eeddc0SDimitry Andric         if (C.isSameEntity(Existing, D))
34780b57cec5SDimitry Andric           return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
34790b57cec5SDimitry Andric                                     TypedefNameForLinkage);
34800b57cec5SDimitry Andric     }
34810b57cec5SDimitry Andric   } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
34820b57cec5SDimitry Andric     DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
34830b57cec5SDimitry Andric     for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
34840b57cec5SDimitry Andric       if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
348504eeddc0SDimitry Andric         if (C.isSameEntity(Existing, D))
34860b57cec5SDimitry Andric           return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
34870b57cec5SDimitry Andric                                     TypedefNameForLinkage);
34880b57cec5SDimitry Andric     }
34890b57cec5SDimitry Andric   } else {
34900b57cec5SDimitry Andric     // Not in a mergeable context.
34910b57cec5SDimitry Andric     return FindExistingResult(Reader);
34920b57cec5SDimitry Andric   }
34930b57cec5SDimitry Andric 
34940b57cec5SDimitry Andric   // If this declaration is from a merged context, make a note that we need to
34950b57cec5SDimitry Andric   // check that the canonical definition of that context contains the decl.
34960b57cec5SDimitry Andric   //
3497b3edf446SDimitry Andric   // Note that we don't perform ODR checks for decls from the global module
3498b3edf446SDimitry Andric   // fragment.
3499b3edf446SDimitry Andric   //
35000b57cec5SDimitry Andric   // FIXME: We should do something similar if we merge two definitions of the
35010b57cec5SDimitry Andric   // same template specialization into the same CXXRecordDecl.
35020b57cec5SDimitry Andric   auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
35030b57cec5SDimitry Andric   if (MergedDCIt != Reader.MergedDeclContexts.end() &&
35040fca6ea1SDimitry Andric       !shouldSkipCheckingODR(D) && MergedDCIt->second == D->getDeclContext())
35050b57cec5SDimitry Andric     Reader.PendingOdrMergeChecks.push_back(D);
35060b57cec5SDimitry Andric 
35070b57cec5SDimitry Andric   return FindExistingResult(Reader, D, /*Existing=*/nullptr,
35080b57cec5SDimitry Andric                             AnonymousDeclNumber, TypedefNameForLinkage);
35090b57cec5SDimitry Andric }
35100b57cec5SDimitry Andric 
35110b57cec5SDimitry Andric template<typename DeclT>
35120b57cec5SDimitry Andric Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
35130b57cec5SDimitry Andric   return D->RedeclLink.getLatestNotUpdated();
35140b57cec5SDimitry Andric }
35150b57cec5SDimitry Andric 
35160b57cec5SDimitry Andric Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
35170b57cec5SDimitry Andric   llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
35180b57cec5SDimitry Andric }
35190b57cec5SDimitry Andric 
35200b57cec5SDimitry Andric Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
35210b57cec5SDimitry Andric   assert(D);
35220b57cec5SDimitry Andric 
35230b57cec5SDimitry Andric   switch (D->getKind()) {
35240b57cec5SDimitry Andric #define ABSTRACT_DECL(TYPE)
35250b57cec5SDimitry Andric #define DECL(TYPE, BASE)                               \
35260b57cec5SDimitry Andric   case Decl::TYPE:                                     \
35270b57cec5SDimitry Andric     return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
35280b57cec5SDimitry Andric #include "clang/AST/DeclNodes.inc"
35290b57cec5SDimitry Andric   }
35300b57cec5SDimitry Andric   llvm_unreachable("unknown decl kind");
35310b57cec5SDimitry Andric }
35320b57cec5SDimitry Andric 
35330b57cec5SDimitry Andric Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
35340b57cec5SDimitry Andric   return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
35350b57cec5SDimitry Andric }
35360b57cec5SDimitry Andric 
3537e8d8bef9SDimitry Andric void ASTDeclReader::mergeInheritableAttributes(ASTReader &Reader, Decl *D,
3538e8d8bef9SDimitry Andric                                                Decl *Previous) {
3539e8d8bef9SDimitry Andric   InheritableAttr *NewAttr = nullptr;
3540e8d8bef9SDimitry Andric   ASTContext &Context = Reader.getContext();
3541e8d8bef9SDimitry Andric   const auto *IA = Previous->getAttr<MSInheritanceAttr>();
3542e8d8bef9SDimitry Andric 
3543e8d8bef9SDimitry Andric   if (IA && !D->hasAttr<MSInheritanceAttr>()) {
3544e8d8bef9SDimitry Andric     NewAttr = cast<InheritableAttr>(IA->clone(Context));
3545e8d8bef9SDimitry Andric     NewAttr->setInherited(true);
3546e8d8bef9SDimitry Andric     D->addAttr(NewAttr);
3547e8d8bef9SDimitry Andric   }
354881ad6265SDimitry Andric 
354981ad6265SDimitry Andric   const auto *AA = Previous->getAttr<AvailabilityAttr>();
355081ad6265SDimitry Andric   if (AA && !D->hasAttr<AvailabilityAttr>()) {
355181ad6265SDimitry Andric     NewAttr = AA->clone(Context);
355281ad6265SDimitry Andric     NewAttr->setInherited(true);
355381ad6265SDimitry Andric     D->addAttr(NewAttr);
355481ad6265SDimitry Andric   }
3555e8d8bef9SDimitry Andric }
3556e8d8bef9SDimitry Andric 
35570b57cec5SDimitry Andric template<typename DeclT>
35580b57cec5SDimitry Andric void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
35590b57cec5SDimitry Andric                                            Redeclarable<DeclT> *D,
35600b57cec5SDimitry Andric                                            Decl *Previous, Decl *Canon) {
35610b57cec5SDimitry Andric   D->RedeclLink.setPrevious(cast<DeclT>(Previous));
35620b57cec5SDimitry Andric   D->First = cast<DeclT>(Previous)->First;
35630b57cec5SDimitry Andric }
35640b57cec5SDimitry Andric 
35650b57cec5SDimitry Andric namespace clang {
35660b57cec5SDimitry Andric 
35670b57cec5SDimitry Andric template<>
35680b57cec5SDimitry Andric void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
35690b57cec5SDimitry Andric                                            Redeclarable<VarDecl> *D,
35700b57cec5SDimitry Andric                                            Decl *Previous, Decl *Canon) {
35710b57cec5SDimitry Andric   auto *VD = static_cast<VarDecl *>(D);
35720b57cec5SDimitry Andric   auto *PrevVD = cast<VarDecl>(Previous);
35730b57cec5SDimitry Andric   D->RedeclLink.setPrevious(PrevVD);
35740b57cec5SDimitry Andric   D->First = PrevVD->First;
35750b57cec5SDimitry Andric 
35760b57cec5SDimitry Andric   // We should keep at most one definition on the chain.
35770b57cec5SDimitry Andric   // FIXME: Cache the definition once we've found it. Building a chain with
35780b57cec5SDimitry Andric   // N definitions currently takes O(N^2) time here.
35790b57cec5SDimitry Andric   if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
35800b57cec5SDimitry Andric     for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
35810b57cec5SDimitry Andric       if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
35820b57cec5SDimitry Andric         Reader.mergeDefinitionVisibility(CurD, VD);
35830b57cec5SDimitry Andric         VD->demoteThisDefinitionToDeclaration();
35840b57cec5SDimitry Andric         break;
35850b57cec5SDimitry Andric       }
35860b57cec5SDimitry Andric     }
35870b57cec5SDimitry Andric   }
35880b57cec5SDimitry Andric }
35890b57cec5SDimitry Andric 
35900b57cec5SDimitry Andric static bool isUndeducedReturnType(QualType T) {
35910b57cec5SDimitry Andric   auto *DT = T->getContainedDeducedType();
35920b57cec5SDimitry Andric   return DT && !DT->isDeduced();
35930b57cec5SDimitry Andric }
35940b57cec5SDimitry Andric 
35950b57cec5SDimitry Andric template<>
35960b57cec5SDimitry Andric void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
35970b57cec5SDimitry Andric                                            Redeclarable<FunctionDecl> *D,
35980b57cec5SDimitry Andric                                            Decl *Previous, Decl *Canon) {
35990b57cec5SDimitry Andric   auto *FD = static_cast<FunctionDecl *>(D);
36000b57cec5SDimitry Andric   auto *PrevFD = cast<FunctionDecl>(Previous);
36010b57cec5SDimitry Andric 
36020b57cec5SDimitry Andric   FD->RedeclLink.setPrevious(PrevFD);
36030b57cec5SDimitry Andric   FD->First = PrevFD->First;
36040b57cec5SDimitry Andric 
36050b57cec5SDimitry Andric   // If the previous declaration is an inline function declaration, then this
36060b57cec5SDimitry Andric   // declaration is too.
36070b57cec5SDimitry Andric   if (PrevFD->isInlined() != FD->isInlined()) {
36080b57cec5SDimitry Andric     // FIXME: [dcl.fct.spec]p4:
36090b57cec5SDimitry Andric     //   If a function with external linkage is declared inline in one
36100b57cec5SDimitry Andric     //   translation unit, it shall be declared inline in all translation
36110b57cec5SDimitry Andric     //   units in which it appears.
36120b57cec5SDimitry Andric     //
36130b57cec5SDimitry Andric     // Be careful of this case:
36140b57cec5SDimitry Andric     //
36150b57cec5SDimitry Andric     // module A:
36160b57cec5SDimitry Andric     //   template<typename T> struct X { void f(); };
36170b57cec5SDimitry Andric     //   template<typename T> inline void X<T>::f() {}
36180b57cec5SDimitry Andric     //
36190b57cec5SDimitry Andric     // module B instantiates the declaration of X<int>::f
36200b57cec5SDimitry Andric     // module C instantiates the definition of X<int>::f
36210b57cec5SDimitry Andric     //
36220b57cec5SDimitry Andric     // If module B and C are merged, we do not have a violation of this rule.
36230b57cec5SDimitry Andric     FD->setImplicitlyInline(true);
36240b57cec5SDimitry Andric   }
36250b57cec5SDimitry Andric 
36260b57cec5SDimitry Andric   auto *FPT = FD->getType()->getAs<FunctionProtoType>();
36270b57cec5SDimitry Andric   auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
36280b57cec5SDimitry Andric   if (FPT && PrevFPT) {
36290b57cec5SDimitry Andric     // If we need to propagate an exception specification along the redecl
36300b57cec5SDimitry Andric     // chain, make a note of that so that we can do so later.
36310b57cec5SDimitry Andric     bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
36320b57cec5SDimitry Andric     bool WasUnresolved =
36330b57cec5SDimitry Andric         isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
36340b57cec5SDimitry Andric     if (IsUnresolved != WasUnresolved)
36350b57cec5SDimitry Andric       Reader.PendingExceptionSpecUpdates.insert(
36360b57cec5SDimitry Andric           {Canon, IsUnresolved ? PrevFD : FD});
36370b57cec5SDimitry Andric 
36380b57cec5SDimitry Andric     // If we need to propagate a deduced return type along the redecl chain,
36390b57cec5SDimitry Andric     // make a note of that so that we can do it later.
36400b57cec5SDimitry Andric     bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
36410b57cec5SDimitry Andric     bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
36420b57cec5SDimitry Andric     if (IsUndeduced != WasUndeduced)
36430b57cec5SDimitry Andric       Reader.PendingDeducedTypeUpdates.insert(
36440b57cec5SDimitry Andric           {cast<FunctionDecl>(Canon),
36450b57cec5SDimitry Andric            (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
36460b57cec5SDimitry Andric   }
36470b57cec5SDimitry Andric }
36480b57cec5SDimitry Andric 
36490b57cec5SDimitry Andric } // namespace clang
36500b57cec5SDimitry Andric 
36510b57cec5SDimitry Andric void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
36520b57cec5SDimitry Andric   llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
36530b57cec5SDimitry Andric }
36540b57cec5SDimitry Andric 
36550b57cec5SDimitry Andric /// Inherit the default template argument from \p From to \p To. Returns
36560b57cec5SDimitry Andric /// \c false if there is no default template for \p From.
36570b57cec5SDimitry Andric template <typename ParmDecl>
36580b57cec5SDimitry Andric static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
36590b57cec5SDimitry Andric                                            Decl *ToD) {
36600b57cec5SDimitry Andric   auto *To = cast<ParmDecl>(ToD);
36610b57cec5SDimitry Andric   if (!From->hasDefaultArgument())
36620b57cec5SDimitry Andric     return false;
36630b57cec5SDimitry Andric   To->setInheritedDefaultArgument(Context, From);
36640b57cec5SDimitry Andric   return true;
36650b57cec5SDimitry Andric }
36660b57cec5SDimitry Andric 
36670b57cec5SDimitry Andric static void inheritDefaultTemplateArguments(ASTContext &Context,
36680b57cec5SDimitry Andric                                             TemplateDecl *From,
36690b57cec5SDimitry Andric                                             TemplateDecl *To) {
36700b57cec5SDimitry Andric   auto *FromTP = From->getTemplateParameters();
36710b57cec5SDimitry Andric   auto *ToTP = To->getTemplateParameters();
36720b57cec5SDimitry Andric   assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
36730b57cec5SDimitry Andric 
36740b57cec5SDimitry Andric   for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
36750b57cec5SDimitry Andric     NamedDecl *FromParam = FromTP->getParam(I);
36760b57cec5SDimitry Andric     NamedDecl *ToParam = ToTP->getParam(I);
36770b57cec5SDimitry Andric 
36780b57cec5SDimitry Andric     if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
36790b57cec5SDimitry Andric       inheritDefaultTemplateArgument(Context, FTTP, ToParam);
36800b57cec5SDimitry Andric     else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
36810b57cec5SDimitry Andric       inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
36820b57cec5SDimitry Andric     else
36830b57cec5SDimitry Andric       inheritDefaultTemplateArgument(
36840b57cec5SDimitry Andric               Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
36850b57cec5SDimitry Andric   }
36860b57cec5SDimitry Andric }
36870b57cec5SDimitry Andric 
368862987288SDimitry Andric // [basic.link]/p10:
368962987288SDimitry Andric //    If two declarations of an entity are attached to different modules,
369062987288SDimitry Andric //    the program is ill-formed;
369162987288SDimitry Andric static void checkMultipleDefinitionInNamedModules(ASTReader &Reader, Decl *D,
369262987288SDimitry Andric                                                   Decl *Previous) {
369362987288SDimitry Andric   Module *M = Previous->getOwningModule();
369462987288SDimitry Andric 
369562987288SDimitry Andric   // We only care about the case in named modules.
369662987288SDimitry Andric   if (!M || !M->isNamedModule())
369762987288SDimitry Andric     return;
369862987288SDimitry Andric 
369962987288SDimitry Andric   // If it is previous implcitly introduced, it is not meaningful to
370062987288SDimitry Andric   // diagnose it.
370162987288SDimitry Andric   if (Previous->isImplicit())
370262987288SDimitry Andric     return;
370362987288SDimitry Andric 
370462987288SDimitry Andric   // FIXME: Get rid of the enumeration of decl types once we have an appropriate
370562987288SDimitry Andric   // abstract for decls of an entity. e.g., the namespace decl and using decl
370662987288SDimitry Andric   // doesn't introduce an entity.
370762987288SDimitry Andric   if (!isa<VarDecl, FunctionDecl, TagDecl, RedeclarableTemplateDecl>(Previous))
370862987288SDimitry Andric     return;
370962987288SDimitry Andric 
371062987288SDimitry Andric   // Skip implicit instantiations since it may give false positive diagnostic
371162987288SDimitry Andric   // messages.
371262987288SDimitry Andric   // FIXME: Maybe this shows the implicit instantiations may have incorrect
371362987288SDimitry Andric   // module owner ships. But given we've finished the compilation of a module,
371462987288SDimitry Andric   // how can we add new entities to that module?
371562987288SDimitry Andric   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Previous);
371662987288SDimitry Andric       VTSD && !VTSD->isExplicitSpecialization())
371762987288SDimitry Andric     return;
371862987288SDimitry Andric   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Previous);
371962987288SDimitry Andric       CTSD && !CTSD->isExplicitSpecialization())
372062987288SDimitry Andric     return;
372162987288SDimitry Andric   if (auto *Func = dyn_cast<FunctionDecl>(Previous))
372262987288SDimitry Andric     if (auto *FTSI = Func->getTemplateSpecializationInfo();
372362987288SDimitry Andric         FTSI && !FTSI->isExplicitSpecialization())
372462987288SDimitry Andric       return;
372562987288SDimitry Andric 
372662987288SDimitry Andric   // It is fine if they are in the same module.
372762987288SDimitry Andric   if (Reader.getContext().isInSameModule(M, D->getOwningModule()))
372862987288SDimitry Andric     return;
372962987288SDimitry Andric 
373062987288SDimitry Andric   Reader.Diag(Previous->getLocation(),
373162987288SDimitry Andric               diag::err_multiple_decl_in_different_modules)
373262987288SDimitry Andric       << cast<NamedDecl>(Previous) << M->Name;
373362987288SDimitry Andric   Reader.Diag(D->getLocation(), diag::note_also_found);
373462987288SDimitry Andric }
373562987288SDimitry Andric 
37360b57cec5SDimitry Andric void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
37370b57cec5SDimitry Andric                                        Decl *Previous, Decl *Canon) {
37380b57cec5SDimitry Andric   assert(D && Previous);
37390b57cec5SDimitry Andric 
37400b57cec5SDimitry Andric   switch (D->getKind()) {
37410b57cec5SDimitry Andric #define ABSTRACT_DECL(TYPE)
37420b57cec5SDimitry Andric #define DECL(TYPE, BASE)                                                  \
37430b57cec5SDimitry Andric   case Decl::TYPE:                                                        \
37440b57cec5SDimitry Andric     attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
37450b57cec5SDimitry Andric     break;
37460b57cec5SDimitry Andric #include "clang/AST/DeclNodes.inc"
37470b57cec5SDimitry Andric   }
37480b57cec5SDimitry Andric 
374962987288SDimitry Andric   checkMultipleDefinitionInNamedModules(Reader, D, Previous);
37500fca6ea1SDimitry Andric 
37510b57cec5SDimitry Andric   // If the declaration was visible in one module, a redeclaration of it in
37520b57cec5SDimitry Andric   // another module remains visible even if it wouldn't be visible by itself.
37530b57cec5SDimitry Andric   //
37540b57cec5SDimitry Andric   // FIXME: In this case, the declaration should only be visible if a module
37550b57cec5SDimitry Andric   //        that makes it visible has been imported.
37560b57cec5SDimitry Andric   D->IdentifierNamespace |=
37570b57cec5SDimitry Andric       Previous->IdentifierNamespace &
37580b57cec5SDimitry Andric       (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
37590b57cec5SDimitry Andric 
37600b57cec5SDimitry Andric   // If the declaration declares a template, it may inherit default arguments
37610b57cec5SDimitry Andric   // from the previous declaration.
37620b57cec5SDimitry Andric   if (auto *TD = dyn_cast<TemplateDecl>(D))
37630b57cec5SDimitry Andric     inheritDefaultTemplateArguments(Reader.getContext(),
37640b57cec5SDimitry Andric                                     cast<TemplateDecl>(Previous), TD);
3765e8d8bef9SDimitry Andric 
3766e8d8bef9SDimitry Andric   // If any of the declaration in the chain contains an Inheritable attribute,
3767e8d8bef9SDimitry Andric   // it needs to be added to all the declarations in the redeclarable chain.
3768e8d8bef9SDimitry Andric   // FIXME: Only the logic of merging MSInheritableAttr is present, it should
3769e8d8bef9SDimitry Andric   // be extended for all inheritable attributes.
3770e8d8bef9SDimitry Andric   mergeInheritableAttributes(Reader, D, Previous);
37710b57cec5SDimitry Andric }
37720b57cec5SDimitry Andric 
37730b57cec5SDimitry Andric template<typename DeclT>
37740b57cec5SDimitry Andric void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
37750b57cec5SDimitry Andric   D->RedeclLink.setLatest(cast<DeclT>(Latest));
37760b57cec5SDimitry Andric }
37770b57cec5SDimitry Andric 
37780b57cec5SDimitry Andric void ASTDeclReader::attachLatestDeclImpl(...) {
37790b57cec5SDimitry Andric   llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
37800b57cec5SDimitry Andric }
37810b57cec5SDimitry Andric 
37820b57cec5SDimitry Andric void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
37830b57cec5SDimitry Andric   assert(D && Latest);
37840b57cec5SDimitry Andric 
37850b57cec5SDimitry Andric   switch (D->getKind()) {
37860b57cec5SDimitry Andric #define ABSTRACT_DECL(TYPE)
37870b57cec5SDimitry Andric #define DECL(TYPE, BASE)                                  \
37880b57cec5SDimitry Andric   case Decl::TYPE:                                        \
37890b57cec5SDimitry Andric     attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
37900b57cec5SDimitry Andric     break;
37910b57cec5SDimitry Andric #include "clang/AST/DeclNodes.inc"
37920b57cec5SDimitry Andric   }
37930b57cec5SDimitry Andric }
37940b57cec5SDimitry Andric 
37950b57cec5SDimitry Andric template<typename DeclT>
37960b57cec5SDimitry Andric void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
37970b57cec5SDimitry Andric   D->RedeclLink.markIncomplete();
37980b57cec5SDimitry Andric }
37990b57cec5SDimitry Andric 
38000b57cec5SDimitry Andric void ASTDeclReader::markIncompleteDeclChainImpl(...) {
38010b57cec5SDimitry Andric   llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
38020b57cec5SDimitry Andric }
38030b57cec5SDimitry Andric 
38040b57cec5SDimitry Andric void ASTReader::markIncompleteDeclChain(Decl *D) {
38050b57cec5SDimitry Andric   switch (D->getKind()) {
38060b57cec5SDimitry Andric #define ABSTRACT_DECL(TYPE)
38070b57cec5SDimitry Andric #define DECL(TYPE, BASE)                                             \
38080b57cec5SDimitry Andric   case Decl::TYPE:                                                   \
38090b57cec5SDimitry Andric     ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
38100b57cec5SDimitry Andric     break;
38110b57cec5SDimitry Andric #include "clang/AST/DeclNodes.inc"
38120b57cec5SDimitry Andric   }
38130b57cec5SDimitry Andric }
38140b57cec5SDimitry Andric 
38150b57cec5SDimitry Andric /// Read the declaration at the given offset from the AST file.
38160fca6ea1SDimitry Andric Decl *ASTReader::ReadDeclRecord(GlobalDeclID ID) {
38170b57cec5SDimitry Andric   SourceLocation DeclLoc;
38180b57cec5SDimitry Andric   RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
38190b57cec5SDimitry Andric   llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
38200b57cec5SDimitry Andric   // Keep track of where we are in the stream, then jump back there
38210b57cec5SDimitry Andric   // after reading this declaration.
38220b57cec5SDimitry Andric   SavedStreamPosition SavedPosition(DeclsCursor);
38230b57cec5SDimitry Andric 
38240b57cec5SDimitry Andric   ReadingKindTracker ReadingKind(Read_Decl, *this);
38250b57cec5SDimitry Andric 
38260b57cec5SDimitry Andric   // Note that we are loading a declaration record.
38270b57cec5SDimitry Andric   Deserializing ADecl(this);
38280b57cec5SDimitry Andric 
38290b57cec5SDimitry Andric   auto Fail = [](const char *what, llvm::Error &&Err) {
3830480093f4SDimitry Andric     llvm::report_fatal_error(Twine("ASTReader::readDeclRecord failed ") + what +
38310b57cec5SDimitry Andric                              ": " + toString(std::move(Err)));
38320b57cec5SDimitry Andric   };
38330b57cec5SDimitry Andric 
38340b57cec5SDimitry Andric   if (llvm::Error JumpFailed = DeclsCursor.JumpToBit(Loc.Offset))
38350b57cec5SDimitry Andric     Fail("jumping", std::move(JumpFailed));
38360b57cec5SDimitry Andric   ASTRecordReader Record(*this, *Loc.F);
38370b57cec5SDimitry Andric   ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
38380b57cec5SDimitry Andric   Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
38390b57cec5SDimitry Andric   if (!MaybeCode)
38400b57cec5SDimitry Andric     Fail("reading code", MaybeCode.takeError());
38410b57cec5SDimitry Andric   unsigned Code = MaybeCode.get();
38420b57cec5SDimitry Andric 
38430b57cec5SDimitry Andric   ASTContext &Context = getContext();
38440b57cec5SDimitry Andric   Decl *D = nullptr;
38450b57cec5SDimitry Andric   Expected<unsigned> MaybeDeclCode = Record.readRecord(DeclsCursor, Code);
38460b57cec5SDimitry Andric   if (!MaybeDeclCode)
38470b57cec5SDimitry Andric     llvm::report_fatal_error(
3848349cc55cSDimitry Andric         Twine("ASTReader::readDeclRecord failed reading decl code: ") +
38490b57cec5SDimitry Andric         toString(MaybeDeclCode.takeError()));
38500fca6ea1SDimitry Andric 
38510b57cec5SDimitry Andric   switch ((DeclCode)MaybeDeclCode.get()) {
38520b57cec5SDimitry Andric   case DECL_CONTEXT_LEXICAL:
38530b57cec5SDimitry Andric   case DECL_CONTEXT_VISIBLE:
3854480093f4SDimitry Andric     llvm_unreachable("Record cannot be de-serialized with readDeclRecord");
38550b57cec5SDimitry Andric   case DECL_TYPEDEF:
38560b57cec5SDimitry Andric     D = TypedefDecl::CreateDeserialized(Context, ID);
38570b57cec5SDimitry Andric     break;
38580b57cec5SDimitry Andric   case DECL_TYPEALIAS:
38590b57cec5SDimitry Andric     D = TypeAliasDecl::CreateDeserialized(Context, ID);
38600b57cec5SDimitry Andric     break;
38610b57cec5SDimitry Andric   case DECL_ENUM:
38620b57cec5SDimitry Andric     D = EnumDecl::CreateDeserialized(Context, ID);
38630b57cec5SDimitry Andric     break;
38640b57cec5SDimitry Andric   case DECL_RECORD:
38650b57cec5SDimitry Andric     D = RecordDecl::CreateDeserialized(Context, ID);
38660b57cec5SDimitry Andric     break;
38670b57cec5SDimitry Andric   case DECL_ENUM_CONSTANT:
38680b57cec5SDimitry Andric     D = EnumConstantDecl::CreateDeserialized(Context, ID);
38690b57cec5SDimitry Andric     break;
38700b57cec5SDimitry Andric   case DECL_FUNCTION:
38710b57cec5SDimitry Andric     D = FunctionDecl::CreateDeserialized(Context, ID);
38720b57cec5SDimitry Andric     break;
38730b57cec5SDimitry Andric   case DECL_LINKAGE_SPEC:
38740b57cec5SDimitry Andric     D = LinkageSpecDecl::CreateDeserialized(Context, ID);
38750b57cec5SDimitry Andric     break;
38760b57cec5SDimitry Andric   case DECL_EXPORT:
38770b57cec5SDimitry Andric     D = ExportDecl::CreateDeserialized(Context, ID);
38780b57cec5SDimitry Andric     break;
38790b57cec5SDimitry Andric   case DECL_LABEL:
38800b57cec5SDimitry Andric     D = LabelDecl::CreateDeserialized(Context, ID);
38810b57cec5SDimitry Andric     break;
38820b57cec5SDimitry Andric   case DECL_NAMESPACE:
38830b57cec5SDimitry Andric     D = NamespaceDecl::CreateDeserialized(Context, ID);
38840b57cec5SDimitry Andric     break;
38850b57cec5SDimitry Andric   case DECL_NAMESPACE_ALIAS:
38860b57cec5SDimitry Andric     D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
38870b57cec5SDimitry Andric     break;
38880b57cec5SDimitry Andric   case DECL_USING:
38890b57cec5SDimitry Andric     D = UsingDecl::CreateDeserialized(Context, ID);
38900b57cec5SDimitry Andric     break;
38910b57cec5SDimitry Andric   case DECL_USING_PACK:
38920b57cec5SDimitry Andric     D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
38930b57cec5SDimitry Andric     break;
38940b57cec5SDimitry Andric   case DECL_USING_SHADOW:
38950b57cec5SDimitry Andric     D = UsingShadowDecl::CreateDeserialized(Context, ID);
38960b57cec5SDimitry Andric     break;
3897fe6060f1SDimitry Andric   case DECL_USING_ENUM:
3898fe6060f1SDimitry Andric     D = UsingEnumDecl::CreateDeserialized(Context, ID);
3899fe6060f1SDimitry Andric     break;
39000b57cec5SDimitry Andric   case DECL_CONSTRUCTOR_USING_SHADOW:
39010b57cec5SDimitry Andric     D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
39020b57cec5SDimitry Andric     break;
39030b57cec5SDimitry Andric   case DECL_USING_DIRECTIVE:
39040b57cec5SDimitry Andric     D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
39050b57cec5SDimitry Andric     break;
39060b57cec5SDimitry Andric   case DECL_UNRESOLVED_USING_VALUE:
39070b57cec5SDimitry Andric     D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
39080b57cec5SDimitry Andric     break;
39090b57cec5SDimitry Andric   case DECL_UNRESOLVED_USING_TYPENAME:
39100b57cec5SDimitry Andric     D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
39110b57cec5SDimitry Andric     break;
3912fe6060f1SDimitry Andric   case DECL_UNRESOLVED_USING_IF_EXISTS:
3913fe6060f1SDimitry Andric     D = UnresolvedUsingIfExistsDecl::CreateDeserialized(Context, ID);
3914fe6060f1SDimitry Andric     break;
39150b57cec5SDimitry Andric   case DECL_CXX_RECORD:
39160b57cec5SDimitry Andric     D = CXXRecordDecl::CreateDeserialized(Context, ID);
39170b57cec5SDimitry Andric     break;
39180b57cec5SDimitry Andric   case DECL_CXX_DEDUCTION_GUIDE:
39190b57cec5SDimitry Andric     D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
39200b57cec5SDimitry Andric     break;
39210b57cec5SDimitry Andric   case DECL_CXX_METHOD:
39220b57cec5SDimitry Andric     D = CXXMethodDecl::CreateDeserialized(Context, ID);
39230b57cec5SDimitry Andric     break;
39240b57cec5SDimitry Andric   case DECL_CXX_CONSTRUCTOR:
39250b57cec5SDimitry Andric     D = CXXConstructorDecl::CreateDeserialized(Context, ID, Record.readInt());
39260b57cec5SDimitry Andric     break;
39270b57cec5SDimitry Andric   case DECL_CXX_DESTRUCTOR:
39280b57cec5SDimitry Andric     D = CXXDestructorDecl::CreateDeserialized(Context, ID);
39290b57cec5SDimitry Andric     break;
39300b57cec5SDimitry Andric   case DECL_CXX_CONVERSION:
39310b57cec5SDimitry Andric     D = CXXConversionDecl::CreateDeserialized(Context, ID);
39320b57cec5SDimitry Andric     break;
39330b57cec5SDimitry Andric   case DECL_ACCESS_SPEC:
39340b57cec5SDimitry Andric     D = AccessSpecDecl::CreateDeserialized(Context, ID);
39350b57cec5SDimitry Andric     break;
39360b57cec5SDimitry Andric   case DECL_FRIEND:
39370b57cec5SDimitry Andric     D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
39380b57cec5SDimitry Andric     break;
39390b57cec5SDimitry Andric   case DECL_FRIEND_TEMPLATE:
39400b57cec5SDimitry Andric     D = FriendTemplateDecl::CreateDeserialized(Context, ID);
39410b57cec5SDimitry Andric     break;
39420b57cec5SDimitry Andric   case DECL_CLASS_TEMPLATE:
39430b57cec5SDimitry Andric     D = ClassTemplateDecl::CreateDeserialized(Context, ID);
39440b57cec5SDimitry Andric     break;
39450b57cec5SDimitry Andric   case DECL_CLASS_TEMPLATE_SPECIALIZATION:
39460b57cec5SDimitry Andric     D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
39470b57cec5SDimitry Andric     break;
39480b57cec5SDimitry Andric   case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
39490b57cec5SDimitry Andric     D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
39500b57cec5SDimitry Andric     break;
39510b57cec5SDimitry Andric   case DECL_VAR_TEMPLATE:
39520b57cec5SDimitry Andric     D = VarTemplateDecl::CreateDeserialized(Context, ID);
39530b57cec5SDimitry Andric     break;
39540b57cec5SDimitry Andric   case DECL_VAR_TEMPLATE_SPECIALIZATION:
39550b57cec5SDimitry Andric     D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
39560b57cec5SDimitry Andric     break;
39570b57cec5SDimitry Andric   case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
39580b57cec5SDimitry Andric     D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
39590b57cec5SDimitry Andric     break;
39600b57cec5SDimitry Andric   case DECL_FUNCTION_TEMPLATE:
39610b57cec5SDimitry Andric     D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
39620b57cec5SDimitry Andric     break;
3963480093f4SDimitry Andric   case DECL_TEMPLATE_TYPE_PARM: {
3964480093f4SDimitry Andric     bool HasTypeConstraint = Record.readInt();
3965480093f4SDimitry Andric     D = TemplateTypeParmDecl::CreateDeserialized(Context, ID,
3966480093f4SDimitry Andric                                                  HasTypeConstraint);
39670b57cec5SDimitry Andric     break;
3968480093f4SDimitry Andric   }
396955e4f9d5SDimitry Andric   case DECL_NON_TYPE_TEMPLATE_PARM: {
397055e4f9d5SDimitry Andric     bool HasTypeConstraint = Record.readInt();
39710b57cec5SDimitry Andric     D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
397255e4f9d5SDimitry Andric                                                     HasTypeConstraint);
39730b57cec5SDimitry Andric     break;
397455e4f9d5SDimitry Andric   }
397555e4f9d5SDimitry Andric   case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK: {
397655e4f9d5SDimitry Andric     bool HasTypeConstraint = Record.readInt();
39770fca6ea1SDimitry Andric     D = NonTypeTemplateParmDecl::CreateDeserialized(
39780fca6ea1SDimitry Andric         Context, ID, Record.readInt(), HasTypeConstraint);
397955e4f9d5SDimitry Andric     break;
398055e4f9d5SDimitry Andric   }
39810b57cec5SDimitry Andric   case DECL_TEMPLATE_TEMPLATE_PARM:
39820b57cec5SDimitry Andric     D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
39830b57cec5SDimitry Andric     break;
39840b57cec5SDimitry Andric   case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
39850b57cec5SDimitry Andric     D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
39860b57cec5SDimitry Andric                                                      Record.readInt());
39870b57cec5SDimitry Andric     break;
39880b57cec5SDimitry Andric   case DECL_TYPE_ALIAS_TEMPLATE:
39890b57cec5SDimitry Andric     D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
39900b57cec5SDimitry Andric     break;
39910b57cec5SDimitry Andric   case DECL_CONCEPT:
39920b57cec5SDimitry Andric     D = ConceptDecl::CreateDeserialized(Context, ID);
39930b57cec5SDimitry Andric     break;
399455e4f9d5SDimitry Andric   case DECL_REQUIRES_EXPR_BODY:
399555e4f9d5SDimitry Andric     D = RequiresExprBodyDecl::CreateDeserialized(Context, ID);
399655e4f9d5SDimitry Andric     break;
39970b57cec5SDimitry Andric   case DECL_STATIC_ASSERT:
39980b57cec5SDimitry Andric     D = StaticAssertDecl::CreateDeserialized(Context, ID);
39990b57cec5SDimitry Andric     break;
40000b57cec5SDimitry Andric   case DECL_OBJC_METHOD:
40010b57cec5SDimitry Andric     D = ObjCMethodDecl::CreateDeserialized(Context, ID);
40020b57cec5SDimitry Andric     break;
40030b57cec5SDimitry Andric   case DECL_OBJC_INTERFACE:
40040b57cec5SDimitry Andric     D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
40050b57cec5SDimitry Andric     break;
40060b57cec5SDimitry Andric   case DECL_OBJC_IVAR:
40070b57cec5SDimitry Andric     D = ObjCIvarDecl::CreateDeserialized(Context, ID);
40080b57cec5SDimitry Andric     break;
40090b57cec5SDimitry Andric   case DECL_OBJC_PROTOCOL:
40100b57cec5SDimitry Andric     D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
40110b57cec5SDimitry Andric     break;
40120b57cec5SDimitry Andric   case DECL_OBJC_AT_DEFS_FIELD:
40130b57cec5SDimitry Andric     D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
40140b57cec5SDimitry Andric     break;
40150b57cec5SDimitry Andric   case DECL_OBJC_CATEGORY:
40160b57cec5SDimitry Andric     D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
40170b57cec5SDimitry Andric     break;
40180b57cec5SDimitry Andric   case DECL_OBJC_CATEGORY_IMPL:
40190b57cec5SDimitry Andric     D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
40200b57cec5SDimitry Andric     break;
40210b57cec5SDimitry Andric   case DECL_OBJC_IMPLEMENTATION:
40220b57cec5SDimitry Andric     D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
40230b57cec5SDimitry Andric     break;
40240b57cec5SDimitry Andric   case DECL_OBJC_COMPATIBLE_ALIAS:
40250b57cec5SDimitry Andric     D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
40260b57cec5SDimitry Andric     break;
40270b57cec5SDimitry Andric   case DECL_OBJC_PROPERTY:
40280b57cec5SDimitry Andric     D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
40290b57cec5SDimitry Andric     break;
40300b57cec5SDimitry Andric   case DECL_OBJC_PROPERTY_IMPL:
40310b57cec5SDimitry Andric     D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
40320b57cec5SDimitry Andric     break;
40330b57cec5SDimitry Andric   case DECL_FIELD:
40340b57cec5SDimitry Andric     D = FieldDecl::CreateDeserialized(Context, ID);
40350b57cec5SDimitry Andric     break;
40360b57cec5SDimitry Andric   case DECL_INDIRECTFIELD:
40370b57cec5SDimitry Andric     D = IndirectFieldDecl::CreateDeserialized(Context, ID);
40380b57cec5SDimitry Andric     break;
40390b57cec5SDimitry Andric   case DECL_VAR:
40400b57cec5SDimitry Andric     D = VarDecl::CreateDeserialized(Context, ID);
40410b57cec5SDimitry Andric     break;
40420b57cec5SDimitry Andric   case DECL_IMPLICIT_PARAM:
40430b57cec5SDimitry Andric     D = ImplicitParamDecl::CreateDeserialized(Context, ID);
40440b57cec5SDimitry Andric     break;
40450b57cec5SDimitry Andric   case DECL_PARM_VAR:
40460b57cec5SDimitry Andric     D = ParmVarDecl::CreateDeserialized(Context, ID);
40470b57cec5SDimitry Andric     break;
40480b57cec5SDimitry Andric   case DECL_DECOMPOSITION:
40490b57cec5SDimitry Andric     D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
40500b57cec5SDimitry Andric     break;
40510b57cec5SDimitry Andric   case DECL_BINDING:
40520b57cec5SDimitry Andric     D = BindingDecl::CreateDeserialized(Context, ID);
40530b57cec5SDimitry Andric     break;
40540b57cec5SDimitry Andric   case DECL_FILE_SCOPE_ASM:
40550b57cec5SDimitry Andric     D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
40560b57cec5SDimitry Andric     break;
4057bdd1243dSDimitry Andric   case DECL_TOP_LEVEL_STMT_DECL:
4058bdd1243dSDimitry Andric     D = TopLevelStmtDecl::CreateDeserialized(Context, ID);
4059bdd1243dSDimitry Andric     break;
40600b57cec5SDimitry Andric   case DECL_BLOCK:
40610b57cec5SDimitry Andric     D = BlockDecl::CreateDeserialized(Context, ID);
40620b57cec5SDimitry Andric     break;
40630b57cec5SDimitry Andric   case DECL_MS_PROPERTY:
40640b57cec5SDimitry Andric     D = MSPropertyDecl::CreateDeserialized(Context, ID);
40650b57cec5SDimitry Andric     break;
40665ffd83dbSDimitry Andric   case DECL_MS_GUID:
40675ffd83dbSDimitry Andric     D = MSGuidDecl::CreateDeserialized(Context, ID);
40685ffd83dbSDimitry Andric     break;
406981ad6265SDimitry Andric   case DECL_UNNAMED_GLOBAL_CONSTANT:
407081ad6265SDimitry Andric     D = UnnamedGlobalConstantDecl::CreateDeserialized(Context, ID);
407181ad6265SDimitry Andric     break;
4072e8d8bef9SDimitry Andric   case DECL_TEMPLATE_PARAM_OBJECT:
4073e8d8bef9SDimitry Andric     D = TemplateParamObjectDecl::CreateDeserialized(Context, ID);
4074e8d8bef9SDimitry Andric     break;
40750b57cec5SDimitry Andric   case DECL_CAPTURED:
40760b57cec5SDimitry Andric     D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
40770b57cec5SDimitry Andric     break;
40780b57cec5SDimitry Andric   case DECL_CXX_BASE_SPECIFIERS:
40790b57cec5SDimitry Andric     Error("attempt to read a C++ base-specifier record as a declaration");
40800b57cec5SDimitry Andric     return nullptr;
40810b57cec5SDimitry Andric   case DECL_CXX_CTOR_INITIALIZERS:
40820b57cec5SDimitry Andric     Error("attempt to read a C++ ctor initializer record as a declaration");
40830b57cec5SDimitry Andric     return nullptr;
40840b57cec5SDimitry Andric   case DECL_IMPORT:
40850b57cec5SDimitry Andric     // Note: last entry of the ImportDecl record is the number of stored source
40860b57cec5SDimitry Andric     // locations.
40870b57cec5SDimitry Andric     D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
40880b57cec5SDimitry Andric     break;
4089e8d8bef9SDimitry Andric   case DECL_OMP_THREADPRIVATE: {
4090e8d8bef9SDimitry Andric     Record.skipInts(1);
4091e8d8bef9SDimitry Andric     unsigned NumChildren = Record.readInt();
4092e8d8bef9SDimitry Andric     Record.skipInts(1);
4093e8d8bef9SDimitry Andric     D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, NumChildren);
40940b57cec5SDimitry Andric     break;
4095e8d8bef9SDimitry Andric   }
40960b57cec5SDimitry Andric   case DECL_OMP_ALLOCATE: {
40970b57cec5SDimitry Andric     unsigned NumClauses = Record.readInt();
4098e8d8bef9SDimitry Andric     unsigned NumVars = Record.readInt();
4099e8d8bef9SDimitry Andric     Record.skipInts(1);
41000b57cec5SDimitry Andric     D = OMPAllocateDecl::CreateDeserialized(Context, ID, NumVars, NumClauses);
41010b57cec5SDimitry Andric     break;
41020b57cec5SDimitry Andric   }
4103e8d8bef9SDimitry Andric   case DECL_OMP_REQUIRES: {
4104e8d8bef9SDimitry Andric     unsigned NumClauses = Record.readInt();
4105e8d8bef9SDimitry Andric     Record.skipInts(2);
4106e8d8bef9SDimitry Andric     D = OMPRequiresDecl::CreateDeserialized(Context, ID, NumClauses);
41070b57cec5SDimitry Andric     break;
4108e8d8bef9SDimitry Andric   }
41090b57cec5SDimitry Andric   case DECL_OMP_DECLARE_REDUCTION:
41100b57cec5SDimitry Andric     D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
41110b57cec5SDimitry Andric     break;
4112e8d8bef9SDimitry Andric   case DECL_OMP_DECLARE_MAPPER: {
4113e8d8bef9SDimitry Andric     unsigned NumClauses = Record.readInt();
4114e8d8bef9SDimitry Andric     Record.skipInts(2);
4115e8d8bef9SDimitry Andric     D = OMPDeclareMapperDecl::CreateDeserialized(Context, ID, NumClauses);
41160b57cec5SDimitry Andric     break;
4117e8d8bef9SDimitry Andric   }
41180b57cec5SDimitry Andric   case DECL_OMP_CAPTUREDEXPR:
41190b57cec5SDimitry Andric     D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
41200b57cec5SDimitry Andric     break;
41210b57cec5SDimitry Andric   case DECL_PRAGMA_COMMENT:
41220b57cec5SDimitry Andric     D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
41230b57cec5SDimitry Andric     break;
41240b57cec5SDimitry Andric   case DECL_PRAGMA_DETECT_MISMATCH:
41250b57cec5SDimitry Andric     D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
41260b57cec5SDimitry Andric                                                      Record.readInt());
41270b57cec5SDimitry Andric     break;
41280b57cec5SDimitry Andric   case DECL_EMPTY:
41290b57cec5SDimitry Andric     D = EmptyDecl::CreateDeserialized(Context, ID);
41300b57cec5SDimitry Andric     break;
4131480093f4SDimitry Andric   case DECL_LIFETIME_EXTENDED_TEMPORARY:
4132480093f4SDimitry Andric     D = LifetimeExtendedTemporaryDecl::CreateDeserialized(Context, ID);
4133480093f4SDimitry Andric     break;
41340b57cec5SDimitry Andric   case DECL_OBJC_TYPE_PARAM:
41350b57cec5SDimitry Andric     D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
41360b57cec5SDimitry Andric     break;
4137bdd1243dSDimitry Andric   case DECL_HLSL_BUFFER:
4138bdd1243dSDimitry Andric     D = HLSLBufferDecl::CreateDeserialized(Context, ID);
4139bdd1243dSDimitry Andric     break;
4140bdd1243dSDimitry Andric   case DECL_IMPLICIT_CONCEPT_SPECIALIZATION:
4141bdd1243dSDimitry Andric     D = ImplicitConceptSpecializationDecl::CreateDeserialized(Context, ID,
4142bdd1243dSDimitry Andric                                                               Record.readInt());
4143bdd1243dSDimitry Andric     break;
41440b57cec5SDimitry Andric   }
41450b57cec5SDimitry Andric 
41460b57cec5SDimitry Andric   assert(D && "Unknown declaration reading AST file");
41470fca6ea1SDimitry Andric   LoadedDecl(translateGlobalDeclIDToIndex(ID), D);
41480b57cec5SDimitry Andric   // Set the DeclContext before doing any deserialization, to make sure internal
41490b57cec5SDimitry Andric   // calls to Decl::getASTContext() by Decl's methods will find the
41500b57cec5SDimitry Andric   // TranslationUnitDecl without crashing.
41510b57cec5SDimitry Andric   D->setDeclContext(Context.getTranslationUnitDecl());
41520fca6ea1SDimitry Andric 
41530fca6ea1SDimitry Andric   // Reading some declarations can result in deep recursion.
41540fca6ea1SDimitry Andric   clang::runWithSufficientStackSpace([&] { warnStackExhausted(DeclLoc); },
41550fca6ea1SDimitry Andric                                      [&] { Reader.Visit(D); });
41560b57cec5SDimitry Andric 
41570b57cec5SDimitry Andric   // If this declaration is also a declaration context, get the
41580b57cec5SDimitry Andric   // offsets for its tables of lexical and visible declarations.
41590b57cec5SDimitry Andric   if (auto *DC = dyn_cast<DeclContext>(D)) {
41600b57cec5SDimitry Andric     std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
41610fca6ea1SDimitry Andric 
41620fca6ea1SDimitry Andric     // Get the lexical and visible block for the delayed namespace.
41630fca6ea1SDimitry Andric     // It is sufficient to judge if ID is in DelayedNamespaceOffsetMap.
41640fca6ea1SDimitry Andric     // But it may be more efficient to filter the other cases.
41650fca6ea1SDimitry Andric     if (!Offsets.first && !Offsets.second && isa<NamespaceDecl>(D))
41660fca6ea1SDimitry Andric       if (auto Iter = DelayedNamespaceOffsetMap.find(ID);
41670fca6ea1SDimitry Andric           Iter != DelayedNamespaceOffsetMap.end())
41680fca6ea1SDimitry Andric         Offsets = Iter->second;
41690fca6ea1SDimitry Andric 
41700b57cec5SDimitry Andric     if (Offsets.first &&
41710b57cec5SDimitry Andric         ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
41720b57cec5SDimitry Andric       return nullptr;
41730b57cec5SDimitry Andric     if (Offsets.second &&
41740b57cec5SDimitry Andric         ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
41750b57cec5SDimitry Andric       return nullptr;
41760b57cec5SDimitry Andric   }
41770b57cec5SDimitry Andric   assert(Record.getIdx() == Record.size());
41780b57cec5SDimitry Andric 
41790b57cec5SDimitry Andric   // Load any relevant update records.
41800b57cec5SDimitry Andric   PendingUpdateRecords.push_back(
41810b57cec5SDimitry Andric       PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
41820b57cec5SDimitry Andric 
41830b57cec5SDimitry Andric   // Load the categories after recursive loading is finished.
41840b57cec5SDimitry Andric   if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
41850b57cec5SDimitry Andric     // If we already have a definition when deserializing the ObjCInterfaceDecl,
41860b57cec5SDimitry Andric     // we put the Decl in PendingDefinitions so we can pull the categories here.
41870b57cec5SDimitry Andric     if (Class->isThisDeclarationADefinition() ||
41880b57cec5SDimitry Andric         PendingDefinitions.count(Class))
41890b57cec5SDimitry Andric       loadObjCCategories(ID, Class);
41900b57cec5SDimitry Andric 
41910b57cec5SDimitry Andric   // If we have deserialized a declaration that has a definition the
41920b57cec5SDimitry Andric   // AST consumer might need to know about, queue it.
41930b57cec5SDimitry Andric   // We don't pass it to the consumer immediately because we may be in recursive
41940b57cec5SDimitry Andric   // loading, and some declarations may still be initializing.
41950fca6ea1SDimitry Andric   PotentiallyInterestingDecls.push_back(D);
41960b57cec5SDimitry Andric 
41970b57cec5SDimitry Andric   return D;
41980b57cec5SDimitry Andric }
41990b57cec5SDimitry Andric 
42000b57cec5SDimitry Andric void ASTReader::PassInterestingDeclsToConsumer() {
42010b57cec5SDimitry Andric   assert(Consumer);
42020b57cec5SDimitry Andric 
42030b57cec5SDimitry Andric   if (PassingDeclsToConsumer)
42040b57cec5SDimitry Andric     return;
42050b57cec5SDimitry Andric 
42060b57cec5SDimitry Andric   // Guard variable to avoid recursively redoing the process of passing
42070b57cec5SDimitry Andric   // decls to consumer.
4208bdd1243dSDimitry Andric   SaveAndRestore GuardPassingDeclsToConsumer(PassingDeclsToConsumer, true);
42090b57cec5SDimitry Andric 
42100b57cec5SDimitry Andric   // Ensure that we've loaded all potentially-interesting declarations
42110b57cec5SDimitry Andric   // that need to be eagerly loaded.
42120b57cec5SDimitry Andric   for (auto ID : EagerlyDeserializedDecls)
42130b57cec5SDimitry Andric     GetDecl(ID);
42140b57cec5SDimitry Andric   EagerlyDeserializedDecls.clear();
42150b57cec5SDimitry Andric 
42160fca6ea1SDimitry Andric   auto ConsumingPotentialInterestingDecls = [this]() {
42170b57cec5SDimitry Andric     while (!PotentiallyInterestingDecls.empty()) {
42180fca6ea1SDimitry Andric       Decl *D = PotentiallyInterestingDecls.front();
42190b57cec5SDimitry Andric       PotentiallyInterestingDecls.pop_front();
42200fca6ea1SDimitry Andric       if (isConsumerInterestedIn(D))
42210fca6ea1SDimitry Andric         PassInterestingDeclToConsumer(D);
42220b57cec5SDimitry Andric     }
42230fca6ea1SDimitry Andric   };
42240fca6ea1SDimitry Andric   std::deque<Decl *> MaybeInterestingDecls =
42250fca6ea1SDimitry Andric       std::move(PotentiallyInterestingDecls);
42260fca6ea1SDimitry Andric   PotentiallyInterestingDecls.clear();
42270fca6ea1SDimitry Andric   assert(PotentiallyInterestingDecls.empty());
42280fca6ea1SDimitry Andric   while (!MaybeInterestingDecls.empty()) {
42290fca6ea1SDimitry Andric     Decl *D = MaybeInterestingDecls.front();
42300fca6ea1SDimitry Andric     MaybeInterestingDecls.pop_front();
42310fca6ea1SDimitry Andric     // Since we load the variable's initializers lazily, it'd be problematic
42320fca6ea1SDimitry Andric     // if the initializers dependent on each other. So here we try to load the
42330fca6ea1SDimitry Andric     // initializers of static variables to make sure they are passed to code
42340fca6ea1SDimitry Andric     // generator by order. If we read anything interesting, we would consume
42350fca6ea1SDimitry Andric     // that before emitting the current declaration.
42360fca6ea1SDimitry Andric     if (auto *VD = dyn_cast<VarDecl>(D);
42370fca6ea1SDimitry Andric         VD && VD->isFileVarDecl() && !VD->isExternallyVisible())
42380fca6ea1SDimitry Andric       VD->getInit();
42390fca6ea1SDimitry Andric     ConsumingPotentialInterestingDecls();
42400fca6ea1SDimitry Andric     if (isConsumerInterestedIn(D))
42410fca6ea1SDimitry Andric       PassInterestingDeclToConsumer(D);
42420fca6ea1SDimitry Andric   }
42430fca6ea1SDimitry Andric 
42440fca6ea1SDimitry Andric   // If we add any new potential interesting decl in the last call, consume it.
42450fca6ea1SDimitry Andric   ConsumingPotentialInterestingDecls();
424662987288SDimitry Andric 
424762987288SDimitry Andric   for (GlobalDeclID ID : VTablesToEmit) {
424862987288SDimitry Andric     auto *RD = cast<CXXRecordDecl>(GetDecl(ID));
424962987288SDimitry Andric     assert(!RD->shouldEmitInExternalSource());
425062987288SDimitry Andric     PassVTableToConsumer(RD);
425162987288SDimitry Andric   }
425262987288SDimitry Andric   VTablesToEmit.clear();
42530b57cec5SDimitry Andric }
42540b57cec5SDimitry Andric 
42550b57cec5SDimitry Andric void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
42560b57cec5SDimitry Andric   // The declaration may have been modified by files later in the chain.
42570b57cec5SDimitry Andric   // If this is the case, read the record containing the updates from each file
42580b57cec5SDimitry Andric   // and pass it to ASTDeclReader to make the modifications.
42590fca6ea1SDimitry Andric   GlobalDeclID ID = Record.ID;
42600b57cec5SDimitry Andric   Decl *D = Record.D;
42610b57cec5SDimitry Andric   ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
42620b57cec5SDimitry Andric   DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
42630b57cec5SDimitry Andric 
42640fca6ea1SDimitry Andric   SmallVector<GlobalDeclID, 8> PendingLazySpecializationIDs;
42650b57cec5SDimitry Andric 
42660b57cec5SDimitry Andric   if (UpdI != DeclUpdateOffsets.end()) {
42670b57cec5SDimitry Andric     auto UpdateOffsets = std::move(UpdI->second);
42680b57cec5SDimitry Andric     DeclUpdateOffsets.erase(UpdI);
42690b57cec5SDimitry Andric 
42700b57cec5SDimitry Andric     // Check if this decl was interesting to the consumer. If we just loaded
42710b57cec5SDimitry Andric     // the declaration, then we know it was interesting and we skip the call
42720b57cec5SDimitry Andric     // to isConsumerInterestedIn because it is unsafe to call in the
42730b57cec5SDimitry Andric     // current ASTReader state.
42740fca6ea1SDimitry Andric     bool WasInteresting = Record.JustLoaded || isConsumerInterestedIn(D);
42750b57cec5SDimitry Andric     for (auto &FileAndOffset : UpdateOffsets) {
42760b57cec5SDimitry Andric       ModuleFile *F = FileAndOffset.first;
42770b57cec5SDimitry Andric       uint64_t Offset = FileAndOffset.second;
42780b57cec5SDimitry Andric       llvm::BitstreamCursor &Cursor = F->DeclsCursor;
42790b57cec5SDimitry Andric       SavedStreamPosition SavedPosition(Cursor);
42800b57cec5SDimitry Andric       if (llvm::Error JumpFailed = Cursor.JumpToBit(Offset))
42810b57cec5SDimitry Andric         // FIXME don't do a fatal error.
42820b57cec5SDimitry Andric         llvm::report_fatal_error(
4283349cc55cSDimitry Andric             Twine("ASTReader::loadDeclUpdateRecords failed jumping: ") +
42840b57cec5SDimitry Andric             toString(std::move(JumpFailed)));
42850b57cec5SDimitry Andric       Expected<unsigned> MaybeCode = Cursor.ReadCode();
42860b57cec5SDimitry Andric       if (!MaybeCode)
42870b57cec5SDimitry Andric         llvm::report_fatal_error(
4288349cc55cSDimitry Andric             Twine("ASTReader::loadDeclUpdateRecords failed reading code: ") +
42890b57cec5SDimitry Andric             toString(MaybeCode.takeError()));
42900b57cec5SDimitry Andric       unsigned Code = MaybeCode.get();
42910b57cec5SDimitry Andric       ASTRecordReader Record(*this, *F);
42920b57cec5SDimitry Andric       if (Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code))
42930b57cec5SDimitry Andric         assert(MaybeRecCode.get() == DECL_UPDATES &&
42940b57cec5SDimitry Andric                "Expected DECL_UPDATES record!");
42950b57cec5SDimitry Andric       else
42960b57cec5SDimitry Andric         llvm::report_fatal_error(
4297349cc55cSDimitry Andric             Twine("ASTReader::loadDeclUpdateRecords failed reading rec code: ") +
42980b57cec5SDimitry Andric             toString(MaybeCode.takeError()));
42990b57cec5SDimitry Andric 
43000b57cec5SDimitry Andric       ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
43010b57cec5SDimitry Andric                            SourceLocation());
43020b57cec5SDimitry Andric       Reader.UpdateDecl(D, PendingLazySpecializationIDs);
43030b57cec5SDimitry Andric 
43040b57cec5SDimitry Andric       // We might have made this declaration interesting. If so, remember that
43050b57cec5SDimitry Andric       // we need to hand it off to the consumer.
43060fca6ea1SDimitry Andric       if (!WasInteresting && isConsumerInterestedIn(D)) {
43070fca6ea1SDimitry Andric         PotentiallyInterestingDecls.push_back(D);
43080b57cec5SDimitry Andric         WasInteresting = true;
43090b57cec5SDimitry Andric       }
43100b57cec5SDimitry Andric     }
43110b57cec5SDimitry Andric   }
43120b57cec5SDimitry Andric   // Add the lazy specializations to the template.
43130b57cec5SDimitry Andric   assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
4314bdd1243dSDimitry Andric           isa<FunctionTemplateDecl, VarTemplateDecl>(D)) &&
43150b57cec5SDimitry Andric          "Must not have pending specializations");
43160b57cec5SDimitry Andric   if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
43170b57cec5SDimitry Andric     ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
43180b57cec5SDimitry Andric   else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
43190b57cec5SDimitry Andric     ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
43200b57cec5SDimitry Andric   else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
43210b57cec5SDimitry Andric     ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
43220b57cec5SDimitry Andric   PendingLazySpecializationIDs.clear();
43230b57cec5SDimitry Andric 
43240b57cec5SDimitry Andric   // Load the pending visible updates for this decl context, if it has any.
43250b57cec5SDimitry Andric   auto I = PendingVisibleUpdates.find(ID);
43260b57cec5SDimitry Andric   if (I != PendingVisibleUpdates.end()) {
43270b57cec5SDimitry Andric     auto VisibleUpdates = std::move(I->second);
43280b57cec5SDimitry Andric     PendingVisibleUpdates.erase(I);
43290b57cec5SDimitry Andric 
43300b57cec5SDimitry Andric     auto *DC = cast<DeclContext>(D)->getPrimaryContext();
43310b57cec5SDimitry Andric     for (const auto &Update : VisibleUpdates)
43320b57cec5SDimitry Andric       Lookups[DC].Table.add(
43330b57cec5SDimitry Andric           Update.Mod, Update.Data,
43340b57cec5SDimitry Andric           reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
43350b57cec5SDimitry Andric     DC->setHasExternalVisibleStorage(true);
43360b57cec5SDimitry Andric   }
43370b57cec5SDimitry Andric }
43380b57cec5SDimitry Andric 
43390b57cec5SDimitry Andric void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
43400b57cec5SDimitry Andric   // Attach FirstLocal to the end of the decl chain.
43410b57cec5SDimitry Andric   Decl *CanonDecl = FirstLocal->getCanonicalDecl();
43420b57cec5SDimitry Andric   if (FirstLocal != CanonDecl) {
43430b57cec5SDimitry Andric     Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
43440b57cec5SDimitry Andric     ASTDeclReader::attachPreviousDecl(
43450b57cec5SDimitry Andric         *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
43460b57cec5SDimitry Andric         CanonDecl);
43470b57cec5SDimitry Andric   }
43480b57cec5SDimitry Andric 
43490b57cec5SDimitry Andric   if (!LocalOffset) {
43500b57cec5SDimitry Andric     ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
43510b57cec5SDimitry Andric     return;
43520b57cec5SDimitry Andric   }
43530b57cec5SDimitry Andric 
43540b57cec5SDimitry Andric   // Load the list of other redeclarations from this module file.
43550b57cec5SDimitry Andric   ModuleFile *M = getOwningModuleFile(FirstLocal);
43560b57cec5SDimitry Andric   assert(M && "imported decl from no module file");
43570b57cec5SDimitry Andric 
43580b57cec5SDimitry Andric   llvm::BitstreamCursor &Cursor = M->DeclsCursor;
43590b57cec5SDimitry Andric   SavedStreamPosition SavedPosition(Cursor);
43600b57cec5SDimitry Andric   if (llvm::Error JumpFailed = Cursor.JumpToBit(LocalOffset))
43610b57cec5SDimitry Andric     llvm::report_fatal_error(
4362349cc55cSDimitry Andric         Twine("ASTReader::loadPendingDeclChain failed jumping: ") +
43630b57cec5SDimitry Andric         toString(std::move(JumpFailed)));
43640b57cec5SDimitry Andric 
43650b57cec5SDimitry Andric   RecordData Record;
43660b57cec5SDimitry Andric   Expected<unsigned> MaybeCode = Cursor.ReadCode();
43670b57cec5SDimitry Andric   if (!MaybeCode)
43680b57cec5SDimitry Andric     llvm::report_fatal_error(
4369349cc55cSDimitry Andric         Twine("ASTReader::loadPendingDeclChain failed reading code: ") +
43700b57cec5SDimitry Andric         toString(MaybeCode.takeError()));
43710b57cec5SDimitry Andric   unsigned Code = MaybeCode.get();
43720b57cec5SDimitry Andric   if (Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record))
43730b57cec5SDimitry Andric     assert(MaybeRecCode.get() == LOCAL_REDECLARATIONS &&
43740b57cec5SDimitry Andric            "expected LOCAL_REDECLARATIONS record!");
43750b57cec5SDimitry Andric   else
43760b57cec5SDimitry Andric     llvm::report_fatal_error(
4377349cc55cSDimitry Andric         Twine("ASTReader::loadPendingDeclChain failed reading rec code: ") +
43780b57cec5SDimitry Andric         toString(MaybeCode.takeError()));
43790b57cec5SDimitry Andric 
43800b57cec5SDimitry Andric   // FIXME: We have several different dispatches on decl kind here; maybe
43810b57cec5SDimitry Andric   // we should instead generate one loop per kind and dispatch up-front?
43820b57cec5SDimitry Andric   Decl *MostRecent = FirstLocal;
43830b57cec5SDimitry Andric   for (unsigned I = 0, N = Record.size(); I != N; ++I) {
43840fca6ea1SDimitry Andric     unsigned Idx = N - I - 1;
43850fca6ea1SDimitry Andric     auto *D = ReadDecl(*M, Record, Idx);
43860b57cec5SDimitry Andric     ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
43870b57cec5SDimitry Andric     MostRecent = D;
43880b57cec5SDimitry Andric   }
43890b57cec5SDimitry Andric   ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
43900b57cec5SDimitry Andric }
43910b57cec5SDimitry Andric 
43920b57cec5SDimitry Andric namespace {
43930b57cec5SDimitry Andric 
43940b57cec5SDimitry Andric   /// Given an ObjC interface, goes through the modules and links to the
43950b57cec5SDimitry Andric   /// interface all the categories for it.
43960b57cec5SDimitry Andric   class ObjCCategoriesVisitor {
43970b57cec5SDimitry Andric     ASTReader &Reader;
43980b57cec5SDimitry Andric     ObjCInterfaceDecl *Interface;
43990b57cec5SDimitry Andric     llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
44000b57cec5SDimitry Andric     ObjCCategoryDecl *Tail = nullptr;
44010b57cec5SDimitry Andric     llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
44020fca6ea1SDimitry Andric     GlobalDeclID InterfaceID;
44030b57cec5SDimitry Andric     unsigned PreviousGeneration;
44040b57cec5SDimitry Andric 
44050b57cec5SDimitry Andric     void add(ObjCCategoryDecl *Cat) {
44060b57cec5SDimitry Andric       // Only process each category once.
44070b57cec5SDimitry Andric       if (!Deserialized.erase(Cat))
44080b57cec5SDimitry Andric         return;
44090b57cec5SDimitry Andric 
44100b57cec5SDimitry Andric       // Check for duplicate categories.
44110b57cec5SDimitry Andric       if (Cat->getDeclName()) {
44120b57cec5SDimitry Andric         ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
441306c3fb27SDimitry Andric         if (Existing && Reader.getOwningModuleFile(Existing) !=
441406c3fb27SDimitry Andric                             Reader.getOwningModuleFile(Cat)) {
441506c3fb27SDimitry Andric           llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;
441606c3fb27SDimitry Andric           StructuralEquivalenceContext Ctx(
441706c3fb27SDimitry Andric               Cat->getASTContext(), Existing->getASTContext(),
441806c3fb27SDimitry Andric               NonEquivalentDecls, StructuralEquivalenceKind::Default,
441906c3fb27SDimitry Andric               /*StrictTypeSpelling =*/false,
442006c3fb27SDimitry Andric               /*Complain =*/false,
442106c3fb27SDimitry Andric               /*ErrorOnTagTypeMismatch =*/true);
442206c3fb27SDimitry Andric           if (!Ctx.IsEquivalent(Cat, Existing)) {
442306c3fb27SDimitry Andric             // Warn only if the categories with the same name are different.
44240b57cec5SDimitry Andric             Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
44250b57cec5SDimitry Andric                 << Interface->getDeclName() << Cat->getDeclName();
442606c3fb27SDimitry Andric             Reader.Diag(Existing->getLocation(),
442706c3fb27SDimitry Andric                         diag::note_previous_definition);
442806c3fb27SDimitry Andric           }
44290b57cec5SDimitry Andric         } else if (!Existing) {
44300b57cec5SDimitry Andric           // Record this category.
44310b57cec5SDimitry Andric           Existing = Cat;
44320b57cec5SDimitry Andric         }
44330b57cec5SDimitry Andric       }
44340b57cec5SDimitry Andric 
44350b57cec5SDimitry Andric       // Add this category to the end of the chain.
44360b57cec5SDimitry Andric       if (Tail)
44370b57cec5SDimitry Andric         ASTDeclReader::setNextObjCCategory(Tail, Cat);
44380b57cec5SDimitry Andric       else
44390b57cec5SDimitry Andric         Interface->setCategoryListRaw(Cat);
44400b57cec5SDimitry Andric       Tail = Cat;
44410b57cec5SDimitry Andric     }
44420b57cec5SDimitry Andric 
44430b57cec5SDimitry Andric   public:
44440fca6ea1SDimitry Andric     ObjCCategoriesVisitor(
44450fca6ea1SDimitry Andric         ASTReader &Reader, ObjCInterfaceDecl *Interface,
44460b57cec5SDimitry Andric         llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
44470fca6ea1SDimitry Andric         GlobalDeclID InterfaceID, unsigned PreviousGeneration)
44480b57cec5SDimitry Andric         : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
44490b57cec5SDimitry Andric           InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
44500b57cec5SDimitry Andric       // Populate the name -> category map with the set of known categories.
44510b57cec5SDimitry Andric       for (auto *Cat : Interface->known_categories()) {
44520b57cec5SDimitry Andric         if (Cat->getDeclName())
44530b57cec5SDimitry Andric           NameCategoryMap[Cat->getDeclName()] = Cat;
44540b57cec5SDimitry Andric 
44550b57cec5SDimitry Andric         // Keep track of the tail of the category list.
44560b57cec5SDimitry Andric         Tail = Cat;
44570b57cec5SDimitry Andric       }
44580b57cec5SDimitry Andric     }
44590b57cec5SDimitry Andric 
44600b57cec5SDimitry Andric     bool operator()(ModuleFile &M) {
44610b57cec5SDimitry Andric       // If we've loaded all of the category information we care about from
44620b57cec5SDimitry Andric       // this module file, we're done.
44630b57cec5SDimitry Andric       if (M.Generation <= PreviousGeneration)
44640b57cec5SDimitry Andric         return true;
44650b57cec5SDimitry Andric 
44660b57cec5SDimitry Andric       // Map global ID of the definition down to the local ID used in this
44670b57cec5SDimitry Andric       // module file. If there is no such mapping, we'll find nothing here
44680b57cec5SDimitry Andric       // (or in any module it imports).
44690fca6ea1SDimitry Andric       LocalDeclID LocalID =
44700fca6ea1SDimitry Andric           Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
44710fca6ea1SDimitry Andric       if (LocalID.isInvalid())
44720b57cec5SDimitry Andric         return true;
44730b57cec5SDimitry Andric 
44740b57cec5SDimitry Andric       // Perform a binary search to find the local redeclarations for this
44750b57cec5SDimitry Andric       // declaration (if any).
44760b57cec5SDimitry Andric       const ObjCCategoriesInfo Compare = { LocalID, 0 };
44770b57cec5SDimitry Andric       const ObjCCategoriesInfo *Result
44780b57cec5SDimitry Andric         = std::lower_bound(M.ObjCCategoriesMap,
44790b57cec5SDimitry Andric                            M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
44800b57cec5SDimitry Andric                            Compare);
44810b57cec5SDimitry Andric       if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
44820fca6ea1SDimitry Andric           LocalID != Result->getDefinitionID()) {
44830b57cec5SDimitry Andric         // We didn't find anything. If the class definition is in this module
44840b57cec5SDimitry Andric         // file, then the module files it depends on cannot have any categories,
44850b57cec5SDimitry Andric         // so suppress further lookup.
44860b57cec5SDimitry Andric         return Reader.isDeclIDFromModule(InterfaceID, M);
44870b57cec5SDimitry Andric       }
44880b57cec5SDimitry Andric 
44890b57cec5SDimitry Andric       // We found something. Dig out all of the categories.
44900b57cec5SDimitry Andric       unsigned Offset = Result->Offset;
44910b57cec5SDimitry Andric       unsigned N = M.ObjCCategories[Offset];
44920b57cec5SDimitry Andric       M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
44930b57cec5SDimitry Andric       for (unsigned I = 0; I != N; ++I)
44940fca6ea1SDimitry Andric         add(Reader.ReadDeclAs<ObjCCategoryDecl>(M, M.ObjCCategories, Offset));
44950b57cec5SDimitry Andric       return true;
44960b57cec5SDimitry Andric     }
44970b57cec5SDimitry Andric   };
44980b57cec5SDimitry Andric 
44990b57cec5SDimitry Andric } // namespace
45000b57cec5SDimitry Andric 
45010fca6ea1SDimitry Andric void ASTReader::loadObjCCategories(GlobalDeclID ID, ObjCInterfaceDecl *D,
45020b57cec5SDimitry Andric                                    unsigned PreviousGeneration) {
45030b57cec5SDimitry Andric   ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
45040b57cec5SDimitry Andric                                 PreviousGeneration);
45050b57cec5SDimitry Andric   ModuleMgr.visit(Visitor);
45060b57cec5SDimitry Andric }
45070b57cec5SDimitry Andric 
45080b57cec5SDimitry Andric template<typename DeclT, typename Fn>
45090b57cec5SDimitry Andric static void forAllLaterRedecls(DeclT *D, Fn F) {
45100b57cec5SDimitry Andric   F(D);
45110b57cec5SDimitry Andric 
45120b57cec5SDimitry Andric   // Check whether we've already merged D into its redeclaration chain.
45130b57cec5SDimitry Andric   // MostRecent may or may not be nullptr if D has not been merged. If
45140b57cec5SDimitry Andric   // not, walk the merged redecl chain and see if it's there.
45150b57cec5SDimitry Andric   auto *MostRecent = D->getMostRecentDecl();
45160b57cec5SDimitry Andric   bool Found = false;
45170b57cec5SDimitry Andric   for (auto *Redecl = MostRecent; Redecl && !Found;
45180b57cec5SDimitry Andric        Redecl = Redecl->getPreviousDecl())
45190b57cec5SDimitry Andric     Found = (Redecl == D);
45200b57cec5SDimitry Andric 
45210b57cec5SDimitry Andric   // If this declaration is merged, apply the functor to all later decls.
45220b57cec5SDimitry Andric   if (Found) {
45230b57cec5SDimitry Andric     for (auto *Redecl = MostRecent; Redecl != D;
45240b57cec5SDimitry Andric          Redecl = Redecl->getPreviousDecl())
45250b57cec5SDimitry Andric       F(Redecl);
45260b57cec5SDimitry Andric   }
45270b57cec5SDimitry Andric }
45280b57cec5SDimitry Andric 
45290fca6ea1SDimitry Andric void ASTDeclReader::UpdateDecl(
45300fca6ea1SDimitry Andric     Decl *D,
45310fca6ea1SDimitry Andric     llvm::SmallVectorImpl<GlobalDeclID> &PendingLazySpecializationIDs) {
45320b57cec5SDimitry Andric   while (Record.getIdx() < Record.size()) {
45330b57cec5SDimitry Andric     switch ((DeclUpdateKind)Record.readInt()) {
45340b57cec5SDimitry Andric     case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
45350b57cec5SDimitry Andric       auto *RD = cast<CXXRecordDecl>(D);
45360b57cec5SDimitry Andric       Decl *MD = Record.readDecl();
45370b57cec5SDimitry Andric       assert(MD && "couldn't read decl from update record");
453806c3fb27SDimitry Andric       Reader.PendingAddedClassMembers.push_back({RD, MD});
45390b57cec5SDimitry Andric       break;
45400b57cec5SDimitry Andric     }
45410b57cec5SDimitry Andric 
45420b57cec5SDimitry Andric     case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
45430b57cec5SDimitry Andric       // It will be added to the template's lazy specialization set.
4544480093f4SDimitry Andric       PendingLazySpecializationIDs.push_back(readDeclID());
45450b57cec5SDimitry Andric       break;
45460b57cec5SDimitry Andric 
45470b57cec5SDimitry Andric     case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
4548480093f4SDimitry Andric       auto *Anon = readDeclAs<NamespaceDecl>();
45490b57cec5SDimitry Andric 
45500b57cec5SDimitry Andric       // Each module has its own anonymous namespace, which is disjoint from
45510b57cec5SDimitry Andric       // any other module's anonymous namespaces, so don't attach the anonymous
45520b57cec5SDimitry Andric       // namespace at all.
45530b57cec5SDimitry Andric       if (!Record.isModule()) {
45540b57cec5SDimitry Andric         if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
45550b57cec5SDimitry Andric           TU->setAnonymousNamespace(Anon);
45560b57cec5SDimitry Andric         else
45570b57cec5SDimitry Andric           cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
45580b57cec5SDimitry Andric       }
45590b57cec5SDimitry Andric       break;
45600b57cec5SDimitry Andric     }
45610b57cec5SDimitry Andric 
45620b57cec5SDimitry Andric     case UPD_CXX_ADDED_VAR_DEFINITION: {
45630b57cec5SDimitry Andric       auto *VD = cast<VarDecl>(D);
45640b57cec5SDimitry Andric       VD->NonParmVarDeclBits.IsInline = Record.readInt();
45650b57cec5SDimitry Andric       VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
456606c3fb27SDimitry Andric       ReadVarDeclInit(VD);
45670b57cec5SDimitry Andric       break;
45680b57cec5SDimitry Andric     }
45690b57cec5SDimitry Andric 
45700b57cec5SDimitry Andric     case UPD_CXX_POINT_OF_INSTANTIATION: {
45710b57cec5SDimitry Andric       SourceLocation POI = Record.readSourceLocation();
45720b57cec5SDimitry Andric       if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
45730b57cec5SDimitry Andric         VTSD->setPointOfInstantiation(POI);
45740b57cec5SDimitry Andric       } else if (auto *VD = dyn_cast<VarDecl>(D)) {
45755f757f3fSDimitry Andric         MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo();
45765f757f3fSDimitry Andric         assert(MSInfo && "No member specialization information");
45775f757f3fSDimitry Andric         MSInfo->setPointOfInstantiation(POI);
45780b57cec5SDimitry Andric       } else {
45790b57cec5SDimitry Andric         auto *FD = cast<FunctionDecl>(D);
45800b57cec5SDimitry Andric         if (auto *FTSInfo = FD->TemplateOrSpecialization
45810b57cec5SDimitry Andric                     .dyn_cast<FunctionTemplateSpecializationInfo *>())
45820b57cec5SDimitry Andric           FTSInfo->setPointOfInstantiation(POI);
45830b57cec5SDimitry Andric         else
45840b57cec5SDimitry Andric           FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
45850b57cec5SDimitry Andric               ->setPointOfInstantiation(POI);
45860b57cec5SDimitry Andric       }
45870b57cec5SDimitry Andric       break;
45880b57cec5SDimitry Andric     }
45890b57cec5SDimitry Andric 
45900b57cec5SDimitry Andric     case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
45910b57cec5SDimitry Andric       auto *Param = cast<ParmVarDecl>(D);
45920b57cec5SDimitry Andric 
45930b57cec5SDimitry Andric       // We have to read the default argument regardless of whether we use it
45940b57cec5SDimitry Andric       // so that hypothetical further update records aren't messed up.
45950b57cec5SDimitry Andric       // TODO: Add a function to skip over the next expr record.
45960b57cec5SDimitry Andric       auto *DefaultArg = Record.readExpr();
45970b57cec5SDimitry Andric 
45980b57cec5SDimitry Andric       // Only apply the update if the parameter still has an uninstantiated
45990b57cec5SDimitry Andric       // default argument.
46000b57cec5SDimitry Andric       if (Param->hasUninstantiatedDefaultArg())
46010b57cec5SDimitry Andric         Param->setDefaultArg(DefaultArg);
46020b57cec5SDimitry Andric       break;
46030b57cec5SDimitry Andric     }
46040b57cec5SDimitry Andric 
46050b57cec5SDimitry Andric     case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
46060b57cec5SDimitry Andric       auto *FD = cast<FieldDecl>(D);
46070b57cec5SDimitry Andric       auto *DefaultInit = Record.readExpr();
46080b57cec5SDimitry Andric 
46090b57cec5SDimitry Andric       // Only apply the update if the field still has an uninstantiated
46100b57cec5SDimitry Andric       // default member initializer.
461106c3fb27SDimitry Andric       if (FD->hasInClassInitializer() && !FD->hasNonNullInClassInitializer()) {
46120b57cec5SDimitry Andric         if (DefaultInit)
46130b57cec5SDimitry Andric           FD->setInClassInitializer(DefaultInit);
46140b57cec5SDimitry Andric         else
46150b57cec5SDimitry Andric           // Instantiation failed. We can get here if we serialized an AST for
46160b57cec5SDimitry Andric           // an invalid program.
46170b57cec5SDimitry Andric           FD->removeInClassInitializer();
46180b57cec5SDimitry Andric       }
46190b57cec5SDimitry Andric       break;
46200b57cec5SDimitry Andric     }
46210b57cec5SDimitry Andric 
46220b57cec5SDimitry Andric     case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
46230b57cec5SDimitry Andric       auto *FD = cast<FunctionDecl>(D);
46240b57cec5SDimitry Andric       if (Reader.PendingBodies[FD]) {
46250b57cec5SDimitry Andric         // FIXME: Maybe check for ODR violations.
46260b57cec5SDimitry Andric         // It's safe to stop now because this update record is always last.
46270b57cec5SDimitry Andric         return;
46280b57cec5SDimitry Andric       }
46290b57cec5SDimitry Andric 
46300b57cec5SDimitry Andric       if (Record.readInt()) {
46310b57cec5SDimitry Andric         // Maintain AST consistency: any later redeclarations of this function
46320b57cec5SDimitry Andric         // are inline if this one is. (We might have merged another declaration
46330b57cec5SDimitry Andric         // into this one.)
46340b57cec5SDimitry Andric         forAllLaterRedecls(FD, [](FunctionDecl *FD) {
46350b57cec5SDimitry Andric           FD->setImplicitlyInline();
46360b57cec5SDimitry Andric         });
46370b57cec5SDimitry Andric       }
4638480093f4SDimitry Andric       FD->setInnerLocStart(readSourceLocation());
46390b57cec5SDimitry Andric       ReadFunctionDefinition(FD);
46400b57cec5SDimitry Andric       assert(Record.getIdx() == Record.size() && "lazy body must be last");
46410b57cec5SDimitry Andric       break;
46420b57cec5SDimitry Andric     }
46430b57cec5SDimitry Andric 
46440b57cec5SDimitry Andric     case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
46450b57cec5SDimitry Andric       auto *RD = cast<CXXRecordDecl>(D);
46460b57cec5SDimitry Andric       auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
46470b57cec5SDimitry Andric       bool HadRealDefinition =
46480b57cec5SDimitry Andric           OldDD && (OldDD->Definition != RD ||
46490b57cec5SDimitry Andric                     !Reader.PendingFakeDefinitionData.count(OldDD));
46500b57cec5SDimitry Andric       RD->setParamDestroyedInCallee(Record.readInt());
46510b57cec5SDimitry Andric       RD->setArgPassingRestrictions(
46525f757f3fSDimitry Andric           static_cast<RecordArgPassingKind>(Record.readInt()));
46530b57cec5SDimitry Andric       ReadCXXRecordDefinition(RD, /*Update*/true);
46540b57cec5SDimitry Andric 
46550b57cec5SDimitry Andric       // Visible update is handled separately.
46560b57cec5SDimitry Andric       uint64_t LexicalOffset = ReadLocalOffset();
46570b57cec5SDimitry Andric       if (!HadRealDefinition && LexicalOffset) {
46580b57cec5SDimitry Andric         Record.readLexicalDeclContextStorage(LexicalOffset, RD);
46590b57cec5SDimitry Andric         Reader.PendingFakeDefinitionData.erase(OldDD);
46600b57cec5SDimitry Andric       }
46610b57cec5SDimitry Andric 
46620b57cec5SDimitry Andric       auto TSK = (TemplateSpecializationKind)Record.readInt();
4663480093f4SDimitry Andric       SourceLocation POI = readSourceLocation();
46640b57cec5SDimitry Andric       if (MemberSpecializationInfo *MSInfo =
46650b57cec5SDimitry Andric               RD->getMemberSpecializationInfo()) {
46660b57cec5SDimitry Andric         MSInfo->setTemplateSpecializationKind(TSK);
46670b57cec5SDimitry Andric         MSInfo->setPointOfInstantiation(POI);
46680b57cec5SDimitry Andric       } else {
46690b57cec5SDimitry Andric         auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
46700b57cec5SDimitry Andric         Spec->setTemplateSpecializationKind(TSK);
46710b57cec5SDimitry Andric         Spec->setPointOfInstantiation(POI);
46720b57cec5SDimitry Andric 
46730b57cec5SDimitry Andric         if (Record.readInt()) {
46740b57cec5SDimitry Andric           auto *PartialSpec =
4675480093f4SDimitry Andric               readDeclAs<ClassTemplatePartialSpecializationDecl>();
46760b57cec5SDimitry Andric           SmallVector<TemplateArgument, 8> TemplArgs;
46770b57cec5SDimitry Andric           Record.readTemplateArgumentList(TemplArgs);
46780b57cec5SDimitry Andric           auto *TemplArgList = TemplateArgumentList::CreateCopy(
46790b57cec5SDimitry Andric               Reader.getContext(), TemplArgs);
46800b57cec5SDimitry Andric 
46810b57cec5SDimitry Andric           // FIXME: If we already have a partial specialization set,
46820b57cec5SDimitry Andric           // check that it matches.
46830b57cec5SDimitry Andric           if (!Spec->getSpecializedTemplateOrPartial()
46840b57cec5SDimitry Andric                    .is<ClassTemplatePartialSpecializationDecl *>())
46850b57cec5SDimitry Andric             Spec->setInstantiationOf(PartialSpec, TemplArgList);
46860b57cec5SDimitry Andric         }
46870b57cec5SDimitry Andric       }
46880b57cec5SDimitry Andric 
46895f757f3fSDimitry Andric       RD->setTagKind(static_cast<TagTypeKind>(Record.readInt()));
4690480093f4SDimitry Andric       RD->setLocation(readSourceLocation());
4691480093f4SDimitry Andric       RD->setLocStart(readSourceLocation());
4692480093f4SDimitry Andric       RD->setBraceRange(readSourceRange());
46930b57cec5SDimitry Andric 
46940b57cec5SDimitry Andric       if (Record.readInt()) {
46950b57cec5SDimitry Andric         AttrVec Attrs;
46960b57cec5SDimitry Andric         Record.readAttributes(Attrs);
46970b57cec5SDimitry Andric         // If the declaration already has attributes, we assume that some other
46980b57cec5SDimitry Andric         // AST file already loaded them.
46990b57cec5SDimitry Andric         if (!D->hasAttrs())
47000b57cec5SDimitry Andric           D->setAttrsImpl(Attrs, Reader.getContext());
47010b57cec5SDimitry Andric       }
47020b57cec5SDimitry Andric       break;
47030b57cec5SDimitry Andric     }
47040b57cec5SDimitry Andric 
47050b57cec5SDimitry Andric     case UPD_CXX_RESOLVED_DTOR_DELETE: {
47060b57cec5SDimitry Andric       // Set the 'operator delete' directly to avoid emitting another update
47070b57cec5SDimitry Andric       // record.
4708480093f4SDimitry Andric       auto *Del = readDeclAs<FunctionDecl>();
47090b57cec5SDimitry Andric       auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
47100b57cec5SDimitry Andric       auto *ThisArg = Record.readExpr();
47110b57cec5SDimitry Andric       // FIXME: Check consistency if we have an old and new operator delete.
47120b57cec5SDimitry Andric       if (!First->OperatorDelete) {
47130b57cec5SDimitry Andric         First->OperatorDelete = Del;
47140b57cec5SDimitry Andric         First->OperatorDeleteThisArg = ThisArg;
47150b57cec5SDimitry Andric       }
47160b57cec5SDimitry Andric       break;
47170b57cec5SDimitry Andric     }
47180b57cec5SDimitry Andric 
47190b57cec5SDimitry Andric     case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
47200b57cec5SDimitry Andric       SmallVector<QualType, 8> ExceptionStorage;
4721480093f4SDimitry Andric       auto ESI = Record.readExceptionSpecInfo(ExceptionStorage);
47220b57cec5SDimitry Andric 
47230b57cec5SDimitry Andric       // Update this declaration's exception specification, if needed.
47240b57cec5SDimitry Andric       auto *FD = cast<FunctionDecl>(D);
47250b57cec5SDimitry Andric       auto *FPT = FD->getType()->castAs<FunctionProtoType>();
47260b57cec5SDimitry Andric       // FIXME: If the exception specification is already present, check that it
47270b57cec5SDimitry Andric       // matches.
47280b57cec5SDimitry Andric       if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
47290b57cec5SDimitry Andric         FD->setType(Reader.getContext().getFunctionType(
47300b57cec5SDimitry Andric             FPT->getReturnType(), FPT->getParamTypes(),
47310b57cec5SDimitry Andric             FPT->getExtProtoInfo().withExceptionSpec(ESI)));
47320b57cec5SDimitry Andric 
47330b57cec5SDimitry Andric         // When we get to the end of deserializing, see if there are other decls
47340b57cec5SDimitry Andric         // that we need to propagate this exception specification onto.
47350b57cec5SDimitry Andric         Reader.PendingExceptionSpecUpdates.insert(
47360b57cec5SDimitry Andric             std::make_pair(FD->getCanonicalDecl(), FD));
47370b57cec5SDimitry Andric       }
47380b57cec5SDimitry Andric       break;
47390b57cec5SDimitry Andric     }
47400b57cec5SDimitry Andric 
47410b57cec5SDimitry Andric     case UPD_CXX_DEDUCED_RETURN_TYPE: {
47420b57cec5SDimitry Andric       auto *FD = cast<FunctionDecl>(D);
47430b57cec5SDimitry Andric       QualType DeducedResultType = Record.readType();
47440b57cec5SDimitry Andric       Reader.PendingDeducedTypeUpdates.insert(
47450b57cec5SDimitry Andric           {FD->getCanonicalDecl(), DeducedResultType});
47460b57cec5SDimitry Andric       break;
47470b57cec5SDimitry Andric     }
47480b57cec5SDimitry Andric 
47490b57cec5SDimitry Andric     case UPD_DECL_MARKED_USED:
47500b57cec5SDimitry Andric       // Maintain AST consistency: any later redeclarations are used too.
47510b57cec5SDimitry Andric       D->markUsed(Reader.getContext());
47520b57cec5SDimitry Andric       break;
47530b57cec5SDimitry Andric 
47540b57cec5SDimitry Andric     case UPD_MANGLING_NUMBER:
47550b57cec5SDimitry Andric       Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
47560b57cec5SDimitry Andric                                             Record.readInt());
47570b57cec5SDimitry Andric       break;
47580b57cec5SDimitry Andric 
47590b57cec5SDimitry Andric     case UPD_STATIC_LOCAL_NUMBER:
47600b57cec5SDimitry Andric       Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
47610b57cec5SDimitry Andric                                                Record.readInt());
47620b57cec5SDimitry Andric       break;
47630b57cec5SDimitry Andric 
47640b57cec5SDimitry Andric     case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
476506c3fb27SDimitry Andric       D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
476606c3fb27SDimitry Andric                                                           readSourceRange()));
47670b57cec5SDimitry Andric       break;
47680b57cec5SDimitry Andric 
47690b57cec5SDimitry Andric     case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
47700b57cec5SDimitry Andric       auto AllocatorKind =
47710b57cec5SDimitry Andric           static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(Record.readInt());
47720b57cec5SDimitry Andric       Expr *Allocator = Record.readExpr();
4773349cc55cSDimitry Andric       Expr *Alignment = Record.readExpr();
4774480093f4SDimitry Andric       SourceRange SR = readSourceRange();
47750b57cec5SDimitry Andric       D->addAttr(OMPAllocateDeclAttr::CreateImplicit(
477606c3fb27SDimitry Andric           Reader.getContext(), AllocatorKind, Allocator, Alignment, SR));
47770b57cec5SDimitry Andric       break;
47780b57cec5SDimitry Andric     }
47790b57cec5SDimitry Andric 
47800b57cec5SDimitry Andric     case UPD_DECL_EXPORTED: {
47810b57cec5SDimitry Andric       unsigned SubmoduleID = readSubmoduleID();
47820b57cec5SDimitry Andric       auto *Exported = cast<NamedDecl>(D);
47830b57cec5SDimitry Andric       Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
47840b57cec5SDimitry Andric       Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
47850b57cec5SDimitry Andric       Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
47860b57cec5SDimitry Andric       break;
47870b57cec5SDimitry Andric     }
47880b57cec5SDimitry Andric 
4789a7dea167SDimitry Andric     case UPD_DECL_MARKED_OPENMP_DECLARETARGET: {
4790e8d8bef9SDimitry Andric       auto MapType = Record.readEnum<OMPDeclareTargetDeclAttr::MapTypeTy>();
4791e8d8bef9SDimitry Andric       auto DevType = Record.readEnum<OMPDeclareTargetDeclAttr::DevTypeTy>();
479204eeddc0SDimitry Andric       Expr *IndirectE = Record.readExpr();
479304eeddc0SDimitry Andric       bool Indirect = Record.readBool();
4794e8d8bef9SDimitry Andric       unsigned Level = Record.readInt();
47950b57cec5SDimitry Andric       D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
479604eeddc0SDimitry Andric           Reader.getContext(), MapType, DevType, IndirectE, Indirect, Level,
479706c3fb27SDimitry Andric           readSourceRange()));
47980b57cec5SDimitry Andric       break;
4799a7dea167SDimitry Andric     }
48000b57cec5SDimitry Andric 
48010b57cec5SDimitry Andric     case UPD_ADDED_ATTR_TO_RECORD:
48020b57cec5SDimitry Andric       AttrVec Attrs;
48030b57cec5SDimitry Andric       Record.readAttributes(Attrs);
48040b57cec5SDimitry Andric       assert(Attrs.size() == 1);
48050b57cec5SDimitry Andric       D->addAttr(Attrs[0]);
48060b57cec5SDimitry Andric       break;
48070b57cec5SDimitry Andric     }
48080b57cec5SDimitry Andric   }
48090b57cec5SDimitry Andric }
4810