xref: /aosp_15_r20/external/clang/lib/Serialization/ASTReaderDecl.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===--- ASTReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file implements the ASTReader::ReadDeclRecord method, which is the
11*67e74705SXin Li // entrypoint for loading a decl.
12*67e74705SXin Li //
13*67e74705SXin Li //===----------------------------------------------------------------------===//
14*67e74705SXin Li 
15*67e74705SXin Li #include "clang/Serialization/ASTReader.h"
16*67e74705SXin Li #include "ASTCommon.h"
17*67e74705SXin Li #include "ASTReaderInternals.h"
18*67e74705SXin Li #include "clang/AST/ASTConsumer.h"
19*67e74705SXin Li #include "clang/AST/ASTContext.h"
20*67e74705SXin Li #include "clang/AST/DeclCXX.h"
21*67e74705SXin Li #include "clang/AST/DeclGroup.h"
22*67e74705SXin Li #include "clang/AST/DeclTemplate.h"
23*67e74705SXin Li #include "clang/AST/DeclVisitor.h"
24*67e74705SXin Li #include "clang/AST/Expr.h"
25*67e74705SXin Li #include "clang/Sema/IdentifierResolver.h"
26*67e74705SXin Li #include "clang/Sema/SemaDiagnostic.h"
27*67e74705SXin Li #include "llvm/Support/SaveAndRestore.h"
28*67e74705SXin Li 
29*67e74705SXin Li using namespace clang;
30*67e74705SXin Li using namespace clang::serialization;
31*67e74705SXin Li 
32*67e74705SXin Li //===----------------------------------------------------------------------===//
33*67e74705SXin Li // Declaration deserialization
34*67e74705SXin Li //===----------------------------------------------------------------------===//
35*67e74705SXin Li 
36*67e74705SXin Li namespace clang {
37*67e74705SXin Li   class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
38*67e74705SXin Li     ASTReader &Reader;
39*67e74705SXin Li     ModuleFile &F;
40*67e74705SXin Li     uint64_t Offset;
41*67e74705SXin Li     const DeclID ThisDeclID;
42*67e74705SXin Li     const SourceLocation ThisDeclLoc;
43*67e74705SXin Li     typedef ASTReader::RecordData RecordData;
44*67e74705SXin Li     const RecordData &Record;
45*67e74705SXin Li     unsigned &Idx;
46*67e74705SXin Li     TypeID TypeIDForTypeDecl;
47*67e74705SXin Li     unsigned AnonymousDeclNumber;
48*67e74705SXin Li     GlobalDeclID NamedDeclForTagDecl;
49*67e74705SXin Li     IdentifierInfo *TypedefNameForLinkage;
50*67e74705SXin Li 
51*67e74705SXin Li     bool HasPendingBody;
52*67e74705SXin Li 
53*67e74705SXin Li     ///\brief A flag to carry the information for a decl from the entity is
54*67e74705SXin Li     /// used. We use it to delay the marking of the canonical decl as used until
55*67e74705SXin Li     /// the entire declaration is deserialized and merged.
56*67e74705SXin Li     bool IsDeclMarkedUsed;
57*67e74705SXin Li 
58*67e74705SXin Li     uint64_t GetCurrentCursorOffset();
59*67e74705SXin Li 
ReadLocalOffset(const RecordData & R,unsigned & I)60*67e74705SXin Li     uint64_t ReadLocalOffset(const RecordData &R, unsigned &I) {
61*67e74705SXin Li       uint64_t LocalOffset = R[I++];
62*67e74705SXin Li       assert(LocalOffset < Offset && "offset point after current record");
63*67e74705SXin Li       return LocalOffset ? Offset - LocalOffset : 0;
64*67e74705SXin Li     }
65*67e74705SXin Li 
ReadGlobalOffset(ModuleFile & F,const RecordData & R,unsigned & I)66*67e74705SXin Li     uint64_t ReadGlobalOffset(ModuleFile &F, const RecordData &R, unsigned &I) {
67*67e74705SXin Li       uint64_t Local = ReadLocalOffset(R, I);
68*67e74705SXin Li       return Local ? Reader.getGlobalBitOffset(F, Local) : 0;
69*67e74705SXin Li     }
70*67e74705SXin Li 
ReadSourceLocation(const RecordData & R,unsigned & I)71*67e74705SXin Li     SourceLocation ReadSourceLocation(const RecordData &R, unsigned &I) {
72*67e74705SXin Li       return Reader.ReadSourceLocation(F, R, I);
73*67e74705SXin Li     }
74*67e74705SXin Li 
ReadSourceRange(const RecordData & R,unsigned & I)75*67e74705SXin Li     SourceRange ReadSourceRange(const RecordData &R, unsigned &I) {
76*67e74705SXin Li       return Reader.ReadSourceRange(F, R, I);
77*67e74705SXin Li     }
78*67e74705SXin Li 
GetTypeSourceInfo(const RecordData & R,unsigned & I)79*67e74705SXin Li     TypeSourceInfo *GetTypeSourceInfo(const RecordData &R, unsigned &I) {
80*67e74705SXin Li       return Reader.GetTypeSourceInfo(F, R, I);
81*67e74705SXin Li     }
82*67e74705SXin Li 
ReadDeclID(const RecordData & R,unsigned & I)83*67e74705SXin Li     serialization::DeclID ReadDeclID(const RecordData &R, unsigned &I) {
84*67e74705SXin Li       return Reader.ReadDeclID(F, R, I);
85*67e74705SXin Li     }
86*67e74705SXin Li 
ReadString(const RecordData & R,unsigned & I)87*67e74705SXin Li     std::string ReadString(const RecordData &R, unsigned &I) {
88*67e74705SXin Li       return Reader.ReadString(R, I);
89*67e74705SXin Li     }
90*67e74705SXin Li 
ReadDeclIDList(SmallVectorImpl<DeclID> & IDs)91*67e74705SXin Li     void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
92*67e74705SXin Li       for (unsigned I = 0, Size = Record[Idx++]; I != Size; ++I)
93*67e74705SXin Li         IDs.push_back(ReadDeclID(Record, Idx));
94*67e74705SXin Li     }
95*67e74705SXin Li 
ReadDecl(const RecordData & R,unsigned & I)96*67e74705SXin Li     Decl *ReadDecl(const RecordData &R, unsigned &I) {
97*67e74705SXin Li       return Reader.ReadDecl(F, R, I);
98*67e74705SXin Li     }
99*67e74705SXin Li 
100*67e74705SXin Li     template<typename T>
ReadDeclAs(const RecordData & R,unsigned & I)101*67e74705SXin Li     T *ReadDeclAs(const RecordData &R, unsigned &I) {
102*67e74705SXin Li       return Reader.ReadDeclAs<T>(F, R, I);
103*67e74705SXin Li     }
104*67e74705SXin Li 
ReadQualifierInfo(QualifierInfo & Info,const RecordData & R,unsigned & I)105*67e74705SXin Li     void ReadQualifierInfo(QualifierInfo &Info,
106*67e74705SXin Li                            const RecordData &R, unsigned &I) {
107*67e74705SXin Li       Reader.ReadQualifierInfo(F, Info, R, I);
108*67e74705SXin Li     }
109*67e74705SXin Li 
ReadDeclarationNameLoc(DeclarationNameLoc & DNLoc,DeclarationName Name,const RecordData & R,unsigned & I)110*67e74705SXin Li     void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name,
111*67e74705SXin Li                                 const RecordData &R, unsigned &I) {
112*67e74705SXin Li       Reader.ReadDeclarationNameLoc(F, DNLoc, Name, R, I);
113*67e74705SXin Li     }
114*67e74705SXin Li 
ReadDeclarationNameInfo(DeclarationNameInfo & NameInfo,const RecordData & R,unsigned & I)115*67e74705SXin Li     void ReadDeclarationNameInfo(DeclarationNameInfo &NameInfo,
116*67e74705SXin Li                                 const RecordData &R, unsigned &I) {
117*67e74705SXin Li       Reader.ReadDeclarationNameInfo(F, NameInfo, R, I);
118*67e74705SXin Li     }
119*67e74705SXin Li 
readSubmoduleID(const RecordData & R,unsigned & I)120*67e74705SXin Li     serialization::SubmoduleID readSubmoduleID(const RecordData &R,
121*67e74705SXin Li                                                unsigned &I) {
122*67e74705SXin Li       if (I >= R.size())
123*67e74705SXin Li         return 0;
124*67e74705SXin Li 
125*67e74705SXin Li       return Reader.getGlobalSubmoduleID(F, R[I++]);
126*67e74705SXin Li     }
127*67e74705SXin Li 
readModule(const RecordData & R,unsigned & I)128*67e74705SXin Li     Module *readModule(const RecordData &R, unsigned &I) {
129*67e74705SXin Li       return Reader.getSubmodule(readSubmoduleID(R, I));
130*67e74705SXin Li     }
131*67e74705SXin Li 
132*67e74705SXin Li     void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
133*67e74705SXin Li     void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
134*67e74705SXin Li                                const RecordData &R, unsigned &I);
135*67e74705SXin Li     void MergeDefinitionData(CXXRecordDecl *D,
136*67e74705SXin Li                              struct CXXRecordDecl::DefinitionData &&NewDD);
137*67e74705SXin Li 
138*67e74705SXin Li     static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
139*67e74705SXin Li                                                  DeclContext *DC,
140*67e74705SXin Li                                                  unsigned Index);
141*67e74705SXin Li     static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
142*67e74705SXin Li                                            unsigned Index, NamedDecl *D);
143*67e74705SXin Li 
144*67e74705SXin Li     /// Results from loading a RedeclarableDecl.
145*67e74705SXin Li     class RedeclarableResult {
146*67e74705SXin Li       GlobalDeclID FirstID;
147*67e74705SXin Li       Decl *MergeWith;
148*67e74705SXin Li       bool IsKeyDecl;
149*67e74705SXin Li 
150*67e74705SXin Li     public:
RedeclarableResult(GlobalDeclID FirstID,Decl * MergeWith,bool IsKeyDecl)151*67e74705SXin Li       RedeclarableResult(GlobalDeclID FirstID, Decl *MergeWith, bool IsKeyDecl)
152*67e74705SXin Li           : FirstID(FirstID), MergeWith(MergeWith), IsKeyDecl(IsKeyDecl) {}
153*67e74705SXin Li 
154*67e74705SXin Li       /// \brief Retrieve the first ID.
getFirstID() const155*67e74705SXin Li       GlobalDeclID getFirstID() const { return FirstID; }
156*67e74705SXin Li 
157*67e74705SXin Li       /// \brief Is this declaration a key declaration?
isKeyDecl() const158*67e74705SXin Li       bool isKeyDecl() const { return IsKeyDecl; }
159*67e74705SXin Li 
160*67e74705SXin Li       /// \brief Get a known declaration that this should be merged with, if
161*67e74705SXin Li       /// any.
getKnownMergeTarget() const162*67e74705SXin Li       Decl *getKnownMergeTarget() const { return MergeWith; }
163*67e74705SXin Li     };
164*67e74705SXin Li 
165*67e74705SXin Li     /// \brief Class used to capture the result of searching for an existing
166*67e74705SXin Li     /// declaration of a specific kind and name, along with the ability
167*67e74705SXin Li     /// to update the place where this result was found (the declaration
168*67e74705SXin Li     /// chain hanging off an identifier or the DeclContext we searched in)
169*67e74705SXin Li     /// if requested.
170*67e74705SXin Li     class FindExistingResult {
171*67e74705SXin Li       ASTReader &Reader;
172*67e74705SXin Li       NamedDecl *New;
173*67e74705SXin Li       NamedDecl *Existing;
174*67e74705SXin Li       mutable bool AddResult;
175*67e74705SXin Li 
176*67e74705SXin Li       unsigned AnonymousDeclNumber;
177*67e74705SXin Li       IdentifierInfo *TypedefNameForLinkage;
178*67e74705SXin Li 
179*67e74705SXin Li       void operator=(FindExistingResult&) = delete;
180*67e74705SXin Li 
181*67e74705SXin Li     public:
FindExistingResult(ASTReader & Reader)182*67e74705SXin Li       FindExistingResult(ASTReader &Reader)
183*67e74705SXin Li           : Reader(Reader), New(nullptr), Existing(nullptr), AddResult(false),
184*67e74705SXin Li             AnonymousDeclNumber(0), TypedefNameForLinkage(nullptr) {}
185*67e74705SXin Li 
FindExistingResult(ASTReader & Reader,NamedDecl * New,NamedDecl * Existing,unsigned AnonymousDeclNumber,IdentifierInfo * TypedefNameForLinkage)186*67e74705SXin Li       FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
187*67e74705SXin Li                          unsigned AnonymousDeclNumber,
188*67e74705SXin Li                          IdentifierInfo *TypedefNameForLinkage)
189*67e74705SXin Li           : Reader(Reader), New(New), Existing(Existing), AddResult(true),
190*67e74705SXin Li             AnonymousDeclNumber(AnonymousDeclNumber),
191*67e74705SXin Li             TypedefNameForLinkage(TypedefNameForLinkage) {}
192*67e74705SXin Li 
FindExistingResult(const FindExistingResult & Other)193*67e74705SXin Li       FindExistingResult(const FindExistingResult &Other)
194*67e74705SXin Li           : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
195*67e74705SXin Li             AddResult(Other.AddResult),
196*67e74705SXin Li             AnonymousDeclNumber(Other.AnonymousDeclNumber),
197*67e74705SXin Li             TypedefNameForLinkage(Other.TypedefNameForLinkage) {
198*67e74705SXin Li         Other.AddResult = false;
199*67e74705SXin Li       }
200*67e74705SXin Li 
201*67e74705SXin Li       ~FindExistingResult();
202*67e74705SXin Li 
203*67e74705SXin Li       /// \brief Suppress the addition of this result into the known set of
204*67e74705SXin Li       /// names.
suppress()205*67e74705SXin Li       void suppress() { AddResult = false; }
206*67e74705SXin Li 
operator NamedDecl*() const207*67e74705SXin Li       operator NamedDecl*() const { return Existing; }
208*67e74705SXin Li 
209*67e74705SXin Li       template<typename T>
operator T*() const210*67e74705SXin Li       operator T*() const { return dyn_cast_or_null<T>(Existing); }
211*67e74705SXin Li     };
212*67e74705SXin Li 
213*67e74705SXin Li     static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
214*67e74705SXin Li                                                     DeclContext *DC);
215*67e74705SXin Li     FindExistingResult findExisting(NamedDecl *D);
216*67e74705SXin Li 
217*67e74705SXin Li   public:
ASTDeclReader(ASTReader & Reader,ASTReader::RecordLocation Loc,DeclID thisDeclID,SourceLocation ThisDeclLoc,const RecordData & Record,unsigned & Idx)218*67e74705SXin Li     ASTDeclReader(ASTReader &Reader, ASTReader::RecordLocation Loc,
219*67e74705SXin Li                   DeclID thisDeclID, SourceLocation ThisDeclLoc,
220*67e74705SXin Li                   const RecordData &Record, unsigned &Idx)
221*67e74705SXin Li         : Reader(Reader), F(*Loc.F), Offset(Loc.Offset), ThisDeclID(thisDeclID),
222*67e74705SXin Li           ThisDeclLoc(ThisDeclLoc), Record(Record), Idx(Idx),
223*67e74705SXin Li           TypeIDForTypeDecl(0), NamedDeclForTagDecl(0),
224*67e74705SXin Li           TypedefNameForLinkage(nullptr), HasPendingBody(false),
225*67e74705SXin Li           IsDeclMarkedUsed(false) {}
226*67e74705SXin Li 
227*67e74705SXin Li     template <typename DeclT>
228*67e74705SXin Li     static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
229*67e74705SXin Li     static Decl *getMostRecentDeclImpl(...);
230*67e74705SXin Li     static Decl *getMostRecentDecl(Decl *D);
231*67e74705SXin Li 
232*67e74705SXin Li     template <typename DeclT>
233*67e74705SXin Li     static void attachPreviousDeclImpl(ASTReader &Reader,
234*67e74705SXin Li                                        Redeclarable<DeclT> *D, Decl *Previous,
235*67e74705SXin Li                                        Decl *Canon);
236*67e74705SXin Li     static void attachPreviousDeclImpl(ASTReader &Reader, ...);
237*67e74705SXin Li     static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
238*67e74705SXin Li                                    Decl *Canon);
239*67e74705SXin Li 
240*67e74705SXin Li     template <typename DeclT>
241*67e74705SXin Li     static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
242*67e74705SXin Li     static void attachLatestDeclImpl(...);
243*67e74705SXin Li     static void attachLatestDecl(Decl *D, Decl *latest);
244*67e74705SXin Li 
245*67e74705SXin Li     template <typename DeclT>
246*67e74705SXin Li     static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
247*67e74705SXin Li     static void markIncompleteDeclChainImpl(...);
248*67e74705SXin Li 
249*67e74705SXin Li     /// \brief Determine whether this declaration has a pending body.
hasPendingBody() const250*67e74705SXin Li     bool hasPendingBody() const { return HasPendingBody; }
251*67e74705SXin Li 
252*67e74705SXin Li     void Visit(Decl *D);
253*67e74705SXin Li 
254*67e74705SXin Li     void UpdateDecl(Decl *D, ModuleFile &ModuleFile,
255*67e74705SXin Li                     const RecordData &Record);
256*67e74705SXin Li 
setNextObjCCategory(ObjCCategoryDecl * Cat,ObjCCategoryDecl * Next)257*67e74705SXin Li     static void setNextObjCCategory(ObjCCategoryDecl *Cat,
258*67e74705SXin Li                                     ObjCCategoryDecl *Next) {
259*67e74705SXin Li       Cat->NextClassCategory = Next;
260*67e74705SXin Li     }
261*67e74705SXin Li 
262*67e74705SXin Li     void VisitDecl(Decl *D);
263*67e74705SXin Li     void VisitPragmaCommentDecl(PragmaCommentDecl *D);
264*67e74705SXin Li     void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
265*67e74705SXin Li     void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
266*67e74705SXin Li     void VisitNamedDecl(NamedDecl *ND);
267*67e74705SXin Li     void VisitLabelDecl(LabelDecl *LD);
268*67e74705SXin Li     void VisitNamespaceDecl(NamespaceDecl *D);
269*67e74705SXin Li     void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
270*67e74705SXin Li     void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
271*67e74705SXin Li     void VisitTypeDecl(TypeDecl *TD);
272*67e74705SXin Li     RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
273*67e74705SXin Li     void VisitTypedefDecl(TypedefDecl *TD);
274*67e74705SXin Li     void VisitTypeAliasDecl(TypeAliasDecl *TD);
275*67e74705SXin Li     void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
276*67e74705SXin Li     RedeclarableResult VisitTagDecl(TagDecl *TD);
277*67e74705SXin Li     void VisitEnumDecl(EnumDecl *ED);
278*67e74705SXin Li     RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
VisitRecordDecl(RecordDecl * RD)279*67e74705SXin Li     void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
280*67e74705SXin Li     RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
VisitCXXRecordDecl(CXXRecordDecl * D)281*67e74705SXin Li     void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
282*67e74705SXin Li     RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
283*67e74705SXin Li                                             ClassTemplateSpecializationDecl *D);
VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl * D)284*67e74705SXin Li     void VisitClassTemplateSpecializationDecl(
285*67e74705SXin Li         ClassTemplateSpecializationDecl *D) {
286*67e74705SXin Li       VisitClassTemplateSpecializationDeclImpl(D);
287*67e74705SXin Li     }
288*67e74705SXin Li     void VisitClassTemplatePartialSpecializationDecl(
289*67e74705SXin Li                                      ClassTemplatePartialSpecializationDecl *D);
290*67e74705SXin Li     void VisitClassScopeFunctionSpecializationDecl(
291*67e74705SXin Li                                        ClassScopeFunctionSpecializationDecl *D);
292*67e74705SXin Li     RedeclarableResult
293*67e74705SXin Li     VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl * D)294*67e74705SXin Li     void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
295*67e74705SXin Li       VisitVarTemplateSpecializationDeclImpl(D);
296*67e74705SXin Li     }
297*67e74705SXin Li     void VisitVarTemplatePartialSpecializationDecl(
298*67e74705SXin Li         VarTemplatePartialSpecializationDecl *D);
299*67e74705SXin Li     void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
300*67e74705SXin Li     void VisitValueDecl(ValueDecl *VD);
301*67e74705SXin Li     void VisitEnumConstantDecl(EnumConstantDecl *ECD);
302*67e74705SXin Li     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
303*67e74705SXin Li     void VisitDeclaratorDecl(DeclaratorDecl *DD);
304*67e74705SXin Li     void VisitFunctionDecl(FunctionDecl *FD);
305*67e74705SXin Li     void VisitCXXMethodDecl(CXXMethodDecl *D);
306*67e74705SXin Li     void VisitCXXConstructorDecl(CXXConstructorDecl *D);
307*67e74705SXin Li     void VisitCXXDestructorDecl(CXXDestructorDecl *D);
308*67e74705SXin Li     void VisitCXXConversionDecl(CXXConversionDecl *D);
309*67e74705SXin Li     void VisitFieldDecl(FieldDecl *FD);
310*67e74705SXin Li     void VisitMSPropertyDecl(MSPropertyDecl *FD);
311*67e74705SXin Li     void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
312*67e74705SXin Li     RedeclarableResult VisitVarDeclImpl(VarDecl *D);
VisitVarDecl(VarDecl * VD)313*67e74705SXin Li     void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
314*67e74705SXin Li     void VisitImplicitParamDecl(ImplicitParamDecl *PD);
315*67e74705SXin Li     void VisitParmVarDecl(ParmVarDecl *PD);
316*67e74705SXin Li     void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
317*67e74705SXin Li     DeclID VisitTemplateDecl(TemplateDecl *D);
318*67e74705SXin Li     RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
319*67e74705SXin Li     void VisitClassTemplateDecl(ClassTemplateDecl *D);
320*67e74705SXin Li     void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
321*67e74705SXin Li     void VisitVarTemplateDecl(VarTemplateDecl *D);
322*67e74705SXin Li     void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
323*67e74705SXin Li     void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
324*67e74705SXin Li     void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
325*67e74705SXin Li     void VisitUsingDecl(UsingDecl *D);
326*67e74705SXin Li     void VisitUsingShadowDecl(UsingShadowDecl *D);
327*67e74705SXin Li     void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
328*67e74705SXin Li     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
329*67e74705SXin Li     void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
330*67e74705SXin Li     void VisitImportDecl(ImportDecl *D);
331*67e74705SXin Li     void VisitAccessSpecDecl(AccessSpecDecl *D);
332*67e74705SXin Li     void VisitFriendDecl(FriendDecl *D);
333*67e74705SXin Li     void VisitFriendTemplateDecl(FriendTemplateDecl *D);
334*67e74705SXin Li     void VisitStaticAssertDecl(StaticAssertDecl *D);
335*67e74705SXin Li     void VisitBlockDecl(BlockDecl *BD);
336*67e74705SXin Li     void VisitCapturedDecl(CapturedDecl *CD);
337*67e74705SXin Li     void VisitEmptyDecl(EmptyDecl *D);
338*67e74705SXin Li 
339*67e74705SXin Li     std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
340*67e74705SXin Li 
341*67e74705SXin Li     template<typename T>
342*67e74705SXin Li     RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
343*67e74705SXin Li 
344*67e74705SXin Li     template<typename T>
345*67e74705SXin Li     void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
346*67e74705SXin Li                            DeclID TemplatePatternID = 0);
347*67e74705SXin Li 
348*67e74705SXin Li     template<typename T>
349*67e74705SXin Li     void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
350*67e74705SXin Li                            RedeclarableResult &Redecl,
351*67e74705SXin Li                            DeclID TemplatePatternID = 0);
352*67e74705SXin Li 
353*67e74705SXin Li     template<typename T>
354*67e74705SXin Li     void mergeMergeable(Mergeable<T> *D);
355*67e74705SXin Li 
356*67e74705SXin Li     void mergeTemplatePattern(RedeclarableTemplateDecl *D,
357*67e74705SXin Li                               RedeclarableTemplateDecl *Existing,
358*67e74705SXin Li                               DeclID DsID, bool IsKeyDecl);
359*67e74705SXin Li 
360*67e74705SXin Li     ObjCTypeParamList *ReadObjCTypeParamList();
361*67e74705SXin Li 
362*67e74705SXin Li     // FIXME: Reorder according to DeclNodes.td?
363*67e74705SXin Li     void VisitObjCMethodDecl(ObjCMethodDecl *D);
364*67e74705SXin Li     void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
365*67e74705SXin Li     void VisitObjCContainerDecl(ObjCContainerDecl *D);
366*67e74705SXin Li     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
367*67e74705SXin Li     void VisitObjCIvarDecl(ObjCIvarDecl *D);
368*67e74705SXin Li     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
369*67e74705SXin Li     void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
370*67e74705SXin Li     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
371*67e74705SXin Li     void VisitObjCImplDecl(ObjCImplDecl *D);
372*67e74705SXin Li     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
373*67e74705SXin Li     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
374*67e74705SXin Li     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
375*67e74705SXin Li     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
376*67e74705SXin Li     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
377*67e74705SXin Li     void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
378*67e74705SXin Li     void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
379*67e74705SXin Li     void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
380*67e74705SXin Li 
381*67e74705SXin Li     /// We've merged the definition \p MergedDef into the existing definition
382*67e74705SXin Li     /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
383*67e74705SXin Li     /// visible.
mergeDefinitionVisibility(NamedDecl * Def,NamedDecl * MergedDef)384*67e74705SXin Li     void mergeDefinitionVisibility(NamedDecl *Def, NamedDecl *MergedDef) {
385*67e74705SXin Li       if (Def->isHidden()) {
386*67e74705SXin Li         // If MergedDef is visible or becomes visible, make the definition visible.
387*67e74705SXin Li         if (!MergedDef->isHidden())
388*67e74705SXin Li           Def->Hidden = false;
389*67e74705SXin Li         else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
390*67e74705SXin Li           Reader.getContext().mergeDefinitionIntoModule(
391*67e74705SXin Li               Def, MergedDef->getImportedOwningModule(),
392*67e74705SXin Li               /*NotifyListeners*/ false);
393*67e74705SXin Li           Reader.PendingMergedDefinitionsToDeduplicate.insert(Def);
394*67e74705SXin Li         } else {
395*67e74705SXin Li           auto SubmoduleID = MergedDef->getOwningModuleID();
396*67e74705SXin Li           assert(SubmoduleID && "hidden definition in no module");
397*67e74705SXin Li           Reader.HiddenNamesMap[Reader.getSubmodule(SubmoduleID)].push_back(Def);
398*67e74705SXin Li         }
399*67e74705SXin Li       }
400*67e74705SXin Li     }
401*67e74705SXin Li   };
402*67e74705SXin Li } // end namespace clang
403*67e74705SXin Li 
404*67e74705SXin Li namespace {
405*67e74705SXin Li /// Iterator over the redeclarations of a declaration that have already
406*67e74705SXin Li /// been merged into the same redeclaration chain.
407*67e74705SXin Li template<typename DeclT>
408*67e74705SXin Li class MergedRedeclIterator {
409*67e74705SXin Li   DeclT *Start, *Canonical, *Current;
410*67e74705SXin Li public:
MergedRedeclIterator()411*67e74705SXin Li   MergedRedeclIterator() : Current(nullptr) {}
MergedRedeclIterator(DeclT * Start)412*67e74705SXin Li   MergedRedeclIterator(DeclT *Start)
413*67e74705SXin Li       : Start(Start), Canonical(nullptr), Current(Start) {}
414*67e74705SXin Li 
operator *()415*67e74705SXin Li   DeclT *operator*() { return Current; }
416*67e74705SXin Li 
operator ++()417*67e74705SXin Li   MergedRedeclIterator &operator++() {
418*67e74705SXin Li     if (Current->isFirstDecl()) {
419*67e74705SXin Li       Canonical = Current;
420*67e74705SXin Li       Current = Current->getMostRecentDecl();
421*67e74705SXin Li     } else
422*67e74705SXin Li       Current = Current->getPreviousDecl();
423*67e74705SXin Li 
424*67e74705SXin Li     // If we started in the merged portion, we'll reach our start position
425*67e74705SXin Li     // eventually. Otherwise, we'll never reach it, but the second declaration
426*67e74705SXin Li     // we reached was the canonical declaration, so stop when we see that one
427*67e74705SXin Li     // again.
428*67e74705SXin Li     if (Current == Start || Current == Canonical)
429*67e74705SXin Li       Current = nullptr;
430*67e74705SXin Li     return *this;
431*67e74705SXin Li   }
432*67e74705SXin Li 
operator !=(const MergedRedeclIterator & A,const MergedRedeclIterator & B)433*67e74705SXin Li   friend bool operator!=(const MergedRedeclIterator &A,
434*67e74705SXin Li                          const MergedRedeclIterator &B) {
435*67e74705SXin Li     return A.Current != B.Current;
436*67e74705SXin Li   }
437*67e74705SXin Li };
438*67e74705SXin Li } // end anonymous namespace
439*67e74705SXin Li 
440*67e74705SXin Li template<typename DeclT>
merged_redecls(DeclT * D)441*67e74705SXin Li llvm::iterator_range<MergedRedeclIterator<DeclT>> merged_redecls(DeclT *D) {
442*67e74705SXin Li   return llvm::make_range(MergedRedeclIterator<DeclT>(D),
443*67e74705SXin Li                           MergedRedeclIterator<DeclT>());
444*67e74705SXin Li }
445*67e74705SXin Li 
GetCurrentCursorOffset()446*67e74705SXin Li uint64_t ASTDeclReader::GetCurrentCursorOffset() {
447*67e74705SXin Li   return F.DeclsCursor.GetCurrentBitNo() + F.GlobalBitOffset;
448*67e74705SXin Li }
449*67e74705SXin Li 
Visit(Decl * D)450*67e74705SXin Li void ASTDeclReader::Visit(Decl *D) {
451*67e74705SXin Li   DeclVisitor<ASTDeclReader, void>::Visit(D);
452*67e74705SXin Li 
453*67e74705SXin Li   // At this point we have deserialized and merged the decl and it is safe to
454*67e74705SXin Li   // update its canonical decl to signal that the entire entity is used.
455*67e74705SXin Li   D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
456*67e74705SXin Li   IsDeclMarkedUsed = false;
457*67e74705SXin Li 
458*67e74705SXin Li   if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
459*67e74705SXin Li     if (DD->DeclInfo) {
460*67e74705SXin Li       DeclaratorDecl::ExtInfo *Info =
461*67e74705SXin Li           DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
462*67e74705SXin Li       Info->TInfo =
463*67e74705SXin Li           GetTypeSourceInfo(Record, Idx);
464*67e74705SXin Li     }
465*67e74705SXin Li     else {
466*67e74705SXin Li       DD->DeclInfo = GetTypeSourceInfo(Record, Idx);
467*67e74705SXin Li     }
468*67e74705SXin Li   }
469*67e74705SXin Li 
470*67e74705SXin Li   if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
471*67e74705SXin Li     // We have a fully initialized TypeDecl. Read its type now.
472*67e74705SXin Li     TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
473*67e74705SXin Li 
474*67e74705SXin Li     // If this is a tag declaration with a typedef name for linkage, it's safe
475*67e74705SXin Li     // to load that typedef now.
476*67e74705SXin Li     if (NamedDeclForTagDecl)
477*67e74705SXin Li       cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
478*67e74705SXin Li           cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
479*67e74705SXin Li   } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
480*67e74705SXin Li     // if we have a fully initialized TypeDecl, we can safely read its type now.
481*67e74705SXin Li     ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull();
482*67e74705SXin Li   } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
483*67e74705SXin Li     // FunctionDecl's body was written last after all other Stmts/Exprs.
484*67e74705SXin Li     // We only read it if FD doesn't already have a body (e.g., from another
485*67e74705SXin Li     // module).
486*67e74705SXin Li     // FIXME: Can we diagnose ODR violations somehow?
487*67e74705SXin Li     if (Record[Idx++]) {
488*67e74705SXin Li       if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
489*67e74705SXin Li         CD->NumCtorInitializers = Record[Idx++];
490*67e74705SXin Li         if (CD->NumCtorInitializers)
491*67e74705SXin Li           CD->CtorInitializers = ReadGlobalOffset(F, Record, Idx);
492*67e74705SXin Li       }
493*67e74705SXin Li       Reader.PendingBodies[FD] = GetCurrentCursorOffset();
494*67e74705SXin Li       HasPendingBody = true;
495*67e74705SXin Li     }
496*67e74705SXin Li   }
497*67e74705SXin Li }
498*67e74705SXin Li 
VisitDecl(Decl * D)499*67e74705SXin Li void ASTDeclReader::VisitDecl(Decl *D) {
500*67e74705SXin Li   if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
501*67e74705SXin Li       isa<ParmVarDecl>(D)) {
502*67e74705SXin Li     // We don't want to deserialize the DeclContext of a template
503*67e74705SXin Li     // parameter or of a parameter of a function template immediately.   These
504*67e74705SXin Li     // entities might be used in the formulation of its DeclContext (for
505*67e74705SXin Li     // example, a function parameter can be used in decltype() in trailing
506*67e74705SXin Li     // return type of the function).  Use the translation unit DeclContext as a
507*67e74705SXin Li     // placeholder.
508*67e74705SXin Li     GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID(Record, Idx);
509*67e74705SXin Li     GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID(Record, Idx);
510*67e74705SXin Li     if (!LexicalDCIDForTemplateParmDecl)
511*67e74705SXin Li       LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
512*67e74705SXin Li     Reader.addPendingDeclContextInfo(D,
513*67e74705SXin Li                                      SemaDCIDForTemplateParmDecl,
514*67e74705SXin Li                                      LexicalDCIDForTemplateParmDecl);
515*67e74705SXin Li     D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
516*67e74705SXin Li   } else {
517*67e74705SXin Li     DeclContext *SemaDC = ReadDeclAs<DeclContext>(Record, Idx);
518*67e74705SXin Li     DeclContext *LexicalDC = ReadDeclAs<DeclContext>(Record, Idx);
519*67e74705SXin Li     if (!LexicalDC)
520*67e74705SXin Li       LexicalDC = SemaDC;
521*67e74705SXin Li     DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
522*67e74705SXin Li     // Avoid calling setLexicalDeclContext() directly because it uses
523*67e74705SXin Li     // Decl::getASTContext() internally which is unsafe during derialization.
524*67e74705SXin Li     D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
525*67e74705SXin Li                            Reader.getContext());
526*67e74705SXin Li   }
527*67e74705SXin Li   D->setLocation(ThisDeclLoc);
528*67e74705SXin Li   D->setInvalidDecl(Record[Idx++]);
529*67e74705SXin Li   if (Record[Idx++]) { // hasAttrs
530*67e74705SXin Li     AttrVec Attrs;
531*67e74705SXin Li     Reader.ReadAttributes(F, Attrs, Record, Idx);
532*67e74705SXin Li     // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
533*67e74705SXin Li     // internally which is unsafe during derialization.
534*67e74705SXin Li     D->setAttrsImpl(Attrs, Reader.getContext());
535*67e74705SXin Li   }
536*67e74705SXin Li   D->setImplicit(Record[Idx++]);
537*67e74705SXin Li   D->Used = Record[Idx++];
538*67e74705SXin Li   IsDeclMarkedUsed |= D->Used;
539*67e74705SXin Li   D->setReferenced(Record[Idx++]);
540*67e74705SXin Li   D->setTopLevelDeclInObjCContainer(Record[Idx++]);
541*67e74705SXin Li   D->setAccess((AccessSpecifier)Record[Idx++]);
542*67e74705SXin Li   D->FromASTFile = true;
543*67e74705SXin Li   D->setModulePrivate(Record[Idx++]);
544*67e74705SXin Li   D->Hidden = D->isModulePrivate();
545*67e74705SXin Li 
546*67e74705SXin Li   // Determine whether this declaration is part of a (sub)module. If so, it
547*67e74705SXin Li   // may not yet be visible.
548*67e74705SXin Li   if (unsigned SubmoduleID = readSubmoduleID(Record, Idx)) {
549*67e74705SXin Li     // Store the owning submodule ID in the declaration.
550*67e74705SXin Li     D->setOwningModuleID(SubmoduleID);
551*67e74705SXin Li 
552*67e74705SXin Li     if (D->Hidden) {
553*67e74705SXin Li       // Module-private declarations are never visible, so there is no work to do.
554*67e74705SXin Li     } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
555*67e74705SXin Li       // If local visibility is being tracked, this declaration will become
556*67e74705SXin Li       // hidden and visible as the owning module does. Inform Sema that this
557*67e74705SXin Li       // declaration might not be visible.
558*67e74705SXin Li       D->Hidden = true;
559*67e74705SXin Li     } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
560*67e74705SXin Li       if (Owner->NameVisibility != Module::AllVisible) {
561*67e74705SXin Li         // The owning module is not visible. Mark this declaration as hidden.
562*67e74705SXin Li         D->Hidden = true;
563*67e74705SXin Li 
564*67e74705SXin Li         // Note that this declaration was hidden because its owning module is
565*67e74705SXin Li         // not yet visible.
566*67e74705SXin Li         Reader.HiddenNamesMap[Owner].push_back(D);
567*67e74705SXin Li       }
568*67e74705SXin Li     }
569*67e74705SXin Li   }
570*67e74705SXin Li }
571*67e74705SXin Li 
VisitPragmaCommentDecl(PragmaCommentDecl * D)572*67e74705SXin Li void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
573*67e74705SXin Li   VisitDecl(D);
574*67e74705SXin Li   D->setLocation(ReadSourceLocation(Record, Idx));
575*67e74705SXin Li   D->CommentKind = (PragmaMSCommentKind)Record[Idx++];
576*67e74705SXin Li   std::string Arg = ReadString(Record, Idx);
577*67e74705SXin Li   memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
578*67e74705SXin Li   D->getTrailingObjects<char>()[Arg.size()] = '\0';
579*67e74705SXin Li }
580*67e74705SXin Li 
VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl * D)581*67e74705SXin Li void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
582*67e74705SXin Li   VisitDecl(D);
583*67e74705SXin Li   D->setLocation(ReadSourceLocation(Record, Idx));
584*67e74705SXin Li   std::string Name = ReadString(Record, Idx);
585*67e74705SXin Li   memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
586*67e74705SXin Li   D->getTrailingObjects<char>()[Name.size()] = '\0';
587*67e74705SXin Li 
588*67e74705SXin Li   D->ValueStart = Name.size() + 1;
589*67e74705SXin Li   std::string Value = ReadString(Record, Idx);
590*67e74705SXin Li   memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
591*67e74705SXin Li          Value.size());
592*67e74705SXin Li   D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
593*67e74705SXin Li }
594*67e74705SXin Li 
VisitTranslationUnitDecl(TranslationUnitDecl * TU)595*67e74705SXin Li void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
596*67e74705SXin Li   llvm_unreachable("Translation units are not serialized");
597*67e74705SXin Li }
598*67e74705SXin Li 
VisitNamedDecl(NamedDecl * ND)599*67e74705SXin Li void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
600*67e74705SXin Li   VisitDecl(ND);
601*67e74705SXin Li   ND->setDeclName(Reader.ReadDeclarationName(F, Record, Idx));
602*67e74705SXin Li   AnonymousDeclNumber = Record[Idx++];
603*67e74705SXin Li }
604*67e74705SXin Li 
VisitTypeDecl(TypeDecl * TD)605*67e74705SXin Li void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
606*67e74705SXin Li   VisitNamedDecl(TD);
607*67e74705SXin Li   TD->setLocStart(ReadSourceLocation(Record, Idx));
608*67e74705SXin Li   // Delay type reading until after we have fully initialized the decl.
609*67e74705SXin Li   TypeIDForTypeDecl = Reader.getGlobalTypeID(F, Record[Idx++]);
610*67e74705SXin Li }
611*67e74705SXin Li 
612*67e74705SXin Li ASTDeclReader::RedeclarableResult
VisitTypedefNameDecl(TypedefNameDecl * TD)613*67e74705SXin Li ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
614*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(TD);
615*67e74705SXin Li   VisitTypeDecl(TD);
616*67e74705SXin Li   TypeSourceInfo *TInfo = GetTypeSourceInfo(Record, Idx);
617*67e74705SXin Li   if (Record[Idx++]) { // isModed
618*67e74705SXin Li     QualType modedT = Reader.readType(F, Record, Idx);
619*67e74705SXin Li     TD->setModedTypeSourceInfo(TInfo, modedT);
620*67e74705SXin Li   } else
621*67e74705SXin Li     TD->setTypeSourceInfo(TInfo);
622*67e74705SXin Li   return Redecl;
623*67e74705SXin Li }
624*67e74705SXin Li 
VisitTypedefDecl(TypedefDecl * TD)625*67e74705SXin Li void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
626*67e74705SXin Li   RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
627*67e74705SXin Li   mergeRedeclarable(TD, Redecl);
628*67e74705SXin Li }
629*67e74705SXin Li 
VisitTypeAliasDecl(TypeAliasDecl * TD)630*67e74705SXin Li void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
631*67e74705SXin Li   RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
632*67e74705SXin Li   if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>(Record, Idx))
633*67e74705SXin Li     // Merged when we merge the template.
634*67e74705SXin Li     TD->setDescribedAliasTemplate(Template);
635*67e74705SXin Li   else
636*67e74705SXin Li     mergeRedeclarable(TD, Redecl);
637*67e74705SXin Li }
638*67e74705SXin Li 
VisitTagDecl(TagDecl * TD)639*67e74705SXin Li ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
640*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(TD);
641*67e74705SXin Li   VisitTypeDecl(TD);
642*67e74705SXin Li 
643*67e74705SXin Li   TD->IdentifierNamespace = Record[Idx++];
644*67e74705SXin Li   TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
645*67e74705SXin Li   if (!isa<CXXRecordDecl>(TD))
646*67e74705SXin Li     TD->setCompleteDefinition(Record[Idx++]);
647*67e74705SXin Li   TD->setEmbeddedInDeclarator(Record[Idx++]);
648*67e74705SXin Li   TD->setFreeStanding(Record[Idx++]);
649*67e74705SXin Li   TD->setCompleteDefinitionRequired(Record[Idx++]);
650*67e74705SXin Li   TD->setRBraceLoc(ReadSourceLocation(Record, Idx));
651*67e74705SXin Li 
652*67e74705SXin Li   switch (Record[Idx++]) {
653*67e74705SXin Li   case 0:
654*67e74705SXin Li     break;
655*67e74705SXin Li   case 1: { // ExtInfo
656*67e74705SXin Li     TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
657*67e74705SXin Li     ReadQualifierInfo(*Info, Record, Idx);
658*67e74705SXin Li     TD->TypedefNameDeclOrQualifier = Info;
659*67e74705SXin Li     break;
660*67e74705SXin Li   }
661*67e74705SXin Li   case 2: // TypedefNameForAnonDecl
662*67e74705SXin Li     NamedDeclForTagDecl = ReadDeclID(Record, Idx);
663*67e74705SXin Li     TypedefNameForLinkage = Reader.GetIdentifierInfo(F, Record, Idx);
664*67e74705SXin Li     break;
665*67e74705SXin Li   default:
666*67e74705SXin Li     llvm_unreachable("unexpected tag info kind");
667*67e74705SXin Li   }
668*67e74705SXin Li 
669*67e74705SXin Li   if (!isa<CXXRecordDecl>(TD))
670*67e74705SXin Li     mergeRedeclarable(TD, Redecl);
671*67e74705SXin Li   return Redecl;
672*67e74705SXin Li }
673*67e74705SXin Li 
VisitEnumDecl(EnumDecl * ED)674*67e74705SXin Li void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
675*67e74705SXin Li   VisitTagDecl(ED);
676*67e74705SXin Li   if (TypeSourceInfo *TI = Reader.GetTypeSourceInfo(F, Record, Idx))
677*67e74705SXin Li     ED->setIntegerTypeSourceInfo(TI);
678*67e74705SXin Li   else
679*67e74705SXin Li     ED->setIntegerType(Reader.readType(F, Record, Idx));
680*67e74705SXin Li   ED->setPromotionType(Reader.readType(F, Record, Idx));
681*67e74705SXin Li   ED->setNumPositiveBits(Record[Idx++]);
682*67e74705SXin Li   ED->setNumNegativeBits(Record[Idx++]);
683*67e74705SXin Li   ED->IsScoped = Record[Idx++];
684*67e74705SXin Li   ED->IsScopedUsingClassTag = Record[Idx++];
685*67e74705SXin Li   ED->IsFixed = Record[Idx++];
686*67e74705SXin Li 
687*67e74705SXin Li   // If this is a definition subject to the ODR, and we already have a
688*67e74705SXin Li   // definition, merge this one into it.
689*67e74705SXin Li   if (ED->IsCompleteDefinition &&
690*67e74705SXin Li       Reader.getContext().getLangOpts().Modules &&
691*67e74705SXin Li       Reader.getContext().getLangOpts().CPlusPlus) {
692*67e74705SXin Li     EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
693*67e74705SXin Li     if (!OldDef) {
694*67e74705SXin Li       // This is the first time we've seen an imported definition. Look for a
695*67e74705SXin Li       // local definition before deciding that we are the first definition.
696*67e74705SXin Li       for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
697*67e74705SXin Li         if (!D->isFromASTFile() && D->isCompleteDefinition()) {
698*67e74705SXin Li           OldDef = D;
699*67e74705SXin Li           break;
700*67e74705SXin Li         }
701*67e74705SXin Li       }
702*67e74705SXin Li     }
703*67e74705SXin Li     if (OldDef) {
704*67e74705SXin Li       Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
705*67e74705SXin Li       ED->IsCompleteDefinition = false;
706*67e74705SXin Li       mergeDefinitionVisibility(OldDef, ED);
707*67e74705SXin Li     } else {
708*67e74705SXin Li       OldDef = ED;
709*67e74705SXin Li     }
710*67e74705SXin Li   }
711*67e74705SXin Li 
712*67e74705SXin Li   if (EnumDecl *InstED = ReadDeclAs<EnumDecl>(Record, Idx)) {
713*67e74705SXin Li     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
714*67e74705SXin Li     SourceLocation POI = ReadSourceLocation(Record, Idx);
715*67e74705SXin Li     ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
716*67e74705SXin Li     ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
717*67e74705SXin Li   }
718*67e74705SXin Li }
719*67e74705SXin Li 
720*67e74705SXin Li ASTDeclReader::RedeclarableResult
VisitRecordDeclImpl(RecordDecl * RD)721*67e74705SXin Li ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
722*67e74705SXin Li   RedeclarableResult Redecl = VisitTagDecl(RD);
723*67e74705SXin Li   RD->setHasFlexibleArrayMember(Record[Idx++]);
724*67e74705SXin Li   RD->setAnonymousStructOrUnion(Record[Idx++]);
725*67e74705SXin Li   RD->setHasObjectMember(Record[Idx++]);
726*67e74705SXin Li   RD->setHasVolatileMember(Record[Idx++]);
727*67e74705SXin Li   return Redecl;
728*67e74705SXin Li }
729*67e74705SXin Li 
VisitValueDecl(ValueDecl * VD)730*67e74705SXin Li void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
731*67e74705SXin Li   VisitNamedDecl(VD);
732*67e74705SXin Li   VD->setType(Reader.readType(F, Record, Idx));
733*67e74705SXin Li }
734*67e74705SXin Li 
VisitEnumConstantDecl(EnumConstantDecl * ECD)735*67e74705SXin Li void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
736*67e74705SXin Li   VisitValueDecl(ECD);
737*67e74705SXin Li   if (Record[Idx++])
738*67e74705SXin Li     ECD->setInitExpr(Reader.ReadExpr(F));
739*67e74705SXin Li   ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
740*67e74705SXin Li   mergeMergeable(ECD);
741*67e74705SXin Li }
742*67e74705SXin Li 
VisitDeclaratorDecl(DeclaratorDecl * DD)743*67e74705SXin Li void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
744*67e74705SXin Li   VisitValueDecl(DD);
745*67e74705SXin Li   DD->setInnerLocStart(ReadSourceLocation(Record, Idx));
746*67e74705SXin Li   if (Record[Idx++]) { // hasExtInfo
747*67e74705SXin Li     DeclaratorDecl::ExtInfo *Info
748*67e74705SXin Li         = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
749*67e74705SXin Li     ReadQualifierInfo(*Info, Record, Idx);
750*67e74705SXin Li     DD->DeclInfo = Info;
751*67e74705SXin Li   }
752*67e74705SXin Li }
753*67e74705SXin Li 
VisitFunctionDecl(FunctionDecl * FD)754*67e74705SXin Li void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
755*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(FD);
756*67e74705SXin Li   VisitDeclaratorDecl(FD);
757*67e74705SXin Li 
758*67e74705SXin Li   ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName(), Record, Idx);
759*67e74705SXin Li   FD->IdentifierNamespace = Record[Idx++];
760*67e74705SXin Li 
761*67e74705SXin Li   // FunctionDecl's body is handled last at ASTDeclReader::Visit,
762*67e74705SXin Li   // after everything else is read.
763*67e74705SXin Li 
764*67e74705SXin Li   FD->SClass = (StorageClass)Record[Idx++];
765*67e74705SXin Li   FD->IsInline = Record[Idx++];
766*67e74705SXin Li   FD->IsInlineSpecified = Record[Idx++];
767*67e74705SXin Li   FD->IsVirtualAsWritten = Record[Idx++];
768*67e74705SXin Li   FD->IsPure = Record[Idx++];
769*67e74705SXin Li   FD->HasInheritedPrototype = Record[Idx++];
770*67e74705SXin Li   FD->HasWrittenPrototype = Record[Idx++];
771*67e74705SXin Li   FD->IsDeleted = Record[Idx++];
772*67e74705SXin Li   FD->IsTrivial = Record[Idx++];
773*67e74705SXin Li   FD->IsDefaulted = Record[Idx++];
774*67e74705SXin Li   FD->IsExplicitlyDefaulted = Record[Idx++];
775*67e74705SXin Li   FD->HasImplicitReturnZero = Record[Idx++];
776*67e74705SXin Li   FD->IsConstexpr = Record[Idx++];
777*67e74705SXin Li   FD->HasSkippedBody = Record[Idx++];
778*67e74705SXin Li   FD->IsLateTemplateParsed = Record[Idx++];
779*67e74705SXin Li   FD->setCachedLinkage(Linkage(Record[Idx++]));
780*67e74705SXin Li   FD->EndRangeLoc = ReadSourceLocation(Record, Idx);
781*67e74705SXin Li 
782*67e74705SXin Li   switch ((FunctionDecl::TemplatedKind)Record[Idx++]) {
783*67e74705SXin Li   case FunctionDecl::TK_NonTemplate:
784*67e74705SXin Li     mergeRedeclarable(FD, Redecl);
785*67e74705SXin Li     break;
786*67e74705SXin Li   case FunctionDecl::TK_FunctionTemplate:
787*67e74705SXin Li     // Merged when we merge the template.
788*67e74705SXin Li     FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>(Record,
789*67e74705SXin Li                                                                       Idx));
790*67e74705SXin Li     break;
791*67e74705SXin Li   case FunctionDecl::TK_MemberSpecialization: {
792*67e74705SXin Li     FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>(Record, Idx);
793*67e74705SXin Li     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
794*67e74705SXin Li     SourceLocation POI = ReadSourceLocation(Record, Idx);
795*67e74705SXin Li     FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
796*67e74705SXin Li     FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
797*67e74705SXin Li     mergeRedeclarable(FD, Redecl);
798*67e74705SXin Li     break;
799*67e74705SXin Li   }
800*67e74705SXin Li   case FunctionDecl::TK_FunctionTemplateSpecialization: {
801*67e74705SXin Li     FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>(Record,
802*67e74705SXin Li                                                                       Idx);
803*67e74705SXin Li     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
804*67e74705SXin Li 
805*67e74705SXin Li     // Template arguments.
806*67e74705SXin Li     SmallVector<TemplateArgument, 8> TemplArgs;
807*67e74705SXin Li     Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx,
808*67e74705SXin Li                                     /*Canonicalize*/ true);
809*67e74705SXin Li 
810*67e74705SXin Li     // Template args as written.
811*67e74705SXin Li     SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
812*67e74705SXin Li     SourceLocation LAngleLoc, RAngleLoc;
813*67e74705SXin Li     bool HasTemplateArgumentsAsWritten = Record[Idx++];
814*67e74705SXin Li     if (HasTemplateArgumentsAsWritten) {
815*67e74705SXin Li       unsigned NumTemplateArgLocs = Record[Idx++];
816*67e74705SXin Li       TemplArgLocs.reserve(NumTemplateArgLocs);
817*67e74705SXin Li       for (unsigned i=0; i != NumTemplateArgLocs; ++i)
818*67e74705SXin Li         TemplArgLocs.push_back(
819*67e74705SXin Li             Reader.ReadTemplateArgumentLoc(F, Record, Idx));
820*67e74705SXin Li 
821*67e74705SXin Li       LAngleLoc = ReadSourceLocation(Record, Idx);
822*67e74705SXin Li       RAngleLoc = ReadSourceLocation(Record, Idx);
823*67e74705SXin Li     }
824*67e74705SXin Li 
825*67e74705SXin Li     SourceLocation POI = ReadSourceLocation(Record, Idx);
826*67e74705SXin Li 
827*67e74705SXin Li     ASTContext &C = Reader.getContext();
828*67e74705SXin Li     TemplateArgumentList *TemplArgList
829*67e74705SXin Li       = TemplateArgumentList::CreateCopy(C, TemplArgs);
830*67e74705SXin Li     TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
831*67e74705SXin Li     for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
832*67e74705SXin Li       TemplArgsInfo.addArgument(TemplArgLocs[i]);
833*67e74705SXin Li     FunctionTemplateSpecializationInfo *FTInfo
834*67e74705SXin Li         = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
835*67e74705SXin Li                                                      TemplArgList,
836*67e74705SXin Li                              HasTemplateArgumentsAsWritten ? &TemplArgsInfo
837*67e74705SXin Li                                                            : nullptr,
838*67e74705SXin Li                                                      POI);
839*67e74705SXin Li     FD->TemplateOrSpecialization = FTInfo;
840*67e74705SXin Li 
841*67e74705SXin Li     if (FD->isCanonicalDecl()) { // if canonical add to template's set.
842*67e74705SXin Li       // The template that contains the specializations set. It's not safe to
843*67e74705SXin Li       // use getCanonicalDecl on Template since it may still be initializing.
844*67e74705SXin Li       FunctionTemplateDecl *CanonTemplate
845*67e74705SXin Li         = ReadDeclAs<FunctionTemplateDecl>(Record, Idx);
846*67e74705SXin Li       // Get the InsertPos by FindNodeOrInsertPos() instead of calling
847*67e74705SXin Li       // InsertNode(FTInfo) directly to avoid the getASTContext() call in
848*67e74705SXin Li       // FunctionTemplateSpecializationInfo's Profile().
849*67e74705SXin Li       // We avoid getASTContext because a decl in the parent hierarchy may
850*67e74705SXin Li       // be initializing.
851*67e74705SXin Li       llvm::FoldingSetNodeID ID;
852*67e74705SXin Li       FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
853*67e74705SXin Li       void *InsertPos = nullptr;
854*67e74705SXin Li       FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
855*67e74705SXin Li       FunctionTemplateSpecializationInfo *ExistingInfo =
856*67e74705SXin Li           CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
857*67e74705SXin Li       if (InsertPos)
858*67e74705SXin Li         CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
859*67e74705SXin Li       else {
860*67e74705SXin Li         assert(Reader.getContext().getLangOpts().Modules &&
861*67e74705SXin Li                "already deserialized this template specialization");
862*67e74705SXin Li         mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
863*67e74705SXin Li       }
864*67e74705SXin Li     }
865*67e74705SXin Li     break;
866*67e74705SXin Li   }
867*67e74705SXin Li   case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
868*67e74705SXin Li     // Templates.
869*67e74705SXin Li     UnresolvedSet<8> TemplDecls;
870*67e74705SXin Li     unsigned NumTemplates = Record[Idx++];
871*67e74705SXin Li     while (NumTemplates--)
872*67e74705SXin Li       TemplDecls.addDecl(ReadDeclAs<NamedDecl>(Record, Idx));
873*67e74705SXin Li 
874*67e74705SXin Li     // Templates args.
875*67e74705SXin Li     TemplateArgumentListInfo TemplArgs;
876*67e74705SXin Li     unsigned NumArgs = Record[Idx++];
877*67e74705SXin Li     while (NumArgs--)
878*67e74705SXin Li       TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(F, Record, Idx));
879*67e74705SXin Li     TemplArgs.setLAngleLoc(ReadSourceLocation(Record, Idx));
880*67e74705SXin Li     TemplArgs.setRAngleLoc(ReadSourceLocation(Record, Idx));
881*67e74705SXin Li 
882*67e74705SXin Li     FD->setDependentTemplateSpecialization(Reader.getContext(),
883*67e74705SXin Li                                            TemplDecls, TemplArgs);
884*67e74705SXin Li     // These are not merged; we don't need to merge redeclarations of dependent
885*67e74705SXin Li     // template friends.
886*67e74705SXin Li     break;
887*67e74705SXin Li   }
888*67e74705SXin Li   }
889*67e74705SXin Li 
890*67e74705SXin Li   // Read in the parameters.
891*67e74705SXin Li   unsigned NumParams = Record[Idx++];
892*67e74705SXin Li   SmallVector<ParmVarDecl *, 16> Params;
893*67e74705SXin Li   Params.reserve(NumParams);
894*67e74705SXin Li   for (unsigned I = 0; I != NumParams; ++I)
895*67e74705SXin Li     Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
896*67e74705SXin Li   FD->setParams(Reader.getContext(), Params);
897*67e74705SXin Li }
898*67e74705SXin Li 
VisitObjCMethodDecl(ObjCMethodDecl * MD)899*67e74705SXin Li void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
900*67e74705SXin Li   VisitNamedDecl(MD);
901*67e74705SXin Li   if (Record[Idx++]) {
902*67e74705SXin Li     // Load the body on-demand. Most clients won't care, because method
903*67e74705SXin Li     // definitions rarely show up in headers.
904*67e74705SXin Li     Reader.PendingBodies[MD] = GetCurrentCursorOffset();
905*67e74705SXin Li     HasPendingBody = true;
906*67e74705SXin Li     MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>(Record, Idx));
907*67e74705SXin Li     MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>(Record, Idx));
908*67e74705SXin Li   }
909*67e74705SXin Li   MD->setInstanceMethod(Record[Idx++]);
910*67e74705SXin Li   MD->setVariadic(Record[Idx++]);
911*67e74705SXin Li   MD->setPropertyAccessor(Record[Idx++]);
912*67e74705SXin Li   MD->setDefined(Record[Idx++]);
913*67e74705SXin Li   MD->IsOverriding = Record[Idx++];
914*67e74705SXin Li   MD->HasSkippedBody = Record[Idx++];
915*67e74705SXin Li 
916*67e74705SXin Li   MD->IsRedeclaration = Record[Idx++];
917*67e74705SXin Li   MD->HasRedeclaration = Record[Idx++];
918*67e74705SXin Li   if (MD->HasRedeclaration)
919*67e74705SXin Li     Reader.getContext().setObjCMethodRedeclaration(MD,
920*67e74705SXin Li                                        ReadDeclAs<ObjCMethodDecl>(Record, Idx));
921*67e74705SXin Li 
922*67e74705SXin Li   MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
923*67e74705SXin Li   MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
924*67e74705SXin Li   MD->SetRelatedResultType(Record[Idx++]);
925*67e74705SXin Li   MD->setReturnType(Reader.readType(F, Record, Idx));
926*67e74705SXin Li   MD->setReturnTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
927*67e74705SXin Li   MD->DeclEndLoc = ReadSourceLocation(Record, Idx);
928*67e74705SXin Li   unsigned NumParams = Record[Idx++];
929*67e74705SXin Li   SmallVector<ParmVarDecl *, 16> Params;
930*67e74705SXin Li   Params.reserve(NumParams);
931*67e74705SXin Li   for (unsigned I = 0; I != NumParams; ++I)
932*67e74705SXin Li     Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
933*67e74705SXin Li 
934*67e74705SXin Li   MD->SelLocsKind = Record[Idx++];
935*67e74705SXin Li   unsigned NumStoredSelLocs = Record[Idx++];
936*67e74705SXin Li   SmallVector<SourceLocation, 16> SelLocs;
937*67e74705SXin Li   SelLocs.reserve(NumStoredSelLocs);
938*67e74705SXin Li   for (unsigned i = 0; i != NumStoredSelLocs; ++i)
939*67e74705SXin Li     SelLocs.push_back(ReadSourceLocation(Record, Idx));
940*67e74705SXin Li 
941*67e74705SXin Li   MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
942*67e74705SXin Li }
943*67e74705SXin Li 
VisitObjCTypeParamDecl(ObjCTypeParamDecl * D)944*67e74705SXin Li void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
945*67e74705SXin Li   VisitTypedefNameDecl(D);
946*67e74705SXin Li 
947*67e74705SXin Li   D->Variance = Record[Idx++];
948*67e74705SXin Li   D->Index = Record[Idx++];
949*67e74705SXin Li   D->VarianceLoc = ReadSourceLocation(Record, Idx);
950*67e74705SXin Li   D->ColonLoc = ReadSourceLocation(Record, Idx);
951*67e74705SXin Li }
952*67e74705SXin Li 
VisitObjCContainerDecl(ObjCContainerDecl * CD)953*67e74705SXin Li void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
954*67e74705SXin Li   VisitNamedDecl(CD);
955*67e74705SXin Li   CD->setAtStartLoc(ReadSourceLocation(Record, Idx));
956*67e74705SXin Li   CD->setAtEndRange(ReadSourceRange(Record, Idx));
957*67e74705SXin Li }
958*67e74705SXin Li 
ReadObjCTypeParamList()959*67e74705SXin Li ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
960*67e74705SXin Li   unsigned numParams = Record[Idx++];
961*67e74705SXin Li   if (numParams == 0)
962*67e74705SXin Li     return nullptr;
963*67e74705SXin Li 
964*67e74705SXin Li   SmallVector<ObjCTypeParamDecl *, 4> typeParams;
965*67e74705SXin Li   typeParams.reserve(numParams);
966*67e74705SXin Li   for (unsigned i = 0; i != numParams; ++i) {
967*67e74705SXin Li     auto typeParam = ReadDeclAs<ObjCTypeParamDecl>(Record, Idx);
968*67e74705SXin Li     if (!typeParam)
969*67e74705SXin Li       return nullptr;
970*67e74705SXin Li 
971*67e74705SXin Li     typeParams.push_back(typeParam);
972*67e74705SXin Li   }
973*67e74705SXin Li 
974*67e74705SXin Li   SourceLocation lAngleLoc = ReadSourceLocation(Record, Idx);
975*67e74705SXin Li   SourceLocation rAngleLoc = ReadSourceLocation(Record, Idx);
976*67e74705SXin Li 
977*67e74705SXin Li   return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
978*67e74705SXin Li                                    typeParams, rAngleLoc);
979*67e74705SXin Li }
980*67e74705SXin Li 
VisitObjCInterfaceDecl(ObjCInterfaceDecl * ID)981*67e74705SXin Li void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
982*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(ID);
983*67e74705SXin Li   VisitObjCContainerDecl(ID);
984*67e74705SXin Li   TypeIDForTypeDecl = Reader.getGlobalTypeID(F, Record[Idx++]);
985*67e74705SXin Li   mergeRedeclarable(ID, Redecl);
986*67e74705SXin Li 
987*67e74705SXin Li   ID->TypeParamList = ReadObjCTypeParamList();
988*67e74705SXin Li   if (Record[Idx++]) {
989*67e74705SXin Li     // Read the definition.
990*67e74705SXin Li     ID->allocateDefinitionData();
991*67e74705SXin Li 
992*67e74705SXin Li     // Set the definition data of the canonical declaration, so other
993*67e74705SXin Li     // redeclarations will see it.
994*67e74705SXin Li     ID->getCanonicalDecl()->Data = ID->Data;
995*67e74705SXin Li 
996*67e74705SXin Li     ObjCInterfaceDecl::DefinitionData &Data = ID->data();
997*67e74705SXin Li 
998*67e74705SXin Li     // Read the superclass.
999*67e74705SXin Li     Data.SuperClassTInfo = GetTypeSourceInfo(Record, Idx);
1000*67e74705SXin Li 
1001*67e74705SXin Li     Data.EndLoc = ReadSourceLocation(Record, Idx);
1002*67e74705SXin Li     Data.HasDesignatedInitializers = Record[Idx++];
1003*67e74705SXin Li 
1004*67e74705SXin Li     // Read the directly referenced protocols and their SourceLocations.
1005*67e74705SXin Li     unsigned NumProtocols = Record[Idx++];
1006*67e74705SXin Li     SmallVector<ObjCProtocolDecl *, 16> Protocols;
1007*67e74705SXin Li     Protocols.reserve(NumProtocols);
1008*67e74705SXin Li     for (unsigned I = 0; I != NumProtocols; ++I)
1009*67e74705SXin Li       Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
1010*67e74705SXin Li     SmallVector<SourceLocation, 16> ProtoLocs;
1011*67e74705SXin Li     ProtoLocs.reserve(NumProtocols);
1012*67e74705SXin Li     for (unsigned I = 0; I != NumProtocols; ++I)
1013*67e74705SXin Li       ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
1014*67e74705SXin Li     ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
1015*67e74705SXin Li                         Reader.getContext());
1016*67e74705SXin Li 
1017*67e74705SXin Li     // Read the transitive closure of protocols referenced by this class.
1018*67e74705SXin Li     NumProtocols = Record[Idx++];
1019*67e74705SXin Li     Protocols.clear();
1020*67e74705SXin Li     Protocols.reserve(NumProtocols);
1021*67e74705SXin Li     for (unsigned I = 0; I != NumProtocols; ++I)
1022*67e74705SXin Li       Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
1023*67e74705SXin Li     ID->data().AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1024*67e74705SXin Li                                           Reader.getContext());
1025*67e74705SXin Li 
1026*67e74705SXin Li     // We will rebuild this list lazily.
1027*67e74705SXin Li     ID->setIvarList(nullptr);
1028*67e74705SXin Li 
1029*67e74705SXin Li     // Note that we have deserialized a definition.
1030*67e74705SXin Li     Reader.PendingDefinitions.insert(ID);
1031*67e74705SXin Li 
1032*67e74705SXin Li     // Note that we've loaded this Objective-C class.
1033*67e74705SXin Li     Reader.ObjCClassesLoaded.push_back(ID);
1034*67e74705SXin Li   } else {
1035*67e74705SXin Li     ID->Data = ID->getCanonicalDecl()->Data;
1036*67e74705SXin Li   }
1037*67e74705SXin Li }
1038*67e74705SXin Li 
VisitObjCIvarDecl(ObjCIvarDecl * IVD)1039*67e74705SXin Li void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
1040*67e74705SXin Li   VisitFieldDecl(IVD);
1041*67e74705SXin Li   IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
1042*67e74705SXin Li   // This field will be built lazily.
1043*67e74705SXin Li   IVD->setNextIvar(nullptr);
1044*67e74705SXin Li   bool synth = Record[Idx++];
1045*67e74705SXin Li   IVD->setSynthesize(synth);
1046*67e74705SXin Li }
1047*67e74705SXin Li 
VisitObjCProtocolDecl(ObjCProtocolDecl * PD)1048*67e74705SXin Li void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1049*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(PD);
1050*67e74705SXin Li   VisitObjCContainerDecl(PD);
1051*67e74705SXin Li   mergeRedeclarable(PD, Redecl);
1052*67e74705SXin Li 
1053*67e74705SXin Li   if (Record[Idx++]) {
1054*67e74705SXin Li     // Read the definition.
1055*67e74705SXin Li     PD->allocateDefinitionData();
1056*67e74705SXin Li 
1057*67e74705SXin Li     // Set the definition data of the canonical declaration, so other
1058*67e74705SXin Li     // redeclarations will see it.
1059*67e74705SXin Li     PD->getCanonicalDecl()->Data = PD->Data;
1060*67e74705SXin Li 
1061*67e74705SXin Li     unsigned NumProtoRefs = Record[Idx++];
1062*67e74705SXin Li     SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1063*67e74705SXin Li     ProtoRefs.reserve(NumProtoRefs);
1064*67e74705SXin Li     for (unsigned I = 0; I != NumProtoRefs; ++I)
1065*67e74705SXin Li       ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
1066*67e74705SXin Li     SmallVector<SourceLocation, 16> ProtoLocs;
1067*67e74705SXin Li     ProtoLocs.reserve(NumProtoRefs);
1068*67e74705SXin Li     for (unsigned I = 0; I != NumProtoRefs; ++I)
1069*67e74705SXin Li       ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
1070*67e74705SXin Li     PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1071*67e74705SXin Li                         Reader.getContext());
1072*67e74705SXin Li 
1073*67e74705SXin Li     // Note that we have deserialized a definition.
1074*67e74705SXin Li     Reader.PendingDefinitions.insert(PD);
1075*67e74705SXin Li   } else {
1076*67e74705SXin Li     PD->Data = PD->getCanonicalDecl()->Data;
1077*67e74705SXin Li   }
1078*67e74705SXin Li }
1079*67e74705SXin Li 
VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl * FD)1080*67e74705SXin Li void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
1081*67e74705SXin Li   VisitFieldDecl(FD);
1082*67e74705SXin Li }
1083*67e74705SXin Li 
VisitObjCCategoryDecl(ObjCCategoryDecl * CD)1084*67e74705SXin Li void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
1085*67e74705SXin Li   VisitObjCContainerDecl(CD);
1086*67e74705SXin Li   CD->setCategoryNameLoc(ReadSourceLocation(Record, Idx));
1087*67e74705SXin Li   CD->setIvarLBraceLoc(ReadSourceLocation(Record, Idx));
1088*67e74705SXin Li   CD->setIvarRBraceLoc(ReadSourceLocation(Record, Idx));
1089*67e74705SXin Li 
1090*67e74705SXin Li   // Note that this category has been deserialized. We do this before
1091*67e74705SXin Li   // deserializing the interface declaration, so that it will consider this
1092*67e74705SXin Li   /// category.
1093*67e74705SXin Li   Reader.CategoriesDeserialized.insert(CD);
1094*67e74705SXin Li 
1095*67e74705SXin Li   CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx);
1096*67e74705SXin Li   CD->TypeParamList = ReadObjCTypeParamList();
1097*67e74705SXin Li   unsigned NumProtoRefs = Record[Idx++];
1098*67e74705SXin Li   SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1099*67e74705SXin Li   ProtoRefs.reserve(NumProtoRefs);
1100*67e74705SXin Li   for (unsigned I = 0; I != NumProtoRefs; ++I)
1101*67e74705SXin Li     ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
1102*67e74705SXin Li   SmallVector<SourceLocation, 16> ProtoLocs;
1103*67e74705SXin Li   ProtoLocs.reserve(NumProtoRefs);
1104*67e74705SXin Li   for (unsigned I = 0; I != NumProtoRefs; ++I)
1105*67e74705SXin Li     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
1106*67e74705SXin Li   CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1107*67e74705SXin Li                       Reader.getContext());
1108*67e74705SXin Li }
1109*67e74705SXin Li 
VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl * CAD)1110*67e74705SXin Li void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
1111*67e74705SXin Li   VisitNamedDecl(CAD);
1112*67e74705SXin Li   CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
1113*67e74705SXin Li }
1114*67e74705SXin Li 
VisitObjCPropertyDecl(ObjCPropertyDecl * D)1115*67e74705SXin Li void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
1116*67e74705SXin Li   VisitNamedDecl(D);
1117*67e74705SXin Li   D->setAtLoc(ReadSourceLocation(Record, Idx));
1118*67e74705SXin Li   D->setLParenLoc(ReadSourceLocation(Record, Idx));
1119*67e74705SXin Li   QualType T = Reader.readType(F, Record, Idx);
1120*67e74705SXin Li   TypeSourceInfo *TSI = GetTypeSourceInfo(Record, Idx);
1121*67e74705SXin Li   D->setType(T, TSI);
1122*67e74705SXin Li   D->setPropertyAttributes(
1123*67e74705SXin Li                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
1124*67e74705SXin Li   D->setPropertyAttributesAsWritten(
1125*67e74705SXin Li                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
1126*67e74705SXin Li   D->setPropertyImplementation(
1127*67e74705SXin Li                             (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
1128*67e74705SXin Li   D->setGetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector());
1129*67e74705SXin Li   D->setSetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector());
1130*67e74705SXin Li   D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
1131*67e74705SXin Li   D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
1132*67e74705SXin Li   D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>(Record, Idx));
1133*67e74705SXin Li }
1134*67e74705SXin Li 
VisitObjCImplDecl(ObjCImplDecl * D)1135*67e74705SXin Li void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
1136*67e74705SXin Li   VisitObjCContainerDecl(D);
1137*67e74705SXin Li   D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
1138*67e74705SXin Li }
1139*67e74705SXin Li 
VisitObjCCategoryImplDecl(ObjCCategoryImplDecl * D)1140*67e74705SXin Li void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1141*67e74705SXin Li   VisitObjCImplDecl(D);
1142*67e74705SXin Li   D->setIdentifier(Reader.GetIdentifierInfo(F, Record, Idx));
1143*67e74705SXin Li   D->CategoryNameLoc = ReadSourceLocation(Record, Idx);
1144*67e74705SXin Li }
1145*67e74705SXin Li 
VisitObjCImplementationDecl(ObjCImplementationDecl * D)1146*67e74705SXin Li void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1147*67e74705SXin Li   VisitObjCImplDecl(D);
1148*67e74705SXin Li   D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
1149*67e74705SXin Li   D->SuperLoc = ReadSourceLocation(Record, Idx);
1150*67e74705SXin Li   D->setIvarLBraceLoc(ReadSourceLocation(Record, Idx));
1151*67e74705SXin Li   D->setIvarRBraceLoc(ReadSourceLocation(Record, Idx));
1152*67e74705SXin Li   D->setHasNonZeroConstructors(Record[Idx++]);
1153*67e74705SXin Li   D->setHasDestructors(Record[Idx++]);
1154*67e74705SXin Li   D->NumIvarInitializers = Record[Idx++];
1155*67e74705SXin Li   if (D->NumIvarInitializers)
1156*67e74705SXin Li     D->IvarInitializers = ReadGlobalOffset(F, Record, Idx);
1157*67e74705SXin Li }
1158*67e74705SXin Li 
VisitObjCPropertyImplDecl(ObjCPropertyImplDecl * D)1159*67e74705SXin Li void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
1160*67e74705SXin Li   VisitDecl(D);
1161*67e74705SXin Li   D->setAtLoc(ReadSourceLocation(Record, Idx));
1162*67e74705SXin Li   D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>(Record, Idx));
1163*67e74705SXin Li   D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>(Record, Idx);
1164*67e74705SXin Li   D->IvarLoc = ReadSourceLocation(Record, Idx);
1165*67e74705SXin Li   D->setGetterCXXConstructor(Reader.ReadExpr(F));
1166*67e74705SXin Li   D->setSetterCXXAssignment(Reader.ReadExpr(F));
1167*67e74705SXin Li }
1168*67e74705SXin Li 
VisitFieldDecl(FieldDecl * FD)1169*67e74705SXin Li void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
1170*67e74705SXin Li   VisitDeclaratorDecl(FD);
1171*67e74705SXin Li   FD->Mutable = Record[Idx++];
1172*67e74705SXin Li   if (int BitWidthOrInitializer = Record[Idx++]) {
1173*67e74705SXin Li     FD->InitStorage.setInt(
1174*67e74705SXin Li           static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1175*67e74705SXin Li     if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
1176*67e74705SXin Li       // Read captured variable length array.
1177*67e74705SXin Li       FD->InitStorage.setPointer(
1178*67e74705SXin Li           Reader.readType(F, Record, Idx).getAsOpaquePtr());
1179*67e74705SXin Li     } else {
1180*67e74705SXin Li       FD->InitStorage.setPointer(Reader.ReadExpr(F));
1181*67e74705SXin Li     }
1182*67e74705SXin Li   }
1183*67e74705SXin Li   if (!FD->getDeclName()) {
1184*67e74705SXin Li     if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>(Record, Idx))
1185*67e74705SXin Li       Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
1186*67e74705SXin Li   }
1187*67e74705SXin Li   mergeMergeable(FD);
1188*67e74705SXin Li }
1189*67e74705SXin Li 
VisitMSPropertyDecl(MSPropertyDecl * PD)1190*67e74705SXin Li void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1191*67e74705SXin Li   VisitDeclaratorDecl(PD);
1192*67e74705SXin Li   PD->GetterId = Reader.GetIdentifierInfo(F, Record, Idx);
1193*67e74705SXin Li   PD->SetterId = Reader.GetIdentifierInfo(F, Record, Idx);
1194*67e74705SXin Li }
1195*67e74705SXin Li 
VisitIndirectFieldDecl(IndirectFieldDecl * FD)1196*67e74705SXin Li void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1197*67e74705SXin Li   VisitValueDecl(FD);
1198*67e74705SXin Li 
1199*67e74705SXin Li   FD->ChainingSize = Record[Idx++];
1200*67e74705SXin Li   assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
1201*67e74705SXin Li   FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
1202*67e74705SXin Li 
1203*67e74705SXin Li   for (unsigned I = 0; I != FD->ChainingSize; ++I)
1204*67e74705SXin Li     FD->Chaining[I] = ReadDeclAs<NamedDecl>(Record, Idx);
1205*67e74705SXin Li 
1206*67e74705SXin Li   mergeMergeable(FD);
1207*67e74705SXin Li }
1208*67e74705SXin Li 
VisitVarDeclImpl(VarDecl * VD)1209*67e74705SXin Li ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
1210*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(VD);
1211*67e74705SXin Li   VisitDeclaratorDecl(VD);
1212*67e74705SXin Li 
1213*67e74705SXin Li   VD->VarDeclBits.SClass = (StorageClass)Record[Idx++];
1214*67e74705SXin Li   VD->VarDeclBits.TSCSpec = Record[Idx++];
1215*67e74705SXin Li   VD->VarDeclBits.InitStyle = Record[Idx++];
1216*67e74705SXin Li   if (!isa<ParmVarDecl>(VD)) {
1217*67e74705SXin Li     VD->NonParmVarDeclBits.ExceptionVar = Record[Idx++];
1218*67e74705SXin Li     VD->NonParmVarDeclBits.NRVOVariable = Record[Idx++];
1219*67e74705SXin Li     VD->NonParmVarDeclBits.CXXForRangeDecl = Record[Idx++];
1220*67e74705SXin Li     VD->NonParmVarDeclBits.ARCPseudoStrong = Record[Idx++];
1221*67e74705SXin Li     VD->NonParmVarDeclBits.IsInline = Record[Idx++];
1222*67e74705SXin Li     VD->NonParmVarDeclBits.IsInlineSpecified = Record[Idx++];
1223*67e74705SXin Li     VD->NonParmVarDeclBits.IsConstexpr = Record[Idx++];
1224*67e74705SXin Li     VD->NonParmVarDeclBits.IsInitCapture = Record[Idx++];
1225*67e74705SXin Li     VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record[Idx++];
1226*67e74705SXin Li   }
1227*67e74705SXin Li   Linkage VarLinkage = Linkage(Record[Idx++]);
1228*67e74705SXin Li   VD->setCachedLinkage(VarLinkage);
1229*67e74705SXin Li 
1230*67e74705SXin Li   // Reconstruct the one piece of the IdentifierNamespace that we need.
1231*67e74705SXin Li   if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
1232*67e74705SXin Li       VD->getLexicalDeclContext()->isFunctionOrMethod())
1233*67e74705SXin Li     VD->setLocalExternDecl();
1234*67e74705SXin Li 
1235*67e74705SXin Li   if (uint64_t Val = Record[Idx++]) {
1236*67e74705SXin Li     VD->setInit(Reader.ReadExpr(F));
1237*67e74705SXin Li     if (Val > 1) {
1238*67e74705SXin Li       EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1239*67e74705SXin Li       Eval->CheckedICE = true;
1240*67e74705SXin Li       Eval->IsICE = Val == 3;
1241*67e74705SXin Li     }
1242*67e74705SXin Li   }
1243*67e74705SXin Li 
1244*67e74705SXin Li   enum VarKind {
1245*67e74705SXin Li     VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1246*67e74705SXin Li   };
1247*67e74705SXin Li   switch ((VarKind)Record[Idx++]) {
1248*67e74705SXin Li   case VarNotTemplate:
1249*67e74705SXin Li     // Only true variables (not parameters or implicit parameters) can be
1250*67e74705SXin Li     // merged; the other kinds are not really redeclarable at all.
1251*67e74705SXin Li     if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1252*67e74705SXin Li         !isa<VarTemplateSpecializationDecl>(VD))
1253*67e74705SXin Li       mergeRedeclarable(VD, Redecl);
1254*67e74705SXin Li     break;
1255*67e74705SXin Li   case VarTemplate:
1256*67e74705SXin Li     // Merged when we merge the template.
1257*67e74705SXin Li     VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>(Record, Idx));
1258*67e74705SXin Li     break;
1259*67e74705SXin Li   case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
1260*67e74705SXin Li     VarDecl *Tmpl = ReadDeclAs<VarDecl>(Record, Idx);
1261*67e74705SXin Li     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
1262*67e74705SXin Li     SourceLocation POI = ReadSourceLocation(Record, Idx);
1263*67e74705SXin Li     Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
1264*67e74705SXin Li     mergeRedeclarable(VD, Redecl);
1265*67e74705SXin Li     break;
1266*67e74705SXin Li   }
1267*67e74705SXin Li   }
1268*67e74705SXin Li 
1269*67e74705SXin Li   return Redecl;
1270*67e74705SXin Li }
1271*67e74705SXin Li 
VisitImplicitParamDecl(ImplicitParamDecl * PD)1272*67e74705SXin Li void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
1273*67e74705SXin Li   VisitVarDecl(PD);
1274*67e74705SXin Li }
1275*67e74705SXin Li 
VisitParmVarDecl(ParmVarDecl * PD)1276*67e74705SXin Li void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
1277*67e74705SXin Li   VisitVarDecl(PD);
1278*67e74705SXin Li   unsigned isObjCMethodParam = Record[Idx++];
1279*67e74705SXin Li   unsigned scopeDepth = Record[Idx++];
1280*67e74705SXin Li   unsigned scopeIndex = Record[Idx++];
1281*67e74705SXin Li   unsigned declQualifier = Record[Idx++];
1282*67e74705SXin Li   if (isObjCMethodParam) {
1283*67e74705SXin Li     assert(scopeDepth == 0);
1284*67e74705SXin Li     PD->setObjCMethodScopeInfo(scopeIndex);
1285*67e74705SXin Li     PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1286*67e74705SXin Li   } else {
1287*67e74705SXin Li     PD->setScopeInfo(scopeDepth, scopeIndex);
1288*67e74705SXin Li   }
1289*67e74705SXin Li   PD->ParmVarDeclBits.IsKNRPromoted = Record[Idx++];
1290*67e74705SXin Li   PD->ParmVarDeclBits.HasInheritedDefaultArg = Record[Idx++];
1291*67e74705SXin Li   if (Record[Idx++]) // hasUninstantiatedDefaultArg.
1292*67e74705SXin Li     PD->setUninstantiatedDefaultArg(Reader.ReadExpr(F));
1293*67e74705SXin Li 
1294*67e74705SXin Li   // FIXME: If this is a redeclaration of a function from another module, handle
1295*67e74705SXin Li   // inheritance of default arguments.
1296*67e74705SXin Li }
1297*67e74705SXin Li 
VisitFileScopeAsmDecl(FileScopeAsmDecl * AD)1298*67e74705SXin Li void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
1299*67e74705SXin Li   VisitDecl(AD);
1300*67e74705SXin Li   AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(F)));
1301*67e74705SXin Li   AD->setRParenLoc(ReadSourceLocation(Record, Idx));
1302*67e74705SXin Li }
1303*67e74705SXin Li 
VisitBlockDecl(BlockDecl * BD)1304*67e74705SXin Li void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
1305*67e74705SXin Li   VisitDecl(BD);
1306*67e74705SXin Li   BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(F)));
1307*67e74705SXin Li   BD->setSignatureAsWritten(GetTypeSourceInfo(Record, Idx));
1308*67e74705SXin Li   unsigned NumParams = Record[Idx++];
1309*67e74705SXin Li   SmallVector<ParmVarDecl *, 16> Params;
1310*67e74705SXin Li   Params.reserve(NumParams);
1311*67e74705SXin Li   for (unsigned I = 0; I != NumParams; ++I)
1312*67e74705SXin Li     Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
1313*67e74705SXin Li   BD->setParams(Params);
1314*67e74705SXin Li 
1315*67e74705SXin Li   BD->setIsVariadic(Record[Idx++]);
1316*67e74705SXin Li   BD->setBlockMissingReturnType(Record[Idx++]);
1317*67e74705SXin Li   BD->setIsConversionFromLambda(Record[Idx++]);
1318*67e74705SXin Li 
1319*67e74705SXin Li   bool capturesCXXThis = Record[Idx++];
1320*67e74705SXin Li   unsigned numCaptures = Record[Idx++];
1321*67e74705SXin Li   SmallVector<BlockDecl::Capture, 16> captures;
1322*67e74705SXin Li   captures.reserve(numCaptures);
1323*67e74705SXin Li   for (unsigned i = 0; i != numCaptures; ++i) {
1324*67e74705SXin Li     VarDecl *decl = ReadDeclAs<VarDecl>(Record, Idx);
1325*67e74705SXin Li     unsigned flags = Record[Idx++];
1326*67e74705SXin Li     bool byRef = (flags & 1);
1327*67e74705SXin Li     bool nested = (flags & 2);
1328*67e74705SXin Li     Expr *copyExpr = ((flags & 4) ? Reader.ReadExpr(F) : nullptr);
1329*67e74705SXin Li 
1330*67e74705SXin Li     captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1331*67e74705SXin Li   }
1332*67e74705SXin Li   BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
1333*67e74705SXin Li }
1334*67e74705SXin Li 
VisitCapturedDecl(CapturedDecl * CD)1335*67e74705SXin Li void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1336*67e74705SXin Li   VisitDecl(CD);
1337*67e74705SXin Li   unsigned ContextParamPos = Record[Idx++];
1338*67e74705SXin Li   CD->setNothrow(Record[Idx++] != 0);
1339*67e74705SXin Li   // Body is set by VisitCapturedStmt.
1340*67e74705SXin Li   for (unsigned I = 0; I < CD->NumParams; ++I) {
1341*67e74705SXin Li     if (I != ContextParamPos)
1342*67e74705SXin Li       CD->setParam(I, ReadDeclAs<ImplicitParamDecl>(Record, Idx));
1343*67e74705SXin Li     else
1344*67e74705SXin Li       CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>(Record, Idx));
1345*67e74705SXin Li   }
1346*67e74705SXin Li }
1347*67e74705SXin Li 
VisitLinkageSpecDecl(LinkageSpecDecl * D)1348*67e74705SXin Li void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1349*67e74705SXin Li   VisitDecl(D);
1350*67e74705SXin Li   D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]);
1351*67e74705SXin Li   D->setExternLoc(ReadSourceLocation(Record, Idx));
1352*67e74705SXin Li   D->setRBraceLoc(ReadSourceLocation(Record, Idx));
1353*67e74705SXin Li }
1354*67e74705SXin Li 
VisitLabelDecl(LabelDecl * D)1355*67e74705SXin Li void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1356*67e74705SXin Li   VisitNamedDecl(D);
1357*67e74705SXin Li   D->setLocStart(ReadSourceLocation(Record, Idx));
1358*67e74705SXin Li }
1359*67e74705SXin Li 
VisitNamespaceDecl(NamespaceDecl * D)1360*67e74705SXin Li void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
1361*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(D);
1362*67e74705SXin Li   VisitNamedDecl(D);
1363*67e74705SXin Li   D->setInline(Record[Idx++]);
1364*67e74705SXin Li   D->LocStart = ReadSourceLocation(Record, Idx);
1365*67e74705SXin Li   D->RBraceLoc = ReadSourceLocation(Record, Idx);
1366*67e74705SXin Li 
1367*67e74705SXin Li   // Defer loading the anonymous namespace until we've finished merging
1368*67e74705SXin Li   // this namespace; loading it might load a later declaration of the
1369*67e74705SXin Li   // same namespace, and we have an invariant that older declarations
1370*67e74705SXin Li   // get merged before newer ones try to merge.
1371*67e74705SXin Li   GlobalDeclID AnonNamespace = 0;
1372*67e74705SXin Li   if (Redecl.getFirstID() == ThisDeclID) {
1373*67e74705SXin Li     AnonNamespace = ReadDeclID(Record, Idx);
1374*67e74705SXin Li   } else {
1375*67e74705SXin Li     // Link this namespace back to the first declaration, which has already
1376*67e74705SXin Li     // been deserialized.
1377*67e74705SXin Li     D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
1378*67e74705SXin Li   }
1379*67e74705SXin Li 
1380*67e74705SXin Li   mergeRedeclarable(D, Redecl);
1381*67e74705SXin Li 
1382*67e74705SXin Li   if (AnonNamespace) {
1383*67e74705SXin Li     // Each module has its own anonymous namespace, which is disjoint from
1384*67e74705SXin Li     // any other module's anonymous namespaces, so don't attach the anonymous
1385*67e74705SXin Li     // namespace at all.
1386*67e74705SXin Li     NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
1387*67e74705SXin Li     if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule)
1388*67e74705SXin Li       D->setAnonymousNamespace(Anon);
1389*67e74705SXin Li   }
1390*67e74705SXin Li }
1391*67e74705SXin Li 
VisitNamespaceAliasDecl(NamespaceAliasDecl * D)1392*67e74705SXin Li void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1393*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(D);
1394*67e74705SXin Li   VisitNamedDecl(D);
1395*67e74705SXin Li   D->NamespaceLoc = ReadSourceLocation(Record, Idx);
1396*67e74705SXin Li   D->IdentLoc = ReadSourceLocation(Record, Idx);
1397*67e74705SXin Li   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1398*67e74705SXin Li   D->Namespace = ReadDeclAs<NamedDecl>(Record, Idx);
1399*67e74705SXin Li   mergeRedeclarable(D, Redecl);
1400*67e74705SXin Li }
1401*67e74705SXin Li 
VisitUsingDecl(UsingDecl * D)1402*67e74705SXin Li void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
1403*67e74705SXin Li   VisitNamedDecl(D);
1404*67e74705SXin Li   D->setUsingLoc(ReadSourceLocation(Record, Idx));
1405*67e74705SXin Li   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1406*67e74705SXin Li   ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx);
1407*67e74705SXin Li   D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>(Record, Idx));
1408*67e74705SXin Li   D->setTypename(Record[Idx++]);
1409*67e74705SXin Li   if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>(Record, Idx))
1410*67e74705SXin Li     Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
1411*67e74705SXin Li   mergeMergeable(D);
1412*67e74705SXin Li }
1413*67e74705SXin Li 
VisitUsingShadowDecl(UsingShadowDecl * D)1414*67e74705SXin Li void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
1415*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(D);
1416*67e74705SXin Li   VisitNamedDecl(D);
1417*67e74705SXin Li   D->setTargetDecl(ReadDeclAs<NamedDecl>(Record, Idx));
1418*67e74705SXin Li   D->UsingOrNextShadow = ReadDeclAs<NamedDecl>(Record, Idx);
1419*67e74705SXin Li   UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>(Record, Idx);
1420*67e74705SXin Li   if (Pattern)
1421*67e74705SXin Li     Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
1422*67e74705SXin Li   mergeRedeclarable(D, Redecl);
1423*67e74705SXin Li }
1424*67e74705SXin Li 
VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl * D)1425*67e74705SXin Li void ASTDeclReader::VisitConstructorUsingShadowDecl(
1426*67e74705SXin Li     ConstructorUsingShadowDecl *D) {
1427*67e74705SXin Li   VisitUsingShadowDecl(D);
1428*67e74705SXin Li   D->NominatedBaseClassShadowDecl =
1429*67e74705SXin Li       ReadDeclAs<ConstructorUsingShadowDecl>(Record, Idx);
1430*67e74705SXin Li   D->ConstructedBaseClassShadowDecl =
1431*67e74705SXin Li       ReadDeclAs<ConstructorUsingShadowDecl>(Record, Idx);
1432*67e74705SXin Li   D->IsVirtual = Record[Idx++];
1433*67e74705SXin Li }
1434*67e74705SXin Li 
VisitUsingDirectiveDecl(UsingDirectiveDecl * D)1435*67e74705SXin Li void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1436*67e74705SXin Li   VisitNamedDecl(D);
1437*67e74705SXin Li   D->UsingLoc = ReadSourceLocation(Record, Idx);
1438*67e74705SXin Li   D->NamespaceLoc = ReadSourceLocation(Record, Idx);
1439*67e74705SXin Li   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1440*67e74705SXin Li   D->NominatedNamespace = ReadDeclAs<NamedDecl>(Record, Idx);
1441*67e74705SXin Li   D->CommonAncestor = ReadDeclAs<DeclContext>(Record, Idx);
1442*67e74705SXin Li }
1443*67e74705SXin Li 
VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl * D)1444*67e74705SXin Li void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1445*67e74705SXin Li   VisitValueDecl(D);
1446*67e74705SXin Li   D->setUsingLoc(ReadSourceLocation(Record, Idx));
1447*67e74705SXin Li   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1448*67e74705SXin Li   ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx);
1449*67e74705SXin Li   mergeMergeable(D);
1450*67e74705SXin Li }
1451*67e74705SXin Li 
VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl * D)1452*67e74705SXin Li void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
1453*67e74705SXin Li                                                UnresolvedUsingTypenameDecl *D) {
1454*67e74705SXin Li   VisitTypeDecl(D);
1455*67e74705SXin Li   D->TypenameLocation = ReadSourceLocation(Record, Idx);
1456*67e74705SXin Li   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1457*67e74705SXin Li   mergeMergeable(D);
1458*67e74705SXin Li }
1459*67e74705SXin Li 
ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData & Data,const RecordData & Record,unsigned & Idx)1460*67e74705SXin Li void ASTDeclReader::ReadCXXDefinitionData(
1461*67e74705SXin Li                                    struct CXXRecordDecl::DefinitionData &Data,
1462*67e74705SXin Li                                    const RecordData &Record, unsigned &Idx) {
1463*67e74705SXin Li   // Note: the caller has deserialized the IsLambda bit already.
1464*67e74705SXin Li   Data.UserDeclaredConstructor = Record[Idx++];
1465*67e74705SXin Li   Data.UserDeclaredSpecialMembers = Record[Idx++];
1466*67e74705SXin Li   Data.Aggregate = Record[Idx++];
1467*67e74705SXin Li   Data.PlainOldData = Record[Idx++];
1468*67e74705SXin Li   Data.Empty = Record[Idx++];
1469*67e74705SXin Li   Data.Polymorphic = Record[Idx++];
1470*67e74705SXin Li   Data.Abstract = Record[Idx++];
1471*67e74705SXin Li   Data.IsStandardLayout = Record[Idx++];
1472*67e74705SXin Li   Data.HasNoNonEmptyBases = Record[Idx++];
1473*67e74705SXin Li   Data.HasPrivateFields = Record[Idx++];
1474*67e74705SXin Li   Data.HasProtectedFields = Record[Idx++];
1475*67e74705SXin Li   Data.HasPublicFields = Record[Idx++];
1476*67e74705SXin Li   Data.HasMutableFields = Record[Idx++];
1477*67e74705SXin Li   Data.HasVariantMembers = Record[Idx++];
1478*67e74705SXin Li   Data.HasOnlyCMembers = Record[Idx++];
1479*67e74705SXin Li   Data.HasInClassInitializer = Record[Idx++];
1480*67e74705SXin Li   Data.HasUninitializedReferenceMember = Record[Idx++];
1481*67e74705SXin Li   Data.HasUninitializedFields = Record[Idx++];
1482*67e74705SXin Li   Data.HasInheritedConstructor = Record[Idx++];
1483*67e74705SXin Li   Data.HasInheritedAssignment = Record[Idx++];
1484*67e74705SXin Li   Data.NeedOverloadResolutionForMoveConstructor = Record[Idx++];
1485*67e74705SXin Li   Data.NeedOverloadResolutionForMoveAssignment = Record[Idx++];
1486*67e74705SXin Li   Data.NeedOverloadResolutionForDestructor = Record[Idx++];
1487*67e74705SXin Li   Data.DefaultedMoveConstructorIsDeleted = Record[Idx++];
1488*67e74705SXin Li   Data.DefaultedMoveAssignmentIsDeleted = Record[Idx++];
1489*67e74705SXin Li   Data.DefaultedDestructorIsDeleted = Record[Idx++];
1490*67e74705SXin Li   Data.HasTrivialSpecialMembers = Record[Idx++];
1491*67e74705SXin Li   Data.DeclaredNonTrivialSpecialMembers = Record[Idx++];
1492*67e74705SXin Li   Data.HasIrrelevantDestructor = Record[Idx++];
1493*67e74705SXin Li   Data.HasConstexprNonCopyMoveConstructor = Record[Idx++];
1494*67e74705SXin Li   Data.HasDefaultedDefaultConstructor = Record[Idx++];
1495*67e74705SXin Li   Data.DefaultedDefaultConstructorIsConstexpr = Record[Idx++];
1496*67e74705SXin Li   Data.HasConstexprDefaultConstructor = Record[Idx++];
1497*67e74705SXin Li   Data.HasNonLiteralTypeFieldsOrBases = Record[Idx++];
1498*67e74705SXin Li   Data.ComputedVisibleConversions = Record[Idx++];
1499*67e74705SXin Li   Data.UserProvidedDefaultConstructor = Record[Idx++];
1500*67e74705SXin Li   Data.DeclaredSpecialMembers = Record[Idx++];
1501*67e74705SXin Li   Data.ImplicitCopyConstructorHasConstParam = Record[Idx++];
1502*67e74705SXin Li   Data.ImplicitCopyAssignmentHasConstParam = Record[Idx++];
1503*67e74705SXin Li   Data.HasDeclaredCopyConstructorWithConstParam = Record[Idx++];
1504*67e74705SXin Li   Data.HasDeclaredCopyAssignmentWithConstParam = Record[Idx++];
1505*67e74705SXin Li 
1506*67e74705SXin Li   Data.NumBases = Record[Idx++];
1507*67e74705SXin Li   if (Data.NumBases)
1508*67e74705SXin Li     Data.Bases = ReadGlobalOffset(F, Record, Idx);
1509*67e74705SXin Li   Data.NumVBases = Record[Idx++];
1510*67e74705SXin Li   if (Data.NumVBases)
1511*67e74705SXin Li     Data.VBases = ReadGlobalOffset(F, Record, Idx);
1512*67e74705SXin Li 
1513*67e74705SXin Li   Reader.ReadUnresolvedSet(F, Data.Conversions, Record, Idx);
1514*67e74705SXin Li   Reader.ReadUnresolvedSet(F, Data.VisibleConversions, Record, Idx);
1515*67e74705SXin Li   assert(Data.Definition && "Data.Definition should be already set!");
1516*67e74705SXin Li   Data.FirstFriend = ReadDeclID(Record, Idx);
1517*67e74705SXin Li 
1518*67e74705SXin Li   if (Data.IsLambda) {
1519*67e74705SXin Li     typedef LambdaCapture Capture;
1520*67e74705SXin Li     CXXRecordDecl::LambdaDefinitionData &Lambda
1521*67e74705SXin Li       = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
1522*67e74705SXin Li     Lambda.Dependent = Record[Idx++];
1523*67e74705SXin Li     Lambda.IsGenericLambda = Record[Idx++];
1524*67e74705SXin Li     Lambda.CaptureDefault = Record[Idx++];
1525*67e74705SXin Li     Lambda.NumCaptures = Record[Idx++];
1526*67e74705SXin Li     Lambda.NumExplicitCaptures = Record[Idx++];
1527*67e74705SXin Li     Lambda.ManglingNumber = Record[Idx++];
1528*67e74705SXin Li     Lambda.ContextDecl = ReadDecl(Record, Idx);
1529*67e74705SXin Li     Lambda.Captures
1530*67e74705SXin Li       = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures);
1531*67e74705SXin Li     Capture *ToCapture = Lambda.Captures;
1532*67e74705SXin Li     Lambda.MethodTyInfo = GetTypeSourceInfo(Record, Idx);
1533*67e74705SXin Li     for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
1534*67e74705SXin Li       SourceLocation Loc = ReadSourceLocation(Record, Idx);
1535*67e74705SXin Li       bool IsImplicit = Record[Idx++];
1536*67e74705SXin Li       LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record[Idx++]);
1537*67e74705SXin Li       switch (Kind) {
1538*67e74705SXin Li       case LCK_StarThis:
1539*67e74705SXin Li       case LCK_This:
1540*67e74705SXin Li       case LCK_VLAType:
1541*67e74705SXin Li         *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
1542*67e74705SXin Li         break;
1543*67e74705SXin Li       case LCK_ByCopy:
1544*67e74705SXin Li       case LCK_ByRef:
1545*67e74705SXin Li         VarDecl *Var = ReadDeclAs<VarDecl>(Record, Idx);
1546*67e74705SXin Li         SourceLocation EllipsisLoc = ReadSourceLocation(Record, Idx);
1547*67e74705SXin Li         *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1548*67e74705SXin Li         break;
1549*67e74705SXin Li       }
1550*67e74705SXin Li     }
1551*67e74705SXin Li   }
1552*67e74705SXin Li }
1553*67e74705SXin Li 
MergeDefinitionData(CXXRecordDecl * D,struct CXXRecordDecl::DefinitionData && MergeDD)1554*67e74705SXin Li void ASTDeclReader::MergeDefinitionData(
1555*67e74705SXin Li     CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
1556*67e74705SXin Li   assert(D->DefinitionData &&
1557*67e74705SXin Li          "merging class definition into non-definition");
1558*67e74705SXin Li   auto &DD = *D->DefinitionData;
1559*67e74705SXin Li 
1560*67e74705SXin Li   if (DD.Definition != MergeDD.Definition) {
1561*67e74705SXin Li     // Track that we merged the definitions.
1562*67e74705SXin Li     Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1563*67e74705SXin Li                                                     DD.Definition));
1564*67e74705SXin Li     Reader.PendingDefinitions.erase(MergeDD.Definition);
1565*67e74705SXin Li     MergeDD.Definition->IsCompleteDefinition = false;
1566*67e74705SXin Li     mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
1567*67e74705SXin Li     assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1568*67e74705SXin Li            "already loaded pending lookups for merged definition");
1569*67e74705SXin Li   }
1570*67e74705SXin Li 
1571*67e74705SXin Li   auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1572*67e74705SXin Li   if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1573*67e74705SXin Li       PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
1574*67e74705SXin Li     // We faked up this definition data because we found a class for which we'd
1575*67e74705SXin Li     // not yet loaded the definition. Replace it with the real thing now.
1576*67e74705SXin Li     assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
1577*67e74705SXin Li     PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
1578*67e74705SXin Li 
1579*67e74705SXin Li     // Don't change which declaration is the definition; that is required
1580*67e74705SXin Li     // to be invariant once we select it.
1581*67e74705SXin Li     auto *Def = DD.Definition;
1582*67e74705SXin Li     DD = std::move(MergeDD);
1583*67e74705SXin Li     DD.Definition = Def;
1584*67e74705SXin Li     return;
1585*67e74705SXin Li   }
1586*67e74705SXin Li 
1587*67e74705SXin Li   // FIXME: Move this out into a .def file?
1588*67e74705SXin Li   bool DetectedOdrViolation = false;
1589*67e74705SXin Li #define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1590*67e74705SXin Li #define MATCH_FIELD(Field) \
1591*67e74705SXin Li     DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1592*67e74705SXin Li     OR_FIELD(Field)
1593*67e74705SXin Li   MATCH_FIELD(UserDeclaredConstructor)
1594*67e74705SXin Li   MATCH_FIELD(UserDeclaredSpecialMembers)
1595*67e74705SXin Li   MATCH_FIELD(Aggregate)
1596*67e74705SXin Li   MATCH_FIELD(PlainOldData)
1597*67e74705SXin Li   MATCH_FIELD(Empty)
1598*67e74705SXin Li   MATCH_FIELD(Polymorphic)
1599*67e74705SXin Li   MATCH_FIELD(Abstract)
1600*67e74705SXin Li   MATCH_FIELD(IsStandardLayout)
1601*67e74705SXin Li   MATCH_FIELD(HasNoNonEmptyBases)
1602*67e74705SXin Li   MATCH_FIELD(HasPrivateFields)
1603*67e74705SXin Li   MATCH_FIELD(HasProtectedFields)
1604*67e74705SXin Li   MATCH_FIELD(HasPublicFields)
1605*67e74705SXin Li   MATCH_FIELD(HasMutableFields)
1606*67e74705SXin Li   MATCH_FIELD(HasVariantMembers)
1607*67e74705SXin Li   MATCH_FIELD(HasOnlyCMembers)
1608*67e74705SXin Li   MATCH_FIELD(HasInClassInitializer)
1609*67e74705SXin Li   MATCH_FIELD(HasUninitializedReferenceMember)
1610*67e74705SXin Li   MATCH_FIELD(HasUninitializedFields)
1611*67e74705SXin Li   MATCH_FIELD(HasInheritedConstructor)
1612*67e74705SXin Li   MATCH_FIELD(HasInheritedAssignment)
1613*67e74705SXin Li   MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1614*67e74705SXin Li   MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1615*67e74705SXin Li   MATCH_FIELD(NeedOverloadResolutionForDestructor)
1616*67e74705SXin Li   MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1617*67e74705SXin Li   MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1618*67e74705SXin Li   MATCH_FIELD(DefaultedDestructorIsDeleted)
1619*67e74705SXin Li   OR_FIELD(HasTrivialSpecialMembers)
1620*67e74705SXin Li   OR_FIELD(DeclaredNonTrivialSpecialMembers)
1621*67e74705SXin Li   MATCH_FIELD(HasIrrelevantDestructor)
1622*67e74705SXin Li   OR_FIELD(HasConstexprNonCopyMoveConstructor)
1623*67e74705SXin Li   OR_FIELD(HasDefaultedDefaultConstructor)
1624*67e74705SXin Li   MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1625*67e74705SXin Li   OR_FIELD(HasConstexprDefaultConstructor)
1626*67e74705SXin Li   MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1627*67e74705SXin Li   // ComputedVisibleConversions is handled below.
1628*67e74705SXin Li   MATCH_FIELD(UserProvidedDefaultConstructor)
1629*67e74705SXin Li   OR_FIELD(DeclaredSpecialMembers)
1630*67e74705SXin Li   MATCH_FIELD(ImplicitCopyConstructorHasConstParam)
1631*67e74705SXin Li   MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1632*67e74705SXin Li   OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1633*67e74705SXin Li   OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1634*67e74705SXin Li   MATCH_FIELD(IsLambda)
1635*67e74705SXin Li #undef OR_FIELD
1636*67e74705SXin Li #undef MATCH_FIELD
1637*67e74705SXin Li 
1638*67e74705SXin Li   if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1639*67e74705SXin Li     DetectedOdrViolation = true;
1640*67e74705SXin Li   // FIXME: Issue a diagnostic if the base classes don't match when we come
1641*67e74705SXin Li   // to lazily load them.
1642*67e74705SXin Li 
1643*67e74705SXin Li   // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1644*67e74705SXin Li   // match when we come to lazily load them.
1645*67e74705SXin Li   if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1646*67e74705SXin Li     DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1647*67e74705SXin Li     DD.ComputedVisibleConversions = true;
1648*67e74705SXin Li   }
1649*67e74705SXin Li 
1650*67e74705SXin Li   // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1651*67e74705SXin Li   // lazily load it.
1652*67e74705SXin Li 
1653*67e74705SXin Li   if (DD.IsLambda) {
1654*67e74705SXin Li     // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1655*67e74705SXin Li     // when they occur within the body of a function template specialization).
1656*67e74705SXin Li   }
1657*67e74705SXin Li 
1658*67e74705SXin Li   if (DetectedOdrViolation)
1659*67e74705SXin Li     Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1660*67e74705SXin Li }
1661*67e74705SXin Li 
ReadCXXRecordDefinition(CXXRecordDecl * D,bool Update)1662*67e74705SXin Li void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
1663*67e74705SXin Li   struct CXXRecordDecl::DefinitionData *DD;
1664*67e74705SXin Li   ASTContext &C = Reader.getContext();
1665*67e74705SXin Li 
1666*67e74705SXin Li   // Determine whether this is a lambda closure type, so that we can
1667*67e74705SXin Li   // allocate the appropriate DefinitionData structure.
1668*67e74705SXin Li   bool IsLambda = Record[Idx++];
1669*67e74705SXin Li   if (IsLambda)
1670*67e74705SXin Li     DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
1671*67e74705SXin Li                                                      LCD_None);
1672*67e74705SXin Li   else
1673*67e74705SXin Li     DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1674*67e74705SXin Li 
1675*67e74705SXin Li   ReadCXXDefinitionData(*DD, Record, Idx);
1676*67e74705SXin Li 
1677*67e74705SXin Li   // We might already have a definition for this record. This can happen either
1678*67e74705SXin Li   // because we're reading an update record, or because we've already done some
1679*67e74705SXin Li   // merging. Either way, just merge into it.
1680*67e74705SXin Li   CXXRecordDecl *Canon = D->getCanonicalDecl();
1681*67e74705SXin Li   if (Canon->DefinitionData) {
1682*67e74705SXin Li     MergeDefinitionData(Canon, std::move(*DD));
1683*67e74705SXin Li     D->DefinitionData = Canon->DefinitionData;
1684*67e74705SXin Li     return;
1685*67e74705SXin Li   }
1686*67e74705SXin Li 
1687*67e74705SXin Li   // Mark this declaration as being a definition.
1688*67e74705SXin Li   D->IsCompleteDefinition = true;
1689*67e74705SXin Li   D->DefinitionData = DD;
1690*67e74705SXin Li 
1691*67e74705SXin Li   // If this is not the first declaration or is an update record, we can have
1692*67e74705SXin Li   // other redeclarations already. Make a note that we need to propagate the
1693*67e74705SXin Li   // DefinitionData pointer onto them.
1694*67e74705SXin Li   if (Update || Canon != D) {
1695*67e74705SXin Li     Canon->DefinitionData = D->DefinitionData;
1696*67e74705SXin Li     Reader.PendingDefinitions.insert(D);
1697*67e74705SXin Li   }
1698*67e74705SXin Li }
1699*67e74705SXin Li 
1700*67e74705SXin Li ASTDeclReader::RedeclarableResult
VisitCXXRecordDeclImpl(CXXRecordDecl * D)1701*67e74705SXin Li ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1702*67e74705SXin Li   RedeclarableResult Redecl = VisitRecordDeclImpl(D);
1703*67e74705SXin Li 
1704*67e74705SXin Li   ASTContext &C = Reader.getContext();
1705*67e74705SXin Li 
1706*67e74705SXin Li   enum CXXRecKind {
1707*67e74705SXin Li     CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1708*67e74705SXin Li   };
1709*67e74705SXin Li   switch ((CXXRecKind)Record[Idx++]) {
1710*67e74705SXin Li   case CXXRecNotTemplate:
1711*67e74705SXin Li     // Merged when we merge the folding set entry in the primary template.
1712*67e74705SXin Li     if (!isa<ClassTemplateSpecializationDecl>(D))
1713*67e74705SXin Li       mergeRedeclarable(D, Redecl);
1714*67e74705SXin Li     break;
1715*67e74705SXin Li   case CXXRecTemplate: {
1716*67e74705SXin Li     // Merged when we merge the template.
1717*67e74705SXin Li     ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>(Record, Idx);
1718*67e74705SXin Li     D->TemplateOrInstantiation = Template;
1719*67e74705SXin Li     if (!Template->getTemplatedDecl()) {
1720*67e74705SXin Li       // We've not actually loaded the ClassTemplateDecl yet, because we're
1721*67e74705SXin Li       // currently being loaded as its pattern. Rely on it to set up our
1722*67e74705SXin Li       // TypeForDecl (see VisitClassTemplateDecl).
1723*67e74705SXin Li       //
1724*67e74705SXin Li       // Beware: we do not yet know our canonical declaration, and may still
1725*67e74705SXin Li       // get merged once the surrounding class template has got off the ground.
1726*67e74705SXin Li       TypeIDForTypeDecl = 0;
1727*67e74705SXin Li     }
1728*67e74705SXin Li     break;
1729*67e74705SXin Li   }
1730*67e74705SXin Li   case CXXRecMemberSpecialization: {
1731*67e74705SXin Li     CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(Record, Idx);
1732*67e74705SXin Li     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
1733*67e74705SXin Li     SourceLocation POI = ReadSourceLocation(Record, Idx);
1734*67e74705SXin Li     MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1735*67e74705SXin Li     MSI->setPointOfInstantiation(POI);
1736*67e74705SXin Li     D->TemplateOrInstantiation = MSI;
1737*67e74705SXin Li     mergeRedeclarable(D, Redecl);
1738*67e74705SXin Li     break;
1739*67e74705SXin Li   }
1740*67e74705SXin Li   }
1741*67e74705SXin Li 
1742*67e74705SXin Li   bool WasDefinition = Record[Idx++];
1743*67e74705SXin Li   if (WasDefinition)
1744*67e74705SXin Li     ReadCXXRecordDefinition(D, /*Update*/false);
1745*67e74705SXin Li   else
1746*67e74705SXin Li     // Propagate DefinitionData pointer from the canonical declaration.
1747*67e74705SXin Li     D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1748*67e74705SXin Li 
1749*67e74705SXin Li   // Lazily load the key function to avoid deserializing every method so we can
1750*67e74705SXin Li   // compute it.
1751*67e74705SXin Li   if (WasDefinition) {
1752*67e74705SXin Li     DeclID KeyFn = ReadDeclID(Record, Idx);
1753*67e74705SXin Li     if (KeyFn && D->IsCompleteDefinition)
1754*67e74705SXin Li       // FIXME: This is wrong for the ARM ABI, where some other module may have
1755*67e74705SXin Li       // made this function no longer be a key function. We need an update
1756*67e74705SXin Li       // record or similar for that case.
1757*67e74705SXin Li       C.KeyFunctions[D] = KeyFn;
1758*67e74705SXin Li   }
1759*67e74705SXin Li 
1760*67e74705SXin Li   return Redecl;
1761*67e74705SXin Li }
1762*67e74705SXin Li 
VisitCXXMethodDecl(CXXMethodDecl * D)1763*67e74705SXin Li void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
1764*67e74705SXin Li   VisitFunctionDecl(D);
1765*67e74705SXin Li 
1766*67e74705SXin Li   unsigned NumOverridenMethods = Record[Idx++];
1767*67e74705SXin Li   if (D->isCanonicalDecl()) {
1768*67e74705SXin Li     while (NumOverridenMethods--) {
1769*67e74705SXin Li       // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1770*67e74705SXin Li       // MD may be initializing.
1771*67e74705SXin Li       if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>(Record, Idx))
1772*67e74705SXin Li         Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1773*67e74705SXin Li     }
1774*67e74705SXin Li   } else {
1775*67e74705SXin Li     // We don't care about which declarations this used to override; we get
1776*67e74705SXin Li     // the relevant information from the canonical declaration.
1777*67e74705SXin Li     Idx += NumOverridenMethods;
1778*67e74705SXin Li   }
1779*67e74705SXin Li }
1780*67e74705SXin Li 
VisitCXXConstructorDecl(CXXConstructorDecl * D)1781*67e74705SXin Li void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1782*67e74705SXin Li   // We need the inherited constructor information to merge the declaration,
1783*67e74705SXin Li   // so we have to read it before we call VisitCXXMethodDecl.
1784*67e74705SXin Li   if (D->isInheritingConstructor()) {
1785*67e74705SXin Li     auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>(Record, Idx);
1786*67e74705SXin Li     auto *Ctor = ReadDeclAs<CXXConstructorDecl>(Record, Idx);
1787*67e74705SXin Li     *D->getTrailingObjects<InheritedConstructor>() =
1788*67e74705SXin Li         InheritedConstructor(Shadow, Ctor);
1789*67e74705SXin Li   }
1790*67e74705SXin Li 
1791*67e74705SXin Li   VisitCXXMethodDecl(D);
1792*67e74705SXin Li 
1793*67e74705SXin Li   D->IsExplicitSpecified = Record[Idx++];
1794*67e74705SXin Li }
1795*67e74705SXin Li 
VisitCXXDestructorDecl(CXXDestructorDecl * D)1796*67e74705SXin Li void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1797*67e74705SXin Li   VisitCXXMethodDecl(D);
1798*67e74705SXin Li 
1799*67e74705SXin Li   if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>(Record, Idx)) {
1800*67e74705SXin Li     auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1801*67e74705SXin Li     // FIXME: Check consistency if we have an old and new operator delete.
1802*67e74705SXin Li     if (!Canon->OperatorDelete)
1803*67e74705SXin Li       Canon->OperatorDelete = OperatorDelete;
1804*67e74705SXin Li   }
1805*67e74705SXin Li }
1806*67e74705SXin Li 
VisitCXXConversionDecl(CXXConversionDecl * D)1807*67e74705SXin Li void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
1808*67e74705SXin Li   VisitCXXMethodDecl(D);
1809*67e74705SXin Li   D->IsExplicitSpecified = Record[Idx++];
1810*67e74705SXin Li }
1811*67e74705SXin Li 
VisitImportDecl(ImportDecl * D)1812*67e74705SXin Li void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1813*67e74705SXin Li   VisitDecl(D);
1814*67e74705SXin Li   D->ImportedAndComplete.setPointer(readModule(Record, Idx));
1815*67e74705SXin Li   D->ImportedAndComplete.setInt(Record[Idx++]);
1816*67e74705SXin Li   SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
1817*67e74705SXin Li   for (unsigned I = 0, N = Record.back(); I != N; ++I)
1818*67e74705SXin Li     StoredLocs[I] = ReadSourceLocation(Record, Idx);
1819*67e74705SXin Li   ++Idx; // The number of stored source locations.
1820*67e74705SXin Li }
1821*67e74705SXin Li 
VisitAccessSpecDecl(AccessSpecDecl * D)1822*67e74705SXin Li void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
1823*67e74705SXin Li   VisitDecl(D);
1824*67e74705SXin Li   D->setColonLoc(ReadSourceLocation(Record, Idx));
1825*67e74705SXin Li }
1826*67e74705SXin Li 
VisitFriendDecl(FriendDecl * D)1827*67e74705SXin Li void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
1828*67e74705SXin Li   VisitDecl(D);
1829*67e74705SXin Li   if (Record[Idx++]) // hasFriendDecl
1830*67e74705SXin Li     D->Friend = ReadDeclAs<NamedDecl>(Record, Idx);
1831*67e74705SXin Li   else
1832*67e74705SXin Li     D->Friend = GetTypeSourceInfo(Record, Idx);
1833*67e74705SXin Li   for (unsigned i = 0; i != D->NumTPLists; ++i)
1834*67e74705SXin Li     D->getTrailingObjects<TemplateParameterList *>()[i] =
1835*67e74705SXin Li         Reader.ReadTemplateParameterList(F, Record, Idx);
1836*67e74705SXin Li   D->NextFriend = ReadDeclID(Record, Idx);
1837*67e74705SXin Li   D->UnsupportedFriend = (Record[Idx++] != 0);
1838*67e74705SXin Li   D->FriendLoc = ReadSourceLocation(Record, Idx);
1839*67e74705SXin Li }
1840*67e74705SXin Li 
VisitFriendTemplateDecl(FriendTemplateDecl * D)1841*67e74705SXin Li void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
1842*67e74705SXin Li   VisitDecl(D);
1843*67e74705SXin Li   unsigned NumParams = Record[Idx++];
1844*67e74705SXin Li   D->NumParams = NumParams;
1845*67e74705SXin Li   D->Params = new TemplateParameterList*[NumParams];
1846*67e74705SXin Li   for (unsigned i = 0; i != NumParams; ++i)
1847*67e74705SXin Li     D->Params[i] = Reader.ReadTemplateParameterList(F, Record, Idx);
1848*67e74705SXin Li   if (Record[Idx++]) // HasFriendDecl
1849*67e74705SXin Li     D->Friend = ReadDeclAs<NamedDecl>(Record, Idx);
1850*67e74705SXin Li   else
1851*67e74705SXin Li     D->Friend = GetTypeSourceInfo(Record, Idx);
1852*67e74705SXin Li   D->FriendLoc = ReadSourceLocation(Record, Idx);
1853*67e74705SXin Li }
1854*67e74705SXin Li 
VisitTemplateDecl(TemplateDecl * D)1855*67e74705SXin Li DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
1856*67e74705SXin Li   VisitNamedDecl(D);
1857*67e74705SXin Li 
1858*67e74705SXin Li   DeclID PatternID = ReadDeclID(Record, Idx);
1859*67e74705SXin Li   NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
1860*67e74705SXin Li   TemplateParameterList* TemplateParams
1861*67e74705SXin Li       = Reader.ReadTemplateParameterList(F, Record, Idx);
1862*67e74705SXin Li   D->init(TemplatedDecl, TemplateParams);
1863*67e74705SXin Li 
1864*67e74705SXin Li   return PatternID;
1865*67e74705SXin Li }
1866*67e74705SXin Li 
1867*67e74705SXin Li ASTDeclReader::RedeclarableResult
VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl * D)1868*67e74705SXin Li ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
1869*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarable(D);
1870*67e74705SXin Li 
1871*67e74705SXin Li   // Make sure we've allocated the Common pointer first. We do this before
1872*67e74705SXin Li   // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1873*67e74705SXin Li   RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1874*67e74705SXin Li   if (!CanonD->Common) {
1875*67e74705SXin Li     CanonD->Common = CanonD->newCommon(Reader.getContext());
1876*67e74705SXin Li     Reader.PendingDefinitions.insert(CanonD);
1877*67e74705SXin Li   }
1878*67e74705SXin Li   D->Common = CanonD->Common;
1879*67e74705SXin Li 
1880*67e74705SXin Li   // If this is the first declaration of the template, fill in the information
1881*67e74705SXin Li   // for the 'common' pointer.
1882*67e74705SXin Li   if (ThisDeclID == Redecl.getFirstID()) {
1883*67e74705SXin Li     if (RedeclarableTemplateDecl *RTD
1884*67e74705SXin Li           = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx)) {
1885*67e74705SXin Li       assert(RTD->getKind() == D->getKind() &&
1886*67e74705SXin Li              "InstantiatedFromMemberTemplate kind mismatch");
1887*67e74705SXin Li       D->setInstantiatedFromMemberTemplate(RTD);
1888*67e74705SXin Li       if (Record[Idx++])
1889*67e74705SXin Li         D->setMemberSpecialization();
1890*67e74705SXin Li     }
1891*67e74705SXin Li   }
1892*67e74705SXin Li 
1893*67e74705SXin Li   DeclID PatternID = VisitTemplateDecl(D);
1894*67e74705SXin Li   D->IdentifierNamespace = Record[Idx++];
1895*67e74705SXin Li 
1896*67e74705SXin Li   mergeRedeclarable(D, Redecl, PatternID);
1897*67e74705SXin Li 
1898*67e74705SXin Li   // If we merged the template with a prior declaration chain, merge the common
1899*67e74705SXin Li   // pointer.
1900*67e74705SXin Li   // FIXME: Actually merge here, don't just overwrite.
1901*67e74705SXin Li   D->Common = D->getCanonicalDecl()->Common;
1902*67e74705SXin Li 
1903*67e74705SXin Li   return Redecl;
1904*67e74705SXin Li }
1905*67e74705SXin Li 
newDeclIDList(ASTContext & Context,DeclID * Old,SmallVectorImpl<DeclID> & IDs)1906*67e74705SXin Li static DeclID *newDeclIDList(ASTContext &Context, DeclID *Old,
1907*67e74705SXin Li                              SmallVectorImpl<DeclID> &IDs) {
1908*67e74705SXin Li   assert(!IDs.empty() && "no IDs to add to list");
1909*67e74705SXin Li   if (Old) {
1910*67e74705SXin Li     IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
1911*67e74705SXin Li     std::sort(IDs.begin(), IDs.end());
1912*67e74705SXin Li     IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
1913*67e74705SXin Li   }
1914*67e74705SXin Li 
1915*67e74705SXin Li   auto *Result = new (Context) DeclID[1 + IDs.size()];
1916*67e74705SXin Li   *Result = IDs.size();
1917*67e74705SXin Li   std::copy(IDs.begin(), IDs.end(), Result + 1);
1918*67e74705SXin Li   return Result;
1919*67e74705SXin Li }
1920*67e74705SXin Li 
VisitClassTemplateDecl(ClassTemplateDecl * D)1921*67e74705SXin Li void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1922*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
1923*67e74705SXin Li 
1924*67e74705SXin Li   if (ThisDeclID == Redecl.getFirstID()) {
1925*67e74705SXin Li     // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1926*67e74705SXin Li     // the specializations.
1927*67e74705SXin Li     SmallVector<serialization::DeclID, 32> SpecIDs;
1928*67e74705SXin Li     ReadDeclIDList(SpecIDs);
1929*67e74705SXin Li 
1930*67e74705SXin Li     if (!SpecIDs.empty()) {
1931*67e74705SXin Li       auto *CommonPtr = D->getCommonPtr();
1932*67e74705SXin Li       CommonPtr->LazySpecializations = newDeclIDList(
1933*67e74705SXin Li           Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
1934*67e74705SXin Li     }
1935*67e74705SXin Li   }
1936*67e74705SXin Li 
1937*67e74705SXin Li   if (D->getTemplatedDecl()->TemplateOrInstantiation) {
1938*67e74705SXin Li     // We were loaded before our templated declaration was. We've not set up
1939*67e74705SXin Li     // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
1940*67e74705SXin Li     // it now.
1941*67e74705SXin Li     Reader.Context.getInjectedClassNameType(
1942*67e74705SXin Li         D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
1943*67e74705SXin Li   }
1944*67e74705SXin Li }
1945*67e74705SXin Li 
VisitBuiltinTemplateDecl(BuiltinTemplateDecl * D)1946*67e74705SXin Li void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
1947*67e74705SXin Li   llvm_unreachable("BuiltinTemplates are not serialized");
1948*67e74705SXin Li }
1949*67e74705SXin Li 
1950*67e74705SXin Li /// TODO: Unify with ClassTemplateDecl version?
1951*67e74705SXin Li ///       May require unifying ClassTemplateDecl and
1952*67e74705SXin Li ///        VarTemplateDecl beyond TemplateDecl...
VisitVarTemplateDecl(VarTemplateDecl * D)1953*67e74705SXin Li void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
1954*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
1955*67e74705SXin Li 
1956*67e74705SXin Li   if (ThisDeclID == Redecl.getFirstID()) {
1957*67e74705SXin Li     // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
1958*67e74705SXin Li     // the specializations.
1959*67e74705SXin Li     SmallVector<serialization::DeclID, 32> SpecIDs;
1960*67e74705SXin Li     ReadDeclIDList(SpecIDs);
1961*67e74705SXin Li 
1962*67e74705SXin Li     if (!SpecIDs.empty()) {
1963*67e74705SXin Li       auto *CommonPtr = D->getCommonPtr();
1964*67e74705SXin Li       CommonPtr->LazySpecializations = newDeclIDList(
1965*67e74705SXin Li           Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
1966*67e74705SXin Li     }
1967*67e74705SXin Li   }
1968*67e74705SXin Li }
1969*67e74705SXin Li 
1970*67e74705SXin Li ASTDeclReader::RedeclarableResult
VisitClassTemplateSpecializationDeclImpl(ClassTemplateSpecializationDecl * D)1971*67e74705SXin Li ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
1972*67e74705SXin Li     ClassTemplateSpecializationDecl *D) {
1973*67e74705SXin Li   RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
1974*67e74705SXin Li 
1975*67e74705SXin Li   ASTContext &C = Reader.getContext();
1976*67e74705SXin Li   if (Decl *InstD = ReadDecl(Record, Idx)) {
1977*67e74705SXin Li     if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
1978*67e74705SXin Li       D->SpecializedTemplate = CTD;
1979*67e74705SXin Li     } else {
1980*67e74705SXin Li       SmallVector<TemplateArgument, 8> TemplArgs;
1981*67e74705SXin Li       Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
1982*67e74705SXin Li       TemplateArgumentList *ArgList
1983*67e74705SXin Li         = TemplateArgumentList::CreateCopy(C, TemplArgs);
1984*67e74705SXin Li       ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
1985*67e74705SXin Li           = new (C) ClassTemplateSpecializationDecl::
1986*67e74705SXin Li                                              SpecializedPartialSpecialization();
1987*67e74705SXin Li       PS->PartialSpecialization
1988*67e74705SXin Li           = cast<ClassTemplatePartialSpecializationDecl>(InstD);
1989*67e74705SXin Li       PS->TemplateArgs = ArgList;
1990*67e74705SXin Li       D->SpecializedTemplate = PS;
1991*67e74705SXin Li     }
1992*67e74705SXin Li   }
1993*67e74705SXin Li 
1994*67e74705SXin Li   SmallVector<TemplateArgument, 8> TemplArgs;
1995*67e74705SXin Li   Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx,
1996*67e74705SXin Li                                   /*Canonicalize*/ true);
1997*67e74705SXin Li   D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
1998*67e74705SXin Li   D->PointOfInstantiation = ReadSourceLocation(Record, Idx);
1999*67e74705SXin Li   D->SpecializationKind = (TemplateSpecializationKind)Record[Idx++];
2000*67e74705SXin Li 
2001*67e74705SXin Li   bool writtenAsCanonicalDecl = Record[Idx++];
2002*67e74705SXin Li   if (writtenAsCanonicalDecl) {
2003*67e74705SXin Li     ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>(Record,Idx);
2004*67e74705SXin Li     if (D->isCanonicalDecl()) { // It's kept in the folding set.
2005*67e74705SXin Li       // Set this as, or find, the canonical declaration for this specialization
2006*67e74705SXin Li       ClassTemplateSpecializationDecl *CanonSpec;
2007*67e74705SXin Li       if (ClassTemplatePartialSpecializationDecl *Partial =
2008*67e74705SXin Li               dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
2009*67e74705SXin Li         CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
2010*67e74705SXin Li             .GetOrInsertNode(Partial);
2011*67e74705SXin Li       } else {
2012*67e74705SXin Li         CanonSpec =
2013*67e74705SXin Li             CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2014*67e74705SXin Li       }
2015*67e74705SXin Li       // If there was already a canonical specialization, merge into it.
2016*67e74705SXin Li       if (CanonSpec != D) {
2017*67e74705SXin Li         mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2018*67e74705SXin Li 
2019*67e74705SXin Li         // This declaration might be a definition. Merge with any existing
2020*67e74705SXin Li         // definition.
2021*67e74705SXin Li         if (auto *DDD = D->DefinitionData) {
2022*67e74705SXin Li           if (CanonSpec->DefinitionData)
2023*67e74705SXin Li             MergeDefinitionData(CanonSpec, std::move(*DDD));
2024*67e74705SXin Li           else
2025*67e74705SXin Li             CanonSpec->DefinitionData = D->DefinitionData;
2026*67e74705SXin Li         }
2027*67e74705SXin Li         D->DefinitionData = CanonSpec->DefinitionData;
2028*67e74705SXin Li       }
2029*67e74705SXin Li     }
2030*67e74705SXin Li   }
2031*67e74705SXin Li 
2032*67e74705SXin Li   // Explicit info.
2033*67e74705SXin Li   if (TypeSourceInfo *TyInfo = GetTypeSourceInfo(Record, Idx)) {
2034*67e74705SXin Li     ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2035*67e74705SXin Li         = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2036*67e74705SXin Li     ExplicitInfo->TypeAsWritten = TyInfo;
2037*67e74705SXin Li     ExplicitInfo->ExternLoc = ReadSourceLocation(Record, Idx);
2038*67e74705SXin Li     ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(Record, Idx);
2039*67e74705SXin Li     D->ExplicitInfo = ExplicitInfo;
2040*67e74705SXin Li   }
2041*67e74705SXin Li 
2042*67e74705SXin Li   return Redecl;
2043*67e74705SXin Li }
2044*67e74705SXin Li 
VisitClassTemplatePartialSpecializationDecl(ClassTemplatePartialSpecializationDecl * D)2045*67e74705SXin Li void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
2046*67e74705SXin Li                                     ClassTemplatePartialSpecializationDecl *D) {
2047*67e74705SXin Li   RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
2048*67e74705SXin Li 
2049*67e74705SXin Li   D->TemplateParams = Reader.ReadTemplateParameterList(F, Record, Idx);
2050*67e74705SXin Li   D->ArgsAsWritten = Reader.ReadASTTemplateArgumentListInfo(F, Record, Idx);
2051*67e74705SXin Li 
2052*67e74705SXin Li   // These are read/set from/to the first declaration.
2053*67e74705SXin Li   if (ThisDeclID == Redecl.getFirstID()) {
2054*67e74705SXin Li     D->InstantiatedFromMember.setPointer(
2055*67e74705SXin Li       ReadDeclAs<ClassTemplatePartialSpecializationDecl>(Record, Idx));
2056*67e74705SXin Li     D->InstantiatedFromMember.setInt(Record[Idx++]);
2057*67e74705SXin Li   }
2058*67e74705SXin Li }
2059*67e74705SXin Li 
VisitClassScopeFunctionSpecializationDecl(ClassScopeFunctionSpecializationDecl * D)2060*67e74705SXin Li void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2061*67e74705SXin Li                                     ClassScopeFunctionSpecializationDecl *D) {
2062*67e74705SXin Li   VisitDecl(D);
2063*67e74705SXin Li   D->Specialization = ReadDeclAs<CXXMethodDecl>(Record, Idx);
2064*67e74705SXin Li }
2065*67e74705SXin Li 
VisitFunctionTemplateDecl(FunctionTemplateDecl * D)2066*67e74705SXin Li void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
2067*67e74705SXin Li   RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2068*67e74705SXin Li 
2069*67e74705SXin Li   if (ThisDeclID == Redecl.getFirstID()) {
2070*67e74705SXin Li     // This FunctionTemplateDecl owns a CommonPtr; read it.
2071*67e74705SXin Li     SmallVector<serialization::DeclID, 32> SpecIDs;
2072*67e74705SXin Li     ReadDeclIDList(SpecIDs);
2073*67e74705SXin Li 
2074*67e74705SXin Li     if (!SpecIDs.empty()) {
2075*67e74705SXin Li       auto *CommonPtr = D->getCommonPtr();
2076*67e74705SXin Li       CommonPtr->LazySpecializations = newDeclIDList(
2077*67e74705SXin Li           Reader.getContext(), CommonPtr->LazySpecializations, SpecIDs);
2078*67e74705SXin Li     }
2079*67e74705SXin Li   }
2080*67e74705SXin Li }
2081*67e74705SXin Li 
2082*67e74705SXin Li /// TODO: Unify with ClassTemplateSpecializationDecl version?
2083*67e74705SXin Li ///       May require unifying ClassTemplate(Partial)SpecializationDecl and
2084*67e74705SXin Li ///        VarTemplate(Partial)SpecializationDecl with a new data
2085*67e74705SXin Li ///        structure Template(Partial)SpecializationDecl, and
2086*67e74705SXin Li ///        using Template(Partial)SpecializationDecl as input type.
2087*67e74705SXin Li ASTDeclReader::RedeclarableResult
VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl * D)2088*67e74705SXin Li ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2089*67e74705SXin Li     VarTemplateSpecializationDecl *D) {
2090*67e74705SXin Li   RedeclarableResult Redecl = VisitVarDeclImpl(D);
2091*67e74705SXin Li 
2092*67e74705SXin Li   ASTContext &C = Reader.getContext();
2093*67e74705SXin Li   if (Decl *InstD = ReadDecl(Record, Idx)) {
2094*67e74705SXin Li     if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2095*67e74705SXin Li       D->SpecializedTemplate = VTD;
2096*67e74705SXin Li     } else {
2097*67e74705SXin Li       SmallVector<TemplateArgument, 8> TemplArgs;
2098*67e74705SXin Li       Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
2099*67e74705SXin Li       TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
2100*67e74705SXin Li           C, TemplArgs);
2101*67e74705SXin Li       VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2102*67e74705SXin Li           new (C)
2103*67e74705SXin Li           VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2104*67e74705SXin Li       PS->PartialSpecialization =
2105*67e74705SXin Li           cast<VarTemplatePartialSpecializationDecl>(InstD);
2106*67e74705SXin Li       PS->TemplateArgs = ArgList;
2107*67e74705SXin Li       D->SpecializedTemplate = PS;
2108*67e74705SXin Li     }
2109*67e74705SXin Li   }
2110*67e74705SXin Li 
2111*67e74705SXin Li   // Explicit info.
2112*67e74705SXin Li   if (TypeSourceInfo *TyInfo = GetTypeSourceInfo(Record, Idx)) {
2113*67e74705SXin Li     VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2114*67e74705SXin Li         new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2115*67e74705SXin Li     ExplicitInfo->TypeAsWritten = TyInfo;
2116*67e74705SXin Li     ExplicitInfo->ExternLoc = ReadSourceLocation(Record, Idx);
2117*67e74705SXin Li     ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(Record, Idx);
2118*67e74705SXin Li     D->ExplicitInfo = ExplicitInfo;
2119*67e74705SXin Li   }
2120*67e74705SXin Li 
2121*67e74705SXin Li   SmallVector<TemplateArgument, 8> TemplArgs;
2122*67e74705SXin Li   Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx,
2123*67e74705SXin Li                                   /*Canonicalize*/ true);
2124*67e74705SXin Li   D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
2125*67e74705SXin Li   D->PointOfInstantiation = ReadSourceLocation(Record, Idx);
2126*67e74705SXin Li   D->SpecializationKind = (TemplateSpecializationKind)Record[Idx++];
2127*67e74705SXin Li 
2128*67e74705SXin Li   bool writtenAsCanonicalDecl = Record[Idx++];
2129*67e74705SXin Li   if (writtenAsCanonicalDecl) {
2130*67e74705SXin Li     VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>(Record, Idx);
2131*67e74705SXin Li     if (D->isCanonicalDecl()) { // It's kept in the folding set.
2132*67e74705SXin Li       // FIXME: If it's already present, merge it.
2133*67e74705SXin Li       if (VarTemplatePartialSpecializationDecl *Partial =
2134*67e74705SXin Li               dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
2135*67e74705SXin Li         CanonPattern->getCommonPtr()->PartialSpecializations
2136*67e74705SXin Li             .GetOrInsertNode(Partial);
2137*67e74705SXin Li       } else {
2138*67e74705SXin Li         CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2139*67e74705SXin Li       }
2140*67e74705SXin Li     }
2141*67e74705SXin Li   }
2142*67e74705SXin Li 
2143*67e74705SXin Li   return Redecl;
2144*67e74705SXin Li }
2145*67e74705SXin Li 
2146*67e74705SXin Li /// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2147*67e74705SXin Li ///       May require unifying ClassTemplate(Partial)SpecializationDecl and
2148*67e74705SXin Li ///        VarTemplate(Partial)SpecializationDecl with a new data
2149*67e74705SXin Li ///        structure Template(Partial)SpecializationDecl, and
2150*67e74705SXin Li ///        using Template(Partial)SpecializationDecl as input type.
VisitVarTemplatePartialSpecializationDecl(VarTemplatePartialSpecializationDecl * D)2151*67e74705SXin Li void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2152*67e74705SXin Li     VarTemplatePartialSpecializationDecl *D) {
2153*67e74705SXin Li   RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2154*67e74705SXin Li 
2155*67e74705SXin Li   D->TemplateParams = Reader.ReadTemplateParameterList(F, Record, Idx);
2156*67e74705SXin Li   D->ArgsAsWritten = Reader.ReadASTTemplateArgumentListInfo(F, Record, Idx);
2157*67e74705SXin Li 
2158*67e74705SXin Li   // These are read/set from/to the first declaration.
2159*67e74705SXin Li   if (ThisDeclID == Redecl.getFirstID()) {
2160*67e74705SXin Li     D->InstantiatedFromMember.setPointer(
2161*67e74705SXin Li         ReadDeclAs<VarTemplatePartialSpecializationDecl>(Record, Idx));
2162*67e74705SXin Li     D->InstantiatedFromMember.setInt(Record[Idx++]);
2163*67e74705SXin Li   }
2164*67e74705SXin Li }
2165*67e74705SXin Li 
VisitTemplateTypeParmDecl(TemplateTypeParmDecl * D)2166*67e74705SXin Li void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
2167*67e74705SXin Li   VisitTypeDecl(D);
2168*67e74705SXin Li 
2169*67e74705SXin Li   D->setDeclaredWithTypename(Record[Idx++]);
2170*67e74705SXin Li 
2171*67e74705SXin Li   if (Record[Idx++])
2172*67e74705SXin Li     D->setDefaultArgument(GetTypeSourceInfo(Record, Idx));
2173*67e74705SXin Li }
2174*67e74705SXin Li 
VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl * D)2175*67e74705SXin Li void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
2176*67e74705SXin Li   VisitDeclaratorDecl(D);
2177*67e74705SXin Li   // TemplateParmPosition.
2178*67e74705SXin Li   D->setDepth(Record[Idx++]);
2179*67e74705SXin Li   D->setPosition(Record[Idx++]);
2180*67e74705SXin Li   if (D->isExpandedParameterPack()) {
2181*67e74705SXin Li     auto TypesAndInfos =
2182*67e74705SXin Li         D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
2183*67e74705SXin Li     for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
2184*67e74705SXin Li       new (&TypesAndInfos[I].first) QualType(Reader.readType(F, Record, Idx));
2185*67e74705SXin Li       TypesAndInfos[I].second = GetTypeSourceInfo(Record, Idx);
2186*67e74705SXin Li     }
2187*67e74705SXin Li   } else {
2188*67e74705SXin Li     // Rest of NonTypeTemplateParmDecl.
2189*67e74705SXin Li     D->ParameterPack = Record[Idx++];
2190*67e74705SXin Li     if (Record[Idx++])
2191*67e74705SXin Li       D->setDefaultArgument(Reader.ReadExpr(F));
2192*67e74705SXin Li   }
2193*67e74705SXin Li }
2194*67e74705SXin Li 
VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl * D)2195*67e74705SXin Li void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
2196*67e74705SXin Li   VisitTemplateDecl(D);
2197*67e74705SXin Li   // TemplateParmPosition.
2198*67e74705SXin Li   D->setDepth(Record[Idx++]);
2199*67e74705SXin Li   D->setPosition(Record[Idx++]);
2200*67e74705SXin Li   if (D->isExpandedParameterPack()) {
2201*67e74705SXin Li     TemplateParameterList **Data =
2202*67e74705SXin Li         D->getTrailingObjects<TemplateParameterList *>();
2203*67e74705SXin Li     for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2204*67e74705SXin Li          I != N; ++I)
2205*67e74705SXin Li       Data[I] = Reader.ReadTemplateParameterList(F, Record, Idx);
2206*67e74705SXin Li   } else {
2207*67e74705SXin Li     // Rest of TemplateTemplateParmDecl.
2208*67e74705SXin Li     D->ParameterPack = Record[Idx++];
2209*67e74705SXin Li     if (Record[Idx++])
2210*67e74705SXin Li       D->setDefaultArgument(Reader.getContext(),
2211*67e74705SXin Li                             Reader.ReadTemplateArgumentLoc(F, Record, Idx));
2212*67e74705SXin Li   }
2213*67e74705SXin Li }
2214*67e74705SXin Li 
VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl * D)2215*67e74705SXin Li void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2216*67e74705SXin Li   VisitRedeclarableTemplateDecl(D);
2217*67e74705SXin Li }
2218*67e74705SXin Li 
VisitStaticAssertDecl(StaticAssertDecl * D)2219*67e74705SXin Li void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
2220*67e74705SXin Li   VisitDecl(D);
2221*67e74705SXin Li   D->AssertExprAndFailed.setPointer(Reader.ReadExpr(F));
2222*67e74705SXin Li   D->AssertExprAndFailed.setInt(Record[Idx++]);
2223*67e74705SXin Li   D->Message = cast<StringLiteral>(Reader.ReadExpr(F));
2224*67e74705SXin Li   D->RParenLoc = ReadSourceLocation(Record, Idx);
2225*67e74705SXin Li }
2226*67e74705SXin Li 
VisitEmptyDecl(EmptyDecl * D)2227*67e74705SXin Li void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2228*67e74705SXin Li   VisitDecl(D);
2229*67e74705SXin Li }
2230*67e74705SXin Li 
2231*67e74705SXin Li std::pair<uint64_t, uint64_t>
VisitDeclContext(DeclContext * DC)2232*67e74705SXin Li ASTDeclReader::VisitDeclContext(DeclContext *DC) {
2233*67e74705SXin Li   uint64_t LexicalOffset = ReadLocalOffset(Record, Idx);
2234*67e74705SXin Li   uint64_t VisibleOffset = ReadLocalOffset(Record, Idx);
2235*67e74705SXin Li   return std::make_pair(LexicalOffset, VisibleOffset);
2236*67e74705SXin Li }
2237*67e74705SXin Li 
2238*67e74705SXin Li template <typename T>
2239*67e74705SXin Li ASTDeclReader::RedeclarableResult
VisitRedeclarable(Redeclarable<T> * D)2240*67e74705SXin Li ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
2241*67e74705SXin Li   DeclID FirstDeclID = ReadDeclID(Record, Idx);
2242*67e74705SXin Li   Decl *MergeWith = nullptr;
2243*67e74705SXin Li 
2244*67e74705SXin Li   bool IsKeyDecl = ThisDeclID == FirstDeclID;
2245*67e74705SXin Li   bool IsFirstLocalDecl = false;
2246*67e74705SXin Li 
2247*67e74705SXin Li   uint64_t RedeclOffset = 0;
2248*67e74705SXin Li 
2249*67e74705SXin Li   // 0 indicates that this declaration was the only declaration of its entity,
2250*67e74705SXin Li   // and is used for space optimization.
2251*67e74705SXin Li   if (FirstDeclID == 0) {
2252*67e74705SXin Li     FirstDeclID = ThisDeclID;
2253*67e74705SXin Li     IsKeyDecl = true;
2254*67e74705SXin Li     IsFirstLocalDecl = true;
2255*67e74705SXin Li   } else if (unsigned N = Record[Idx++]) {
2256*67e74705SXin Li     // This declaration was the first local declaration, but may have imported
2257*67e74705SXin Li     // other declarations.
2258*67e74705SXin Li     IsKeyDecl = N == 1;
2259*67e74705SXin Li     IsFirstLocalDecl = true;
2260*67e74705SXin Li 
2261*67e74705SXin Li     // We have some declarations that must be before us in our redeclaration
2262*67e74705SXin Li     // chain. Read them now, and remember that we ought to merge with one of
2263*67e74705SXin Li     // them.
2264*67e74705SXin Li     // FIXME: Provide a known merge target to the second and subsequent such
2265*67e74705SXin Li     // declaration.
2266*67e74705SXin Li     for (unsigned I = 0; I != N - 1; ++I)
2267*67e74705SXin Li       MergeWith = ReadDecl(Record, Idx/*, MergeWith*/);
2268*67e74705SXin Li 
2269*67e74705SXin Li     RedeclOffset = ReadLocalOffset(Record, Idx);
2270*67e74705SXin Li   } else {
2271*67e74705SXin Li     // This declaration was not the first local declaration. Read the first
2272*67e74705SXin Li     // local declaration now, to trigger the import of other redeclarations.
2273*67e74705SXin Li     (void)ReadDecl(Record, Idx);
2274*67e74705SXin Li   }
2275*67e74705SXin Li 
2276*67e74705SXin Li   T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2277*67e74705SXin Li   if (FirstDecl != D) {
2278*67e74705SXin Li     // We delay loading of the redeclaration chain to avoid deeply nested calls.
2279*67e74705SXin Li     // We temporarily set the first (canonical) declaration as the previous one
2280*67e74705SXin Li     // which is the one that matters and mark the real previous DeclID to be
2281*67e74705SXin Li     // loaded & attached later on.
2282*67e74705SXin Li     D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
2283*67e74705SXin Li     D->First = FirstDecl->getCanonicalDecl();
2284*67e74705SXin Li   }
2285*67e74705SXin Li 
2286*67e74705SXin Li   T *DAsT = static_cast<T*>(D);
2287*67e74705SXin Li 
2288*67e74705SXin Li   // Note that we need to load local redeclarations of this decl and build a
2289*67e74705SXin Li   // decl chain for them. This must happen *after* we perform the preloading
2290*67e74705SXin Li   // above; this ensures that the redeclaration chain is built in the correct
2291*67e74705SXin Li   // order.
2292*67e74705SXin Li   if (IsFirstLocalDecl)
2293*67e74705SXin Li     Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
2294*67e74705SXin Li 
2295*67e74705SXin Li   return RedeclarableResult(FirstDeclID, MergeWith, IsKeyDecl);
2296*67e74705SXin Li }
2297*67e74705SXin Li 
2298*67e74705SXin Li /// \brief Attempts to merge the given declaration (D) with another declaration
2299*67e74705SXin Li /// of the same entity.
2300*67e74705SXin Li template<typename T>
mergeRedeclarable(Redeclarable<T> * DBase,RedeclarableResult & Redecl,DeclID TemplatePatternID)2301*67e74705SXin Li void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
2302*67e74705SXin Li                                       RedeclarableResult &Redecl,
2303*67e74705SXin Li                                       DeclID TemplatePatternID) {
2304*67e74705SXin Li   T *D = static_cast<T*>(DBase);
2305*67e74705SXin Li 
2306*67e74705SXin Li   // If modules are not available, there is no reason to perform this merge.
2307*67e74705SXin Li   if (!Reader.getContext().getLangOpts().Modules)
2308*67e74705SXin Li     return;
2309*67e74705SXin Li 
2310*67e74705SXin Li   // If we're not the canonical declaration, we don't need to merge.
2311*67e74705SXin Li   if (!DBase->isFirstDecl())
2312*67e74705SXin Li     return;
2313*67e74705SXin Li 
2314*67e74705SXin Li   if (auto *Existing = Redecl.getKnownMergeTarget())
2315*67e74705SXin Li     // We already know of an existing declaration we should merge with.
2316*67e74705SXin Li     mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2317*67e74705SXin Li   else if (FindExistingResult ExistingRes = findExisting(D))
2318*67e74705SXin Li     if (T *Existing = ExistingRes)
2319*67e74705SXin Li       mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2320*67e74705SXin Li }
2321*67e74705SXin Li 
2322*67e74705SXin Li /// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2323*67e74705SXin Li /// We use this to put code in a template that will only be valid for certain
2324*67e74705SXin Li /// instantiations.
assert_cast(T t)2325*67e74705SXin Li template<typename T> static T assert_cast(T t) { return t; }
assert_cast(...)2326*67e74705SXin Li template<typename T> static T assert_cast(...) {
2327*67e74705SXin Li   llvm_unreachable("bad assert_cast");
2328*67e74705SXin Li }
2329*67e74705SXin Li 
2330*67e74705SXin Li /// \brief Merge together the pattern declarations from two template
2331*67e74705SXin Li /// declarations.
mergeTemplatePattern(RedeclarableTemplateDecl * D,RedeclarableTemplateDecl * Existing,DeclID DsID,bool IsKeyDecl)2332*67e74705SXin Li void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2333*67e74705SXin Li                                          RedeclarableTemplateDecl *Existing,
2334*67e74705SXin Li                                          DeclID DsID, bool IsKeyDecl) {
2335*67e74705SXin Li   auto *DPattern = D->getTemplatedDecl();
2336*67e74705SXin Li   auto *ExistingPattern = Existing->getTemplatedDecl();
2337*67e74705SXin Li   RedeclarableResult Result(DPattern->getCanonicalDecl()->getGlobalID(),
2338*67e74705SXin Li                             /*MergeWith*/ ExistingPattern, IsKeyDecl);
2339*67e74705SXin Li 
2340*67e74705SXin Li   if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2341*67e74705SXin Li     // Merge with any existing definition.
2342*67e74705SXin Li     // FIXME: This is duplicated in several places. Refactor.
2343*67e74705SXin Li     auto *ExistingClass =
2344*67e74705SXin Li         cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
2345*67e74705SXin Li     if (auto *DDD = DClass->DefinitionData) {
2346*67e74705SXin Li       if (ExistingClass->DefinitionData) {
2347*67e74705SXin Li         MergeDefinitionData(ExistingClass, std::move(*DDD));
2348*67e74705SXin Li       } else {
2349*67e74705SXin Li         ExistingClass->DefinitionData = DClass->DefinitionData;
2350*67e74705SXin Li         // We may have skipped this before because we thought that DClass
2351*67e74705SXin Li         // was the canonical declaration.
2352*67e74705SXin Li         Reader.PendingDefinitions.insert(DClass);
2353*67e74705SXin Li       }
2354*67e74705SXin Li     }
2355*67e74705SXin Li     DClass->DefinitionData = ExistingClass->DefinitionData;
2356*67e74705SXin Li 
2357*67e74705SXin Li     return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2358*67e74705SXin Li                              Result);
2359*67e74705SXin Li   }
2360*67e74705SXin Li   if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2361*67e74705SXin Li     return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2362*67e74705SXin Li                              Result);
2363*67e74705SXin Li   if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2364*67e74705SXin Li     return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
2365*67e74705SXin Li   if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2366*67e74705SXin Li     return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2367*67e74705SXin Li                              Result);
2368*67e74705SXin Li   llvm_unreachable("merged an unknown kind of redeclarable template");
2369*67e74705SXin Li }
2370*67e74705SXin Li 
2371*67e74705SXin Li /// \brief Attempts to merge the given declaration (D) with another declaration
2372*67e74705SXin Li /// of the same entity.
2373*67e74705SXin Li template<typename T>
mergeRedeclarable(Redeclarable<T> * DBase,T * Existing,RedeclarableResult & Redecl,DeclID TemplatePatternID)2374*67e74705SXin Li void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2375*67e74705SXin Li                                       RedeclarableResult &Redecl,
2376*67e74705SXin Li                                       DeclID TemplatePatternID) {
2377*67e74705SXin Li   T *D = static_cast<T*>(DBase);
2378*67e74705SXin Li   T *ExistingCanon = Existing->getCanonicalDecl();
2379*67e74705SXin Li   T *DCanon = D->getCanonicalDecl();
2380*67e74705SXin Li   if (ExistingCanon != DCanon) {
2381*67e74705SXin Li     assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2382*67e74705SXin Li            "already merged this declaration");
2383*67e74705SXin Li 
2384*67e74705SXin Li     // Have our redeclaration link point back at the canonical declaration
2385*67e74705SXin Li     // of the existing declaration, so that this declaration has the
2386*67e74705SXin Li     // appropriate canonical declaration.
2387*67e74705SXin Li     D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
2388*67e74705SXin Li     D->First = ExistingCanon;
2389*67e74705SXin Li     ExistingCanon->Used |= D->Used;
2390*67e74705SXin Li     D->Used = false;
2391*67e74705SXin Li 
2392*67e74705SXin Li     // When we merge a namespace, update its pointer to the first namespace.
2393*67e74705SXin Li     // We cannot have loaded any redeclarations of this declaration yet, so
2394*67e74705SXin Li     // there's nothing else that needs to be updated.
2395*67e74705SXin Li     if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
2396*67e74705SXin Li       Namespace->AnonOrFirstNamespaceAndInline.setPointer(
2397*67e74705SXin Li           assert_cast<NamespaceDecl*>(ExistingCanon));
2398*67e74705SXin Li 
2399*67e74705SXin Li     // When we merge a template, merge its pattern.
2400*67e74705SXin Li     if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2401*67e74705SXin Li       mergeTemplatePattern(
2402*67e74705SXin Li           DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
2403*67e74705SXin Li           TemplatePatternID, Redecl.isKeyDecl());
2404*67e74705SXin Li 
2405*67e74705SXin Li     // If this declaration is a key declaration, make a note of that.
2406*67e74705SXin Li     if (Redecl.isKeyDecl())
2407*67e74705SXin Li       Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
2408*67e74705SXin Li   }
2409*67e74705SXin Li }
2410*67e74705SXin Li 
2411*67e74705SXin Li /// \brief Attempts to merge the given declaration (D) with another declaration
2412*67e74705SXin Li /// of the same entity, for the case where the entity is not actually
2413*67e74705SXin Li /// redeclarable. This happens, for instance, when merging the fields of
2414*67e74705SXin Li /// identical class definitions from two different modules.
2415*67e74705SXin Li template<typename T>
mergeMergeable(Mergeable<T> * D)2416*67e74705SXin Li void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2417*67e74705SXin Li   // If modules are not available, there is no reason to perform this merge.
2418*67e74705SXin Li   if (!Reader.getContext().getLangOpts().Modules)
2419*67e74705SXin Li     return;
2420*67e74705SXin Li 
2421*67e74705SXin Li   // ODR-based merging is only performed in C++. In C, identically-named things
2422*67e74705SXin Li   // in different translation units are not redeclarations (but may still have
2423*67e74705SXin Li   // compatible types).
2424*67e74705SXin Li   if (!Reader.getContext().getLangOpts().CPlusPlus)
2425*67e74705SXin Li     return;
2426*67e74705SXin Li 
2427*67e74705SXin Li   if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2428*67e74705SXin Li     if (T *Existing = ExistingRes)
2429*67e74705SXin Li       Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
2430*67e74705SXin Li                                           Existing->getCanonicalDecl());
2431*67e74705SXin Li }
2432*67e74705SXin Li 
VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl * D)2433*67e74705SXin Li void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2434*67e74705SXin Li   VisitDecl(D);
2435*67e74705SXin Li   unsigned NumVars = D->varlist_size();
2436*67e74705SXin Li   SmallVector<Expr *, 16> Vars;
2437*67e74705SXin Li   Vars.reserve(NumVars);
2438*67e74705SXin Li   for (unsigned i = 0; i != NumVars; ++i) {
2439*67e74705SXin Li     Vars.push_back(Reader.ReadExpr(F));
2440*67e74705SXin Li   }
2441*67e74705SXin Li   D->setVars(Vars);
2442*67e74705SXin Li }
2443*67e74705SXin Li 
VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl * D)2444*67e74705SXin Li void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
2445*67e74705SXin Li   VisitValueDecl(D);
2446*67e74705SXin Li   D->setLocation(Reader.ReadSourceLocation(F, Record, Idx));
2447*67e74705SXin Li   D->setCombiner(Reader.ReadExpr(F));
2448*67e74705SXin Li   D->setInitializer(Reader.ReadExpr(F));
2449*67e74705SXin Li   D->PrevDeclInScope = Reader.ReadDeclID(F, Record, Idx);
2450*67e74705SXin Li }
2451*67e74705SXin Li 
VisitOMPCapturedExprDecl(OMPCapturedExprDecl * D)2452*67e74705SXin Li void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
2453*67e74705SXin Li   VisitVarDecl(D);
2454*67e74705SXin Li }
2455*67e74705SXin Li 
2456*67e74705SXin Li //===----------------------------------------------------------------------===//
2457*67e74705SXin Li // Attribute Reading
2458*67e74705SXin Li //===----------------------------------------------------------------------===//
2459*67e74705SXin Li 
2460*67e74705SXin Li /// \brief Reads attributes from the current stream position.
ReadAttributes(ModuleFile & F,AttrVec & Attrs,const RecordData & Record,unsigned & Idx)2461*67e74705SXin Li void ASTReader::ReadAttributes(ModuleFile &F, AttrVec &Attrs,
2462*67e74705SXin Li                                const RecordData &Record, unsigned &Idx) {
2463*67e74705SXin Li   for (unsigned i = 0, e = Record[Idx++]; i != e; ++i) {
2464*67e74705SXin Li     Attr *New = nullptr;
2465*67e74705SXin Li     attr::Kind Kind = (attr::Kind)Record[Idx++];
2466*67e74705SXin Li     SourceRange Range = ReadSourceRange(F, Record, Idx);
2467*67e74705SXin Li 
2468*67e74705SXin Li #include "clang/Serialization/AttrPCHRead.inc"
2469*67e74705SXin Li 
2470*67e74705SXin Li     assert(New && "Unable to decode attribute?");
2471*67e74705SXin Li     Attrs.push_back(New);
2472*67e74705SXin Li   }
2473*67e74705SXin Li }
2474*67e74705SXin Li 
2475*67e74705SXin Li //===----------------------------------------------------------------------===//
2476*67e74705SXin Li // ASTReader Implementation
2477*67e74705SXin Li //===----------------------------------------------------------------------===//
2478*67e74705SXin Li 
2479*67e74705SXin Li /// \brief Note that we have loaded the declaration with the given
2480*67e74705SXin Li /// Index.
2481*67e74705SXin Li ///
2482*67e74705SXin Li /// This routine notes that this declaration has already been loaded,
2483*67e74705SXin Li /// so that future GetDecl calls will return this declaration rather
2484*67e74705SXin Li /// than trying to load a new declaration.
LoadedDecl(unsigned Index,Decl * D)2485*67e74705SXin Li inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
2486*67e74705SXin Li   assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2487*67e74705SXin Li   DeclsLoaded[Index] = D;
2488*67e74705SXin Li }
2489*67e74705SXin Li 
2490*67e74705SXin Li 
2491*67e74705SXin Li /// \brief Determine whether the consumer will be interested in seeing
2492*67e74705SXin Li /// this declaration (via HandleTopLevelDecl).
2493*67e74705SXin Li ///
2494*67e74705SXin Li /// This routine should return true for anything that might affect
2495*67e74705SXin Li /// code generation, e.g., inline function definitions, Objective-C
2496*67e74705SXin Li /// declarations with metadata, etc.
isConsumerInterestedIn(Decl * D,bool HasBody)2497*67e74705SXin Li static bool isConsumerInterestedIn(Decl *D, bool HasBody) {
2498*67e74705SXin Li   // An ObjCMethodDecl is never considered as "interesting" because its
2499*67e74705SXin Li   // implementation container always is.
2500*67e74705SXin Li 
2501*67e74705SXin Li   if (isa<FileScopeAsmDecl>(D) ||
2502*67e74705SXin Li       isa<ObjCProtocolDecl>(D) ||
2503*67e74705SXin Li       isa<ObjCImplDecl>(D) ||
2504*67e74705SXin Li       isa<ImportDecl>(D) ||
2505*67e74705SXin Li       isa<PragmaCommentDecl>(D) ||
2506*67e74705SXin Li       isa<PragmaDetectMismatchDecl>(D))
2507*67e74705SXin Li     return true;
2508*67e74705SXin Li   if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2509*67e74705SXin Li     return !D->getDeclContext()->isFunctionOrMethod();
2510*67e74705SXin Li   if (VarDecl *Var = dyn_cast<VarDecl>(D))
2511*67e74705SXin Li     return Var->isFileVarDecl() &&
2512*67e74705SXin Li            Var->isThisDeclarationADefinition() == VarDecl::Definition;
2513*67e74705SXin Li   if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
2514*67e74705SXin Li     return Func->doesThisDeclarationHaveABody() || HasBody;
2515*67e74705SXin Li 
2516*67e74705SXin Li   return false;
2517*67e74705SXin Li }
2518*67e74705SXin Li 
2519*67e74705SXin Li /// \brief Get the correct cursor and offset for loading a declaration.
2520*67e74705SXin Li ASTReader::RecordLocation
DeclCursorForID(DeclID ID,SourceLocation & Loc)2521*67e74705SXin Li ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
2522*67e74705SXin Li   GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2523*67e74705SXin Li   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
2524*67e74705SXin Li   ModuleFile *M = I->second;
2525*67e74705SXin Li   const DeclOffset &DOffs =
2526*67e74705SXin Li       M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
2527*67e74705SXin Li   Loc = TranslateSourceLocation(*M, DOffs.getLocation());
2528*67e74705SXin Li   return RecordLocation(M, DOffs.BitOffset);
2529*67e74705SXin Li }
2530*67e74705SXin Li 
getLocalBitOffset(uint64_t GlobalOffset)2531*67e74705SXin Li ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
2532*67e74705SXin Li   ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
2533*67e74705SXin Li     = GlobalBitOffsetsMap.find(GlobalOffset);
2534*67e74705SXin Li 
2535*67e74705SXin Li   assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2536*67e74705SXin Li   return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2537*67e74705SXin Li }
2538*67e74705SXin Li 
getGlobalBitOffset(ModuleFile & M,uint32_t LocalOffset)2539*67e74705SXin Li uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
2540*67e74705SXin Li   return LocalOffset + M.GlobalBitOffset;
2541*67e74705SXin Li }
2542*67e74705SXin Li 
2543*67e74705SXin Li static bool isSameTemplateParameterList(const TemplateParameterList *X,
2544*67e74705SXin Li                                         const TemplateParameterList *Y);
2545*67e74705SXin Li 
2546*67e74705SXin Li /// \brief Determine whether two template parameters are similar enough
2547*67e74705SXin Li /// that they may be used in declarations of the same template.
isSameTemplateParameter(const NamedDecl * X,const NamedDecl * Y)2548*67e74705SXin Li static bool isSameTemplateParameter(const NamedDecl *X,
2549*67e74705SXin Li                                     const NamedDecl *Y) {
2550*67e74705SXin Li   if (X->getKind() != Y->getKind())
2551*67e74705SXin Li     return false;
2552*67e74705SXin Li 
2553*67e74705SXin Li   if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2554*67e74705SXin Li     const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2555*67e74705SXin Li     return TX->isParameterPack() == TY->isParameterPack();
2556*67e74705SXin Li   }
2557*67e74705SXin Li 
2558*67e74705SXin Li   if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2559*67e74705SXin Li     const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2560*67e74705SXin Li     return TX->isParameterPack() == TY->isParameterPack() &&
2561*67e74705SXin Li            TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2562*67e74705SXin Li   }
2563*67e74705SXin Li 
2564*67e74705SXin Li   const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2565*67e74705SXin Li   const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2566*67e74705SXin Li   return TX->isParameterPack() == TY->isParameterPack() &&
2567*67e74705SXin Li          isSameTemplateParameterList(TX->getTemplateParameters(),
2568*67e74705SXin Li                                      TY->getTemplateParameters());
2569*67e74705SXin Li }
2570*67e74705SXin Li 
getNamespace(const NestedNameSpecifier * X)2571*67e74705SXin Li static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2572*67e74705SXin Li   if (auto *NS = X->getAsNamespace())
2573*67e74705SXin Li     return NS;
2574*67e74705SXin Li   if (auto *NAS = X->getAsNamespaceAlias())
2575*67e74705SXin Li     return NAS->getNamespace();
2576*67e74705SXin Li   return nullptr;
2577*67e74705SXin Li }
2578*67e74705SXin Li 
isSameQualifier(const NestedNameSpecifier * X,const NestedNameSpecifier * Y)2579*67e74705SXin Li static bool isSameQualifier(const NestedNameSpecifier *X,
2580*67e74705SXin Li                             const NestedNameSpecifier *Y) {
2581*67e74705SXin Li   if (auto *NSX = getNamespace(X)) {
2582*67e74705SXin Li     auto *NSY = getNamespace(Y);
2583*67e74705SXin Li     if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2584*67e74705SXin Li       return false;
2585*67e74705SXin Li   } else if (X->getKind() != Y->getKind())
2586*67e74705SXin Li     return false;
2587*67e74705SXin Li 
2588*67e74705SXin Li   // FIXME: For namespaces and types, we're permitted to check that the entity
2589*67e74705SXin Li   // is named via the same tokens. We should probably do so.
2590*67e74705SXin Li   switch (X->getKind()) {
2591*67e74705SXin Li   case NestedNameSpecifier::Identifier:
2592*67e74705SXin Li     if (X->getAsIdentifier() != Y->getAsIdentifier())
2593*67e74705SXin Li       return false;
2594*67e74705SXin Li     break;
2595*67e74705SXin Li   case NestedNameSpecifier::Namespace:
2596*67e74705SXin Li   case NestedNameSpecifier::NamespaceAlias:
2597*67e74705SXin Li     // We've already checked that we named the same namespace.
2598*67e74705SXin Li     break;
2599*67e74705SXin Li   case NestedNameSpecifier::TypeSpec:
2600*67e74705SXin Li   case NestedNameSpecifier::TypeSpecWithTemplate:
2601*67e74705SXin Li     if (X->getAsType()->getCanonicalTypeInternal() !=
2602*67e74705SXin Li         Y->getAsType()->getCanonicalTypeInternal())
2603*67e74705SXin Li       return false;
2604*67e74705SXin Li     break;
2605*67e74705SXin Li   case NestedNameSpecifier::Global:
2606*67e74705SXin Li   case NestedNameSpecifier::Super:
2607*67e74705SXin Li     return true;
2608*67e74705SXin Li   }
2609*67e74705SXin Li 
2610*67e74705SXin Li   // Recurse into earlier portion of NNS, if any.
2611*67e74705SXin Li   auto *PX = X->getPrefix();
2612*67e74705SXin Li   auto *PY = Y->getPrefix();
2613*67e74705SXin Li   if (PX && PY)
2614*67e74705SXin Li     return isSameQualifier(PX, PY);
2615*67e74705SXin Li   return !PX && !PY;
2616*67e74705SXin Li }
2617*67e74705SXin Li 
2618*67e74705SXin Li /// \brief Determine whether two template parameter lists are similar enough
2619*67e74705SXin Li /// that they may be used in declarations of the same template.
isSameTemplateParameterList(const TemplateParameterList * X,const TemplateParameterList * Y)2620*67e74705SXin Li static bool isSameTemplateParameterList(const TemplateParameterList *X,
2621*67e74705SXin Li                                         const TemplateParameterList *Y) {
2622*67e74705SXin Li   if (X->size() != Y->size())
2623*67e74705SXin Li     return false;
2624*67e74705SXin Li 
2625*67e74705SXin Li   for (unsigned I = 0, N = X->size(); I != N; ++I)
2626*67e74705SXin Li     if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2627*67e74705SXin Li       return false;
2628*67e74705SXin Li 
2629*67e74705SXin Li   return true;
2630*67e74705SXin Li }
2631*67e74705SXin Li 
2632*67e74705SXin Li /// \brief Determine whether the two declarations refer to the same entity.
isSameEntity(NamedDecl * X,NamedDecl * Y)2633*67e74705SXin Li static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2634*67e74705SXin Li   assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
2635*67e74705SXin Li 
2636*67e74705SXin Li   if (X == Y)
2637*67e74705SXin Li     return true;
2638*67e74705SXin Li 
2639*67e74705SXin Li   // Must be in the same context.
2640*67e74705SXin Li   if (!X->getDeclContext()->getRedeclContext()->Equals(
2641*67e74705SXin Li          Y->getDeclContext()->getRedeclContext()))
2642*67e74705SXin Li     return false;
2643*67e74705SXin Li 
2644*67e74705SXin Li   // Two typedefs refer to the same entity if they have the same underlying
2645*67e74705SXin Li   // type.
2646*67e74705SXin Li   if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2647*67e74705SXin Li     if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2648*67e74705SXin Li       return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2649*67e74705SXin Li                                             TypedefY->getUnderlyingType());
2650*67e74705SXin Li 
2651*67e74705SXin Li   // Must have the same kind.
2652*67e74705SXin Li   if (X->getKind() != Y->getKind())
2653*67e74705SXin Li     return false;
2654*67e74705SXin Li 
2655*67e74705SXin Li   // Objective-C classes and protocols with the same name always match.
2656*67e74705SXin Li   if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
2657*67e74705SXin Li     return true;
2658*67e74705SXin Li 
2659*67e74705SXin Li   if (isa<ClassTemplateSpecializationDecl>(X)) {
2660*67e74705SXin Li     // No need to handle these here: we merge them when adding them to the
2661*67e74705SXin Li     // template.
2662*67e74705SXin Li     return false;
2663*67e74705SXin Li   }
2664*67e74705SXin Li 
2665*67e74705SXin Li   // Compatible tags match.
2666*67e74705SXin Li   if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2667*67e74705SXin Li     TagDecl *TagY = cast<TagDecl>(Y);
2668*67e74705SXin Li     return (TagX->getTagKind() == TagY->getTagKind()) ||
2669*67e74705SXin Li       ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2670*67e74705SXin Li         TagX->getTagKind() == TTK_Interface) &&
2671*67e74705SXin Li        (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2672*67e74705SXin Li         TagY->getTagKind() == TTK_Interface));
2673*67e74705SXin Li   }
2674*67e74705SXin Li 
2675*67e74705SXin Li   // Functions with the same type and linkage match.
2676*67e74705SXin Li   // FIXME: This needs to cope with merging of prototyped/non-prototyped
2677*67e74705SXin Li   // functions, etc.
2678*67e74705SXin Li   if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2679*67e74705SXin Li     FunctionDecl *FuncY = cast<FunctionDecl>(Y);
2680*67e74705SXin Li     if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2681*67e74705SXin Li       CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2682*67e74705SXin Li       if (CtorX->getInheritedConstructor() &&
2683*67e74705SXin Li           !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2684*67e74705SXin Li                         CtorY->getInheritedConstructor().getConstructor()))
2685*67e74705SXin Li         return false;
2686*67e74705SXin Li     }
2687*67e74705SXin Li     return (FuncX->getLinkageInternal() == FuncY->getLinkageInternal()) &&
2688*67e74705SXin Li       FuncX->getASTContext().hasSameType(FuncX->getType(), FuncY->getType());
2689*67e74705SXin Li   }
2690*67e74705SXin Li 
2691*67e74705SXin Li   // Variables with the same type and linkage match.
2692*67e74705SXin Li   if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2693*67e74705SXin Li     VarDecl *VarY = cast<VarDecl>(Y);
2694*67e74705SXin Li     if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2695*67e74705SXin Li       ASTContext &C = VarX->getASTContext();
2696*67e74705SXin Li       if (C.hasSameType(VarX->getType(), VarY->getType()))
2697*67e74705SXin Li         return true;
2698*67e74705SXin Li 
2699*67e74705SXin Li       // We can get decls with different types on the redecl chain. Eg.
2700*67e74705SXin Li       // template <typename T> struct S { static T Var[]; }; // #1
2701*67e74705SXin Li       // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2702*67e74705SXin Li       // Only? happens when completing an incomplete array type. In this case
2703*67e74705SXin Li       // when comparing #1 and #2 we should go through their element type.
2704*67e74705SXin Li       const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2705*67e74705SXin Li       const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2706*67e74705SXin Li       if (!VarXTy || !VarYTy)
2707*67e74705SXin Li         return false;
2708*67e74705SXin Li       if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2709*67e74705SXin Li         return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2710*67e74705SXin Li     }
2711*67e74705SXin Li     return false;
2712*67e74705SXin Li   }
2713*67e74705SXin Li 
2714*67e74705SXin Li   // Namespaces with the same name and inlinedness match.
2715*67e74705SXin Li   if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2716*67e74705SXin Li     NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2717*67e74705SXin Li     return NamespaceX->isInline() == NamespaceY->isInline();
2718*67e74705SXin Li   }
2719*67e74705SXin Li 
2720*67e74705SXin Li   // Identical template names and kinds match if their template parameter lists
2721*67e74705SXin Li   // and patterns match.
2722*67e74705SXin Li   if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
2723*67e74705SXin Li     TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
2724*67e74705SXin Li     return isSameEntity(TemplateX->getTemplatedDecl(),
2725*67e74705SXin Li                         TemplateY->getTemplatedDecl()) &&
2726*67e74705SXin Li            isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2727*67e74705SXin Li                                        TemplateY->getTemplateParameters());
2728*67e74705SXin Li   }
2729*67e74705SXin Li 
2730*67e74705SXin Li   // Fields with the same name and the same type match.
2731*67e74705SXin Li   if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2732*67e74705SXin Li     FieldDecl *FDY = cast<FieldDecl>(Y);
2733*67e74705SXin Li     // FIXME: Also check the bitwidth is odr-equivalent, if any.
2734*67e74705SXin Li     return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2735*67e74705SXin Li   }
2736*67e74705SXin Li 
2737*67e74705SXin Li   // Indirect fields with the same target field match.
2738*67e74705SXin Li   if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2739*67e74705SXin Li     auto *IFDY = cast<IndirectFieldDecl>(Y);
2740*67e74705SXin Li     return IFDX->getAnonField()->getCanonicalDecl() ==
2741*67e74705SXin Li            IFDY->getAnonField()->getCanonicalDecl();
2742*67e74705SXin Li   }
2743*67e74705SXin Li 
2744*67e74705SXin Li   // Enumerators with the same name match.
2745*67e74705SXin Li   if (isa<EnumConstantDecl>(X))
2746*67e74705SXin Li     // FIXME: Also check the value is odr-equivalent.
2747*67e74705SXin Li     return true;
2748*67e74705SXin Li 
2749*67e74705SXin Li   // Using shadow declarations with the same target match.
2750*67e74705SXin Li   if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2751*67e74705SXin Li     UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2752*67e74705SXin Li     return USX->getTargetDecl() == USY->getTargetDecl();
2753*67e74705SXin Li   }
2754*67e74705SXin Li 
2755*67e74705SXin Li   // Using declarations with the same qualifier match. (We already know that
2756*67e74705SXin Li   // the name matches.)
2757*67e74705SXin Li   if (auto *UX = dyn_cast<UsingDecl>(X)) {
2758*67e74705SXin Li     auto *UY = cast<UsingDecl>(Y);
2759*67e74705SXin Li     return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2760*67e74705SXin Li            UX->hasTypename() == UY->hasTypename() &&
2761*67e74705SXin Li            UX->isAccessDeclaration() == UY->isAccessDeclaration();
2762*67e74705SXin Li   }
2763*67e74705SXin Li   if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2764*67e74705SXin Li     auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2765*67e74705SXin Li     return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2766*67e74705SXin Li            UX->isAccessDeclaration() == UY->isAccessDeclaration();
2767*67e74705SXin Li   }
2768*67e74705SXin Li   if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2769*67e74705SXin Li     return isSameQualifier(
2770*67e74705SXin Li         UX->getQualifier(),
2771*67e74705SXin Li         cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2772*67e74705SXin Li 
2773*67e74705SXin Li   // Namespace alias definitions with the same target match.
2774*67e74705SXin Li   if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2775*67e74705SXin Li     auto *NAY = cast<NamespaceAliasDecl>(Y);
2776*67e74705SXin Li     return NAX->getNamespace()->Equals(NAY->getNamespace());
2777*67e74705SXin Li   }
2778*67e74705SXin Li 
2779*67e74705SXin Li   return false;
2780*67e74705SXin Li }
2781*67e74705SXin Li 
2782*67e74705SXin Li /// Find the context in which we should search for previous declarations when
2783*67e74705SXin Li /// looking for declarations to merge.
getPrimaryContextForMerging(ASTReader & Reader,DeclContext * DC)2784*67e74705SXin Li DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2785*67e74705SXin Li                                                         DeclContext *DC) {
2786*67e74705SXin Li   if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2787*67e74705SXin Li     return ND->getOriginalNamespace();
2788*67e74705SXin Li 
2789*67e74705SXin Li   if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2790*67e74705SXin Li     // Try to dig out the definition.
2791*67e74705SXin Li     auto *DD = RD->DefinitionData;
2792*67e74705SXin Li     if (!DD)
2793*67e74705SXin Li       DD = RD->getCanonicalDecl()->DefinitionData;
2794*67e74705SXin Li 
2795*67e74705SXin Li     // If there's no definition yet, then DC's definition is added by an update
2796*67e74705SXin Li     // record, but we've not yet loaded that update record. In this case, we
2797*67e74705SXin Li     // commit to DC being the canonical definition now, and will fix this when
2798*67e74705SXin Li     // we load the update record.
2799*67e74705SXin Li     if (!DD) {
2800*67e74705SXin Li       DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD);
2801*67e74705SXin Li       RD->IsCompleteDefinition = true;
2802*67e74705SXin Li       RD->DefinitionData = DD;
2803*67e74705SXin Li       RD->getCanonicalDecl()->DefinitionData = DD;
2804*67e74705SXin Li 
2805*67e74705SXin Li       // Track that we did this horrible thing so that we can fix it later.
2806*67e74705SXin Li       Reader.PendingFakeDefinitionData.insert(
2807*67e74705SXin Li           std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
2808*67e74705SXin Li     }
2809*67e74705SXin Li 
2810*67e74705SXin Li     return DD->Definition;
2811*67e74705SXin Li   }
2812*67e74705SXin Li 
2813*67e74705SXin Li   if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
2814*67e74705SXin Li     return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2815*67e74705SXin Li                                                       : nullptr;
2816*67e74705SXin Li 
2817*67e74705SXin Li   // We can see the TU here only if we have no Sema object. In that case,
2818*67e74705SXin Li   // there's no TU scope to look in, so using the DC alone is sufficient.
2819*67e74705SXin Li   if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2820*67e74705SXin Li     return TU;
2821*67e74705SXin Li 
2822*67e74705SXin Li   return nullptr;
2823*67e74705SXin Li }
2824*67e74705SXin Li 
~FindExistingResult()2825*67e74705SXin Li ASTDeclReader::FindExistingResult::~FindExistingResult() {
2826*67e74705SXin Li   // Record that we had a typedef name for linkage whether or not we merge
2827*67e74705SXin Li   // with that declaration.
2828*67e74705SXin Li   if (TypedefNameForLinkage) {
2829*67e74705SXin Li     DeclContext *DC = New->getDeclContext()->getRedeclContext();
2830*67e74705SXin Li     Reader.ImportedTypedefNamesForLinkage.insert(
2831*67e74705SXin Li         std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2832*67e74705SXin Li     return;
2833*67e74705SXin Li   }
2834*67e74705SXin Li 
2835*67e74705SXin Li   if (!AddResult || Existing)
2836*67e74705SXin Li     return;
2837*67e74705SXin Li 
2838*67e74705SXin Li   DeclarationName Name = New->getDeclName();
2839*67e74705SXin Li   DeclContext *DC = New->getDeclContext()->getRedeclContext();
2840*67e74705SXin Li   if (needsAnonymousDeclarationNumber(New)) {
2841*67e74705SXin Li     setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2842*67e74705SXin Li                                AnonymousDeclNumber, New);
2843*67e74705SXin Li   } else if (DC->isTranslationUnit() &&
2844*67e74705SXin Li              !Reader.getContext().getLangOpts().CPlusPlus) {
2845*67e74705SXin Li     if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
2846*67e74705SXin Li       Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2847*67e74705SXin Li             .push_back(New);
2848*67e74705SXin Li   } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
2849*67e74705SXin Li     // Add the declaration to its redeclaration context so later merging
2850*67e74705SXin Li     // lookups will find it.
2851*67e74705SXin Li     MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
2852*67e74705SXin Li   }
2853*67e74705SXin Li }
2854*67e74705SXin Li 
2855*67e74705SXin Li /// Find the declaration that should be merged into, given the declaration found
2856*67e74705SXin Li /// by name lookup. If we're merging an anonymous declaration within a typedef,
2857*67e74705SXin Li /// we need a matching typedef, and we merge with the type inside it.
getDeclForMerging(NamedDecl * Found,bool IsTypedefNameForLinkage)2858*67e74705SXin Li static NamedDecl *getDeclForMerging(NamedDecl *Found,
2859*67e74705SXin Li                                     bool IsTypedefNameForLinkage) {
2860*67e74705SXin Li   if (!IsTypedefNameForLinkage)
2861*67e74705SXin Li     return Found;
2862*67e74705SXin Li 
2863*67e74705SXin Li   // If we found a typedef declaration that gives a name to some other
2864*67e74705SXin Li   // declaration, then we want that inner declaration. Declarations from
2865*67e74705SXin Li   // AST files are handled via ImportedTypedefNamesForLinkage.
2866*67e74705SXin Li   if (Found->isFromASTFile())
2867*67e74705SXin Li     return nullptr;
2868*67e74705SXin Li 
2869*67e74705SXin Li   if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
2870*67e74705SXin Li     return TND->getAnonDeclWithTypedefName();
2871*67e74705SXin Li 
2872*67e74705SXin Li   return nullptr;
2873*67e74705SXin Li }
2874*67e74705SXin Li 
getAnonymousDeclForMerging(ASTReader & Reader,DeclContext * DC,unsigned Index)2875*67e74705SXin Li NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
2876*67e74705SXin Li                                                      DeclContext *DC,
2877*67e74705SXin Li                                                      unsigned Index) {
2878*67e74705SXin Li   // If the lexical context has been merged, look into the now-canonical
2879*67e74705SXin Li   // definition.
2880*67e74705SXin Li   if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2881*67e74705SXin Li     DC = Merged;
2882*67e74705SXin Li 
2883*67e74705SXin Li   // If we've seen this before, return the canonical declaration.
2884*67e74705SXin Li   auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2885*67e74705SXin Li   if (Index < Previous.size() && Previous[Index])
2886*67e74705SXin Li     return Previous[Index];
2887*67e74705SXin Li 
2888*67e74705SXin Li   // If this is the first time, but we have parsed a declaration of the context,
2889*67e74705SXin Li   // build the anonymous declaration list from the parsed declaration.
2890*67e74705SXin Li   if (!cast<Decl>(DC)->isFromASTFile()) {
2891*67e74705SXin Li     numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
2892*67e74705SXin Li       if (Previous.size() == Number)
2893*67e74705SXin Li         Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
2894*67e74705SXin Li       else
2895*67e74705SXin Li         Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
2896*67e74705SXin Li     });
2897*67e74705SXin Li   }
2898*67e74705SXin Li 
2899*67e74705SXin Li   return Index < Previous.size() ? Previous[Index] : nullptr;
2900*67e74705SXin Li }
2901*67e74705SXin Li 
setAnonymousDeclForMerging(ASTReader & Reader,DeclContext * DC,unsigned Index,NamedDecl * D)2902*67e74705SXin Li void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
2903*67e74705SXin Li                                                DeclContext *DC, unsigned Index,
2904*67e74705SXin Li                                                NamedDecl *D) {
2905*67e74705SXin Li   if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2906*67e74705SXin Li     DC = Merged;
2907*67e74705SXin Li 
2908*67e74705SXin Li   auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2909*67e74705SXin Li   if (Index >= Previous.size())
2910*67e74705SXin Li     Previous.resize(Index + 1);
2911*67e74705SXin Li   if (!Previous[Index])
2912*67e74705SXin Li     Previous[Index] = D;
2913*67e74705SXin Li }
2914*67e74705SXin Li 
findExisting(NamedDecl * D)2915*67e74705SXin Li ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
2916*67e74705SXin Li   DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
2917*67e74705SXin Li                                                : D->getDeclName();
2918*67e74705SXin Li 
2919*67e74705SXin Li   if (!Name && !needsAnonymousDeclarationNumber(D)) {
2920*67e74705SXin Li     // Don't bother trying to find unnamed declarations that are in
2921*67e74705SXin Li     // unmergeable contexts.
2922*67e74705SXin Li     FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
2923*67e74705SXin Li                               AnonymousDeclNumber, TypedefNameForLinkage);
2924*67e74705SXin Li     Result.suppress();
2925*67e74705SXin Li     return Result;
2926*67e74705SXin Li   }
2927*67e74705SXin Li 
2928*67e74705SXin Li   DeclContext *DC = D->getDeclContext()->getRedeclContext();
2929*67e74705SXin Li   if (TypedefNameForLinkage) {
2930*67e74705SXin Li     auto It = Reader.ImportedTypedefNamesForLinkage.find(
2931*67e74705SXin Li         std::make_pair(DC, TypedefNameForLinkage));
2932*67e74705SXin Li     if (It != Reader.ImportedTypedefNamesForLinkage.end())
2933*67e74705SXin Li       if (isSameEntity(It->second, D))
2934*67e74705SXin Li         return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
2935*67e74705SXin Li                                   TypedefNameForLinkage);
2936*67e74705SXin Li     // Go on to check in other places in case an existing typedef name
2937*67e74705SXin Li     // was not imported.
2938*67e74705SXin Li   }
2939*67e74705SXin Li 
2940*67e74705SXin Li   if (needsAnonymousDeclarationNumber(D)) {
2941*67e74705SXin Li     // This is an anonymous declaration that we may need to merge. Look it up
2942*67e74705SXin Li     // in its context by number.
2943*67e74705SXin Li     if (auto *Existing = getAnonymousDeclForMerging(
2944*67e74705SXin Li             Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
2945*67e74705SXin Li       if (isSameEntity(Existing, D))
2946*67e74705SXin Li         return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
2947*67e74705SXin Li                                   TypedefNameForLinkage);
2948*67e74705SXin Li   } else if (DC->isTranslationUnit() &&
2949*67e74705SXin Li              !Reader.getContext().getLangOpts().CPlusPlus) {
2950*67e74705SXin Li     IdentifierResolver &IdResolver = Reader.getIdResolver();
2951*67e74705SXin Li 
2952*67e74705SXin Li     // Temporarily consider the identifier to be up-to-date. We don't want to
2953*67e74705SXin Li     // cause additional lookups here.
2954*67e74705SXin Li     class UpToDateIdentifierRAII {
2955*67e74705SXin Li       IdentifierInfo *II;
2956*67e74705SXin Li       bool WasOutToDate;
2957*67e74705SXin Li 
2958*67e74705SXin Li     public:
2959*67e74705SXin Li       explicit UpToDateIdentifierRAII(IdentifierInfo *II)
2960*67e74705SXin Li         : II(II), WasOutToDate(false)
2961*67e74705SXin Li       {
2962*67e74705SXin Li         if (II) {
2963*67e74705SXin Li           WasOutToDate = II->isOutOfDate();
2964*67e74705SXin Li           if (WasOutToDate)
2965*67e74705SXin Li             II->setOutOfDate(false);
2966*67e74705SXin Li         }
2967*67e74705SXin Li       }
2968*67e74705SXin Li 
2969*67e74705SXin Li       ~UpToDateIdentifierRAII() {
2970*67e74705SXin Li         if (WasOutToDate)
2971*67e74705SXin Li           II->setOutOfDate(true);
2972*67e74705SXin Li       }
2973*67e74705SXin Li     } UpToDate(Name.getAsIdentifierInfo());
2974*67e74705SXin Li 
2975*67e74705SXin Li     for (IdentifierResolver::iterator I = IdResolver.begin(Name),
2976*67e74705SXin Li                                    IEnd = IdResolver.end();
2977*67e74705SXin Li          I != IEnd; ++I) {
2978*67e74705SXin Li       if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
2979*67e74705SXin Li         if (isSameEntity(Existing, D))
2980*67e74705SXin Li           return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
2981*67e74705SXin Li                                     TypedefNameForLinkage);
2982*67e74705SXin Li     }
2983*67e74705SXin Li   } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
2984*67e74705SXin Li     DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
2985*67e74705SXin Li     for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
2986*67e74705SXin Li       if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
2987*67e74705SXin Li         if (isSameEntity(Existing, D))
2988*67e74705SXin Li           return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
2989*67e74705SXin Li                                     TypedefNameForLinkage);
2990*67e74705SXin Li     }
2991*67e74705SXin Li   } else {
2992*67e74705SXin Li     // Not in a mergeable context.
2993*67e74705SXin Li     return FindExistingResult(Reader);
2994*67e74705SXin Li   }
2995*67e74705SXin Li 
2996*67e74705SXin Li   // If this declaration is from a merged context, make a note that we need to
2997*67e74705SXin Li   // check that the canonical definition of that context contains the decl.
2998*67e74705SXin Li   //
2999*67e74705SXin Li   // FIXME: We should do something similar if we merge two definitions of the
3000*67e74705SXin Li   // same template specialization into the same CXXRecordDecl.
3001*67e74705SXin Li   auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3002*67e74705SXin Li   if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3003*67e74705SXin Li       MergedDCIt->second == D->getDeclContext())
3004*67e74705SXin Li     Reader.PendingOdrMergeChecks.push_back(D);
3005*67e74705SXin Li 
3006*67e74705SXin Li   return FindExistingResult(Reader, D, /*Existing=*/nullptr,
3007*67e74705SXin Li                             AnonymousDeclNumber, TypedefNameForLinkage);
3008*67e74705SXin Li }
3009*67e74705SXin Li 
3010*67e74705SXin Li template<typename DeclT>
getMostRecentDeclImpl(Redeclarable<DeclT> * D)3011*67e74705SXin Li Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3012*67e74705SXin Li   return D->RedeclLink.getLatestNotUpdated();
3013*67e74705SXin Li }
getMostRecentDeclImpl(...)3014*67e74705SXin Li Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3015*67e74705SXin Li   llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3016*67e74705SXin Li }
3017*67e74705SXin Li 
getMostRecentDecl(Decl * D)3018*67e74705SXin Li Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3019*67e74705SXin Li   assert(D);
3020*67e74705SXin Li 
3021*67e74705SXin Li   switch (D->getKind()) {
3022*67e74705SXin Li #define ABSTRACT_DECL(TYPE)
3023*67e74705SXin Li #define DECL(TYPE, BASE)                               \
3024*67e74705SXin Li   case Decl::TYPE:                                     \
3025*67e74705SXin Li     return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3026*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
3027*67e74705SXin Li   }
3028*67e74705SXin Li   llvm_unreachable("unknown decl kind");
3029*67e74705SXin Li }
3030*67e74705SXin Li 
getMostRecentExistingDecl(Decl * D)3031*67e74705SXin Li Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3032*67e74705SXin Li   return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3033*67e74705SXin Li }
3034*67e74705SXin Li 
3035*67e74705SXin Li template<typename DeclT>
attachPreviousDeclImpl(ASTReader & Reader,Redeclarable<DeclT> * D,Decl * Previous,Decl * Canon)3036*67e74705SXin Li void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3037*67e74705SXin Li                                            Redeclarable<DeclT> *D,
3038*67e74705SXin Li                                            Decl *Previous, Decl *Canon) {
3039*67e74705SXin Li   D->RedeclLink.setPrevious(cast<DeclT>(Previous));
3040*67e74705SXin Li   D->First = cast<DeclT>(Previous)->First;
3041*67e74705SXin Li }
3042*67e74705SXin Li 
3043*67e74705SXin Li namespace clang {
3044*67e74705SXin Li template<>
attachPreviousDeclImpl(ASTReader & Reader,Redeclarable<FunctionDecl> * D,Decl * Previous,Decl * Canon)3045*67e74705SXin Li void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3046*67e74705SXin Li                                            Redeclarable<FunctionDecl> *D,
3047*67e74705SXin Li                                            Decl *Previous, Decl *Canon) {
3048*67e74705SXin Li   FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3049*67e74705SXin Li   FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3050*67e74705SXin Li 
3051*67e74705SXin Li   FD->RedeclLink.setPrevious(PrevFD);
3052*67e74705SXin Li   FD->First = PrevFD->First;
3053*67e74705SXin Li 
3054*67e74705SXin Li   // If the previous declaration is an inline function declaration, then this
3055*67e74705SXin Li   // declaration is too.
3056*67e74705SXin Li   if (PrevFD->IsInline != FD->IsInline) {
3057*67e74705SXin Li     // FIXME: [dcl.fct.spec]p4:
3058*67e74705SXin Li     //   If a function with external linkage is declared inline in one
3059*67e74705SXin Li     //   translation unit, it shall be declared inline in all translation
3060*67e74705SXin Li     //   units in which it appears.
3061*67e74705SXin Li     //
3062*67e74705SXin Li     // Be careful of this case:
3063*67e74705SXin Li     //
3064*67e74705SXin Li     // module A:
3065*67e74705SXin Li     //   template<typename T> struct X { void f(); };
3066*67e74705SXin Li     //   template<typename T> inline void X<T>::f() {}
3067*67e74705SXin Li     //
3068*67e74705SXin Li     // module B instantiates the declaration of X<int>::f
3069*67e74705SXin Li     // module C instantiates the definition of X<int>::f
3070*67e74705SXin Li     //
3071*67e74705SXin Li     // If module B and C are merged, we do not have a violation of this rule.
3072*67e74705SXin Li     FD->IsInline = true;
3073*67e74705SXin Li   }
3074*67e74705SXin Li 
3075*67e74705SXin Li   // If we need to propagate an exception specification along the redecl
3076*67e74705SXin Li   // chain, make a note of that so that we can do so later.
3077*67e74705SXin Li   auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3078*67e74705SXin Li   auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
3079*67e74705SXin Li   if (FPT && PrevFPT) {
3080*67e74705SXin Li     bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3081*67e74705SXin Li     bool WasUnresolved =
3082*67e74705SXin Li         isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3083*67e74705SXin Li     if (IsUnresolved != WasUnresolved)
3084*67e74705SXin Li       Reader.PendingExceptionSpecUpdates.insert(
3085*67e74705SXin Li           std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
3086*67e74705SXin Li   }
3087*67e74705SXin Li }
3088*67e74705SXin Li } // end namespace clang
3089*67e74705SXin Li 
attachPreviousDeclImpl(ASTReader & Reader,...)3090*67e74705SXin Li void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
3091*67e74705SXin Li   llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3092*67e74705SXin Li }
3093*67e74705SXin Li 
3094*67e74705SXin Li /// Inherit the default template argument from \p From to \p To. Returns
3095*67e74705SXin Li /// \c false if there is no default template for \p From.
3096*67e74705SXin Li template <typename ParmDecl>
inheritDefaultTemplateArgument(ASTContext & Context,ParmDecl * From,Decl * ToD)3097*67e74705SXin Li static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3098*67e74705SXin Li                                            Decl *ToD) {
3099*67e74705SXin Li   auto *To = cast<ParmDecl>(ToD);
3100*67e74705SXin Li   if (!From->hasDefaultArgument())
3101*67e74705SXin Li     return false;
3102*67e74705SXin Li   To->setInheritedDefaultArgument(Context, From);
3103*67e74705SXin Li   return true;
3104*67e74705SXin Li }
3105*67e74705SXin Li 
inheritDefaultTemplateArguments(ASTContext & Context,TemplateDecl * From,TemplateDecl * To)3106*67e74705SXin Li static void inheritDefaultTemplateArguments(ASTContext &Context,
3107*67e74705SXin Li                                             TemplateDecl *From,
3108*67e74705SXin Li                                             TemplateDecl *To) {
3109*67e74705SXin Li   auto *FromTP = From->getTemplateParameters();
3110*67e74705SXin Li   auto *ToTP = To->getTemplateParameters();
3111*67e74705SXin Li   assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3112*67e74705SXin Li 
3113*67e74705SXin Li   for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3114*67e74705SXin Li     NamedDecl *FromParam = FromTP->getParam(N - I - 1);
3115*67e74705SXin Li     if (FromParam->isParameterPack())
3116*67e74705SXin Li       continue;
3117*67e74705SXin Li     NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3118*67e74705SXin Li 
3119*67e74705SXin Li     if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
3120*67e74705SXin Li       if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
3121*67e74705SXin Li         break;
3122*67e74705SXin Li     } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
3123*67e74705SXin Li       if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
3124*67e74705SXin Li         break;
3125*67e74705SXin Li     } else {
3126*67e74705SXin Li       if (!inheritDefaultTemplateArgument(
3127*67e74705SXin Li               Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3128*67e74705SXin Li         break;
3129*67e74705SXin Li     }
3130*67e74705SXin Li   }
3131*67e74705SXin Li }
3132*67e74705SXin Li 
attachPreviousDecl(ASTReader & Reader,Decl * D,Decl * Previous,Decl * Canon)3133*67e74705SXin Li void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
3134*67e74705SXin Li                                        Decl *Previous, Decl *Canon) {
3135*67e74705SXin Li   assert(D && Previous);
3136*67e74705SXin Li 
3137*67e74705SXin Li   switch (D->getKind()) {
3138*67e74705SXin Li #define ABSTRACT_DECL(TYPE)
3139*67e74705SXin Li #define DECL(TYPE, BASE)                                                  \
3140*67e74705SXin Li   case Decl::TYPE:                                                        \
3141*67e74705SXin Li     attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
3142*67e74705SXin Li     break;
3143*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
3144*67e74705SXin Li   }
3145*67e74705SXin Li 
3146*67e74705SXin Li   // If the declaration was visible in one module, a redeclaration of it in
3147*67e74705SXin Li   // another module remains visible even if it wouldn't be visible by itself.
3148*67e74705SXin Li   //
3149*67e74705SXin Li   // FIXME: In this case, the declaration should only be visible if a module
3150*67e74705SXin Li   //        that makes it visible has been imported.
3151*67e74705SXin Li   D->IdentifierNamespace |=
3152*67e74705SXin Li       Previous->IdentifierNamespace &
3153*67e74705SXin Li       (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
3154*67e74705SXin Li 
3155*67e74705SXin Li   // If the declaration declares a template, it may inherit default arguments
3156*67e74705SXin Li   // from the previous declaration.
3157*67e74705SXin Li   if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3158*67e74705SXin Li     inheritDefaultTemplateArguments(Reader.getContext(),
3159*67e74705SXin Li                                     cast<TemplateDecl>(Previous), TD);
3160*67e74705SXin Li }
3161*67e74705SXin Li 
3162*67e74705SXin Li template<typename DeclT>
attachLatestDeclImpl(Redeclarable<DeclT> * D,Decl * Latest)3163*67e74705SXin Li void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
3164*67e74705SXin Li   D->RedeclLink.setLatest(cast<DeclT>(Latest));
3165*67e74705SXin Li }
attachLatestDeclImpl(...)3166*67e74705SXin Li void ASTDeclReader::attachLatestDeclImpl(...) {
3167*67e74705SXin Li   llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3168*67e74705SXin Li }
3169*67e74705SXin Li 
attachLatestDecl(Decl * D,Decl * Latest)3170*67e74705SXin Li void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3171*67e74705SXin Li   assert(D && Latest);
3172*67e74705SXin Li 
3173*67e74705SXin Li   switch (D->getKind()) {
3174*67e74705SXin Li #define ABSTRACT_DECL(TYPE)
3175*67e74705SXin Li #define DECL(TYPE, BASE)                                  \
3176*67e74705SXin Li   case Decl::TYPE:                                        \
3177*67e74705SXin Li     attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3178*67e74705SXin Li     break;
3179*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
3180*67e74705SXin Li   }
3181*67e74705SXin Li }
3182*67e74705SXin Li 
3183*67e74705SXin Li template<typename DeclT>
markIncompleteDeclChainImpl(Redeclarable<DeclT> * D)3184*67e74705SXin Li void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3185*67e74705SXin Li   D->RedeclLink.markIncomplete();
3186*67e74705SXin Li }
markIncompleteDeclChainImpl(...)3187*67e74705SXin Li void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3188*67e74705SXin Li   llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3189*67e74705SXin Li }
3190*67e74705SXin Li 
markIncompleteDeclChain(Decl * D)3191*67e74705SXin Li void ASTReader::markIncompleteDeclChain(Decl *D) {
3192*67e74705SXin Li   switch (D->getKind()) {
3193*67e74705SXin Li #define ABSTRACT_DECL(TYPE)
3194*67e74705SXin Li #define DECL(TYPE, BASE)                                             \
3195*67e74705SXin Li   case Decl::TYPE:                                                   \
3196*67e74705SXin Li     ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3197*67e74705SXin Li     break;
3198*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
3199*67e74705SXin Li   }
3200*67e74705SXin Li }
3201*67e74705SXin Li 
3202*67e74705SXin Li /// \brief Read the declaration at the given offset from the AST file.
ReadDeclRecord(DeclID ID)3203*67e74705SXin Li Decl *ASTReader::ReadDeclRecord(DeclID ID) {
3204*67e74705SXin Li   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
3205*67e74705SXin Li   SourceLocation DeclLoc;
3206*67e74705SXin Li   RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
3207*67e74705SXin Li   llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
3208*67e74705SXin Li   // Keep track of where we are in the stream, then jump back there
3209*67e74705SXin Li   // after reading this declaration.
3210*67e74705SXin Li   SavedStreamPosition SavedPosition(DeclsCursor);
3211*67e74705SXin Li 
3212*67e74705SXin Li   ReadingKindTracker ReadingKind(Read_Decl, *this);
3213*67e74705SXin Li 
3214*67e74705SXin Li   // Note that we are loading a declaration record.
3215*67e74705SXin Li   Deserializing ADecl(this);
3216*67e74705SXin Li 
3217*67e74705SXin Li   DeclsCursor.JumpToBit(Loc.Offset);
3218*67e74705SXin Li   RecordData Record;
3219*67e74705SXin Li   unsigned Code = DeclsCursor.ReadCode();
3220*67e74705SXin Li   unsigned Idx = 0;
3221*67e74705SXin Li   ASTDeclReader Reader(*this, Loc, ID, DeclLoc, Record,Idx);
3222*67e74705SXin Li 
3223*67e74705SXin Li   Decl *D = nullptr;
3224*67e74705SXin Li   switch ((DeclCode)DeclsCursor.readRecord(Code, Record)) {
3225*67e74705SXin Li   case DECL_CONTEXT_LEXICAL:
3226*67e74705SXin Li   case DECL_CONTEXT_VISIBLE:
3227*67e74705SXin Li     llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
3228*67e74705SXin Li   case DECL_TYPEDEF:
3229*67e74705SXin Li     D = TypedefDecl::CreateDeserialized(Context, ID);
3230*67e74705SXin Li     break;
3231*67e74705SXin Li   case DECL_TYPEALIAS:
3232*67e74705SXin Li     D = TypeAliasDecl::CreateDeserialized(Context, ID);
3233*67e74705SXin Li     break;
3234*67e74705SXin Li   case DECL_ENUM:
3235*67e74705SXin Li     D = EnumDecl::CreateDeserialized(Context, ID);
3236*67e74705SXin Li     break;
3237*67e74705SXin Li   case DECL_RECORD:
3238*67e74705SXin Li     D = RecordDecl::CreateDeserialized(Context, ID);
3239*67e74705SXin Li     break;
3240*67e74705SXin Li   case DECL_ENUM_CONSTANT:
3241*67e74705SXin Li     D = EnumConstantDecl::CreateDeserialized(Context, ID);
3242*67e74705SXin Li     break;
3243*67e74705SXin Li   case DECL_FUNCTION:
3244*67e74705SXin Li     D = FunctionDecl::CreateDeserialized(Context, ID);
3245*67e74705SXin Li     break;
3246*67e74705SXin Li   case DECL_LINKAGE_SPEC:
3247*67e74705SXin Li     D = LinkageSpecDecl::CreateDeserialized(Context, ID);
3248*67e74705SXin Li     break;
3249*67e74705SXin Li   case DECL_LABEL:
3250*67e74705SXin Li     D = LabelDecl::CreateDeserialized(Context, ID);
3251*67e74705SXin Li     break;
3252*67e74705SXin Li   case DECL_NAMESPACE:
3253*67e74705SXin Li     D = NamespaceDecl::CreateDeserialized(Context, ID);
3254*67e74705SXin Li     break;
3255*67e74705SXin Li   case DECL_NAMESPACE_ALIAS:
3256*67e74705SXin Li     D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
3257*67e74705SXin Li     break;
3258*67e74705SXin Li   case DECL_USING:
3259*67e74705SXin Li     D = UsingDecl::CreateDeserialized(Context, ID);
3260*67e74705SXin Li     break;
3261*67e74705SXin Li   case DECL_USING_SHADOW:
3262*67e74705SXin Li     D = UsingShadowDecl::CreateDeserialized(Context, ID);
3263*67e74705SXin Li     break;
3264*67e74705SXin Li   case DECL_CONSTRUCTOR_USING_SHADOW:
3265*67e74705SXin Li     D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3266*67e74705SXin Li     break;
3267*67e74705SXin Li   case DECL_USING_DIRECTIVE:
3268*67e74705SXin Li     D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
3269*67e74705SXin Li     break;
3270*67e74705SXin Li   case DECL_UNRESOLVED_USING_VALUE:
3271*67e74705SXin Li     D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
3272*67e74705SXin Li     break;
3273*67e74705SXin Li   case DECL_UNRESOLVED_USING_TYPENAME:
3274*67e74705SXin Li     D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
3275*67e74705SXin Li     break;
3276*67e74705SXin Li   case DECL_CXX_RECORD:
3277*67e74705SXin Li     D = CXXRecordDecl::CreateDeserialized(Context, ID);
3278*67e74705SXin Li     break;
3279*67e74705SXin Li   case DECL_CXX_METHOD:
3280*67e74705SXin Li     D = CXXMethodDecl::CreateDeserialized(Context, ID);
3281*67e74705SXin Li     break;
3282*67e74705SXin Li   case DECL_CXX_CONSTRUCTOR:
3283*67e74705SXin Li     D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3284*67e74705SXin Li     break;
3285*67e74705SXin Li   case DECL_CXX_INHERITED_CONSTRUCTOR:
3286*67e74705SXin Li     D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
3287*67e74705SXin Li     break;
3288*67e74705SXin Li   case DECL_CXX_DESTRUCTOR:
3289*67e74705SXin Li     D = CXXDestructorDecl::CreateDeserialized(Context, ID);
3290*67e74705SXin Li     break;
3291*67e74705SXin Li   case DECL_CXX_CONVERSION:
3292*67e74705SXin Li     D = CXXConversionDecl::CreateDeserialized(Context, ID);
3293*67e74705SXin Li     break;
3294*67e74705SXin Li   case DECL_ACCESS_SPEC:
3295*67e74705SXin Li     D = AccessSpecDecl::CreateDeserialized(Context, ID);
3296*67e74705SXin Li     break;
3297*67e74705SXin Li   case DECL_FRIEND:
3298*67e74705SXin Li     D = FriendDecl::CreateDeserialized(Context, ID, Record[Idx++]);
3299*67e74705SXin Li     break;
3300*67e74705SXin Li   case DECL_FRIEND_TEMPLATE:
3301*67e74705SXin Li     D = FriendTemplateDecl::CreateDeserialized(Context, ID);
3302*67e74705SXin Li     break;
3303*67e74705SXin Li   case DECL_CLASS_TEMPLATE:
3304*67e74705SXin Li     D = ClassTemplateDecl::CreateDeserialized(Context, ID);
3305*67e74705SXin Li     break;
3306*67e74705SXin Li   case DECL_CLASS_TEMPLATE_SPECIALIZATION:
3307*67e74705SXin Li     D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3308*67e74705SXin Li     break;
3309*67e74705SXin Li   case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
3310*67e74705SXin Li     D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3311*67e74705SXin Li     break;
3312*67e74705SXin Li   case DECL_VAR_TEMPLATE:
3313*67e74705SXin Li     D = VarTemplateDecl::CreateDeserialized(Context, ID);
3314*67e74705SXin Li     break;
3315*67e74705SXin Li   case DECL_VAR_TEMPLATE_SPECIALIZATION:
3316*67e74705SXin Li     D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3317*67e74705SXin Li     break;
3318*67e74705SXin Li   case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3319*67e74705SXin Li     D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3320*67e74705SXin Li     break;
3321*67e74705SXin Li   case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
3322*67e74705SXin Li     D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
3323*67e74705SXin Li     break;
3324*67e74705SXin Li   case DECL_FUNCTION_TEMPLATE:
3325*67e74705SXin Li     D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
3326*67e74705SXin Li     break;
3327*67e74705SXin Li   case DECL_TEMPLATE_TYPE_PARM:
3328*67e74705SXin Li     D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
3329*67e74705SXin Li     break;
3330*67e74705SXin Li   case DECL_NON_TYPE_TEMPLATE_PARM:
3331*67e74705SXin Li     D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
3332*67e74705SXin Li     break;
3333*67e74705SXin Li   case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
3334*67e74705SXin Li     D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID, Record[Idx++]);
3335*67e74705SXin Li     break;
3336*67e74705SXin Li   case DECL_TEMPLATE_TEMPLATE_PARM:
3337*67e74705SXin Li     D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
3338*67e74705SXin Li     break;
3339*67e74705SXin Li   case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3340*67e74705SXin Li     D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
3341*67e74705SXin Li                                                      Record[Idx++]);
3342*67e74705SXin Li     break;
3343*67e74705SXin Li   case DECL_TYPE_ALIAS_TEMPLATE:
3344*67e74705SXin Li     D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
3345*67e74705SXin Li     break;
3346*67e74705SXin Li   case DECL_STATIC_ASSERT:
3347*67e74705SXin Li     D = StaticAssertDecl::CreateDeserialized(Context, ID);
3348*67e74705SXin Li     break;
3349*67e74705SXin Li   case DECL_OBJC_METHOD:
3350*67e74705SXin Li     D = ObjCMethodDecl::CreateDeserialized(Context, ID);
3351*67e74705SXin Li     break;
3352*67e74705SXin Li   case DECL_OBJC_INTERFACE:
3353*67e74705SXin Li     D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
3354*67e74705SXin Li     break;
3355*67e74705SXin Li   case DECL_OBJC_IVAR:
3356*67e74705SXin Li     D = ObjCIvarDecl::CreateDeserialized(Context, ID);
3357*67e74705SXin Li     break;
3358*67e74705SXin Li   case DECL_OBJC_PROTOCOL:
3359*67e74705SXin Li     D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
3360*67e74705SXin Li     break;
3361*67e74705SXin Li   case DECL_OBJC_AT_DEFS_FIELD:
3362*67e74705SXin Li     D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
3363*67e74705SXin Li     break;
3364*67e74705SXin Li   case DECL_OBJC_CATEGORY:
3365*67e74705SXin Li     D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
3366*67e74705SXin Li     break;
3367*67e74705SXin Li   case DECL_OBJC_CATEGORY_IMPL:
3368*67e74705SXin Li     D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
3369*67e74705SXin Li     break;
3370*67e74705SXin Li   case DECL_OBJC_IMPLEMENTATION:
3371*67e74705SXin Li     D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
3372*67e74705SXin Li     break;
3373*67e74705SXin Li   case DECL_OBJC_COMPATIBLE_ALIAS:
3374*67e74705SXin Li     D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
3375*67e74705SXin Li     break;
3376*67e74705SXin Li   case DECL_OBJC_PROPERTY:
3377*67e74705SXin Li     D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
3378*67e74705SXin Li     break;
3379*67e74705SXin Li   case DECL_OBJC_PROPERTY_IMPL:
3380*67e74705SXin Li     D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
3381*67e74705SXin Li     break;
3382*67e74705SXin Li   case DECL_FIELD:
3383*67e74705SXin Li     D = FieldDecl::CreateDeserialized(Context, ID);
3384*67e74705SXin Li     break;
3385*67e74705SXin Li   case DECL_INDIRECTFIELD:
3386*67e74705SXin Li     D = IndirectFieldDecl::CreateDeserialized(Context, ID);
3387*67e74705SXin Li     break;
3388*67e74705SXin Li   case DECL_VAR:
3389*67e74705SXin Li     D = VarDecl::CreateDeserialized(Context, ID);
3390*67e74705SXin Li     break;
3391*67e74705SXin Li   case DECL_IMPLICIT_PARAM:
3392*67e74705SXin Li     D = ImplicitParamDecl::CreateDeserialized(Context, ID);
3393*67e74705SXin Li     break;
3394*67e74705SXin Li   case DECL_PARM_VAR:
3395*67e74705SXin Li     D = ParmVarDecl::CreateDeserialized(Context, ID);
3396*67e74705SXin Li     break;
3397*67e74705SXin Li   case DECL_FILE_SCOPE_ASM:
3398*67e74705SXin Li     D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
3399*67e74705SXin Li     break;
3400*67e74705SXin Li   case DECL_BLOCK:
3401*67e74705SXin Li     D = BlockDecl::CreateDeserialized(Context, ID);
3402*67e74705SXin Li     break;
3403*67e74705SXin Li   case DECL_MS_PROPERTY:
3404*67e74705SXin Li     D = MSPropertyDecl::CreateDeserialized(Context, ID);
3405*67e74705SXin Li     break;
3406*67e74705SXin Li   case DECL_CAPTURED:
3407*67e74705SXin Li     D = CapturedDecl::CreateDeserialized(Context, ID, Record[Idx++]);
3408*67e74705SXin Li     break;
3409*67e74705SXin Li   case DECL_CXX_BASE_SPECIFIERS:
3410*67e74705SXin Li     Error("attempt to read a C++ base-specifier record as a declaration");
3411*67e74705SXin Li     return nullptr;
3412*67e74705SXin Li   case DECL_CXX_CTOR_INITIALIZERS:
3413*67e74705SXin Li     Error("attempt to read a C++ ctor initializer record as a declaration");
3414*67e74705SXin Li     return nullptr;
3415*67e74705SXin Li   case DECL_IMPORT:
3416*67e74705SXin Li     // Note: last entry of the ImportDecl record is the number of stored source
3417*67e74705SXin Li     // locations.
3418*67e74705SXin Li     D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
3419*67e74705SXin Li     break;
3420*67e74705SXin Li   case DECL_OMP_THREADPRIVATE:
3421*67e74705SXin Li     D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record[Idx++]);
3422*67e74705SXin Li     break;
3423*67e74705SXin Li   case DECL_OMP_DECLARE_REDUCTION:
3424*67e74705SXin Li     D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3425*67e74705SXin Li     break;
3426*67e74705SXin Li   case DECL_OMP_CAPTUREDEXPR:
3427*67e74705SXin Li     D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
3428*67e74705SXin Li     break;
3429*67e74705SXin Li   case DECL_PRAGMA_COMMENT:
3430*67e74705SXin Li     D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record[Idx++]);
3431*67e74705SXin Li     break;
3432*67e74705SXin Li   case DECL_PRAGMA_DETECT_MISMATCH:
3433*67e74705SXin Li     D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
3434*67e74705SXin Li                                                      Record[Idx++]);
3435*67e74705SXin Li     break;
3436*67e74705SXin Li   case DECL_EMPTY:
3437*67e74705SXin Li     D = EmptyDecl::CreateDeserialized(Context, ID);
3438*67e74705SXin Li     break;
3439*67e74705SXin Li   case DECL_OBJC_TYPE_PARAM:
3440*67e74705SXin Li     D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3441*67e74705SXin Li     break;
3442*67e74705SXin Li   }
3443*67e74705SXin Li 
3444*67e74705SXin Li   assert(D && "Unknown declaration reading AST file");
3445*67e74705SXin Li   LoadedDecl(Index, D);
3446*67e74705SXin Li   // Set the DeclContext before doing any deserialization, to make sure internal
3447*67e74705SXin Li   // calls to Decl::getASTContext() by Decl's methods will find the
3448*67e74705SXin Li   // TranslationUnitDecl without crashing.
3449*67e74705SXin Li   D->setDeclContext(Context.getTranslationUnitDecl());
3450*67e74705SXin Li   Reader.Visit(D);
3451*67e74705SXin Li 
3452*67e74705SXin Li   // If this declaration is also a declaration context, get the
3453*67e74705SXin Li   // offsets for its tables of lexical and visible declarations.
3454*67e74705SXin Li   if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3455*67e74705SXin Li     std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
3456*67e74705SXin Li     if (Offsets.first &&
3457*67e74705SXin Li         ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3458*67e74705SXin Li       return nullptr;
3459*67e74705SXin Li     if (Offsets.second &&
3460*67e74705SXin Li         ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3461*67e74705SXin Li       return nullptr;
3462*67e74705SXin Li   }
3463*67e74705SXin Li   assert(Idx == Record.size());
3464*67e74705SXin Li 
3465*67e74705SXin Li   // Load any relevant update records.
3466*67e74705SXin Li   PendingUpdateRecords.push_back(std::make_pair(ID, D));
3467*67e74705SXin Li 
3468*67e74705SXin Li   // Load the categories after recursive loading is finished.
3469*67e74705SXin Li   if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
3470*67e74705SXin Li     if (Class->isThisDeclarationADefinition())
3471*67e74705SXin Li       loadObjCCategories(ID, Class);
3472*67e74705SXin Li 
3473*67e74705SXin Li   // If we have deserialized a declaration that has a definition the
3474*67e74705SXin Li   // AST consumer might need to know about, queue it.
3475*67e74705SXin Li   // We don't pass it to the consumer immediately because we may be in recursive
3476*67e74705SXin Li   // loading, and some declarations may still be initializing.
3477*67e74705SXin Li   if (isConsumerInterestedIn(D, Reader.hasPendingBody()))
3478*67e74705SXin Li     InterestingDecls.push_back(D);
3479*67e74705SXin Li 
3480*67e74705SXin Li   return D;
3481*67e74705SXin Li }
3482*67e74705SXin Li 
loadDeclUpdateRecords(serialization::DeclID ID,Decl * D)3483*67e74705SXin Li void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) {
3484*67e74705SXin Li   // The declaration may have been modified by files later in the chain.
3485*67e74705SXin Li   // If this is the case, read the record containing the updates from each file
3486*67e74705SXin Li   // and pass it to ASTDeclReader to make the modifications.
3487*67e74705SXin Li   DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
3488*67e74705SXin Li   if (UpdI != DeclUpdateOffsets.end()) {
3489*67e74705SXin Li     auto UpdateOffsets = std::move(UpdI->second);
3490*67e74705SXin Li     DeclUpdateOffsets.erase(UpdI);
3491*67e74705SXin Li 
3492*67e74705SXin Li     bool WasInteresting = isConsumerInterestedIn(D, false);
3493*67e74705SXin Li     for (auto &FileAndOffset : UpdateOffsets) {
3494*67e74705SXin Li       ModuleFile *F = FileAndOffset.first;
3495*67e74705SXin Li       uint64_t Offset = FileAndOffset.second;
3496*67e74705SXin Li       llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3497*67e74705SXin Li       SavedStreamPosition SavedPosition(Cursor);
3498*67e74705SXin Li       Cursor.JumpToBit(Offset);
3499*67e74705SXin Li       RecordData Record;
3500*67e74705SXin Li       unsigned Code = Cursor.ReadCode();
3501*67e74705SXin Li       unsigned RecCode = Cursor.readRecord(Code, Record);
3502*67e74705SXin Li       (void)RecCode;
3503*67e74705SXin Li       assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
3504*67e74705SXin Li 
3505*67e74705SXin Li       unsigned Idx = 0;
3506*67e74705SXin Li       ASTDeclReader Reader(*this, RecordLocation(F, Offset), ID,
3507*67e74705SXin Li                            SourceLocation(), Record, Idx);
3508*67e74705SXin Li       Reader.UpdateDecl(D, *F, Record);
3509*67e74705SXin Li 
3510*67e74705SXin Li       // We might have made this declaration interesting. If so, remember that
3511*67e74705SXin Li       // we need to hand it off to the consumer.
3512*67e74705SXin Li       if (!WasInteresting &&
3513*67e74705SXin Li           isConsumerInterestedIn(D, Reader.hasPendingBody())) {
3514*67e74705SXin Li         InterestingDecls.push_back(D);
3515*67e74705SXin Li         WasInteresting = true;
3516*67e74705SXin Li       }
3517*67e74705SXin Li     }
3518*67e74705SXin Li   }
3519*67e74705SXin Li 
3520*67e74705SXin Li   // Load the pending visible updates for this decl context, if it has any.
3521*67e74705SXin Li   auto I = PendingVisibleUpdates.find(ID);
3522*67e74705SXin Li   if (I != PendingVisibleUpdates.end()) {
3523*67e74705SXin Li     auto VisibleUpdates = std::move(I->second);
3524*67e74705SXin Li     PendingVisibleUpdates.erase(I);
3525*67e74705SXin Li 
3526*67e74705SXin Li     auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3527*67e74705SXin Li     for (const PendingVisibleUpdate &Update : VisibleUpdates)
3528*67e74705SXin Li       Lookups[DC].Table.add(
3529*67e74705SXin Li           Update.Mod, Update.Data,
3530*67e74705SXin Li           reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3531*67e74705SXin Li     DC->setHasExternalVisibleStorage(true);
3532*67e74705SXin Li   }
3533*67e74705SXin Li }
3534*67e74705SXin Li 
loadPendingDeclChain(Decl * FirstLocal,uint64_t LocalOffset)3535*67e74705SXin Li void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3536*67e74705SXin Li   // Attach FirstLocal to the end of the decl chain.
3537*67e74705SXin Li   Decl *CanonDecl = FirstLocal->getCanonicalDecl();
3538*67e74705SXin Li   if (FirstLocal != CanonDecl) {
3539*67e74705SXin Li     Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3540*67e74705SXin Li     ASTDeclReader::attachPreviousDecl(
3541*67e74705SXin Li         *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3542*67e74705SXin Li         CanonDecl);
3543*67e74705SXin Li   }
3544*67e74705SXin Li 
3545*67e74705SXin Li   if (!LocalOffset) {
3546*67e74705SXin Li     ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3547*67e74705SXin Li     return;
3548*67e74705SXin Li   }
3549*67e74705SXin Li 
3550*67e74705SXin Li   // Load the list of other redeclarations from this module file.
3551*67e74705SXin Li   ModuleFile *M = getOwningModuleFile(FirstLocal);
3552*67e74705SXin Li   assert(M && "imported decl from no module file");
3553*67e74705SXin Li 
3554*67e74705SXin Li   llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3555*67e74705SXin Li   SavedStreamPosition SavedPosition(Cursor);
3556*67e74705SXin Li   Cursor.JumpToBit(LocalOffset);
3557*67e74705SXin Li 
3558*67e74705SXin Li   RecordData Record;
3559*67e74705SXin Li   unsigned Code = Cursor.ReadCode();
3560*67e74705SXin Li   unsigned RecCode = Cursor.readRecord(Code, Record);
3561*67e74705SXin Li   (void)RecCode;
3562*67e74705SXin Li   assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3563*67e74705SXin Li 
3564*67e74705SXin Li   // FIXME: We have several different dispatches on decl kind here; maybe
3565*67e74705SXin Li   // we should instead generate one loop per kind and dispatch up-front?
3566*67e74705SXin Li   Decl *MostRecent = FirstLocal;
3567*67e74705SXin Li   for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3568*67e74705SXin Li     auto *D = GetLocalDecl(*M, Record[N - I - 1]);
3569*67e74705SXin Li     ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3570*67e74705SXin Li     MostRecent = D;
3571*67e74705SXin Li   }
3572*67e74705SXin Li   ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
3573*67e74705SXin Li }
3574*67e74705SXin Li 
3575*67e74705SXin Li namespace {
3576*67e74705SXin Li   /// \brief Given an ObjC interface, goes through the modules and links to the
3577*67e74705SXin Li   /// interface all the categories for it.
3578*67e74705SXin Li   class ObjCCategoriesVisitor {
3579*67e74705SXin Li     ASTReader &Reader;
3580*67e74705SXin Li     serialization::GlobalDeclID InterfaceID;
3581*67e74705SXin Li     ObjCInterfaceDecl *Interface;
3582*67e74705SXin Li     llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
3583*67e74705SXin Li     unsigned PreviousGeneration;
3584*67e74705SXin Li     ObjCCategoryDecl *Tail;
3585*67e74705SXin Li     llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
3586*67e74705SXin Li 
add(ObjCCategoryDecl * Cat)3587*67e74705SXin Li     void add(ObjCCategoryDecl *Cat) {
3588*67e74705SXin Li       // Only process each category once.
3589*67e74705SXin Li       if (!Deserialized.erase(Cat))
3590*67e74705SXin Li         return;
3591*67e74705SXin Li 
3592*67e74705SXin Li       // Check for duplicate categories.
3593*67e74705SXin Li       if (Cat->getDeclName()) {
3594*67e74705SXin Li         ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3595*67e74705SXin Li         if (Existing &&
3596*67e74705SXin Li             Reader.getOwningModuleFile(Existing)
3597*67e74705SXin Li                                           != Reader.getOwningModuleFile(Cat)) {
3598*67e74705SXin Li           // FIXME: We should not warn for duplicates in diamond:
3599*67e74705SXin Li           //
3600*67e74705SXin Li           //   MT     //
3601*67e74705SXin Li           //  /  \    //
3602*67e74705SXin Li           // ML  MR   //
3603*67e74705SXin Li           //  \  /    //
3604*67e74705SXin Li           //   MB     //
3605*67e74705SXin Li           //
3606*67e74705SXin Li           // If there are duplicates in ML/MR, there will be warning when
3607*67e74705SXin Li           // creating MB *and* when importing MB. We should not warn when
3608*67e74705SXin Li           // importing.
3609*67e74705SXin Li           Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3610*67e74705SXin Li             << Interface->getDeclName() << Cat->getDeclName();
3611*67e74705SXin Li           Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3612*67e74705SXin Li         } else if (!Existing) {
3613*67e74705SXin Li           // Record this category.
3614*67e74705SXin Li           Existing = Cat;
3615*67e74705SXin Li         }
3616*67e74705SXin Li       }
3617*67e74705SXin Li 
3618*67e74705SXin Li       // Add this category to the end of the chain.
3619*67e74705SXin Li       if (Tail)
3620*67e74705SXin Li         ASTDeclReader::setNextObjCCategory(Tail, Cat);
3621*67e74705SXin Li       else
3622*67e74705SXin Li         Interface->setCategoryListRaw(Cat);
3623*67e74705SXin Li       Tail = Cat;
3624*67e74705SXin Li     }
3625*67e74705SXin Li 
3626*67e74705SXin Li   public:
ObjCCategoriesVisitor(ASTReader & Reader,serialization::GlobalDeclID InterfaceID,ObjCInterfaceDecl * Interface,llvm::SmallPtrSetImpl<ObjCCategoryDecl * > & Deserialized,unsigned PreviousGeneration)3627*67e74705SXin Li     ObjCCategoriesVisitor(ASTReader &Reader,
3628*67e74705SXin Li                           serialization::GlobalDeclID InterfaceID,
3629*67e74705SXin Li                           ObjCInterfaceDecl *Interface,
3630*67e74705SXin Li                         llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3631*67e74705SXin Li                           unsigned PreviousGeneration)
3632*67e74705SXin Li       : Reader(Reader), InterfaceID(InterfaceID), Interface(Interface),
3633*67e74705SXin Li         Deserialized(Deserialized), PreviousGeneration(PreviousGeneration),
3634*67e74705SXin Li         Tail(nullptr)
3635*67e74705SXin Li     {
3636*67e74705SXin Li       // Populate the name -> category map with the set of known categories.
3637*67e74705SXin Li       for (auto *Cat : Interface->known_categories()) {
3638*67e74705SXin Li         if (Cat->getDeclName())
3639*67e74705SXin Li           NameCategoryMap[Cat->getDeclName()] = Cat;
3640*67e74705SXin Li 
3641*67e74705SXin Li         // Keep track of the tail of the category list.
3642*67e74705SXin Li         Tail = Cat;
3643*67e74705SXin Li       }
3644*67e74705SXin Li     }
3645*67e74705SXin Li 
operator ()(ModuleFile & M)3646*67e74705SXin Li     bool operator()(ModuleFile &M) {
3647*67e74705SXin Li       // If we've loaded all of the category information we care about from
3648*67e74705SXin Li       // this module file, we're done.
3649*67e74705SXin Li       if (M.Generation <= PreviousGeneration)
3650*67e74705SXin Li         return true;
3651*67e74705SXin Li 
3652*67e74705SXin Li       // Map global ID of the definition down to the local ID used in this
3653*67e74705SXin Li       // module file. If there is no such mapping, we'll find nothing here
3654*67e74705SXin Li       // (or in any module it imports).
3655*67e74705SXin Li       DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3656*67e74705SXin Li       if (!LocalID)
3657*67e74705SXin Li         return true;
3658*67e74705SXin Li 
3659*67e74705SXin Li       // Perform a binary search to find the local redeclarations for this
3660*67e74705SXin Li       // declaration (if any).
3661*67e74705SXin Li       const ObjCCategoriesInfo Compare = { LocalID, 0 };
3662*67e74705SXin Li       const ObjCCategoriesInfo *Result
3663*67e74705SXin Li         = std::lower_bound(M.ObjCCategoriesMap,
3664*67e74705SXin Li                            M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
3665*67e74705SXin Li                            Compare);
3666*67e74705SXin Li       if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3667*67e74705SXin Li           Result->DefinitionID != LocalID) {
3668*67e74705SXin Li         // We didn't find anything. If the class definition is in this module
3669*67e74705SXin Li         // file, then the module files it depends on cannot have any categories,
3670*67e74705SXin Li         // so suppress further lookup.
3671*67e74705SXin Li         return Reader.isDeclIDFromModule(InterfaceID, M);
3672*67e74705SXin Li       }
3673*67e74705SXin Li 
3674*67e74705SXin Li       // We found something. Dig out all of the categories.
3675*67e74705SXin Li       unsigned Offset = Result->Offset;
3676*67e74705SXin Li       unsigned N = M.ObjCCategories[Offset];
3677*67e74705SXin Li       M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3678*67e74705SXin Li       for (unsigned I = 0; I != N; ++I)
3679*67e74705SXin Li         add(cast_or_null<ObjCCategoryDecl>(
3680*67e74705SXin Li               Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3681*67e74705SXin Li       return true;
3682*67e74705SXin Li     }
3683*67e74705SXin Li   };
3684*67e74705SXin Li } // end anonymous namespace
3685*67e74705SXin Li 
loadObjCCategories(serialization::GlobalDeclID ID,ObjCInterfaceDecl * D,unsigned PreviousGeneration)3686*67e74705SXin Li void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3687*67e74705SXin Li                                    ObjCInterfaceDecl *D,
3688*67e74705SXin Li                                    unsigned PreviousGeneration) {
3689*67e74705SXin Li   ObjCCategoriesVisitor Visitor(*this, ID, D, CategoriesDeserialized,
3690*67e74705SXin Li                                 PreviousGeneration);
3691*67e74705SXin Li   ModuleMgr.visit(Visitor);
3692*67e74705SXin Li }
3693*67e74705SXin Li 
3694*67e74705SXin Li template<typename DeclT, typename Fn>
forAllLaterRedecls(DeclT * D,Fn F)3695*67e74705SXin Li static void forAllLaterRedecls(DeclT *D, Fn F) {
3696*67e74705SXin Li   F(D);
3697*67e74705SXin Li 
3698*67e74705SXin Li   // Check whether we've already merged D into its redeclaration chain.
3699*67e74705SXin Li   // MostRecent may or may not be nullptr if D has not been merged. If
3700*67e74705SXin Li   // not, walk the merged redecl chain and see if it's there.
3701*67e74705SXin Li   auto *MostRecent = D->getMostRecentDecl();
3702*67e74705SXin Li   bool Found = false;
3703*67e74705SXin Li   for (auto *Redecl = MostRecent; Redecl && !Found;
3704*67e74705SXin Li        Redecl = Redecl->getPreviousDecl())
3705*67e74705SXin Li     Found = (Redecl == D);
3706*67e74705SXin Li 
3707*67e74705SXin Li   // If this declaration is merged, apply the functor to all later decls.
3708*67e74705SXin Li   if (Found) {
3709*67e74705SXin Li     for (auto *Redecl = MostRecent; Redecl != D;
3710*67e74705SXin Li          Redecl = Redecl->getPreviousDecl())
3711*67e74705SXin Li       F(Redecl);
3712*67e74705SXin Li   }
3713*67e74705SXin Li }
3714*67e74705SXin Li 
UpdateDecl(Decl * D,ModuleFile & ModuleFile,const RecordData & Record)3715*67e74705SXin Li void ASTDeclReader::UpdateDecl(Decl *D, ModuleFile &ModuleFile,
3716*67e74705SXin Li                                const RecordData &Record) {
3717*67e74705SXin Li   while (Idx < Record.size()) {
3718*67e74705SXin Li     switch ((DeclUpdateKind)Record[Idx++]) {
3719*67e74705SXin Li     case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
3720*67e74705SXin Li       auto *RD = cast<CXXRecordDecl>(D);
3721*67e74705SXin Li       // FIXME: If we also have an update record for instantiating the
3722*67e74705SXin Li       // definition of D, we need that to happen before we get here.
3723*67e74705SXin Li       Decl *MD = Reader.ReadDecl(ModuleFile, Record, Idx);
3724*67e74705SXin Li       assert(MD && "couldn't read decl from update record");
3725*67e74705SXin Li       // FIXME: We should call addHiddenDecl instead, to add the member
3726*67e74705SXin Li       // to its DeclContext.
3727*67e74705SXin Li       RD->addedMember(MD);
3728*67e74705SXin Li       break;
3729*67e74705SXin Li     }
3730*67e74705SXin Li 
3731*67e74705SXin Li     case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3732*67e74705SXin Li       // It will be added to the template's specializations set when loaded.
3733*67e74705SXin Li       (void)Reader.ReadDecl(ModuleFile, Record, Idx);
3734*67e74705SXin Li       break;
3735*67e74705SXin Li 
3736*67e74705SXin Li     case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
3737*67e74705SXin Li       NamespaceDecl *Anon
3738*67e74705SXin Li         = Reader.ReadDeclAs<NamespaceDecl>(ModuleFile, Record, Idx);
3739*67e74705SXin Li 
3740*67e74705SXin Li       // Each module has its own anonymous namespace, which is disjoint from
3741*67e74705SXin Li       // any other module's anonymous namespaces, so don't attach the anonymous
3742*67e74705SXin Li       // namespace at all.
3743*67e74705SXin Li       if (ModuleFile.Kind != MK_ImplicitModule &&
3744*67e74705SXin Li           ModuleFile.Kind != MK_ExplicitModule) {
3745*67e74705SXin Li         if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3746*67e74705SXin Li           TU->setAnonymousNamespace(Anon);
3747*67e74705SXin Li         else
3748*67e74705SXin Li           cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3749*67e74705SXin Li       }
3750*67e74705SXin Li       break;
3751*67e74705SXin Li     }
3752*67e74705SXin Li 
3753*67e74705SXin Li     case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3754*67e74705SXin Li       cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
3755*67e74705SXin Li           Reader.ReadSourceLocation(ModuleFile, Record, Idx));
3756*67e74705SXin Li       break;
3757*67e74705SXin Li 
3758*67e74705SXin Li     case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3759*67e74705SXin Li       auto Param = cast<ParmVarDecl>(D);
3760*67e74705SXin Li 
3761*67e74705SXin Li       // We have to read the default argument regardless of whether we use it
3762*67e74705SXin Li       // so that hypothetical further update records aren't messed up.
3763*67e74705SXin Li       // TODO: Add a function to skip over the next expr record.
3764*67e74705SXin Li       auto DefaultArg = Reader.ReadExpr(F);
3765*67e74705SXin Li 
3766*67e74705SXin Li       // Only apply the update if the parameter still has an uninstantiated
3767*67e74705SXin Li       // default argument.
3768*67e74705SXin Li       if (Param->hasUninstantiatedDefaultArg())
3769*67e74705SXin Li         Param->setDefaultArg(DefaultArg);
3770*67e74705SXin Li       break;
3771*67e74705SXin Li     }
3772*67e74705SXin Li 
3773*67e74705SXin Li     case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
3774*67e74705SXin Li       FunctionDecl *FD = cast<FunctionDecl>(D);
3775*67e74705SXin Li       if (Reader.PendingBodies[FD]) {
3776*67e74705SXin Li         // FIXME: Maybe check for ODR violations.
3777*67e74705SXin Li         // It's safe to stop now because this update record is always last.
3778*67e74705SXin Li         return;
3779*67e74705SXin Li       }
3780*67e74705SXin Li 
3781*67e74705SXin Li       if (Record[Idx++]) {
3782*67e74705SXin Li         // Maintain AST consistency: any later redeclarations of this function
3783*67e74705SXin Li         // are inline if this one is. (We might have merged another declaration
3784*67e74705SXin Li         // into this one.)
3785*67e74705SXin Li         forAllLaterRedecls(FD, [](FunctionDecl *FD) {
3786*67e74705SXin Li           FD->setImplicitlyInline();
3787*67e74705SXin Li         });
3788*67e74705SXin Li       }
3789*67e74705SXin Li       FD->setInnerLocStart(Reader.ReadSourceLocation(ModuleFile, Record, Idx));
3790*67e74705SXin Li       if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
3791*67e74705SXin Li         CD->NumCtorInitializers = Record[Idx++];
3792*67e74705SXin Li         if (CD->NumCtorInitializers)
3793*67e74705SXin Li           CD->CtorInitializers = ReadGlobalOffset(F, Record, Idx);
3794*67e74705SXin Li       }
3795*67e74705SXin Li       // Store the offset of the body so we can lazily load it later.
3796*67e74705SXin Li       Reader.PendingBodies[FD] = GetCurrentCursorOffset();
3797*67e74705SXin Li       HasPendingBody = true;
3798*67e74705SXin Li       assert(Idx == Record.size() && "lazy body must be last");
3799*67e74705SXin Li       break;
3800*67e74705SXin Li     }
3801*67e74705SXin Li 
3802*67e74705SXin Li     case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
3803*67e74705SXin Li       auto *RD = cast<CXXRecordDecl>(D);
3804*67e74705SXin Li       auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
3805*67e74705SXin Li       bool HadRealDefinition =
3806*67e74705SXin Li           OldDD && (OldDD->Definition != RD ||
3807*67e74705SXin Li                     !Reader.PendingFakeDefinitionData.count(OldDD));
3808*67e74705SXin Li       ReadCXXRecordDefinition(RD, /*Update*/true);
3809*67e74705SXin Li 
3810*67e74705SXin Li       // Visible update is handled separately.
3811*67e74705SXin Li       uint64_t LexicalOffset = ReadLocalOffset(Record, Idx);
3812*67e74705SXin Li       if (!HadRealDefinition && LexicalOffset) {
3813*67e74705SXin Li         Reader.ReadLexicalDeclContextStorage(ModuleFile, ModuleFile.DeclsCursor,
3814*67e74705SXin Li                                              LexicalOffset, RD);
3815*67e74705SXin Li         Reader.PendingFakeDefinitionData.erase(OldDD);
3816*67e74705SXin Li       }
3817*67e74705SXin Li 
3818*67e74705SXin Li       auto TSK = (TemplateSpecializationKind)Record[Idx++];
3819*67e74705SXin Li       SourceLocation POI = Reader.ReadSourceLocation(ModuleFile, Record, Idx);
3820*67e74705SXin Li       if (MemberSpecializationInfo *MSInfo =
3821*67e74705SXin Li               RD->getMemberSpecializationInfo()) {
3822*67e74705SXin Li         MSInfo->setTemplateSpecializationKind(TSK);
3823*67e74705SXin Li         MSInfo->setPointOfInstantiation(POI);
3824*67e74705SXin Li       } else {
3825*67e74705SXin Li         ClassTemplateSpecializationDecl *Spec =
3826*67e74705SXin Li             cast<ClassTemplateSpecializationDecl>(RD);
3827*67e74705SXin Li         Spec->setTemplateSpecializationKind(TSK);
3828*67e74705SXin Li         Spec->setPointOfInstantiation(POI);
3829*67e74705SXin Li 
3830*67e74705SXin Li         if (Record[Idx++]) {
3831*67e74705SXin Li           auto PartialSpec =
3832*67e74705SXin Li               ReadDeclAs<ClassTemplatePartialSpecializationDecl>(Record, Idx);
3833*67e74705SXin Li           SmallVector<TemplateArgument, 8> TemplArgs;
3834*67e74705SXin Li           Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
3835*67e74705SXin Li           auto *TemplArgList = TemplateArgumentList::CreateCopy(
3836*67e74705SXin Li               Reader.getContext(), TemplArgs);
3837*67e74705SXin Li 
3838*67e74705SXin Li           // FIXME: If we already have a partial specialization set,
3839*67e74705SXin Li           // check that it matches.
3840*67e74705SXin Li           if (!Spec->getSpecializedTemplateOrPartial()
3841*67e74705SXin Li                    .is<ClassTemplatePartialSpecializationDecl *>())
3842*67e74705SXin Li             Spec->setInstantiationOf(PartialSpec, TemplArgList);
3843*67e74705SXin Li         }
3844*67e74705SXin Li       }
3845*67e74705SXin Li 
3846*67e74705SXin Li       RD->setTagKind((TagTypeKind)Record[Idx++]);
3847*67e74705SXin Li       RD->setLocation(Reader.ReadSourceLocation(ModuleFile, Record, Idx));
3848*67e74705SXin Li       RD->setLocStart(Reader.ReadSourceLocation(ModuleFile, Record, Idx));
3849*67e74705SXin Li       RD->setRBraceLoc(Reader.ReadSourceLocation(ModuleFile, Record, Idx));
3850*67e74705SXin Li 
3851*67e74705SXin Li       if (Record[Idx++]) {
3852*67e74705SXin Li         AttrVec Attrs;
3853*67e74705SXin Li         Reader.ReadAttributes(F, Attrs, Record, Idx);
3854*67e74705SXin Li         D->setAttrsImpl(Attrs, Reader.getContext());
3855*67e74705SXin Li       }
3856*67e74705SXin Li       break;
3857*67e74705SXin Li     }
3858*67e74705SXin Li 
3859*67e74705SXin Li     case UPD_CXX_RESOLVED_DTOR_DELETE: {
3860*67e74705SXin Li       // Set the 'operator delete' directly to avoid emitting another update
3861*67e74705SXin Li       // record.
3862*67e74705SXin Li       auto *Del = Reader.ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
3863*67e74705SXin Li       auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
3864*67e74705SXin Li       // FIXME: Check consistency if we have an old and new operator delete.
3865*67e74705SXin Li       if (!First->OperatorDelete)
3866*67e74705SXin Li         First->OperatorDelete = Del;
3867*67e74705SXin Li       break;
3868*67e74705SXin Li     }
3869*67e74705SXin Li 
3870*67e74705SXin Li     case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
3871*67e74705SXin Li       FunctionProtoType::ExceptionSpecInfo ESI;
3872*67e74705SXin Li       SmallVector<QualType, 8> ExceptionStorage;
3873*67e74705SXin Li       Reader.readExceptionSpec(ModuleFile, ExceptionStorage, ESI, Record, Idx);
3874*67e74705SXin Li 
3875*67e74705SXin Li       // Update this declaration's exception specification, if needed.
3876*67e74705SXin Li       auto *FD = cast<FunctionDecl>(D);
3877*67e74705SXin Li       auto *FPT = FD->getType()->castAs<FunctionProtoType>();
3878*67e74705SXin Li       // FIXME: If the exception specification is already present, check that it
3879*67e74705SXin Li       // matches.
3880*67e74705SXin Li       if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
3881*67e74705SXin Li         FD->setType(Reader.Context.getFunctionType(
3882*67e74705SXin Li             FPT->getReturnType(), FPT->getParamTypes(),
3883*67e74705SXin Li             FPT->getExtProtoInfo().withExceptionSpec(ESI)));
3884*67e74705SXin Li 
3885*67e74705SXin Li         // When we get to the end of deserializing, see if there are other decls
3886*67e74705SXin Li         // that we need to propagate this exception specification onto.
3887*67e74705SXin Li         Reader.PendingExceptionSpecUpdates.insert(
3888*67e74705SXin Li             std::make_pair(FD->getCanonicalDecl(), FD));
3889*67e74705SXin Li       }
3890*67e74705SXin Li       break;
3891*67e74705SXin Li     }
3892*67e74705SXin Li 
3893*67e74705SXin Li     case UPD_CXX_DEDUCED_RETURN_TYPE: {
3894*67e74705SXin Li       // FIXME: Also do this when merging redecls.
3895*67e74705SXin Li       QualType DeducedResultType = Reader.readType(ModuleFile, Record, Idx);
3896*67e74705SXin Li       for (auto *Redecl : merged_redecls(D)) {
3897*67e74705SXin Li         // FIXME: If the return type is already deduced, check that it matches.
3898*67e74705SXin Li         FunctionDecl *FD = cast<FunctionDecl>(Redecl);
3899*67e74705SXin Li         Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType);
3900*67e74705SXin Li       }
3901*67e74705SXin Li       break;
3902*67e74705SXin Li     }
3903*67e74705SXin Li 
3904*67e74705SXin Li     case UPD_DECL_MARKED_USED: {
3905*67e74705SXin Li       // FIXME: This doesn't send the right notifications if there are
3906*67e74705SXin Li       // ASTMutationListeners other than an ASTWriter.
3907*67e74705SXin Li 
3908*67e74705SXin Li       // Maintain AST consistency: any later redeclarations are used too.
3909*67e74705SXin Li       D->setIsUsed();
3910*67e74705SXin Li       break;
3911*67e74705SXin Li     }
3912*67e74705SXin Li 
3913*67e74705SXin Li     case UPD_MANGLING_NUMBER:
3914*67e74705SXin Li       Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record[Idx++]);
3915*67e74705SXin Li       break;
3916*67e74705SXin Li 
3917*67e74705SXin Li     case UPD_STATIC_LOCAL_NUMBER:
3918*67e74705SXin Li       Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record[Idx++]);
3919*67e74705SXin Li       break;
3920*67e74705SXin Li 
3921*67e74705SXin Li     case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
3922*67e74705SXin Li       D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
3923*67e74705SXin Li           Reader.Context, ReadSourceRange(Record, Idx)));
3924*67e74705SXin Li       break;
3925*67e74705SXin Li 
3926*67e74705SXin Li     case UPD_DECL_EXPORTED: {
3927*67e74705SXin Li       unsigned SubmoduleID = readSubmoduleID(Record, Idx);
3928*67e74705SXin Li       auto *Exported = cast<NamedDecl>(D);
3929*67e74705SXin Li       if (auto *TD = dyn_cast<TagDecl>(Exported))
3930*67e74705SXin Li         Exported = TD->getDefinition();
3931*67e74705SXin Li       Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
3932*67e74705SXin Li       if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
3933*67e74705SXin Li         // FIXME: This doesn't send the right notifications if there are
3934*67e74705SXin Li         // ASTMutationListeners other than an ASTWriter.
3935*67e74705SXin Li         Reader.getContext().mergeDefinitionIntoModule(
3936*67e74705SXin Li             cast<NamedDecl>(Exported), Owner,
3937*67e74705SXin Li             /*NotifyListeners*/ false);
3938*67e74705SXin Li         Reader.PendingMergedDefinitionsToDeduplicate.insert(
3939*67e74705SXin Li             cast<NamedDecl>(Exported));
3940*67e74705SXin Li       } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
3941*67e74705SXin Li         // If Owner is made visible at some later point, make this declaration
3942*67e74705SXin Li         // visible too.
3943*67e74705SXin Li         Reader.HiddenNamesMap[Owner].push_back(Exported);
3944*67e74705SXin Li       } else {
3945*67e74705SXin Li         // The declaration is now visible.
3946*67e74705SXin Li         Exported->Hidden = false;
3947*67e74705SXin Li       }
3948*67e74705SXin Li       break;
3949*67e74705SXin Li     }
3950*67e74705SXin Li 
3951*67e74705SXin Li     case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
3952*67e74705SXin Li     case UPD_ADDED_ATTR_TO_RECORD:
3953*67e74705SXin Li       AttrVec Attrs;
3954*67e74705SXin Li       Reader.ReadAttributes(F, Attrs, Record, Idx);
3955*67e74705SXin Li       assert(Attrs.size() == 1);
3956*67e74705SXin Li       D->addAttr(Attrs[0]);
3957*67e74705SXin Li       break;
3958*67e74705SXin Li     }
3959*67e74705SXin Li   }
3960*67e74705SXin Li }
3961