1e5dd7070Spatrick //===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This file implements the ASTReader::readDeclRecord method, which is the
10e5dd7070Spatrick // entrypoint for loading a decl.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick
14e5dd7070Spatrick #include "ASTCommon.h"
15e5dd7070Spatrick #include "ASTReaderInternals.h"
16e5dd7070Spatrick #include "clang/AST/ASTContext.h"
17e5dd7070Spatrick #include "clang/AST/Attr.h"
18e5dd7070Spatrick #include "clang/AST/AttrIterator.h"
19e5dd7070Spatrick #include "clang/AST/Decl.h"
20e5dd7070Spatrick #include "clang/AST/DeclBase.h"
21e5dd7070Spatrick #include "clang/AST/DeclCXX.h"
22e5dd7070Spatrick #include "clang/AST/DeclFriend.h"
23e5dd7070Spatrick #include "clang/AST/DeclObjC.h"
24e5dd7070Spatrick #include "clang/AST/DeclOpenMP.h"
25e5dd7070Spatrick #include "clang/AST/DeclTemplate.h"
26e5dd7070Spatrick #include "clang/AST/DeclVisitor.h"
27e5dd7070Spatrick #include "clang/AST/DeclarationName.h"
28e5dd7070Spatrick #include "clang/AST/Expr.h"
29e5dd7070Spatrick #include "clang/AST/ExternalASTSource.h"
30e5dd7070Spatrick #include "clang/AST/LambdaCapture.h"
31e5dd7070Spatrick #include "clang/AST/NestedNameSpecifier.h"
32e5dd7070Spatrick #include "clang/AST/OpenMPClause.h"
33e5dd7070Spatrick #include "clang/AST/Redeclarable.h"
34e5dd7070Spatrick #include "clang/AST/Stmt.h"
35e5dd7070Spatrick #include "clang/AST/TemplateBase.h"
36e5dd7070Spatrick #include "clang/AST/Type.h"
37e5dd7070Spatrick #include "clang/AST/UnresolvedSet.h"
38e5dd7070Spatrick #include "clang/Basic/AttrKinds.h"
39*12c85518Srobert #include "clang/Basic/DiagnosticSema.h"
40e5dd7070Spatrick #include "clang/Basic/ExceptionSpecificationType.h"
41e5dd7070Spatrick #include "clang/Basic/IdentifierTable.h"
42e5dd7070Spatrick #include "clang/Basic/LLVM.h"
43e5dd7070Spatrick #include "clang/Basic/Lambda.h"
44e5dd7070Spatrick #include "clang/Basic/LangOptions.h"
45e5dd7070Spatrick #include "clang/Basic/Linkage.h"
46e5dd7070Spatrick #include "clang/Basic/Module.h"
47e5dd7070Spatrick #include "clang/Basic/PragmaKinds.h"
48e5dd7070Spatrick #include "clang/Basic/SourceLocation.h"
49e5dd7070Spatrick #include "clang/Basic/Specifiers.h"
50e5dd7070Spatrick #include "clang/Sema/IdentifierResolver.h"
51e5dd7070Spatrick #include "clang/Serialization/ASTBitCodes.h"
52e5dd7070Spatrick #include "clang/Serialization/ASTRecordReader.h"
53e5dd7070Spatrick #include "clang/Serialization/ContinuousRangeMap.h"
54e5dd7070Spatrick #include "clang/Serialization/ModuleFile.h"
55e5dd7070Spatrick #include "llvm/ADT/DenseMap.h"
56e5dd7070Spatrick #include "llvm/ADT/FoldingSet.h"
57e5dd7070Spatrick #include "llvm/ADT/STLExtras.h"
58e5dd7070Spatrick #include "llvm/ADT/SmallPtrSet.h"
59e5dd7070Spatrick #include "llvm/ADT/SmallVector.h"
60e5dd7070Spatrick #include "llvm/ADT/iterator_range.h"
61e5dd7070Spatrick #include "llvm/Bitstream/BitstreamReader.h"
62e5dd7070Spatrick #include "llvm/Support/Casting.h"
63e5dd7070Spatrick #include "llvm/Support/ErrorHandling.h"
64e5dd7070Spatrick #include "llvm/Support/SaveAndRestore.h"
65e5dd7070Spatrick #include <algorithm>
66e5dd7070Spatrick #include <cassert>
67e5dd7070Spatrick #include <cstdint>
68e5dd7070Spatrick #include <cstring>
69e5dd7070Spatrick #include <string>
70e5dd7070Spatrick #include <utility>
71e5dd7070Spatrick
72e5dd7070Spatrick using namespace clang;
73e5dd7070Spatrick using namespace serialization;
74e5dd7070Spatrick
75e5dd7070Spatrick //===----------------------------------------------------------------------===//
76e5dd7070Spatrick // Declaration deserialization
77e5dd7070Spatrick //===----------------------------------------------------------------------===//
78e5dd7070Spatrick
79e5dd7070Spatrick namespace clang {
80e5dd7070Spatrick
81e5dd7070Spatrick class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
82e5dd7070Spatrick ASTReader &Reader;
83e5dd7070Spatrick ASTRecordReader &Record;
84e5dd7070Spatrick ASTReader::RecordLocation Loc;
85e5dd7070Spatrick const DeclID ThisDeclID;
86e5dd7070Spatrick const SourceLocation ThisDeclLoc;
87e5dd7070Spatrick
88e5dd7070Spatrick using RecordData = ASTReader::RecordData;
89e5dd7070Spatrick
90e5dd7070Spatrick TypeID DeferredTypeID = 0;
91e5dd7070Spatrick unsigned AnonymousDeclNumber;
92e5dd7070Spatrick GlobalDeclID NamedDeclForTagDecl = 0;
93e5dd7070Spatrick IdentifierInfo *TypedefNameForLinkage = nullptr;
94e5dd7070Spatrick
95e5dd7070Spatrick bool HasPendingBody = false;
96e5dd7070Spatrick
97e5dd7070Spatrick ///A flag to carry the information for a decl from the entity is
98e5dd7070Spatrick /// used. We use it to delay the marking of the canonical decl as used until
99e5dd7070Spatrick /// the entire declaration is deserialized and merged.
100e5dd7070Spatrick bool IsDeclMarkedUsed = false;
101e5dd7070Spatrick
102e5dd7070Spatrick uint64_t GetCurrentCursorOffset();
103e5dd7070Spatrick
ReadLocalOffset()104e5dd7070Spatrick uint64_t ReadLocalOffset() {
105e5dd7070Spatrick uint64_t LocalOffset = Record.readInt();
106e5dd7070Spatrick assert(LocalOffset < Loc.Offset && "offset point after current record");
107e5dd7070Spatrick return LocalOffset ? Loc.Offset - LocalOffset : 0;
108e5dd7070Spatrick }
109e5dd7070Spatrick
ReadGlobalOffset()110e5dd7070Spatrick uint64_t ReadGlobalOffset() {
111e5dd7070Spatrick uint64_t Local = ReadLocalOffset();
112e5dd7070Spatrick return Local ? Record.getGlobalBitOffset(Local) : 0;
113e5dd7070Spatrick }
114e5dd7070Spatrick
readSourceLocation()115e5dd7070Spatrick SourceLocation readSourceLocation() {
116e5dd7070Spatrick return Record.readSourceLocation();
117e5dd7070Spatrick }
118e5dd7070Spatrick
readSourceRange()119e5dd7070Spatrick SourceRange readSourceRange() {
120e5dd7070Spatrick return Record.readSourceRange();
121e5dd7070Spatrick }
122e5dd7070Spatrick
readTypeSourceInfo()123e5dd7070Spatrick TypeSourceInfo *readTypeSourceInfo() {
124e5dd7070Spatrick return Record.readTypeSourceInfo();
125e5dd7070Spatrick }
126e5dd7070Spatrick
readDeclID()127e5dd7070Spatrick serialization::DeclID readDeclID() {
128e5dd7070Spatrick return Record.readDeclID();
129e5dd7070Spatrick }
130e5dd7070Spatrick
readString()131e5dd7070Spatrick std::string readString() {
132e5dd7070Spatrick return Record.readString();
133e5dd7070Spatrick }
134e5dd7070Spatrick
readDeclIDList(SmallVectorImpl<DeclID> & IDs)135e5dd7070Spatrick void readDeclIDList(SmallVectorImpl<DeclID> &IDs) {
136e5dd7070Spatrick for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
137e5dd7070Spatrick IDs.push_back(readDeclID());
138e5dd7070Spatrick }
139e5dd7070Spatrick
readDecl()140e5dd7070Spatrick Decl *readDecl() {
141e5dd7070Spatrick return Record.readDecl();
142e5dd7070Spatrick }
143e5dd7070Spatrick
144e5dd7070Spatrick template<typename T>
readDeclAs()145e5dd7070Spatrick T *readDeclAs() {
146e5dd7070Spatrick return Record.readDeclAs<T>();
147e5dd7070Spatrick }
148e5dd7070Spatrick
readSubmoduleID()149e5dd7070Spatrick serialization::SubmoduleID readSubmoduleID() {
150e5dd7070Spatrick if (Record.getIdx() == Record.size())
151e5dd7070Spatrick return 0;
152e5dd7070Spatrick
153e5dd7070Spatrick return Record.getGlobalSubmoduleID(Record.readInt());
154e5dd7070Spatrick }
155e5dd7070Spatrick
readModule()156e5dd7070Spatrick Module *readModule() {
157e5dd7070Spatrick return Record.getSubmodule(readSubmoduleID());
158e5dd7070Spatrick }
159e5dd7070Spatrick
160e5dd7070Spatrick void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
161e5dd7070Spatrick void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
162e5dd7070Spatrick const CXXRecordDecl *D);
163e5dd7070Spatrick void MergeDefinitionData(CXXRecordDecl *D,
164e5dd7070Spatrick struct CXXRecordDecl::DefinitionData &&NewDD);
165e5dd7070Spatrick void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
166e5dd7070Spatrick void MergeDefinitionData(ObjCInterfaceDecl *D,
167e5dd7070Spatrick struct ObjCInterfaceDecl::DefinitionData &&NewDD);
168e5dd7070Spatrick void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
169e5dd7070Spatrick void MergeDefinitionData(ObjCProtocolDecl *D,
170e5dd7070Spatrick struct ObjCProtocolDecl::DefinitionData &&NewDD);
171e5dd7070Spatrick
172e5dd7070Spatrick static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
173e5dd7070Spatrick
174e5dd7070Spatrick static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
175e5dd7070Spatrick DeclContext *DC,
176e5dd7070Spatrick unsigned Index);
177e5dd7070Spatrick static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
178e5dd7070Spatrick unsigned Index, NamedDecl *D);
179e5dd7070Spatrick
180e5dd7070Spatrick /// Results from loading a RedeclarableDecl.
181e5dd7070Spatrick class RedeclarableResult {
182e5dd7070Spatrick Decl *MergeWith;
183e5dd7070Spatrick GlobalDeclID FirstID;
184e5dd7070Spatrick bool IsKeyDecl;
185e5dd7070Spatrick
186e5dd7070Spatrick public:
RedeclarableResult(Decl * MergeWith,GlobalDeclID FirstID,bool IsKeyDecl)187e5dd7070Spatrick RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
188e5dd7070Spatrick : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
189e5dd7070Spatrick
190e5dd7070Spatrick /// Retrieve the first ID.
getFirstID() const191e5dd7070Spatrick GlobalDeclID getFirstID() const { return FirstID; }
192e5dd7070Spatrick
193e5dd7070Spatrick /// Is this declaration a key declaration?
isKeyDecl() const194e5dd7070Spatrick bool isKeyDecl() const { return IsKeyDecl; }
195e5dd7070Spatrick
196e5dd7070Spatrick /// Get a known declaration that this should be merged with, if
197e5dd7070Spatrick /// any.
getKnownMergeTarget() const198e5dd7070Spatrick Decl *getKnownMergeTarget() const { return MergeWith; }
199e5dd7070Spatrick };
200e5dd7070Spatrick
201e5dd7070Spatrick /// Class used to capture the result of searching for an existing
202e5dd7070Spatrick /// declaration of a specific kind and name, along with the ability
203e5dd7070Spatrick /// to update the place where this result was found (the declaration
204e5dd7070Spatrick /// chain hanging off an identifier or the DeclContext we searched in)
205e5dd7070Spatrick /// if requested.
206e5dd7070Spatrick class FindExistingResult {
207e5dd7070Spatrick ASTReader &Reader;
208e5dd7070Spatrick NamedDecl *New = nullptr;
209e5dd7070Spatrick NamedDecl *Existing = nullptr;
210e5dd7070Spatrick bool AddResult = false;
211e5dd7070Spatrick unsigned AnonymousDeclNumber = 0;
212e5dd7070Spatrick IdentifierInfo *TypedefNameForLinkage = nullptr;
213e5dd7070Spatrick
214e5dd7070Spatrick public:
FindExistingResult(ASTReader & Reader)215e5dd7070Spatrick FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
216e5dd7070Spatrick
FindExistingResult(ASTReader & Reader,NamedDecl * New,NamedDecl * Existing,unsigned AnonymousDeclNumber,IdentifierInfo * TypedefNameForLinkage)217e5dd7070Spatrick FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
218e5dd7070Spatrick unsigned AnonymousDeclNumber,
219e5dd7070Spatrick IdentifierInfo *TypedefNameForLinkage)
220e5dd7070Spatrick : Reader(Reader), New(New), Existing(Existing), AddResult(true),
221e5dd7070Spatrick AnonymousDeclNumber(AnonymousDeclNumber),
222e5dd7070Spatrick TypedefNameForLinkage(TypedefNameForLinkage) {}
223e5dd7070Spatrick
FindExistingResult(FindExistingResult && Other)224e5dd7070Spatrick FindExistingResult(FindExistingResult &&Other)
225e5dd7070Spatrick : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
226e5dd7070Spatrick AddResult(Other.AddResult),
227e5dd7070Spatrick AnonymousDeclNumber(Other.AnonymousDeclNumber),
228e5dd7070Spatrick TypedefNameForLinkage(Other.TypedefNameForLinkage) {
229e5dd7070Spatrick Other.AddResult = false;
230e5dd7070Spatrick }
231e5dd7070Spatrick
232e5dd7070Spatrick FindExistingResult &operator=(FindExistingResult &&) = delete;
233e5dd7070Spatrick ~FindExistingResult();
234e5dd7070Spatrick
235e5dd7070Spatrick /// Suppress the addition of this result into the known set of
236e5dd7070Spatrick /// names.
suppress()237e5dd7070Spatrick void suppress() { AddResult = false; }
238e5dd7070Spatrick
operator NamedDecl*() const239e5dd7070Spatrick operator NamedDecl*() const { return Existing; }
240e5dd7070Spatrick
241e5dd7070Spatrick template<typename T>
operator T*() const242e5dd7070Spatrick operator T*() const { return dyn_cast_or_null<T>(Existing); }
243e5dd7070Spatrick };
244e5dd7070Spatrick
245e5dd7070Spatrick static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
246e5dd7070Spatrick DeclContext *DC);
247e5dd7070Spatrick FindExistingResult findExisting(NamedDecl *D);
248e5dd7070Spatrick
249e5dd7070Spatrick public:
ASTDeclReader(ASTReader & Reader,ASTRecordReader & Record,ASTReader::RecordLocation Loc,DeclID thisDeclID,SourceLocation ThisDeclLoc)250e5dd7070Spatrick ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
251e5dd7070Spatrick ASTReader::RecordLocation Loc,
252e5dd7070Spatrick DeclID thisDeclID, SourceLocation ThisDeclLoc)
253e5dd7070Spatrick : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
254e5dd7070Spatrick ThisDeclLoc(ThisDeclLoc) {}
255e5dd7070Spatrick
256e5dd7070Spatrick template <typename T> static
AddLazySpecializations(T * D,SmallVectorImpl<serialization::DeclID> & IDs)257e5dd7070Spatrick void AddLazySpecializations(T *D,
258e5dd7070Spatrick SmallVectorImpl<serialization::DeclID>& IDs) {
259e5dd7070Spatrick if (IDs.empty())
260e5dd7070Spatrick return;
261e5dd7070Spatrick
262e5dd7070Spatrick // FIXME: We should avoid this pattern of getting the ASTContext.
263e5dd7070Spatrick ASTContext &C = D->getASTContext();
264e5dd7070Spatrick
265e5dd7070Spatrick auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
266e5dd7070Spatrick
267e5dd7070Spatrick if (auto &Old = LazySpecializations) {
268e5dd7070Spatrick IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
269e5dd7070Spatrick llvm::sort(IDs);
270e5dd7070Spatrick IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
271e5dd7070Spatrick }
272e5dd7070Spatrick
273e5dd7070Spatrick auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
274e5dd7070Spatrick *Result = IDs.size();
275e5dd7070Spatrick std::copy(IDs.begin(), IDs.end(), Result + 1);
276e5dd7070Spatrick
277e5dd7070Spatrick LazySpecializations = Result;
278e5dd7070Spatrick }
279e5dd7070Spatrick
280e5dd7070Spatrick template <typename DeclT>
281e5dd7070Spatrick static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
282e5dd7070Spatrick static Decl *getMostRecentDeclImpl(...);
283e5dd7070Spatrick static Decl *getMostRecentDecl(Decl *D);
284e5dd7070Spatrick
285a9ac8606Spatrick static void mergeInheritableAttributes(ASTReader &Reader, Decl *D,
286a9ac8606Spatrick Decl *Previous);
287a9ac8606Spatrick
288e5dd7070Spatrick template <typename DeclT>
289e5dd7070Spatrick static void attachPreviousDeclImpl(ASTReader &Reader,
290e5dd7070Spatrick Redeclarable<DeclT> *D, Decl *Previous,
291e5dd7070Spatrick Decl *Canon);
292e5dd7070Spatrick static void attachPreviousDeclImpl(ASTReader &Reader, ...);
293e5dd7070Spatrick static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
294e5dd7070Spatrick Decl *Canon);
295e5dd7070Spatrick
296e5dd7070Spatrick template <typename DeclT>
297e5dd7070Spatrick static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
298e5dd7070Spatrick static void attachLatestDeclImpl(...);
299e5dd7070Spatrick static void attachLatestDecl(Decl *D, Decl *latest);
300e5dd7070Spatrick
301e5dd7070Spatrick template <typename DeclT>
302e5dd7070Spatrick static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
303e5dd7070Spatrick static void markIncompleteDeclChainImpl(...);
304e5dd7070Spatrick
305e5dd7070Spatrick /// Determine whether this declaration has a pending body.
hasPendingBody() const306e5dd7070Spatrick bool hasPendingBody() const { return HasPendingBody; }
307e5dd7070Spatrick
308e5dd7070Spatrick void ReadFunctionDefinition(FunctionDecl *FD);
309e5dd7070Spatrick void Visit(Decl *D);
310e5dd7070Spatrick
311e5dd7070Spatrick void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
312e5dd7070Spatrick
setNextObjCCategory(ObjCCategoryDecl * Cat,ObjCCategoryDecl * Next)313e5dd7070Spatrick static void setNextObjCCategory(ObjCCategoryDecl *Cat,
314e5dd7070Spatrick ObjCCategoryDecl *Next) {
315e5dd7070Spatrick Cat->NextClassCategory = Next;
316e5dd7070Spatrick }
317e5dd7070Spatrick
318e5dd7070Spatrick void VisitDecl(Decl *D);
319e5dd7070Spatrick void VisitPragmaCommentDecl(PragmaCommentDecl *D);
320e5dd7070Spatrick void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
321e5dd7070Spatrick void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
322e5dd7070Spatrick void VisitNamedDecl(NamedDecl *ND);
323e5dd7070Spatrick void VisitLabelDecl(LabelDecl *LD);
324e5dd7070Spatrick void VisitNamespaceDecl(NamespaceDecl *D);
325*12c85518Srobert void VisitHLSLBufferDecl(HLSLBufferDecl *D);
326e5dd7070Spatrick void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
327e5dd7070Spatrick void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
328e5dd7070Spatrick void VisitTypeDecl(TypeDecl *TD);
329e5dd7070Spatrick RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
330e5dd7070Spatrick void VisitTypedefDecl(TypedefDecl *TD);
331e5dd7070Spatrick void VisitTypeAliasDecl(TypeAliasDecl *TD);
332e5dd7070Spatrick void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
333a9ac8606Spatrick void VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl *D);
334e5dd7070Spatrick RedeclarableResult VisitTagDecl(TagDecl *TD);
335e5dd7070Spatrick void VisitEnumDecl(EnumDecl *ED);
336e5dd7070Spatrick RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
337*12c85518Srobert void VisitRecordDecl(RecordDecl *RD);
338e5dd7070Spatrick RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
VisitCXXRecordDecl(CXXRecordDecl * D)339e5dd7070Spatrick void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
340e5dd7070Spatrick RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
341e5dd7070Spatrick ClassTemplateSpecializationDecl *D);
342e5dd7070Spatrick
VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl * D)343e5dd7070Spatrick void VisitClassTemplateSpecializationDecl(
344e5dd7070Spatrick ClassTemplateSpecializationDecl *D) {
345e5dd7070Spatrick VisitClassTemplateSpecializationDeclImpl(D);
346e5dd7070Spatrick }
347e5dd7070Spatrick
348e5dd7070Spatrick void VisitClassTemplatePartialSpecializationDecl(
349e5dd7070Spatrick ClassTemplatePartialSpecializationDecl *D);
350e5dd7070Spatrick void VisitClassScopeFunctionSpecializationDecl(
351e5dd7070Spatrick ClassScopeFunctionSpecializationDecl *D);
352e5dd7070Spatrick RedeclarableResult
353e5dd7070Spatrick VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
354e5dd7070Spatrick
VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl * D)355e5dd7070Spatrick void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
356e5dd7070Spatrick VisitVarTemplateSpecializationDeclImpl(D);
357e5dd7070Spatrick }
358e5dd7070Spatrick
359e5dd7070Spatrick void VisitVarTemplatePartialSpecializationDecl(
360e5dd7070Spatrick VarTemplatePartialSpecializationDecl *D);
361e5dd7070Spatrick void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
362e5dd7070Spatrick void VisitValueDecl(ValueDecl *VD);
363e5dd7070Spatrick void VisitEnumConstantDecl(EnumConstantDecl *ECD);
364e5dd7070Spatrick void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
365e5dd7070Spatrick void VisitDeclaratorDecl(DeclaratorDecl *DD);
366e5dd7070Spatrick void VisitFunctionDecl(FunctionDecl *FD);
367e5dd7070Spatrick void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
368e5dd7070Spatrick void VisitCXXMethodDecl(CXXMethodDecl *D);
369e5dd7070Spatrick void VisitCXXConstructorDecl(CXXConstructorDecl *D);
370e5dd7070Spatrick void VisitCXXDestructorDecl(CXXDestructorDecl *D);
371e5dd7070Spatrick void VisitCXXConversionDecl(CXXConversionDecl *D);
372e5dd7070Spatrick void VisitFieldDecl(FieldDecl *FD);
373e5dd7070Spatrick void VisitMSPropertyDecl(MSPropertyDecl *FD);
374ec727ea7Spatrick void VisitMSGuidDecl(MSGuidDecl *D);
375*12c85518Srobert void VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl *D);
376a9ac8606Spatrick void VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D);
377e5dd7070Spatrick void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
378e5dd7070Spatrick RedeclarableResult VisitVarDeclImpl(VarDecl *D);
VisitVarDecl(VarDecl * VD)379e5dd7070Spatrick void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
380e5dd7070Spatrick void VisitImplicitParamDecl(ImplicitParamDecl *PD);
381e5dd7070Spatrick void VisitParmVarDecl(ParmVarDecl *PD);
382e5dd7070Spatrick void VisitDecompositionDecl(DecompositionDecl *DD);
383e5dd7070Spatrick void VisitBindingDecl(BindingDecl *BD);
384e5dd7070Spatrick void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
385*12c85518Srobert void VisitTemplateDecl(TemplateDecl *D);
386e5dd7070Spatrick void VisitConceptDecl(ConceptDecl *D);
387*12c85518Srobert void VisitImplicitConceptSpecializationDecl(
388*12c85518Srobert ImplicitConceptSpecializationDecl *D);
389e5dd7070Spatrick void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);
390e5dd7070Spatrick RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
391e5dd7070Spatrick void VisitClassTemplateDecl(ClassTemplateDecl *D);
392e5dd7070Spatrick void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
393e5dd7070Spatrick void VisitVarTemplateDecl(VarTemplateDecl *D);
394e5dd7070Spatrick void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
395e5dd7070Spatrick void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
396e5dd7070Spatrick void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
397e5dd7070Spatrick void VisitUsingDecl(UsingDecl *D);
398a9ac8606Spatrick void VisitUsingEnumDecl(UsingEnumDecl *D);
399e5dd7070Spatrick void VisitUsingPackDecl(UsingPackDecl *D);
400e5dd7070Spatrick void VisitUsingShadowDecl(UsingShadowDecl *D);
401e5dd7070Spatrick void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
402e5dd7070Spatrick void VisitLinkageSpecDecl(LinkageSpecDecl *D);
403e5dd7070Spatrick void VisitExportDecl(ExportDecl *D);
404e5dd7070Spatrick void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
405*12c85518Srobert void VisitTopLevelStmtDecl(TopLevelStmtDecl *D);
406e5dd7070Spatrick void VisitImportDecl(ImportDecl *D);
407e5dd7070Spatrick void VisitAccessSpecDecl(AccessSpecDecl *D);
408e5dd7070Spatrick void VisitFriendDecl(FriendDecl *D);
409e5dd7070Spatrick void VisitFriendTemplateDecl(FriendTemplateDecl *D);
410e5dd7070Spatrick void VisitStaticAssertDecl(StaticAssertDecl *D);
411e5dd7070Spatrick void VisitBlockDecl(BlockDecl *BD);
412e5dd7070Spatrick void VisitCapturedDecl(CapturedDecl *CD);
413e5dd7070Spatrick void VisitEmptyDecl(EmptyDecl *D);
414e5dd7070Spatrick void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);
415e5dd7070Spatrick
416e5dd7070Spatrick std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
417e5dd7070Spatrick
418e5dd7070Spatrick template<typename T>
419e5dd7070Spatrick RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
420e5dd7070Spatrick
421e5dd7070Spatrick template <typename T>
422*12c85518Srobert void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl);
423*12c85518Srobert
424*12c85518Srobert void mergeRedeclarableTemplate(RedeclarableTemplateDecl *D,
425*12c85518Srobert RedeclarableResult &Redecl);
426e5dd7070Spatrick
427e5dd7070Spatrick template <typename T>
428e5dd7070Spatrick void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
429*12c85518Srobert RedeclarableResult &Redecl);
430e5dd7070Spatrick
431e5dd7070Spatrick template<typename T>
432e5dd7070Spatrick void mergeMergeable(Mergeable<T> *D);
433e5dd7070Spatrick
434e5dd7070Spatrick void mergeMergeable(LifetimeExtendedTemporaryDecl *D);
435e5dd7070Spatrick
436e5dd7070Spatrick void mergeTemplatePattern(RedeclarableTemplateDecl *D,
437e5dd7070Spatrick RedeclarableTemplateDecl *Existing,
438*12c85518Srobert bool IsKeyDecl);
439e5dd7070Spatrick
440e5dd7070Spatrick ObjCTypeParamList *ReadObjCTypeParamList();
441e5dd7070Spatrick
442e5dd7070Spatrick // FIXME: Reorder according to DeclNodes.td?
443e5dd7070Spatrick void VisitObjCMethodDecl(ObjCMethodDecl *D);
444e5dd7070Spatrick void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
445e5dd7070Spatrick void VisitObjCContainerDecl(ObjCContainerDecl *D);
446e5dd7070Spatrick void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
447e5dd7070Spatrick void VisitObjCIvarDecl(ObjCIvarDecl *D);
448e5dd7070Spatrick void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
449e5dd7070Spatrick void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
450e5dd7070Spatrick void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
451e5dd7070Spatrick void VisitObjCImplDecl(ObjCImplDecl *D);
452e5dd7070Spatrick void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
453e5dd7070Spatrick void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
454e5dd7070Spatrick void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
455e5dd7070Spatrick void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
456e5dd7070Spatrick void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
457e5dd7070Spatrick void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
458e5dd7070Spatrick void VisitOMPAllocateDecl(OMPAllocateDecl *D);
459e5dd7070Spatrick void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
460e5dd7070Spatrick void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
461e5dd7070Spatrick void VisitOMPRequiresDecl(OMPRequiresDecl *D);
462e5dd7070Spatrick void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
463e5dd7070Spatrick };
464e5dd7070Spatrick
465e5dd7070Spatrick } // namespace clang
466e5dd7070Spatrick
467e5dd7070Spatrick namespace {
468e5dd7070Spatrick
469e5dd7070Spatrick /// Iterator over the redeclarations of a declaration that have already
470e5dd7070Spatrick /// been merged into the same redeclaration chain.
471e5dd7070Spatrick template<typename DeclT>
472e5dd7070Spatrick class MergedRedeclIterator {
473e5dd7070Spatrick DeclT *Start;
474e5dd7070Spatrick DeclT *Canonical = nullptr;
475e5dd7070Spatrick DeclT *Current = nullptr;
476e5dd7070Spatrick
477e5dd7070Spatrick public:
478e5dd7070Spatrick MergedRedeclIterator() = default;
MergedRedeclIterator(DeclT * Start)479e5dd7070Spatrick MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
480e5dd7070Spatrick
operator *()481e5dd7070Spatrick DeclT *operator*() { return Current; }
482e5dd7070Spatrick
operator ++()483e5dd7070Spatrick MergedRedeclIterator &operator++() {
484e5dd7070Spatrick if (Current->isFirstDecl()) {
485e5dd7070Spatrick Canonical = Current;
486e5dd7070Spatrick Current = Current->getMostRecentDecl();
487e5dd7070Spatrick } else
488e5dd7070Spatrick Current = Current->getPreviousDecl();
489e5dd7070Spatrick
490e5dd7070Spatrick // If we started in the merged portion, we'll reach our start position
491e5dd7070Spatrick // eventually. Otherwise, we'll never reach it, but the second declaration
492e5dd7070Spatrick // we reached was the canonical declaration, so stop when we see that one
493e5dd7070Spatrick // again.
494e5dd7070Spatrick if (Current == Start || Current == Canonical)
495e5dd7070Spatrick Current = nullptr;
496e5dd7070Spatrick return *this;
497e5dd7070Spatrick }
498e5dd7070Spatrick
operator !=(const MergedRedeclIterator & A,const MergedRedeclIterator & B)499e5dd7070Spatrick friend bool operator!=(const MergedRedeclIterator &A,
500e5dd7070Spatrick const MergedRedeclIterator &B) {
501e5dd7070Spatrick return A.Current != B.Current;
502e5dd7070Spatrick }
503e5dd7070Spatrick };
504e5dd7070Spatrick
505e5dd7070Spatrick } // namespace
506e5dd7070Spatrick
507e5dd7070Spatrick template <typename DeclT>
508e5dd7070Spatrick static llvm::iterator_range<MergedRedeclIterator<DeclT>>
merged_redecls(DeclT * D)509e5dd7070Spatrick merged_redecls(DeclT *D) {
510e5dd7070Spatrick return llvm::make_range(MergedRedeclIterator<DeclT>(D),
511e5dd7070Spatrick MergedRedeclIterator<DeclT>());
512e5dd7070Spatrick }
513e5dd7070Spatrick
GetCurrentCursorOffset()514e5dd7070Spatrick uint64_t ASTDeclReader::GetCurrentCursorOffset() {
515e5dd7070Spatrick return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
516e5dd7070Spatrick }
517e5dd7070Spatrick
ReadFunctionDefinition(FunctionDecl * FD)518e5dd7070Spatrick void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
519ec727ea7Spatrick if (Record.readInt()) {
520a9ac8606Spatrick Reader.DefinitionSource[FD] =
521a9ac8606Spatrick Loc.F->Kind == ModuleKind::MK_MainFile ||
522a9ac8606Spatrick Reader.getContext().getLangOpts().BuildingPCHWithObjectFile;
523ec727ea7Spatrick }
524e5dd7070Spatrick if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
525e5dd7070Spatrick CD->setNumCtorInitializers(Record.readInt());
526e5dd7070Spatrick if (CD->getNumCtorInitializers())
527e5dd7070Spatrick CD->CtorInitializers = ReadGlobalOffset();
528e5dd7070Spatrick }
529e5dd7070Spatrick // Store the offset of the body so we can lazily load it later.
530e5dd7070Spatrick Reader.PendingBodies[FD] = GetCurrentCursorOffset();
531e5dd7070Spatrick HasPendingBody = true;
532e5dd7070Spatrick }
533e5dd7070Spatrick
Visit(Decl * D)534e5dd7070Spatrick void ASTDeclReader::Visit(Decl *D) {
535e5dd7070Spatrick DeclVisitor<ASTDeclReader, void>::Visit(D);
536e5dd7070Spatrick
537e5dd7070Spatrick // At this point we have deserialized and merged the decl and it is safe to
538e5dd7070Spatrick // update its canonical decl to signal that the entire entity is used.
539e5dd7070Spatrick D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
540e5dd7070Spatrick IsDeclMarkedUsed = false;
541e5dd7070Spatrick
542e5dd7070Spatrick if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
543e5dd7070Spatrick if (auto *TInfo = DD->getTypeSourceInfo())
544e5dd7070Spatrick Record.readTypeLoc(TInfo->getTypeLoc());
545e5dd7070Spatrick }
546e5dd7070Spatrick
547e5dd7070Spatrick if (auto *TD = dyn_cast<TypeDecl>(D)) {
548e5dd7070Spatrick // We have a fully initialized TypeDecl. Read its type now.
549e5dd7070Spatrick TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
550e5dd7070Spatrick
551e5dd7070Spatrick // If this is a tag declaration with a typedef name for linkage, it's safe
552e5dd7070Spatrick // to load that typedef now.
553e5dd7070Spatrick if (NamedDeclForTagDecl)
554e5dd7070Spatrick cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
555e5dd7070Spatrick cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
556e5dd7070Spatrick } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
557e5dd7070Spatrick // if we have a fully initialized TypeDecl, we can safely read its type now.
558e5dd7070Spatrick ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
559e5dd7070Spatrick } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
560e5dd7070Spatrick // FunctionDecl's body was written last after all other Stmts/Exprs.
561e5dd7070Spatrick // We only read it if FD doesn't already have a body (e.g., from another
562e5dd7070Spatrick // module).
563e5dd7070Spatrick // FIXME: Can we diagnose ODR violations somehow?
564e5dd7070Spatrick if (Record.readInt())
565e5dd7070Spatrick ReadFunctionDefinition(FD);
566e5dd7070Spatrick }
567e5dd7070Spatrick }
568e5dd7070Spatrick
VisitDecl(Decl * D)569e5dd7070Spatrick void ASTDeclReader::VisitDecl(Decl *D) {
570e5dd7070Spatrick if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
571*12c85518Srobert isa<ParmVarDecl, ObjCTypeParamDecl>(D)) {
572e5dd7070Spatrick // We don't want to deserialize the DeclContext of a template
573e5dd7070Spatrick // parameter or of a parameter of a function template immediately. These
574e5dd7070Spatrick // entities might be used in the formulation of its DeclContext (for
575e5dd7070Spatrick // example, a function parameter can be used in decltype() in trailing
576e5dd7070Spatrick // return type of the function). Use the translation unit DeclContext as a
577e5dd7070Spatrick // placeholder.
578e5dd7070Spatrick GlobalDeclID SemaDCIDForTemplateParmDecl = readDeclID();
579e5dd7070Spatrick GlobalDeclID LexicalDCIDForTemplateParmDecl = readDeclID();
580e5dd7070Spatrick if (!LexicalDCIDForTemplateParmDecl)
581e5dd7070Spatrick LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
582e5dd7070Spatrick Reader.addPendingDeclContextInfo(D,
583e5dd7070Spatrick SemaDCIDForTemplateParmDecl,
584e5dd7070Spatrick LexicalDCIDForTemplateParmDecl);
585e5dd7070Spatrick D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
586e5dd7070Spatrick } else {
587e5dd7070Spatrick auto *SemaDC = readDeclAs<DeclContext>();
588e5dd7070Spatrick auto *LexicalDC = readDeclAs<DeclContext>();
589e5dd7070Spatrick if (!LexicalDC)
590e5dd7070Spatrick LexicalDC = SemaDC;
591e5dd7070Spatrick DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
592e5dd7070Spatrick // Avoid calling setLexicalDeclContext() directly because it uses
593e5dd7070Spatrick // Decl::getASTContext() internally which is unsafe during derialization.
594e5dd7070Spatrick D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
595e5dd7070Spatrick Reader.getContext());
596e5dd7070Spatrick }
597e5dd7070Spatrick D->setLocation(ThisDeclLoc);
598ec727ea7Spatrick D->InvalidDecl = Record.readInt();
599e5dd7070Spatrick if (Record.readInt()) { // hasAttrs
600e5dd7070Spatrick AttrVec Attrs;
601e5dd7070Spatrick Record.readAttributes(Attrs);
602e5dd7070Spatrick // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
603e5dd7070Spatrick // internally which is unsafe during derialization.
604e5dd7070Spatrick D->setAttrsImpl(Attrs, Reader.getContext());
605e5dd7070Spatrick }
606e5dd7070Spatrick D->setImplicit(Record.readInt());
607e5dd7070Spatrick D->Used = Record.readInt();
608e5dd7070Spatrick IsDeclMarkedUsed |= D->Used;
609e5dd7070Spatrick D->setReferenced(Record.readInt());
610e5dd7070Spatrick D->setTopLevelDeclInObjCContainer(Record.readInt());
611e5dd7070Spatrick D->setAccess((AccessSpecifier)Record.readInt());
612e5dd7070Spatrick D->FromASTFile = true;
613*12c85518Srobert auto ModuleOwnership = (Decl::ModuleOwnershipKind)Record.readInt();
614*12c85518Srobert bool ModulePrivate =
615*12c85518Srobert (ModuleOwnership == Decl::ModuleOwnershipKind::ModulePrivate);
616e5dd7070Spatrick
617e5dd7070Spatrick // Determine whether this declaration is part of a (sub)module. If so, it
618e5dd7070Spatrick // may not yet be visible.
619e5dd7070Spatrick if (unsigned SubmoduleID = readSubmoduleID()) {
620*12c85518Srobert
621*12c85518Srobert switch (ModuleOwnership) {
622*12c85518Srobert case Decl::ModuleOwnershipKind::Visible:
623*12c85518Srobert ModuleOwnership = Decl::ModuleOwnershipKind::VisibleWhenImported;
624*12c85518Srobert break;
625*12c85518Srobert case Decl::ModuleOwnershipKind::Unowned:
626*12c85518Srobert case Decl::ModuleOwnershipKind::VisibleWhenImported:
627*12c85518Srobert case Decl::ModuleOwnershipKind::ReachableWhenImported:
628*12c85518Srobert case Decl::ModuleOwnershipKind::ModulePrivate:
629*12c85518Srobert break;
630*12c85518Srobert }
631*12c85518Srobert
632*12c85518Srobert D->setModuleOwnershipKind(ModuleOwnership);
633e5dd7070Spatrick // Store the owning submodule ID in the declaration.
634e5dd7070Spatrick D->setOwningModuleID(SubmoduleID);
635e5dd7070Spatrick
636e5dd7070Spatrick if (ModulePrivate) {
637e5dd7070Spatrick // Module-private declarations are never visible, so there is no work to
638e5dd7070Spatrick // do.
639e5dd7070Spatrick } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
640e5dd7070Spatrick // If local visibility is being tracked, this declaration will become
641e5dd7070Spatrick // hidden and visible as the owning module does.
642e5dd7070Spatrick } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
643e5dd7070Spatrick // Mark the declaration as visible when its owning module becomes visible.
644e5dd7070Spatrick if (Owner->NameVisibility == Module::AllVisible)
645e5dd7070Spatrick D->setVisibleDespiteOwningModule();
646e5dd7070Spatrick else
647e5dd7070Spatrick Reader.HiddenNamesMap[Owner].push_back(D);
648e5dd7070Spatrick }
649e5dd7070Spatrick } else if (ModulePrivate) {
650e5dd7070Spatrick D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
651e5dd7070Spatrick }
652e5dd7070Spatrick }
653e5dd7070Spatrick
VisitPragmaCommentDecl(PragmaCommentDecl * D)654e5dd7070Spatrick void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
655e5dd7070Spatrick VisitDecl(D);
656e5dd7070Spatrick D->setLocation(readSourceLocation());
657e5dd7070Spatrick D->CommentKind = (PragmaMSCommentKind)Record.readInt();
658e5dd7070Spatrick std::string Arg = readString();
659e5dd7070Spatrick memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
660e5dd7070Spatrick D->getTrailingObjects<char>()[Arg.size()] = '\0';
661e5dd7070Spatrick }
662e5dd7070Spatrick
VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl * D)663e5dd7070Spatrick void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
664e5dd7070Spatrick VisitDecl(D);
665e5dd7070Spatrick D->setLocation(readSourceLocation());
666e5dd7070Spatrick std::string Name = readString();
667e5dd7070Spatrick memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
668e5dd7070Spatrick D->getTrailingObjects<char>()[Name.size()] = '\0';
669e5dd7070Spatrick
670e5dd7070Spatrick D->ValueStart = Name.size() + 1;
671e5dd7070Spatrick std::string Value = readString();
672e5dd7070Spatrick memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
673e5dd7070Spatrick Value.size());
674e5dd7070Spatrick D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
675e5dd7070Spatrick }
676e5dd7070Spatrick
VisitTranslationUnitDecl(TranslationUnitDecl * TU)677e5dd7070Spatrick void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
678e5dd7070Spatrick llvm_unreachable("Translation units are not serialized");
679e5dd7070Spatrick }
680e5dd7070Spatrick
VisitNamedDecl(NamedDecl * ND)681e5dd7070Spatrick void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
682e5dd7070Spatrick VisitDecl(ND);
683e5dd7070Spatrick ND->setDeclName(Record.readDeclarationName());
684e5dd7070Spatrick AnonymousDeclNumber = Record.readInt();
685e5dd7070Spatrick }
686e5dd7070Spatrick
VisitTypeDecl(TypeDecl * TD)687e5dd7070Spatrick void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
688e5dd7070Spatrick VisitNamedDecl(TD);
689e5dd7070Spatrick TD->setLocStart(readSourceLocation());
690e5dd7070Spatrick // Delay type reading until after we have fully initialized the decl.
691e5dd7070Spatrick DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
692e5dd7070Spatrick }
693e5dd7070Spatrick
694e5dd7070Spatrick ASTDeclReader::RedeclarableResult
VisitTypedefNameDecl(TypedefNameDecl * TD)695e5dd7070Spatrick ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
696e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(TD);
697e5dd7070Spatrick VisitTypeDecl(TD);
698e5dd7070Spatrick TypeSourceInfo *TInfo = readTypeSourceInfo();
699e5dd7070Spatrick if (Record.readInt()) { // isModed
700e5dd7070Spatrick QualType modedT = Record.readType();
701e5dd7070Spatrick TD->setModedTypeSourceInfo(TInfo, modedT);
702e5dd7070Spatrick } else
703e5dd7070Spatrick TD->setTypeSourceInfo(TInfo);
704e5dd7070Spatrick // Read and discard the declaration for which this is a typedef name for
705e5dd7070Spatrick // linkage, if it exists. We cannot rely on our type to pull in this decl,
706e5dd7070Spatrick // because it might have been merged with a type from another module and
707e5dd7070Spatrick // thus might not refer to our version of the declaration.
708e5dd7070Spatrick readDecl();
709e5dd7070Spatrick return Redecl;
710e5dd7070Spatrick }
711e5dd7070Spatrick
VisitTypedefDecl(TypedefDecl * TD)712e5dd7070Spatrick void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
713e5dd7070Spatrick RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
714e5dd7070Spatrick mergeRedeclarable(TD, Redecl);
715e5dd7070Spatrick }
716e5dd7070Spatrick
VisitTypeAliasDecl(TypeAliasDecl * TD)717e5dd7070Spatrick void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
718e5dd7070Spatrick RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
719e5dd7070Spatrick if (auto *Template = readDeclAs<TypeAliasTemplateDecl>())
720e5dd7070Spatrick // Merged when we merge the template.
721e5dd7070Spatrick TD->setDescribedAliasTemplate(Template);
722e5dd7070Spatrick else
723e5dd7070Spatrick mergeRedeclarable(TD, Redecl);
724e5dd7070Spatrick }
725e5dd7070Spatrick
VisitTagDecl(TagDecl * TD)726e5dd7070Spatrick ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
727e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(TD);
728e5dd7070Spatrick VisitTypeDecl(TD);
729e5dd7070Spatrick
730e5dd7070Spatrick TD->IdentifierNamespace = Record.readInt();
731e5dd7070Spatrick TD->setTagKind((TagDecl::TagKind)Record.readInt());
732e5dd7070Spatrick if (!isa<CXXRecordDecl>(TD))
733e5dd7070Spatrick TD->setCompleteDefinition(Record.readInt());
734e5dd7070Spatrick TD->setEmbeddedInDeclarator(Record.readInt());
735e5dd7070Spatrick TD->setFreeStanding(Record.readInt());
736e5dd7070Spatrick TD->setCompleteDefinitionRequired(Record.readInt());
737e5dd7070Spatrick TD->setBraceRange(readSourceRange());
738e5dd7070Spatrick
739e5dd7070Spatrick switch (Record.readInt()) {
740e5dd7070Spatrick case 0:
741e5dd7070Spatrick break;
742e5dd7070Spatrick case 1: { // ExtInfo
743e5dd7070Spatrick auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
744e5dd7070Spatrick Record.readQualifierInfo(*Info);
745e5dd7070Spatrick TD->TypedefNameDeclOrQualifier = Info;
746e5dd7070Spatrick break;
747e5dd7070Spatrick }
748e5dd7070Spatrick case 2: // TypedefNameForAnonDecl
749e5dd7070Spatrick NamedDeclForTagDecl = readDeclID();
750e5dd7070Spatrick TypedefNameForLinkage = Record.readIdentifier();
751e5dd7070Spatrick break;
752e5dd7070Spatrick default:
753e5dd7070Spatrick llvm_unreachable("unexpected tag info kind");
754e5dd7070Spatrick }
755e5dd7070Spatrick
756e5dd7070Spatrick if (!isa<CXXRecordDecl>(TD))
757e5dd7070Spatrick mergeRedeclarable(TD, Redecl);
758e5dd7070Spatrick return Redecl;
759e5dd7070Spatrick }
760e5dd7070Spatrick
VisitEnumDecl(EnumDecl * ED)761e5dd7070Spatrick void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
762e5dd7070Spatrick VisitTagDecl(ED);
763e5dd7070Spatrick if (TypeSourceInfo *TI = readTypeSourceInfo())
764e5dd7070Spatrick ED->setIntegerTypeSourceInfo(TI);
765e5dd7070Spatrick else
766e5dd7070Spatrick ED->setIntegerType(Record.readType());
767e5dd7070Spatrick ED->setPromotionType(Record.readType());
768e5dd7070Spatrick ED->setNumPositiveBits(Record.readInt());
769e5dd7070Spatrick ED->setNumNegativeBits(Record.readInt());
770e5dd7070Spatrick ED->setScoped(Record.readInt());
771e5dd7070Spatrick ED->setScopedUsingClassTag(Record.readInt());
772e5dd7070Spatrick ED->setFixed(Record.readInt());
773e5dd7070Spatrick
774e5dd7070Spatrick ED->setHasODRHash(true);
775e5dd7070Spatrick ED->ODRHash = Record.readInt();
776e5dd7070Spatrick
777e5dd7070Spatrick // If this is a definition subject to the ODR, and we already have a
778e5dd7070Spatrick // definition, merge this one into it.
779e5dd7070Spatrick if (ED->isCompleteDefinition() &&
780e5dd7070Spatrick Reader.getContext().getLangOpts().Modules &&
781e5dd7070Spatrick Reader.getContext().getLangOpts().CPlusPlus) {
782e5dd7070Spatrick EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
783e5dd7070Spatrick if (!OldDef) {
784e5dd7070Spatrick // This is the first time we've seen an imported definition. Look for a
785e5dd7070Spatrick // local definition before deciding that we are the first definition.
786e5dd7070Spatrick for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
787e5dd7070Spatrick if (!D->isFromASTFile() && D->isCompleteDefinition()) {
788e5dd7070Spatrick OldDef = D;
789e5dd7070Spatrick break;
790e5dd7070Spatrick }
791e5dd7070Spatrick }
792e5dd7070Spatrick }
793e5dd7070Spatrick if (OldDef) {
794e5dd7070Spatrick Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
795*12c85518Srobert ED->demoteThisDefinitionToDeclaration();
796e5dd7070Spatrick Reader.mergeDefinitionVisibility(OldDef, ED);
797e5dd7070Spatrick if (OldDef->getODRHash() != ED->getODRHash())
798e5dd7070Spatrick Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
799e5dd7070Spatrick } else {
800e5dd7070Spatrick OldDef = ED;
801e5dd7070Spatrick }
802e5dd7070Spatrick }
803e5dd7070Spatrick
804e5dd7070Spatrick if (auto *InstED = readDeclAs<EnumDecl>()) {
805e5dd7070Spatrick auto TSK = (TemplateSpecializationKind)Record.readInt();
806e5dd7070Spatrick SourceLocation POI = readSourceLocation();
807e5dd7070Spatrick ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
808e5dd7070Spatrick ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
809e5dd7070Spatrick }
810e5dd7070Spatrick }
811e5dd7070Spatrick
812e5dd7070Spatrick ASTDeclReader::RedeclarableResult
VisitRecordDeclImpl(RecordDecl * RD)813e5dd7070Spatrick ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
814e5dd7070Spatrick RedeclarableResult Redecl = VisitTagDecl(RD);
815e5dd7070Spatrick RD->setHasFlexibleArrayMember(Record.readInt());
816e5dd7070Spatrick RD->setAnonymousStructOrUnion(Record.readInt());
817e5dd7070Spatrick RD->setHasObjectMember(Record.readInt());
818e5dd7070Spatrick RD->setHasVolatileMember(Record.readInt());
819e5dd7070Spatrick RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
820e5dd7070Spatrick RD->setNonTrivialToPrimitiveCopy(Record.readInt());
821e5dd7070Spatrick RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
822e5dd7070Spatrick RD->setHasNonTrivialToPrimitiveDefaultInitializeCUnion(Record.readInt());
823e5dd7070Spatrick RD->setHasNonTrivialToPrimitiveDestructCUnion(Record.readInt());
824e5dd7070Spatrick RD->setHasNonTrivialToPrimitiveCopyCUnion(Record.readInt());
825e5dd7070Spatrick RD->setParamDestroyedInCallee(Record.readInt());
826e5dd7070Spatrick RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
827e5dd7070Spatrick return Redecl;
828e5dd7070Spatrick }
829e5dd7070Spatrick
VisitRecordDecl(RecordDecl * RD)830*12c85518Srobert void ASTDeclReader::VisitRecordDecl(RecordDecl *RD) {
831*12c85518Srobert VisitRecordDeclImpl(RD);
832*12c85518Srobert RD->setODRHash(Record.readInt());
833*12c85518Srobert
834*12c85518Srobert // Maintain the invariant of a redeclaration chain containing only
835*12c85518Srobert // a single definition.
836*12c85518Srobert if (RD->isCompleteDefinition()) {
837*12c85518Srobert RecordDecl *Canon = static_cast<RecordDecl *>(RD->getCanonicalDecl());
838*12c85518Srobert RecordDecl *&OldDef = Reader.RecordDefinitions[Canon];
839*12c85518Srobert if (!OldDef) {
840*12c85518Srobert // This is the first time we've seen an imported definition. Look for a
841*12c85518Srobert // local definition before deciding that we are the first definition.
842*12c85518Srobert for (auto *D : merged_redecls(Canon)) {
843*12c85518Srobert if (!D->isFromASTFile() && D->isCompleteDefinition()) {
844*12c85518Srobert OldDef = D;
845*12c85518Srobert break;
846*12c85518Srobert }
847*12c85518Srobert }
848*12c85518Srobert }
849*12c85518Srobert if (OldDef) {
850*12c85518Srobert Reader.MergedDeclContexts.insert(std::make_pair(RD, OldDef));
851*12c85518Srobert RD->demoteThisDefinitionToDeclaration();
852*12c85518Srobert Reader.mergeDefinitionVisibility(OldDef, RD);
853*12c85518Srobert if (OldDef->getODRHash() != RD->getODRHash())
854*12c85518Srobert Reader.PendingRecordOdrMergeFailures[OldDef].push_back(RD);
855*12c85518Srobert } else {
856*12c85518Srobert OldDef = RD;
857*12c85518Srobert }
858*12c85518Srobert }
859*12c85518Srobert }
860*12c85518Srobert
VisitValueDecl(ValueDecl * VD)861e5dd7070Spatrick void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
862e5dd7070Spatrick VisitNamedDecl(VD);
863e5dd7070Spatrick // For function declarations, defer reading the type in case the function has
864e5dd7070Spatrick // a deduced return type that references an entity declared within the
865e5dd7070Spatrick // function.
866e5dd7070Spatrick if (isa<FunctionDecl>(VD))
867e5dd7070Spatrick DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
868e5dd7070Spatrick else
869e5dd7070Spatrick VD->setType(Record.readType());
870e5dd7070Spatrick }
871e5dd7070Spatrick
VisitEnumConstantDecl(EnumConstantDecl * ECD)872e5dd7070Spatrick void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
873e5dd7070Spatrick VisitValueDecl(ECD);
874e5dd7070Spatrick if (Record.readInt())
875e5dd7070Spatrick ECD->setInitExpr(Record.readExpr());
876e5dd7070Spatrick ECD->setInitVal(Record.readAPSInt());
877e5dd7070Spatrick mergeMergeable(ECD);
878e5dd7070Spatrick }
879e5dd7070Spatrick
VisitDeclaratorDecl(DeclaratorDecl * DD)880e5dd7070Spatrick void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
881e5dd7070Spatrick VisitValueDecl(DD);
882e5dd7070Spatrick DD->setInnerLocStart(readSourceLocation());
883e5dd7070Spatrick if (Record.readInt()) { // hasExtInfo
884e5dd7070Spatrick auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
885e5dd7070Spatrick Record.readQualifierInfo(*Info);
886e5dd7070Spatrick Info->TrailingRequiresClause = Record.readExpr();
887e5dd7070Spatrick DD->DeclInfo = Info;
888e5dd7070Spatrick }
889e5dd7070Spatrick QualType TSIType = Record.readType();
890e5dd7070Spatrick DD->setTypeSourceInfo(
891e5dd7070Spatrick TSIType.isNull() ? nullptr
892e5dd7070Spatrick : Reader.getContext().CreateTypeSourceInfo(TSIType));
893e5dd7070Spatrick }
894e5dd7070Spatrick
VisitFunctionDecl(FunctionDecl * FD)895e5dd7070Spatrick void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
896e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(FD);
897e5dd7070Spatrick
898*12c85518Srobert FunctionDecl *Existing = nullptr;
899e5dd7070Spatrick
900e5dd7070Spatrick switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
901e5dd7070Spatrick case FunctionDecl::TK_NonTemplate:
902e5dd7070Spatrick break;
903*12c85518Srobert case FunctionDecl::TK_DependentNonTemplate:
904*12c85518Srobert FD->setInstantiatedFromDecl(readDeclAs<FunctionDecl>());
905e5dd7070Spatrick break;
906*12c85518Srobert case FunctionDecl::TK_FunctionTemplate: {
907*12c85518Srobert auto *Template = readDeclAs<FunctionTemplateDecl>();
908*12c85518Srobert Template->init(FD);
909*12c85518Srobert FD->setDescribedFunctionTemplate(Template);
910*12c85518Srobert break;
911*12c85518Srobert }
912e5dd7070Spatrick case FunctionDecl::TK_MemberSpecialization: {
913e5dd7070Spatrick auto *InstFD = readDeclAs<FunctionDecl>();
914e5dd7070Spatrick auto TSK = (TemplateSpecializationKind)Record.readInt();
915e5dd7070Spatrick SourceLocation POI = readSourceLocation();
916e5dd7070Spatrick FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
917e5dd7070Spatrick FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
918e5dd7070Spatrick break;
919e5dd7070Spatrick }
920e5dd7070Spatrick case FunctionDecl::TK_FunctionTemplateSpecialization: {
921e5dd7070Spatrick auto *Template = readDeclAs<FunctionTemplateDecl>();
922e5dd7070Spatrick auto TSK = (TemplateSpecializationKind)Record.readInt();
923e5dd7070Spatrick
924e5dd7070Spatrick // Template arguments.
925e5dd7070Spatrick SmallVector<TemplateArgument, 8> TemplArgs;
926e5dd7070Spatrick Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
927e5dd7070Spatrick
928e5dd7070Spatrick // Template args as written.
929e5dd7070Spatrick SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
930e5dd7070Spatrick SourceLocation LAngleLoc, RAngleLoc;
931e5dd7070Spatrick bool HasTemplateArgumentsAsWritten = Record.readInt();
932e5dd7070Spatrick if (HasTemplateArgumentsAsWritten) {
933e5dd7070Spatrick unsigned NumTemplateArgLocs = Record.readInt();
934e5dd7070Spatrick TemplArgLocs.reserve(NumTemplateArgLocs);
935e5dd7070Spatrick for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
936e5dd7070Spatrick TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
937e5dd7070Spatrick
938e5dd7070Spatrick LAngleLoc = readSourceLocation();
939e5dd7070Spatrick RAngleLoc = readSourceLocation();
940e5dd7070Spatrick }
941e5dd7070Spatrick
942e5dd7070Spatrick SourceLocation POI = readSourceLocation();
943e5dd7070Spatrick
944e5dd7070Spatrick ASTContext &C = Reader.getContext();
945e5dd7070Spatrick TemplateArgumentList *TemplArgList
946e5dd7070Spatrick = TemplateArgumentList::CreateCopy(C, TemplArgs);
947e5dd7070Spatrick TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
948e5dd7070Spatrick for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
949e5dd7070Spatrick TemplArgsInfo.addArgument(TemplArgLocs[i]);
950e5dd7070Spatrick
951e5dd7070Spatrick MemberSpecializationInfo *MSInfo = nullptr;
952e5dd7070Spatrick if (Record.readInt()) {
953e5dd7070Spatrick auto *FD = readDeclAs<FunctionDecl>();
954e5dd7070Spatrick auto TSK = (TemplateSpecializationKind)Record.readInt();
955e5dd7070Spatrick SourceLocation POI = readSourceLocation();
956e5dd7070Spatrick
957e5dd7070Spatrick MSInfo = new (C) MemberSpecializationInfo(FD, TSK);
958e5dd7070Spatrick MSInfo->setPointOfInstantiation(POI);
959e5dd7070Spatrick }
960e5dd7070Spatrick
961e5dd7070Spatrick FunctionTemplateSpecializationInfo *FTInfo =
962e5dd7070Spatrick FunctionTemplateSpecializationInfo::Create(
963e5dd7070Spatrick C, FD, Template, TSK, TemplArgList,
964e5dd7070Spatrick HasTemplateArgumentsAsWritten ? &TemplArgsInfo : nullptr, POI,
965e5dd7070Spatrick MSInfo);
966e5dd7070Spatrick FD->TemplateOrSpecialization = FTInfo;
967e5dd7070Spatrick
968e5dd7070Spatrick if (FD->isCanonicalDecl()) { // if canonical add to template's set.
969e5dd7070Spatrick // The template that contains the specializations set. It's not safe to
970e5dd7070Spatrick // use getCanonicalDecl on Template since it may still be initializing.
971e5dd7070Spatrick auto *CanonTemplate = readDeclAs<FunctionTemplateDecl>();
972e5dd7070Spatrick // Get the InsertPos by FindNodeOrInsertPos() instead of calling
973e5dd7070Spatrick // InsertNode(FTInfo) directly to avoid the getASTContext() call in
974e5dd7070Spatrick // FunctionTemplateSpecializationInfo's Profile().
975e5dd7070Spatrick // We avoid getASTContext because a decl in the parent hierarchy may
976e5dd7070Spatrick // be initializing.
977e5dd7070Spatrick llvm::FoldingSetNodeID ID;
978e5dd7070Spatrick FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
979e5dd7070Spatrick void *InsertPos = nullptr;
980e5dd7070Spatrick FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
981e5dd7070Spatrick FunctionTemplateSpecializationInfo *ExistingInfo =
982e5dd7070Spatrick CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
983e5dd7070Spatrick if (InsertPos)
984e5dd7070Spatrick CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
985e5dd7070Spatrick else {
986e5dd7070Spatrick assert(Reader.getContext().getLangOpts().Modules &&
987e5dd7070Spatrick "already deserialized this template specialization");
988*12c85518Srobert Existing = ExistingInfo->getFunction();
989e5dd7070Spatrick }
990e5dd7070Spatrick }
991e5dd7070Spatrick break;
992e5dd7070Spatrick }
993e5dd7070Spatrick case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
994e5dd7070Spatrick // Templates.
995e5dd7070Spatrick UnresolvedSet<8> TemplDecls;
996e5dd7070Spatrick unsigned NumTemplates = Record.readInt();
997e5dd7070Spatrick while (NumTemplates--)
998e5dd7070Spatrick TemplDecls.addDecl(readDeclAs<NamedDecl>());
999e5dd7070Spatrick
1000e5dd7070Spatrick // Templates args.
1001e5dd7070Spatrick TemplateArgumentListInfo TemplArgs;
1002e5dd7070Spatrick unsigned NumArgs = Record.readInt();
1003e5dd7070Spatrick while (NumArgs--)
1004e5dd7070Spatrick TemplArgs.addArgument(Record.readTemplateArgumentLoc());
1005e5dd7070Spatrick TemplArgs.setLAngleLoc(readSourceLocation());
1006e5dd7070Spatrick TemplArgs.setRAngleLoc(readSourceLocation());
1007e5dd7070Spatrick
1008e5dd7070Spatrick FD->setDependentTemplateSpecialization(Reader.getContext(),
1009e5dd7070Spatrick TemplDecls, TemplArgs);
1010e5dd7070Spatrick // These are not merged; we don't need to merge redeclarations of dependent
1011e5dd7070Spatrick // template friends.
1012e5dd7070Spatrick break;
1013e5dd7070Spatrick }
1014e5dd7070Spatrick }
1015e5dd7070Spatrick
1016*12c85518Srobert VisitDeclaratorDecl(FD);
1017*12c85518Srobert
1018*12c85518Srobert // Attach a type to this function. Use the real type if possible, but fall
1019*12c85518Srobert // back to the type as written if it involves a deduced return type.
1020*12c85518Srobert if (FD->getTypeSourceInfo() && FD->getTypeSourceInfo()
1021*12c85518Srobert ->getType()
1022*12c85518Srobert ->castAs<FunctionType>()
1023*12c85518Srobert ->getReturnType()
1024*12c85518Srobert ->getContainedAutoType()) {
1025*12c85518Srobert // We'll set up the real type in Visit, once we've finished loading the
1026*12c85518Srobert // function.
1027*12c85518Srobert FD->setType(FD->getTypeSourceInfo()->getType());
1028*12c85518Srobert Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
1029*12c85518Srobert } else {
1030*12c85518Srobert FD->setType(Reader.GetType(DeferredTypeID));
1031*12c85518Srobert }
1032*12c85518Srobert DeferredTypeID = 0;
1033*12c85518Srobert
1034*12c85518Srobert FD->DNLoc = Record.readDeclarationNameLoc(FD->getDeclName());
1035*12c85518Srobert FD->IdentifierNamespace = Record.readInt();
1036*12c85518Srobert
1037*12c85518Srobert // FunctionDecl's body is handled last at ASTDeclReader::Visit,
1038*12c85518Srobert // after everything else is read.
1039*12c85518Srobert
1040*12c85518Srobert FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
1041*12c85518Srobert FD->setInlineSpecified(Record.readInt());
1042*12c85518Srobert FD->setImplicitlyInline(Record.readInt());
1043*12c85518Srobert FD->setVirtualAsWritten(Record.readInt());
1044*12c85518Srobert // We defer calling `FunctionDecl::setPure()` here as for methods of
1045*12c85518Srobert // `CXXTemplateSpecializationDecl`s, we may not have connected up the
1046*12c85518Srobert // definition (which is required for `setPure`).
1047*12c85518Srobert const bool Pure = Record.readInt();
1048*12c85518Srobert FD->setHasInheritedPrototype(Record.readInt());
1049*12c85518Srobert FD->setHasWrittenPrototype(Record.readInt());
1050*12c85518Srobert FD->setDeletedAsWritten(Record.readInt());
1051*12c85518Srobert FD->setTrivial(Record.readInt());
1052*12c85518Srobert FD->setTrivialForCall(Record.readInt());
1053*12c85518Srobert FD->setDefaulted(Record.readInt());
1054*12c85518Srobert FD->setExplicitlyDefaulted(Record.readInt());
1055*12c85518Srobert FD->setIneligibleOrNotSelected(Record.readInt());
1056*12c85518Srobert FD->setHasImplicitReturnZero(Record.readInt());
1057*12c85518Srobert FD->setConstexprKind(static_cast<ConstexprSpecKind>(Record.readInt()));
1058*12c85518Srobert FD->setUsesSEHTry(Record.readInt());
1059*12c85518Srobert FD->setHasSkippedBody(Record.readInt());
1060*12c85518Srobert FD->setIsMultiVersion(Record.readInt());
1061*12c85518Srobert FD->setLateTemplateParsed(Record.readInt());
1062*12c85518Srobert FD->setFriendConstraintRefersToEnclosingTemplate(Record.readInt());
1063*12c85518Srobert
1064*12c85518Srobert FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
1065*12c85518Srobert FD->EndRangeLoc = readSourceLocation();
1066*12c85518Srobert FD->setDefaultLoc(readSourceLocation());
1067*12c85518Srobert
1068*12c85518Srobert FD->ODRHash = Record.readInt();
1069*12c85518Srobert FD->setHasODRHash(true);
1070*12c85518Srobert
1071*12c85518Srobert if (FD->isDefaulted()) {
1072*12c85518Srobert if (unsigned NumLookups = Record.readInt()) {
1073*12c85518Srobert SmallVector<DeclAccessPair, 8> Lookups;
1074*12c85518Srobert for (unsigned I = 0; I != NumLookups; ++I) {
1075*12c85518Srobert NamedDecl *ND = Record.readDeclAs<NamedDecl>();
1076*12c85518Srobert AccessSpecifier AS = (AccessSpecifier)Record.readInt();
1077*12c85518Srobert Lookups.push_back(DeclAccessPair::make(ND, AS));
1078*12c85518Srobert }
1079*12c85518Srobert FD->setDefaultedFunctionInfo(FunctionDecl::DefaultedFunctionInfo::Create(
1080*12c85518Srobert Reader.getContext(), Lookups));
1081*12c85518Srobert }
1082*12c85518Srobert }
1083*12c85518Srobert
1084*12c85518Srobert if (Existing)
1085*12c85518Srobert mergeRedeclarable(FD, Existing, Redecl);
1086*12c85518Srobert else if (auto Kind = FD->getTemplatedKind();
1087*12c85518Srobert Kind == FunctionDecl::TK_FunctionTemplate ||
1088*12c85518Srobert Kind == FunctionDecl::TK_FunctionTemplateSpecialization) {
1089*12c85518Srobert // Function Templates have their FunctionTemplateDecls merged instead of
1090*12c85518Srobert // their FunctionDecls.
1091*12c85518Srobert auto merge = [this, &Redecl, FD](auto &&F) {
1092*12c85518Srobert auto *Existing = cast_or_null<FunctionDecl>(Redecl.getKnownMergeTarget());
1093*12c85518Srobert RedeclarableResult NewRedecl(Existing ? F(Existing) : nullptr,
1094*12c85518Srobert Redecl.getFirstID(), Redecl.isKeyDecl());
1095*12c85518Srobert mergeRedeclarableTemplate(F(FD), NewRedecl);
1096*12c85518Srobert };
1097*12c85518Srobert if (Kind == FunctionDecl::TK_FunctionTemplate)
1098*12c85518Srobert merge(
1099*12c85518Srobert [](FunctionDecl *FD) { return FD->getDescribedFunctionTemplate(); });
1100*12c85518Srobert else
1101*12c85518Srobert merge([](FunctionDecl *FD) {
1102*12c85518Srobert return FD->getTemplateSpecializationInfo()->getTemplate();
1103*12c85518Srobert });
1104*12c85518Srobert } else
1105*12c85518Srobert mergeRedeclarable(FD, Redecl);
1106*12c85518Srobert
1107a9ac8606Spatrick // Defer calling `setPure` until merging above has guaranteed we've set
1108a9ac8606Spatrick // `DefinitionData` (as this will need to access it).
1109a9ac8606Spatrick FD->setPure(Pure);
1110a9ac8606Spatrick
1111e5dd7070Spatrick // Read in the parameters.
1112e5dd7070Spatrick unsigned NumParams = Record.readInt();
1113e5dd7070Spatrick SmallVector<ParmVarDecl *, 16> Params;
1114e5dd7070Spatrick Params.reserve(NumParams);
1115e5dd7070Spatrick for (unsigned I = 0; I != NumParams; ++I)
1116e5dd7070Spatrick Params.push_back(readDeclAs<ParmVarDecl>());
1117e5dd7070Spatrick FD->setParams(Reader.getContext(), Params);
1118e5dd7070Spatrick }
1119e5dd7070Spatrick
VisitObjCMethodDecl(ObjCMethodDecl * MD)1120e5dd7070Spatrick void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
1121e5dd7070Spatrick VisitNamedDecl(MD);
1122e5dd7070Spatrick if (Record.readInt()) {
1123e5dd7070Spatrick // Load the body on-demand. Most clients won't care, because method
1124e5dd7070Spatrick // definitions rarely show up in headers.
1125e5dd7070Spatrick Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1126e5dd7070Spatrick HasPendingBody = true;
1127e5dd7070Spatrick }
1128e5dd7070Spatrick MD->setSelfDecl(readDeclAs<ImplicitParamDecl>());
1129e5dd7070Spatrick MD->setCmdDecl(readDeclAs<ImplicitParamDecl>());
1130e5dd7070Spatrick MD->setInstanceMethod(Record.readInt());
1131e5dd7070Spatrick MD->setVariadic(Record.readInt());
1132e5dd7070Spatrick MD->setPropertyAccessor(Record.readInt());
1133e5dd7070Spatrick MD->setSynthesizedAccessorStub(Record.readInt());
1134e5dd7070Spatrick MD->setDefined(Record.readInt());
1135e5dd7070Spatrick MD->setOverriding(Record.readInt());
1136e5dd7070Spatrick MD->setHasSkippedBody(Record.readInt());
1137e5dd7070Spatrick
1138e5dd7070Spatrick MD->setIsRedeclaration(Record.readInt());
1139e5dd7070Spatrick MD->setHasRedeclaration(Record.readInt());
1140e5dd7070Spatrick if (MD->hasRedeclaration())
1141e5dd7070Spatrick Reader.getContext().setObjCMethodRedeclaration(MD,
1142e5dd7070Spatrick readDeclAs<ObjCMethodDecl>());
1143e5dd7070Spatrick
1144e5dd7070Spatrick MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1145e5dd7070Spatrick MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
1146e5dd7070Spatrick MD->setRelatedResultType(Record.readInt());
1147e5dd7070Spatrick MD->setReturnType(Record.readType());
1148e5dd7070Spatrick MD->setReturnTypeSourceInfo(readTypeSourceInfo());
1149e5dd7070Spatrick MD->DeclEndLoc = readSourceLocation();
1150e5dd7070Spatrick unsigned NumParams = Record.readInt();
1151e5dd7070Spatrick SmallVector<ParmVarDecl *, 16> Params;
1152e5dd7070Spatrick Params.reserve(NumParams);
1153e5dd7070Spatrick for (unsigned I = 0; I != NumParams; ++I)
1154e5dd7070Spatrick Params.push_back(readDeclAs<ParmVarDecl>());
1155e5dd7070Spatrick
1156e5dd7070Spatrick MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
1157e5dd7070Spatrick unsigned NumStoredSelLocs = Record.readInt();
1158e5dd7070Spatrick SmallVector<SourceLocation, 16> SelLocs;
1159e5dd7070Spatrick SelLocs.reserve(NumStoredSelLocs);
1160e5dd7070Spatrick for (unsigned i = 0; i != NumStoredSelLocs; ++i)
1161e5dd7070Spatrick SelLocs.push_back(readSourceLocation());
1162e5dd7070Spatrick
1163e5dd7070Spatrick MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
1164e5dd7070Spatrick }
1165e5dd7070Spatrick
VisitObjCTypeParamDecl(ObjCTypeParamDecl * D)1166e5dd7070Spatrick void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
1167e5dd7070Spatrick VisitTypedefNameDecl(D);
1168e5dd7070Spatrick
1169e5dd7070Spatrick D->Variance = Record.readInt();
1170e5dd7070Spatrick D->Index = Record.readInt();
1171e5dd7070Spatrick D->VarianceLoc = readSourceLocation();
1172e5dd7070Spatrick D->ColonLoc = readSourceLocation();
1173e5dd7070Spatrick }
1174e5dd7070Spatrick
VisitObjCContainerDecl(ObjCContainerDecl * CD)1175e5dd7070Spatrick void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
1176e5dd7070Spatrick VisitNamedDecl(CD);
1177e5dd7070Spatrick CD->setAtStartLoc(readSourceLocation());
1178e5dd7070Spatrick CD->setAtEndRange(readSourceRange());
1179e5dd7070Spatrick }
1180e5dd7070Spatrick
ReadObjCTypeParamList()1181e5dd7070Spatrick ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
1182e5dd7070Spatrick unsigned numParams = Record.readInt();
1183e5dd7070Spatrick if (numParams == 0)
1184e5dd7070Spatrick return nullptr;
1185e5dd7070Spatrick
1186e5dd7070Spatrick SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1187e5dd7070Spatrick typeParams.reserve(numParams);
1188e5dd7070Spatrick for (unsigned i = 0; i != numParams; ++i) {
1189e5dd7070Spatrick auto *typeParam = readDeclAs<ObjCTypeParamDecl>();
1190e5dd7070Spatrick if (!typeParam)
1191e5dd7070Spatrick return nullptr;
1192e5dd7070Spatrick
1193e5dd7070Spatrick typeParams.push_back(typeParam);
1194e5dd7070Spatrick }
1195e5dd7070Spatrick
1196e5dd7070Spatrick SourceLocation lAngleLoc = readSourceLocation();
1197e5dd7070Spatrick SourceLocation rAngleLoc = readSourceLocation();
1198e5dd7070Spatrick
1199e5dd7070Spatrick return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1200e5dd7070Spatrick typeParams, rAngleLoc);
1201e5dd7070Spatrick }
1202e5dd7070Spatrick
ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData & Data)1203e5dd7070Spatrick void ASTDeclReader::ReadObjCDefinitionData(
1204e5dd7070Spatrick struct ObjCInterfaceDecl::DefinitionData &Data) {
1205e5dd7070Spatrick // Read the superclass.
1206e5dd7070Spatrick Data.SuperClassTInfo = readTypeSourceInfo();
1207e5dd7070Spatrick
1208e5dd7070Spatrick Data.EndLoc = readSourceLocation();
1209e5dd7070Spatrick Data.HasDesignatedInitializers = Record.readInt();
1210*12c85518Srobert Data.ODRHash = Record.readInt();
1211*12c85518Srobert Data.HasODRHash = true;
1212e5dd7070Spatrick
1213e5dd7070Spatrick // Read the directly referenced protocols and their SourceLocations.
1214e5dd7070Spatrick unsigned NumProtocols = Record.readInt();
1215e5dd7070Spatrick SmallVector<ObjCProtocolDecl *, 16> Protocols;
1216e5dd7070Spatrick Protocols.reserve(NumProtocols);
1217e5dd7070Spatrick for (unsigned I = 0; I != NumProtocols; ++I)
1218e5dd7070Spatrick Protocols.push_back(readDeclAs<ObjCProtocolDecl>());
1219e5dd7070Spatrick SmallVector<SourceLocation, 16> ProtoLocs;
1220e5dd7070Spatrick ProtoLocs.reserve(NumProtocols);
1221e5dd7070Spatrick for (unsigned I = 0; I != NumProtocols; ++I)
1222e5dd7070Spatrick ProtoLocs.push_back(readSourceLocation());
1223e5dd7070Spatrick Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1224e5dd7070Spatrick Reader.getContext());
1225e5dd7070Spatrick
1226e5dd7070Spatrick // Read the transitive closure of protocols referenced by this class.
1227e5dd7070Spatrick NumProtocols = Record.readInt();
1228e5dd7070Spatrick Protocols.clear();
1229e5dd7070Spatrick Protocols.reserve(NumProtocols);
1230e5dd7070Spatrick for (unsigned I = 0; I != NumProtocols; ++I)
1231e5dd7070Spatrick Protocols.push_back(readDeclAs<ObjCProtocolDecl>());
1232e5dd7070Spatrick Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1233e5dd7070Spatrick Reader.getContext());
1234e5dd7070Spatrick }
1235e5dd7070Spatrick
MergeDefinitionData(ObjCInterfaceDecl * D,struct ObjCInterfaceDecl::DefinitionData && NewDD)1236e5dd7070Spatrick void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1237e5dd7070Spatrick struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1238*12c85518Srobert struct ObjCInterfaceDecl::DefinitionData &DD = D->data();
1239*12c85518Srobert if (DD.Definition == NewDD.Definition)
1240*12c85518Srobert return;
1241*12c85518Srobert
1242*12c85518Srobert Reader.MergedDeclContexts.insert(
1243*12c85518Srobert std::make_pair(NewDD.Definition, DD.Definition));
1244*12c85518Srobert Reader.mergeDefinitionVisibility(DD.Definition, NewDD.Definition);
1245*12c85518Srobert
1246*12c85518Srobert if (D->getODRHash() != NewDD.ODRHash)
1247*12c85518Srobert Reader.PendingObjCInterfaceOdrMergeFailures[DD.Definition].push_back(
1248*12c85518Srobert {NewDD.Definition, &NewDD});
1249e5dd7070Spatrick }
1250e5dd7070Spatrick
VisitObjCInterfaceDecl(ObjCInterfaceDecl * ID)1251e5dd7070Spatrick void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
1252e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(ID);
1253e5dd7070Spatrick VisitObjCContainerDecl(ID);
1254e5dd7070Spatrick DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
1255e5dd7070Spatrick mergeRedeclarable(ID, Redecl);
1256e5dd7070Spatrick
1257e5dd7070Spatrick ID->TypeParamList = ReadObjCTypeParamList();
1258e5dd7070Spatrick if (Record.readInt()) {
1259e5dd7070Spatrick // Read the definition.
1260e5dd7070Spatrick ID->allocateDefinitionData();
1261e5dd7070Spatrick
1262e5dd7070Spatrick ReadObjCDefinitionData(ID->data());
1263e5dd7070Spatrick ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1264e5dd7070Spatrick if (Canon->Data.getPointer()) {
1265e5dd7070Spatrick // If we already have a definition, keep the definition invariant and
1266e5dd7070Spatrick // merge the data.
1267e5dd7070Spatrick MergeDefinitionData(Canon, std::move(ID->data()));
1268e5dd7070Spatrick ID->Data = Canon->Data;
1269e5dd7070Spatrick } else {
1270e5dd7070Spatrick // Set the definition data of the canonical declaration, so other
1271e5dd7070Spatrick // redeclarations will see it.
1272e5dd7070Spatrick ID->getCanonicalDecl()->Data = ID->Data;
1273e5dd7070Spatrick
1274e5dd7070Spatrick // We will rebuild this list lazily.
1275e5dd7070Spatrick ID->setIvarList(nullptr);
1276e5dd7070Spatrick }
1277e5dd7070Spatrick
1278e5dd7070Spatrick // Note that we have deserialized a definition.
1279e5dd7070Spatrick Reader.PendingDefinitions.insert(ID);
1280e5dd7070Spatrick
1281e5dd7070Spatrick // Note that we've loaded this Objective-C class.
1282e5dd7070Spatrick Reader.ObjCClassesLoaded.push_back(ID);
1283e5dd7070Spatrick } else {
1284e5dd7070Spatrick ID->Data = ID->getCanonicalDecl()->Data;
1285e5dd7070Spatrick }
1286e5dd7070Spatrick }
1287e5dd7070Spatrick
VisitObjCIvarDecl(ObjCIvarDecl * IVD)1288e5dd7070Spatrick void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
1289e5dd7070Spatrick VisitFieldDecl(IVD);
1290e5dd7070Spatrick IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
1291e5dd7070Spatrick // This field will be built lazily.
1292e5dd7070Spatrick IVD->setNextIvar(nullptr);
1293e5dd7070Spatrick bool synth = Record.readInt();
1294e5dd7070Spatrick IVD->setSynthesize(synth);
1295*12c85518Srobert
1296*12c85518Srobert // Check ivar redeclaration.
1297*12c85518Srobert if (IVD->isInvalidDecl())
1298*12c85518Srobert return;
1299*12c85518Srobert // Don't check ObjCInterfaceDecl as interfaces are named and mismatches can be
1300*12c85518Srobert // detected in VisitObjCInterfaceDecl. Here we are looking for redeclarations
1301*12c85518Srobert // in extensions.
1302*12c85518Srobert if (isa<ObjCInterfaceDecl>(IVD->getDeclContext()))
1303*12c85518Srobert return;
1304*12c85518Srobert ObjCInterfaceDecl *CanonIntf =
1305*12c85518Srobert IVD->getContainingInterface()->getCanonicalDecl();
1306*12c85518Srobert IdentifierInfo *II = IVD->getIdentifier();
1307*12c85518Srobert ObjCIvarDecl *PrevIvar = CanonIntf->lookupInstanceVariable(II);
1308*12c85518Srobert if (PrevIvar && PrevIvar != IVD) {
1309*12c85518Srobert auto *ParentExt = dyn_cast<ObjCCategoryDecl>(IVD->getDeclContext());
1310*12c85518Srobert auto *PrevParentExt =
1311*12c85518Srobert dyn_cast<ObjCCategoryDecl>(PrevIvar->getDeclContext());
1312*12c85518Srobert if (ParentExt && PrevParentExt) {
1313*12c85518Srobert // Postpone diagnostic as we should merge identical extensions from
1314*12c85518Srobert // different modules.
1315*12c85518Srobert Reader
1316*12c85518Srobert .PendingObjCExtensionIvarRedeclarations[std::make_pair(ParentExt,
1317*12c85518Srobert PrevParentExt)]
1318*12c85518Srobert .push_back(std::make_pair(IVD, PrevIvar));
1319*12c85518Srobert } else if (ParentExt || PrevParentExt) {
1320*12c85518Srobert // Duplicate ivars in extension + implementation are never compatible.
1321*12c85518Srobert // Compatibility of implementation + implementation should be handled in
1322*12c85518Srobert // VisitObjCImplementationDecl.
1323*12c85518Srobert Reader.Diag(IVD->getLocation(), diag::err_duplicate_ivar_declaration)
1324*12c85518Srobert << II;
1325*12c85518Srobert Reader.Diag(PrevIvar->getLocation(), diag::note_previous_definition);
1326*12c85518Srobert }
1327*12c85518Srobert }
1328e5dd7070Spatrick }
1329e5dd7070Spatrick
ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData & Data)1330e5dd7070Spatrick void ASTDeclReader::ReadObjCDefinitionData(
1331e5dd7070Spatrick struct ObjCProtocolDecl::DefinitionData &Data) {
1332e5dd7070Spatrick unsigned NumProtoRefs = Record.readInt();
1333e5dd7070Spatrick SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1334e5dd7070Spatrick ProtoRefs.reserve(NumProtoRefs);
1335e5dd7070Spatrick for (unsigned I = 0; I != NumProtoRefs; ++I)
1336e5dd7070Spatrick ProtoRefs.push_back(readDeclAs<ObjCProtocolDecl>());
1337e5dd7070Spatrick SmallVector<SourceLocation, 16> ProtoLocs;
1338e5dd7070Spatrick ProtoLocs.reserve(NumProtoRefs);
1339e5dd7070Spatrick for (unsigned I = 0; I != NumProtoRefs; ++I)
1340e5dd7070Spatrick ProtoLocs.push_back(readSourceLocation());
1341e5dd7070Spatrick Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1342e5dd7070Spatrick ProtoLocs.data(), Reader.getContext());
1343*12c85518Srobert Data.ODRHash = Record.readInt();
1344*12c85518Srobert Data.HasODRHash = true;
1345e5dd7070Spatrick }
1346e5dd7070Spatrick
MergeDefinitionData(ObjCProtocolDecl * D,struct ObjCProtocolDecl::DefinitionData && NewDD)1347*12c85518Srobert void ASTDeclReader::MergeDefinitionData(
1348*12c85518Srobert ObjCProtocolDecl *D, struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1349*12c85518Srobert struct ObjCProtocolDecl::DefinitionData &DD = D->data();
1350*12c85518Srobert if (DD.Definition == NewDD.Definition)
1351*12c85518Srobert return;
1352*12c85518Srobert
1353*12c85518Srobert Reader.MergedDeclContexts.insert(
1354*12c85518Srobert std::make_pair(NewDD.Definition, DD.Definition));
1355*12c85518Srobert Reader.mergeDefinitionVisibility(DD.Definition, NewDD.Definition);
1356*12c85518Srobert
1357*12c85518Srobert if (D->getODRHash() != NewDD.ODRHash)
1358*12c85518Srobert Reader.PendingObjCProtocolOdrMergeFailures[DD.Definition].push_back(
1359*12c85518Srobert {NewDD.Definition, &NewDD});
1360e5dd7070Spatrick }
1361e5dd7070Spatrick
VisitObjCProtocolDecl(ObjCProtocolDecl * PD)1362e5dd7070Spatrick void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1363e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(PD);
1364e5dd7070Spatrick VisitObjCContainerDecl(PD);
1365e5dd7070Spatrick mergeRedeclarable(PD, Redecl);
1366e5dd7070Spatrick
1367e5dd7070Spatrick if (Record.readInt()) {
1368e5dd7070Spatrick // Read the definition.
1369e5dd7070Spatrick PD->allocateDefinitionData();
1370e5dd7070Spatrick
1371e5dd7070Spatrick ReadObjCDefinitionData(PD->data());
1372e5dd7070Spatrick
1373e5dd7070Spatrick ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1374e5dd7070Spatrick if (Canon->Data.getPointer()) {
1375e5dd7070Spatrick // If we already have a definition, keep the definition invariant and
1376e5dd7070Spatrick // merge the data.
1377e5dd7070Spatrick MergeDefinitionData(Canon, std::move(PD->data()));
1378e5dd7070Spatrick PD->Data = Canon->Data;
1379e5dd7070Spatrick } else {
1380e5dd7070Spatrick // Set the definition data of the canonical declaration, so other
1381e5dd7070Spatrick // redeclarations will see it.
1382e5dd7070Spatrick PD->getCanonicalDecl()->Data = PD->Data;
1383e5dd7070Spatrick }
1384e5dd7070Spatrick // Note that we have deserialized a definition.
1385e5dd7070Spatrick Reader.PendingDefinitions.insert(PD);
1386e5dd7070Spatrick } else {
1387e5dd7070Spatrick PD->Data = PD->getCanonicalDecl()->Data;
1388e5dd7070Spatrick }
1389e5dd7070Spatrick }
1390e5dd7070Spatrick
VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl * FD)1391e5dd7070Spatrick void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
1392e5dd7070Spatrick VisitFieldDecl(FD);
1393e5dd7070Spatrick }
1394e5dd7070Spatrick
VisitObjCCategoryDecl(ObjCCategoryDecl * CD)1395e5dd7070Spatrick void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
1396e5dd7070Spatrick VisitObjCContainerDecl(CD);
1397e5dd7070Spatrick CD->setCategoryNameLoc(readSourceLocation());
1398e5dd7070Spatrick CD->setIvarLBraceLoc(readSourceLocation());
1399e5dd7070Spatrick CD->setIvarRBraceLoc(readSourceLocation());
1400e5dd7070Spatrick
1401e5dd7070Spatrick // Note that this category has been deserialized. We do this before
1402e5dd7070Spatrick // deserializing the interface declaration, so that it will consider this
1403e5dd7070Spatrick /// category.
1404e5dd7070Spatrick Reader.CategoriesDeserialized.insert(CD);
1405e5dd7070Spatrick
1406e5dd7070Spatrick CD->ClassInterface = readDeclAs<ObjCInterfaceDecl>();
1407e5dd7070Spatrick CD->TypeParamList = ReadObjCTypeParamList();
1408e5dd7070Spatrick unsigned NumProtoRefs = Record.readInt();
1409e5dd7070Spatrick SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1410e5dd7070Spatrick ProtoRefs.reserve(NumProtoRefs);
1411e5dd7070Spatrick for (unsigned I = 0; I != NumProtoRefs; ++I)
1412e5dd7070Spatrick ProtoRefs.push_back(readDeclAs<ObjCProtocolDecl>());
1413e5dd7070Spatrick SmallVector<SourceLocation, 16> ProtoLocs;
1414e5dd7070Spatrick ProtoLocs.reserve(NumProtoRefs);
1415e5dd7070Spatrick for (unsigned I = 0; I != NumProtoRefs; ++I)
1416e5dd7070Spatrick ProtoLocs.push_back(readSourceLocation());
1417e5dd7070Spatrick CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1418e5dd7070Spatrick Reader.getContext());
1419e5dd7070Spatrick
1420e5dd7070Spatrick // Protocols in the class extension belong to the class.
1421e5dd7070Spatrick if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1422e5dd7070Spatrick CD->ClassInterface->mergeClassExtensionProtocolList(
1423e5dd7070Spatrick (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1424e5dd7070Spatrick Reader.getContext());
1425e5dd7070Spatrick }
1426e5dd7070Spatrick
VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl * CAD)1427e5dd7070Spatrick void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
1428e5dd7070Spatrick VisitNamedDecl(CAD);
1429e5dd7070Spatrick CAD->setClassInterface(readDeclAs<ObjCInterfaceDecl>());
1430e5dd7070Spatrick }
1431e5dd7070Spatrick
VisitObjCPropertyDecl(ObjCPropertyDecl * D)1432e5dd7070Spatrick void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
1433e5dd7070Spatrick VisitNamedDecl(D);
1434e5dd7070Spatrick D->setAtLoc(readSourceLocation());
1435e5dd7070Spatrick D->setLParenLoc(readSourceLocation());
1436e5dd7070Spatrick QualType T = Record.readType();
1437e5dd7070Spatrick TypeSourceInfo *TSI = readTypeSourceInfo();
1438e5dd7070Spatrick D->setType(T, TSI);
1439ec727ea7Spatrick D->setPropertyAttributes((ObjCPropertyAttribute::Kind)Record.readInt());
1440e5dd7070Spatrick D->setPropertyAttributesAsWritten(
1441ec727ea7Spatrick (ObjCPropertyAttribute::Kind)Record.readInt());
1442e5dd7070Spatrick D->setPropertyImplementation(
1443e5dd7070Spatrick (ObjCPropertyDecl::PropertyControl)Record.readInt());
1444e5dd7070Spatrick DeclarationName GetterName = Record.readDeclarationName();
1445e5dd7070Spatrick SourceLocation GetterLoc = readSourceLocation();
1446e5dd7070Spatrick D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1447e5dd7070Spatrick DeclarationName SetterName = Record.readDeclarationName();
1448e5dd7070Spatrick SourceLocation SetterLoc = readSourceLocation();
1449e5dd7070Spatrick D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
1450e5dd7070Spatrick D->setGetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1451e5dd7070Spatrick D->setSetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1452e5dd7070Spatrick D->setPropertyIvarDecl(readDeclAs<ObjCIvarDecl>());
1453e5dd7070Spatrick }
1454e5dd7070Spatrick
VisitObjCImplDecl(ObjCImplDecl * D)1455e5dd7070Spatrick void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
1456e5dd7070Spatrick VisitObjCContainerDecl(D);
1457e5dd7070Spatrick D->setClassInterface(readDeclAs<ObjCInterfaceDecl>());
1458e5dd7070Spatrick }
1459e5dd7070Spatrick
VisitObjCCategoryImplDecl(ObjCCategoryImplDecl * D)1460e5dd7070Spatrick void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
1461e5dd7070Spatrick VisitObjCImplDecl(D);
1462e5dd7070Spatrick D->CategoryNameLoc = readSourceLocation();
1463e5dd7070Spatrick }
1464e5dd7070Spatrick
VisitObjCImplementationDecl(ObjCImplementationDecl * D)1465e5dd7070Spatrick void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
1466e5dd7070Spatrick VisitObjCImplDecl(D);
1467e5dd7070Spatrick D->setSuperClass(readDeclAs<ObjCInterfaceDecl>());
1468e5dd7070Spatrick D->SuperLoc = readSourceLocation();
1469e5dd7070Spatrick D->setIvarLBraceLoc(readSourceLocation());
1470e5dd7070Spatrick D->setIvarRBraceLoc(readSourceLocation());
1471e5dd7070Spatrick D->setHasNonZeroConstructors(Record.readInt());
1472e5dd7070Spatrick D->setHasDestructors(Record.readInt());
1473e5dd7070Spatrick D->NumIvarInitializers = Record.readInt();
1474e5dd7070Spatrick if (D->NumIvarInitializers)
1475e5dd7070Spatrick D->IvarInitializers = ReadGlobalOffset();
1476e5dd7070Spatrick }
1477e5dd7070Spatrick
VisitObjCPropertyImplDecl(ObjCPropertyImplDecl * D)1478e5dd7070Spatrick void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
1479e5dd7070Spatrick VisitDecl(D);
1480e5dd7070Spatrick D->setAtLoc(readSourceLocation());
1481e5dd7070Spatrick D->setPropertyDecl(readDeclAs<ObjCPropertyDecl>());
1482e5dd7070Spatrick D->PropertyIvarDecl = readDeclAs<ObjCIvarDecl>();
1483e5dd7070Spatrick D->IvarLoc = readSourceLocation();
1484e5dd7070Spatrick D->setGetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1485e5dd7070Spatrick D->setSetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1486e5dd7070Spatrick D->setGetterCXXConstructor(Record.readExpr());
1487e5dd7070Spatrick D->setSetterCXXAssignment(Record.readExpr());
1488e5dd7070Spatrick }
1489e5dd7070Spatrick
VisitFieldDecl(FieldDecl * FD)1490e5dd7070Spatrick void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
1491e5dd7070Spatrick VisitDeclaratorDecl(FD);
1492e5dd7070Spatrick FD->Mutable = Record.readInt();
1493e5dd7070Spatrick
1494e5dd7070Spatrick if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1495e5dd7070Spatrick FD->InitStorage.setInt(ISK);
1496e5dd7070Spatrick FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1497e5dd7070Spatrick ? Record.readType().getAsOpaquePtr()
1498e5dd7070Spatrick : Record.readExpr());
1499e5dd7070Spatrick }
1500e5dd7070Spatrick
1501e5dd7070Spatrick if (auto *BW = Record.readExpr())
1502e5dd7070Spatrick FD->setBitWidth(BW);
1503e5dd7070Spatrick
1504e5dd7070Spatrick if (!FD->getDeclName()) {
1505e5dd7070Spatrick if (auto *Tmpl = readDeclAs<FieldDecl>())
1506e5dd7070Spatrick Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
1507e5dd7070Spatrick }
1508e5dd7070Spatrick mergeMergeable(FD);
1509e5dd7070Spatrick }
1510e5dd7070Spatrick
VisitMSPropertyDecl(MSPropertyDecl * PD)1511e5dd7070Spatrick void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1512e5dd7070Spatrick VisitDeclaratorDecl(PD);
1513e5dd7070Spatrick PD->GetterId = Record.readIdentifier();
1514e5dd7070Spatrick PD->SetterId = Record.readIdentifier();
1515e5dd7070Spatrick }
1516e5dd7070Spatrick
VisitMSGuidDecl(MSGuidDecl * D)1517ec727ea7Spatrick void ASTDeclReader::VisitMSGuidDecl(MSGuidDecl *D) {
1518ec727ea7Spatrick VisitValueDecl(D);
1519ec727ea7Spatrick D->PartVal.Part1 = Record.readInt();
1520ec727ea7Spatrick D->PartVal.Part2 = Record.readInt();
1521ec727ea7Spatrick D->PartVal.Part3 = Record.readInt();
1522ec727ea7Spatrick for (auto &C : D->PartVal.Part4And5)
1523ec727ea7Spatrick C = Record.readInt();
1524ec727ea7Spatrick
1525ec727ea7Spatrick // Add this GUID to the AST context's lookup structure, and merge if needed.
1526ec727ea7Spatrick if (MSGuidDecl *Existing = Reader.getContext().MSGuidDecls.GetOrInsertNode(D))
1527ec727ea7Spatrick Reader.getContext().setPrimaryMergedDecl(D, Existing->getCanonicalDecl());
1528ec727ea7Spatrick }
1529ec727ea7Spatrick
VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl * D)1530*12c85518Srobert void ASTDeclReader::VisitUnnamedGlobalConstantDecl(
1531*12c85518Srobert UnnamedGlobalConstantDecl *D) {
1532*12c85518Srobert VisitValueDecl(D);
1533*12c85518Srobert D->Value = Record.readAPValue();
1534*12c85518Srobert
1535*12c85518Srobert // Add this to the AST context's lookup structure, and merge if needed.
1536*12c85518Srobert if (UnnamedGlobalConstantDecl *Existing =
1537*12c85518Srobert Reader.getContext().UnnamedGlobalConstantDecls.GetOrInsertNode(D))
1538*12c85518Srobert Reader.getContext().setPrimaryMergedDecl(D, Existing->getCanonicalDecl());
1539*12c85518Srobert }
1540*12c85518Srobert
VisitTemplateParamObjectDecl(TemplateParamObjectDecl * D)1541a9ac8606Spatrick void ASTDeclReader::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) {
1542a9ac8606Spatrick VisitValueDecl(D);
1543a9ac8606Spatrick D->Value = Record.readAPValue();
1544a9ac8606Spatrick
1545a9ac8606Spatrick // Add this template parameter object to the AST context's lookup structure,
1546a9ac8606Spatrick // and merge if needed.
1547a9ac8606Spatrick if (TemplateParamObjectDecl *Existing =
1548a9ac8606Spatrick Reader.getContext().TemplateParamObjectDecls.GetOrInsertNode(D))
1549a9ac8606Spatrick Reader.getContext().setPrimaryMergedDecl(D, Existing->getCanonicalDecl());
1550a9ac8606Spatrick }
1551a9ac8606Spatrick
VisitIndirectFieldDecl(IndirectFieldDecl * FD)1552e5dd7070Spatrick void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1553e5dd7070Spatrick VisitValueDecl(FD);
1554e5dd7070Spatrick
1555e5dd7070Spatrick FD->ChainingSize = Record.readInt();
1556e5dd7070Spatrick assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
1557e5dd7070Spatrick FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
1558e5dd7070Spatrick
1559e5dd7070Spatrick for (unsigned I = 0; I != FD->ChainingSize; ++I)
1560e5dd7070Spatrick FD->Chaining[I] = readDeclAs<NamedDecl>();
1561e5dd7070Spatrick
1562e5dd7070Spatrick mergeMergeable(FD);
1563e5dd7070Spatrick }
1564e5dd7070Spatrick
VisitVarDeclImpl(VarDecl * VD)1565e5dd7070Spatrick ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
1566e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(VD);
1567e5dd7070Spatrick VisitDeclaratorDecl(VD);
1568e5dd7070Spatrick
1569e5dd7070Spatrick VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1570e5dd7070Spatrick VD->VarDeclBits.TSCSpec = Record.readInt();
1571e5dd7070Spatrick VD->VarDeclBits.InitStyle = Record.readInt();
1572e5dd7070Spatrick VD->VarDeclBits.ARCPseudoStrong = Record.readInt();
1573e5dd7070Spatrick if (!isa<ParmVarDecl>(VD)) {
1574e5dd7070Spatrick VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1575e5dd7070Spatrick Record.readInt();
1576e5dd7070Spatrick VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1577e5dd7070Spatrick VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1578e5dd7070Spatrick VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1579e5dd7070Spatrick VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
1580e5dd7070Spatrick VD->NonParmVarDeclBits.IsInline = Record.readInt();
1581e5dd7070Spatrick VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1582e5dd7070Spatrick VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1583e5dd7070Spatrick VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1584e5dd7070Spatrick VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
1585e5dd7070Spatrick VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
1586e5dd7070Spatrick VD->NonParmVarDeclBits.EscapingByref = Record.readInt();
1587e5dd7070Spatrick }
1588e5dd7070Spatrick auto VarLinkage = Linkage(Record.readInt());
1589e5dd7070Spatrick VD->setCachedLinkage(VarLinkage);
1590e5dd7070Spatrick
1591e5dd7070Spatrick // Reconstruct the one piece of the IdentifierNamespace that we need.
1592e5dd7070Spatrick if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
1593e5dd7070Spatrick VD->getLexicalDeclContext()->isFunctionOrMethod())
1594e5dd7070Spatrick VD->setLocalExternDecl();
1595e5dd7070Spatrick
1596e5dd7070Spatrick if (uint64_t Val = Record.readInt()) {
1597e5dd7070Spatrick VD->setInit(Record.readExpr());
1598a9ac8606Spatrick if (Val != 1) {
1599e5dd7070Spatrick EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1600a9ac8606Spatrick Eval->HasConstantInitialization = (Val & 2) != 0;
1601e5dd7070Spatrick Eval->HasConstantDestruction = (Val & 4) != 0;
1602e5dd7070Spatrick }
1603e5dd7070Spatrick }
1604e5dd7070Spatrick
1605e5dd7070Spatrick if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1606e5dd7070Spatrick Expr *CopyExpr = Record.readExpr();
1607e5dd7070Spatrick if (CopyExpr)
1608e5dd7070Spatrick Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1609e5dd7070Spatrick }
1610e5dd7070Spatrick
1611ec727ea7Spatrick if (VD->getStorageDuration() == SD_Static && Record.readInt()) {
1612a9ac8606Spatrick Reader.DefinitionSource[VD] =
1613a9ac8606Spatrick Loc.F->Kind == ModuleKind::MK_MainFile ||
1614a9ac8606Spatrick Reader.getContext().getLangOpts().BuildingPCHWithObjectFile;
1615ec727ea7Spatrick }
1616e5dd7070Spatrick
1617e5dd7070Spatrick enum VarKind {
1618e5dd7070Spatrick VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1619e5dd7070Spatrick };
1620e5dd7070Spatrick switch ((VarKind)Record.readInt()) {
1621e5dd7070Spatrick case VarNotTemplate:
1622e5dd7070Spatrick // Only true variables (not parameters or implicit parameters) can be
1623e5dd7070Spatrick // merged; the other kinds are not really redeclarable at all.
1624e5dd7070Spatrick if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1625e5dd7070Spatrick !isa<VarTemplateSpecializationDecl>(VD))
1626e5dd7070Spatrick mergeRedeclarable(VD, Redecl);
1627e5dd7070Spatrick break;
1628e5dd7070Spatrick case VarTemplate:
1629e5dd7070Spatrick // Merged when we merge the template.
1630e5dd7070Spatrick VD->setDescribedVarTemplate(readDeclAs<VarTemplateDecl>());
1631e5dd7070Spatrick break;
1632e5dd7070Spatrick case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
1633e5dd7070Spatrick auto *Tmpl = readDeclAs<VarDecl>();
1634e5dd7070Spatrick auto TSK = (TemplateSpecializationKind)Record.readInt();
1635e5dd7070Spatrick SourceLocation POI = readSourceLocation();
1636e5dd7070Spatrick Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
1637e5dd7070Spatrick mergeRedeclarable(VD, Redecl);
1638e5dd7070Spatrick break;
1639e5dd7070Spatrick }
1640e5dd7070Spatrick }
1641e5dd7070Spatrick
1642e5dd7070Spatrick return Redecl;
1643e5dd7070Spatrick }
1644e5dd7070Spatrick
VisitImplicitParamDecl(ImplicitParamDecl * PD)1645e5dd7070Spatrick void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
1646e5dd7070Spatrick VisitVarDecl(PD);
1647e5dd7070Spatrick }
1648e5dd7070Spatrick
VisitParmVarDecl(ParmVarDecl * PD)1649e5dd7070Spatrick void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
1650e5dd7070Spatrick VisitVarDecl(PD);
1651e5dd7070Spatrick unsigned isObjCMethodParam = Record.readInt();
1652e5dd7070Spatrick unsigned scopeDepth = Record.readInt();
1653e5dd7070Spatrick unsigned scopeIndex = Record.readInt();
1654e5dd7070Spatrick unsigned declQualifier = Record.readInt();
1655e5dd7070Spatrick if (isObjCMethodParam) {
1656e5dd7070Spatrick assert(scopeDepth == 0);
1657e5dd7070Spatrick PD->setObjCMethodScopeInfo(scopeIndex);
1658e5dd7070Spatrick PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1659e5dd7070Spatrick } else {
1660e5dd7070Spatrick PD->setScopeInfo(scopeDepth, scopeIndex);
1661e5dd7070Spatrick }
1662e5dd7070Spatrick PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1663e5dd7070Spatrick PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1664e5dd7070Spatrick if (Record.readInt()) // hasUninstantiatedDefaultArg.
1665e5dd7070Spatrick PD->setUninstantiatedDefaultArg(Record.readExpr());
1666e5dd7070Spatrick
1667e5dd7070Spatrick // FIXME: If this is a redeclaration of a function from another module, handle
1668e5dd7070Spatrick // inheritance of default arguments.
1669e5dd7070Spatrick }
1670e5dd7070Spatrick
VisitDecompositionDecl(DecompositionDecl * DD)1671e5dd7070Spatrick void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1672e5dd7070Spatrick VisitVarDecl(DD);
1673e5dd7070Spatrick auto **BDs = DD->getTrailingObjects<BindingDecl *>();
1674e5dd7070Spatrick for (unsigned I = 0; I != DD->NumBindings; ++I) {
1675e5dd7070Spatrick BDs[I] = readDeclAs<BindingDecl>();
1676e5dd7070Spatrick BDs[I]->setDecomposedDecl(DD);
1677e5dd7070Spatrick }
1678e5dd7070Spatrick }
1679e5dd7070Spatrick
VisitBindingDecl(BindingDecl * BD)1680e5dd7070Spatrick void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1681e5dd7070Spatrick VisitValueDecl(BD);
1682e5dd7070Spatrick BD->Binding = Record.readExpr();
1683e5dd7070Spatrick }
1684e5dd7070Spatrick
VisitFileScopeAsmDecl(FileScopeAsmDecl * AD)1685e5dd7070Spatrick void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
1686e5dd7070Spatrick VisitDecl(AD);
1687e5dd7070Spatrick AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
1688e5dd7070Spatrick AD->setRParenLoc(readSourceLocation());
1689e5dd7070Spatrick }
1690e5dd7070Spatrick
VisitTopLevelStmtDecl(TopLevelStmtDecl * D)1691*12c85518Srobert void ASTDeclReader::VisitTopLevelStmtDecl(TopLevelStmtDecl *D) {
1692*12c85518Srobert VisitDecl(D);
1693*12c85518Srobert D->Statement = Record.readStmt();
1694*12c85518Srobert }
1695*12c85518Srobert
VisitBlockDecl(BlockDecl * BD)1696e5dd7070Spatrick void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
1697e5dd7070Spatrick VisitDecl(BD);
1698e5dd7070Spatrick BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
1699e5dd7070Spatrick BD->setSignatureAsWritten(readTypeSourceInfo());
1700e5dd7070Spatrick unsigned NumParams = Record.readInt();
1701e5dd7070Spatrick SmallVector<ParmVarDecl *, 16> Params;
1702e5dd7070Spatrick Params.reserve(NumParams);
1703e5dd7070Spatrick for (unsigned I = 0; I != NumParams; ++I)
1704e5dd7070Spatrick Params.push_back(readDeclAs<ParmVarDecl>());
1705e5dd7070Spatrick BD->setParams(Params);
1706e5dd7070Spatrick
1707e5dd7070Spatrick BD->setIsVariadic(Record.readInt());
1708e5dd7070Spatrick BD->setBlockMissingReturnType(Record.readInt());
1709e5dd7070Spatrick BD->setIsConversionFromLambda(Record.readInt());
1710e5dd7070Spatrick BD->setDoesNotEscape(Record.readInt());
1711e5dd7070Spatrick BD->setCanAvoidCopyToHeap(Record.readInt());
1712e5dd7070Spatrick
1713e5dd7070Spatrick bool capturesCXXThis = Record.readInt();
1714e5dd7070Spatrick unsigned numCaptures = Record.readInt();
1715e5dd7070Spatrick SmallVector<BlockDecl::Capture, 16> captures;
1716e5dd7070Spatrick captures.reserve(numCaptures);
1717e5dd7070Spatrick for (unsigned i = 0; i != numCaptures; ++i) {
1718e5dd7070Spatrick auto *decl = readDeclAs<VarDecl>();
1719e5dd7070Spatrick unsigned flags = Record.readInt();
1720e5dd7070Spatrick bool byRef = (flags & 1);
1721e5dd7070Spatrick bool nested = (flags & 2);
1722e5dd7070Spatrick Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
1723e5dd7070Spatrick
1724e5dd7070Spatrick captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1725e5dd7070Spatrick }
1726e5dd7070Spatrick BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
1727e5dd7070Spatrick }
1728e5dd7070Spatrick
VisitCapturedDecl(CapturedDecl * CD)1729e5dd7070Spatrick void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1730e5dd7070Spatrick VisitDecl(CD);
1731e5dd7070Spatrick unsigned ContextParamPos = Record.readInt();
1732e5dd7070Spatrick CD->setNothrow(Record.readInt() != 0);
1733e5dd7070Spatrick // Body is set by VisitCapturedStmt.
1734e5dd7070Spatrick for (unsigned I = 0; I < CD->NumParams; ++I) {
1735e5dd7070Spatrick if (I != ContextParamPos)
1736e5dd7070Spatrick CD->setParam(I, readDeclAs<ImplicitParamDecl>());
1737e5dd7070Spatrick else
1738e5dd7070Spatrick CD->setContextParam(I, readDeclAs<ImplicitParamDecl>());
1739e5dd7070Spatrick }
1740e5dd7070Spatrick }
1741e5dd7070Spatrick
VisitLinkageSpecDecl(LinkageSpecDecl * D)1742e5dd7070Spatrick void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1743e5dd7070Spatrick VisitDecl(D);
1744e5dd7070Spatrick D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
1745e5dd7070Spatrick D->setExternLoc(readSourceLocation());
1746e5dd7070Spatrick D->setRBraceLoc(readSourceLocation());
1747e5dd7070Spatrick }
1748e5dd7070Spatrick
VisitExportDecl(ExportDecl * D)1749e5dd7070Spatrick void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1750e5dd7070Spatrick VisitDecl(D);
1751e5dd7070Spatrick D->RBraceLoc = readSourceLocation();
1752e5dd7070Spatrick }
1753e5dd7070Spatrick
VisitLabelDecl(LabelDecl * D)1754e5dd7070Spatrick void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1755e5dd7070Spatrick VisitNamedDecl(D);
1756e5dd7070Spatrick D->setLocStart(readSourceLocation());
1757e5dd7070Spatrick }
1758e5dd7070Spatrick
VisitNamespaceDecl(NamespaceDecl * D)1759e5dd7070Spatrick void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
1760e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(D);
1761e5dd7070Spatrick VisitNamedDecl(D);
1762e5dd7070Spatrick D->setInline(Record.readInt());
1763*12c85518Srobert D->setNested(Record.readInt());
1764e5dd7070Spatrick D->LocStart = readSourceLocation();
1765e5dd7070Spatrick D->RBraceLoc = readSourceLocation();
1766e5dd7070Spatrick
1767e5dd7070Spatrick // Defer loading the anonymous namespace until we've finished merging
1768e5dd7070Spatrick // this namespace; loading it might load a later declaration of the
1769e5dd7070Spatrick // same namespace, and we have an invariant that older declarations
1770e5dd7070Spatrick // get merged before newer ones try to merge.
1771e5dd7070Spatrick GlobalDeclID AnonNamespace = 0;
1772e5dd7070Spatrick if (Redecl.getFirstID() == ThisDeclID) {
1773e5dd7070Spatrick AnonNamespace = readDeclID();
1774e5dd7070Spatrick } else {
1775e5dd7070Spatrick // Link this namespace back to the first declaration, which has already
1776e5dd7070Spatrick // been deserialized.
1777*12c85518Srobert D->AnonOrFirstNamespaceAndFlags.setPointer(D->getFirstDecl());
1778e5dd7070Spatrick }
1779e5dd7070Spatrick
1780e5dd7070Spatrick mergeRedeclarable(D, Redecl);
1781e5dd7070Spatrick
1782e5dd7070Spatrick if (AnonNamespace) {
1783e5dd7070Spatrick // Each module has its own anonymous namespace, which is disjoint from
1784e5dd7070Spatrick // any other module's anonymous namespaces, so don't attach the anonymous
1785e5dd7070Spatrick // namespace at all.
1786e5dd7070Spatrick auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
1787e5dd7070Spatrick if (!Record.isModule())
1788e5dd7070Spatrick D->setAnonymousNamespace(Anon);
1789e5dd7070Spatrick }
1790e5dd7070Spatrick }
1791e5dd7070Spatrick
VisitHLSLBufferDecl(HLSLBufferDecl * D)1792*12c85518Srobert void ASTDeclReader::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
1793*12c85518Srobert VisitNamedDecl(D);
1794*12c85518Srobert VisitDeclContext(D);
1795*12c85518Srobert D->IsCBuffer = Record.readBool();
1796*12c85518Srobert D->KwLoc = readSourceLocation();
1797*12c85518Srobert D->LBraceLoc = readSourceLocation();
1798*12c85518Srobert D->RBraceLoc = readSourceLocation();
1799*12c85518Srobert }
1800*12c85518Srobert
VisitNamespaceAliasDecl(NamespaceAliasDecl * D)1801e5dd7070Spatrick void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1802e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(D);
1803e5dd7070Spatrick VisitNamedDecl(D);
1804e5dd7070Spatrick D->NamespaceLoc = readSourceLocation();
1805e5dd7070Spatrick D->IdentLoc = readSourceLocation();
1806e5dd7070Spatrick D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1807e5dd7070Spatrick D->Namespace = readDeclAs<NamedDecl>();
1808e5dd7070Spatrick mergeRedeclarable(D, Redecl);
1809e5dd7070Spatrick }
1810e5dd7070Spatrick
VisitUsingDecl(UsingDecl * D)1811e5dd7070Spatrick void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
1812e5dd7070Spatrick VisitNamedDecl(D);
1813e5dd7070Spatrick D->setUsingLoc(readSourceLocation());
1814e5dd7070Spatrick D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1815e5dd7070Spatrick D->DNLoc = Record.readDeclarationNameLoc(D->getDeclName());
1816e5dd7070Spatrick D->FirstUsingShadow.setPointer(readDeclAs<UsingShadowDecl>());
1817e5dd7070Spatrick D->setTypename(Record.readInt());
1818e5dd7070Spatrick if (auto *Pattern = readDeclAs<NamedDecl>())
1819e5dd7070Spatrick Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
1820e5dd7070Spatrick mergeMergeable(D);
1821e5dd7070Spatrick }
1822e5dd7070Spatrick
VisitUsingEnumDecl(UsingEnumDecl * D)1823a9ac8606Spatrick void ASTDeclReader::VisitUsingEnumDecl(UsingEnumDecl *D) {
1824a9ac8606Spatrick VisitNamedDecl(D);
1825a9ac8606Spatrick D->setUsingLoc(readSourceLocation());
1826a9ac8606Spatrick D->setEnumLoc(readSourceLocation());
1827*12c85518Srobert D->setEnumType(Record.readTypeSourceInfo());
1828a9ac8606Spatrick D->FirstUsingShadow.setPointer(readDeclAs<UsingShadowDecl>());
1829a9ac8606Spatrick if (auto *Pattern = readDeclAs<UsingEnumDecl>())
1830a9ac8606Spatrick Reader.getContext().setInstantiatedFromUsingEnumDecl(D, Pattern);
1831a9ac8606Spatrick mergeMergeable(D);
1832a9ac8606Spatrick }
1833a9ac8606Spatrick
VisitUsingPackDecl(UsingPackDecl * D)1834e5dd7070Spatrick void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1835e5dd7070Spatrick VisitNamedDecl(D);
1836e5dd7070Spatrick D->InstantiatedFrom = readDeclAs<NamedDecl>();
1837e5dd7070Spatrick auto **Expansions = D->getTrailingObjects<NamedDecl *>();
1838e5dd7070Spatrick for (unsigned I = 0; I != D->NumExpansions; ++I)
1839e5dd7070Spatrick Expansions[I] = readDeclAs<NamedDecl>();
1840e5dd7070Spatrick mergeMergeable(D);
1841e5dd7070Spatrick }
1842e5dd7070Spatrick
VisitUsingShadowDecl(UsingShadowDecl * D)1843e5dd7070Spatrick void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
1844e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(D);
1845e5dd7070Spatrick VisitNamedDecl(D);
1846e5dd7070Spatrick D->Underlying = readDeclAs<NamedDecl>();
1847e5dd7070Spatrick D->IdentifierNamespace = Record.readInt();
1848e5dd7070Spatrick D->UsingOrNextShadow = readDeclAs<NamedDecl>();
1849e5dd7070Spatrick auto *Pattern = readDeclAs<UsingShadowDecl>();
1850e5dd7070Spatrick if (Pattern)
1851e5dd7070Spatrick Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
1852e5dd7070Spatrick mergeRedeclarable(D, Redecl);
1853e5dd7070Spatrick }
1854e5dd7070Spatrick
VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl * D)1855e5dd7070Spatrick void ASTDeclReader::VisitConstructorUsingShadowDecl(
1856e5dd7070Spatrick ConstructorUsingShadowDecl *D) {
1857e5dd7070Spatrick VisitUsingShadowDecl(D);
1858e5dd7070Spatrick D->NominatedBaseClassShadowDecl = readDeclAs<ConstructorUsingShadowDecl>();
1859e5dd7070Spatrick D->ConstructedBaseClassShadowDecl = readDeclAs<ConstructorUsingShadowDecl>();
1860e5dd7070Spatrick D->IsVirtual = Record.readInt();
1861e5dd7070Spatrick }
1862e5dd7070Spatrick
VisitUsingDirectiveDecl(UsingDirectiveDecl * D)1863e5dd7070Spatrick void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1864e5dd7070Spatrick VisitNamedDecl(D);
1865e5dd7070Spatrick D->UsingLoc = readSourceLocation();
1866e5dd7070Spatrick D->NamespaceLoc = readSourceLocation();
1867e5dd7070Spatrick D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1868e5dd7070Spatrick D->NominatedNamespace = readDeclAs<NamedDecl>();
1869e5dd7070Spatrick D->CommonAncestor = readDeclAs<DeclContext>();
1870e5dd7070Spatrick }
1871e5dd7070Spatrick
VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl * D)1872e5dd7070Spatrick void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1873e5dd7070Spatrick VisitValueDecl(D);
1874e5dd7070Spatrick D->setUsingLoc(readSourceLocation());
1875e5dd7070Spatrick D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1876e5dd7070Spatrick D->DNLoc = Record.readDeclarationNameLoc(D->getDeclName());
1877e5dd7070Spatrick D->EllipsisLoc = readSourceLocation();
1878e5dd7070Spatrick mergeMergeable(D);
1879e5dd7070Spatrick }
1880e5dd7070Spatrick
VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl * D)1881e5dd7070Spatrick void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
1882e5dd7070Spatrick UnresolvedUsingTypenameDecl *D) {
1883e5dd7070Spatrick VisitTypeDecl(D);
1884e5dd7070Spatrick D->TypenameLocation = readSourceLocation();
1885e5dd7070Spatrick D->QualifierLoc = Record.readNestedNameSpecifierLoc();
1886e5dd7070Spatrick D->EllipsisLoc = readSourceLocation();
1887e5dd7070Spatrick mergeMergeable(D);
1888e5dd7070Spatrick }
1889e5dd7070Spatrick
VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl * D)1890a9ac8606Spatrick void ASTDeclReader::VisitUnresolvedUsingIfExistsDecl(
1891a9ac8606Spatrick UnresolvedUsingIfExistsDecl *D) {
1892a9ac8606Spatrick VisitNamedDecl(D);
1893a9ac8606Spatrick }
1894a9ac8606Spatrick
ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData & Data,const CXXRecordDecl * D)1895e5dd7070Spatrick void ASTDeclReader::ReadCXXDefinitionData(
1896e5dd7070Spatrick struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
1897e5dd7070Spatrick #define FIELD(Name, Width, Merge) \
1898e5dd7070Spatrick Data.Name = Record.readInt();
1899e5dd7070Spatrick #include "clang/AST/CXXRecordDeclDefinitionBits.def"
1900e5dd7070Spatrick
1901e5dd7070Spatrick // Note: the caller has deserialized the IsLambda bit already.
1902e5dd7070Spatrick Data.ODRHash = Record.readInt();
1903e5dd7070Spatrick Data.HasODRHash = true;
1904e5dd7070Spatrick
1905ec727ea7Spatrick if (Record.readInt()) {
1906a9ac8606Spatrick Reader.DefinitionSource[D] =
1907a9ac8606Spatrick Loc.F->Kind == ModuleKind::MK_MainFile ||
1908a9ac8606Spatrick Reader.getContext().getLangOpts().BuildingPCHWithObjectFile;
1909ec727ea7Spatrick }
1910e5dd7070Spatrick
1911e5dd7070Spatrick Data.NumBases = Record.readInt();
1912e5dd7070Spatrick if (Data.NumBases)
1913e5dd7070Spatrick Data.Bases = ReadGlobalOffset();
1914e5dd7070Spatrick Data.NumVBases = Record.readInt();
1915e5dd7070Spatrick if (Data.NumVBases)
1916e5dd7070Spatrick Data.VBases = ReadGlobalOffset();
1917e5dd7070Spatrick
1918e5dd7070Spatrick Record.readUnresolvedSet(Data.Conversions);
1919e5dd7070Spatrick Data.ComputedVisibleConversions = Record.readInt();
1920e5dd7070Spatrick if (Data.ComputedVisibleConversions)
1921e5dd7070Spatrick Record.readUnresolvedSet(Data.VisibleConversions);
1922e5dd7070Spatrick assert(Data.Definition && "Data.Definition should be already set!");
1923e5dd7070Spatrick Data.FirstFriend = readDeclID();
1924e5dd7070Spatrick
1925e5dd7070Spatrick if (Data.IsLambda) {
1926e5dd7070Spatrick using Capture = LambdaCapture;
1927e5dd7070Spatrick
1928e5dd7070Spatrick auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
1929*12c85518Srobert Lambda.DependencyKind = Record.readInt();
1930e5dd7070Spatrick Lambda.IsGenericLambda = Record.readInt();
1931e5dd7070Spatrick Lambda.CaptureDefault = Record.readInt();
1932e5dd7070Spatrick Lambda.NumCaptures = Record.readInt();
1933e5dd7070Spatrick Lambda.NumExplicitCaptures = Record.readInt();
1934e5dd7070Spatrick Lambda.HasKnownInternalLinkage = Record.readInt();
1935e5dd7070Spatrick Lambda.ManglingNumber = Record.readInt();
1936a9ac8606Spatrick D->setDeviceLambdaManglingNumber(Record.readInt());
1937e5dd7070Spatrick Lambda.ContextDecl = readDeclID();
1938*12c85518Srobert Capture *ToCapture = nullptr;
1939*12c85518Srobert if (Lambda.NumCaptures) {
1940*12c85518Srobert ToCapture = (Capture *)Reader.getContext().Allocate(sizeof(Capture) *
1941*12c85518Srobert Lambda.NumCaptures);
1942*12c85518Srobert Lambda.AddCaptureList(Reader.getContext(), ToCapture);
1943*12c85518Srobert }
1944e5dd7070Spatrick Lambda.MethodTyInfo = readTypeSourceInfo();
1945e5dd7070Spatrick for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
1946e5dd7070Spatrick SourceLocation Loc = readSourceLocation();
1947e5dd7070Spatrick bool IsImplicit = Record.readInt();
1948e5dd7070Spatrick auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
1949e5dd7070Spatrick switch (Kind) {
1950e5dd7070Spatrick case LCK_StarThis:
1951e5dd7070Spatrick case LCK_This:
1952e5dd7070Spatrick case LCK_VLAType:
1953e5dd7070Spatrick *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
1954e5dd7070Spatrick break;
1955e5dd7070Spatrick case LCK_ByCopy:
1956e5dd7070Spatrick case LCK_ByRef:
1957e5dd7070Spatrick auto *Var = readDeclAs<VarDecl>();
1958e5dd7070Spatrick SourceLocation EllipsisLoc = readSourceLocation();
1959e5dd7070Spatrick *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1960e5dd7070Spatrick break;
1961e5dd7070Spatrick }
1962e5dd7070Spatrick }
1963e5dd7070Spatrick }
1964e5dd7070Spatrick }
1965e5dd7070Spatrick
MergeDefinitionData(CXXRecordDecl * D,struct CXXRecordDecl::DefinitionData && MergeDD)1966e5dd7070Spatrick void ASTDeclReader::MergeDefinitionData(
1967e5dd7070Spatrick CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
1968e5dd7070Spatrick assert(D->DefinitionData &&
1969e5dd7070Spatrick "merging class definition into non-definition");
1970e5dd7070Spatrick auto &DD = *D->DefinitionData;
1971e5dd7070Spatrick
1972e5dd7070Spatrick if (DD.Definition != MergeDD.Definition) {
1973e5dd7070Spatrick // Track that we merged the definitions.
1974e5dd7070Spatrick Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1975e5dd7070Spatrick DD.Definition));
1976e5dd7070Spatrick Reader.PendingDefinitions.erase(MergeDD.Definition);
1977e5dd7070Spatrick MergeDD.Definition->setCompleteDefinition(false);
1978e5dd7070Spatrick Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
1979e5dd7070Spatrick assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1980e5dd7070Spatrick "already loaded pending lookups for merged definition");
1981e5dd7070Spatrick }
1982e5dd7070Spatrick
1983e5dd7070Spatrick auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1984e5dd7070Spatrick if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1985e5dd7070Spatrick PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
1986e5dd7070Spatrick // We faked up this definition data because we found a class for which we'd
1987e5dd7070Spatrick // not yet loaded the definition. Replace it with the real thing now.
1988e5dd7070Spatrick assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
1989e5dd7070Spatrick PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
1990e5dd7070Spatrick
1991e5dd7070Spatrick // Don't change which declaration is the definition; that is required
1992e5dd7070Spatrick // to be invariant once we select it.
1993e5dd7070Spatrick auto *Def = DD.Definition;
1994e5dd7070Spatrick DD = std::move(MergeDD);
1995e5dd7070Spatrick DD.Definition = Def;
1996e5dd7070Spatrick return;
1997e5dd7070Spatrick }
1998e5dd7070Spatrick
1999e5dd7070Spatrick bool DetectedOdrViolation = false;
2000e5dd7070Spatrick
2001e5dd7070Spatrick #define FIELD(Name, Width, Merge) Merge(Name)
2002e5dd7070Spatrick #define MERGE_OR(Field) DD.Field |= MergeDD.Field;
2003e5dd7070Spatrick #define NO_MERGE(Field) \
2004e5dd7070Spatrick DetectedOdrViolation |= DD.Field != MergeDD.Field; \
2005e5dd7070Spatrick MERGE_OR(Field)
2006e5dd7070Spatrick #include "clang/AST/CXXRecordDeclDefinitionBits.def"
2007e5dd7070Spatrick NO_MERGE(IsLambda)
2008e5dd7070Spatrick #undef NO_MERGE
2009e5dd7070Spatrick #undef MERGE_OR
2010e5dd7070Spatrick
2011e5dd7070Spatrick if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
2012e5dd7070Spatrick DetectedOdrViolation = true;
2013e5dd7070Spatrick // FIXME: Issue a diagnostic if the base classes don't match when we come
2014e5dd7070Spatrick // to lazily load them.
2015e5dd7070Spatrick
2016e5dd7070Spatrick // FIXME: Issue a diagnostic if the list of conversion functions doesn't
2017e5dd7070Spatrick // match when we come to lazily load them.
2018e5dd7070Spatrick if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
2019e5dd7070Spatrick DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
2020e5dd7070Spatrick DD.ComputedVisibleConversions = true;
2021e5dd7070Spatrick }
2022e5dd7070Spatrick
2023e5dd7070Spatrick // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
2024e5dd7070Spatrick // lazily load it.
2025e5dd7070Spatrick
2026e5dd7070Spatrick if (DD.IsLambda) {
2027*12c85518Srobert auto &Lambda1 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(DD);
2028*12c85518Srobert auto &Lambda2 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(MergeDD);
2029*12c85518Srobert DetectedOdrViolation |= Lambda1.DependencyKind != Lambda2.DependencyKind;
2030*12c85518Srobert DetectedOdrViolation |= Lambda1.IsGenericLambda != Lambda2.IsGenericLambda;
2031*12c85518Srobert DetectedOdrViolation |= Lambda1.CaptureDefault != Lambda2.CaptureDefault;
2032*12c85518Srobert DetectedOdrViolation |= Lambda1.NumCaptures != Lambda2.NumCaptures;
2033*12c85518Srobert DetectedOdrViolation |=
2034*12c85518Srobert Lambda1.NumExplicitCaptures != Lambda2.NumExplicitCaptures;
2035*12c85518Srobert DetectedOdrViolation |=
2036*12c85518Srobert Lambda1.HasKnownInternalLinkage != Lambda2.HasKnownInternalLinkage;
2037*12c85518Srobert DetectedOdrViolation |= Lambda1.ManglingNumber != Lambda2.ManglingNumber;
2038*12c85518Srobert
2039*12c85518Srobert if (Lambda1.NumCaptures && Lambda1.NumCaptures == Lambda2.NumCaptures) {
2040*12c85518Srobert for (unsigned I = 0, N = Lambda1.NumCaptures; I != N; ++I) {
2041*12c85518Srobert LambdaCapture &Cap1 = Lambda1.Captures.front()[I];
2042*12c85518Srobert LambdaCapture &Cap2 = Lambda2.Captures.front()[I];
2043*12c85518Srobert DetectedOdrViolation |= Cap1.getCaptureKind() != Cap2.getCaptureKind();
2044*12c85518Srobert }
2045*12c85518Srobert Lambda1.AddCaptureList(Reader.getContext(), Lambda2.Captures.front());
2046*12c85518Srobert }
2047e5dd7070Spatrick }
2048e5dd7070Spatrick
2049e5dd7070Spatrick if (D->getODRHash() != MergeDD.ODRHash) {
2050e5dd7070Spatrick DetectedOdrViolation = true;
2051e5dd7070Spatrick }
2052e5dd7070Spatrick
2053e5dd7070Spatrick if (DetectedOdrViolation)
2054e5dd7070Spatrick Reader.PendingOdrMergeFailures[DD.Definition].push_back(
2055e5dd7070Spatrick {MergeDD.Definition, &MergeDD});
2056e5dd7070Spatrick }
2057e5dd7070Spatrick
ReadCXXRecordDefinition(CXXRecordDecl * D,bool Update)2058e5dd7070Spatrick void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
2059e5dd7070Spatrick struct CXXRecordDecl::DefinitionData *DD;
2060e5dd7070Spatrick ASTContext &C = Reader.getContext();
2061e5dd7070Spatrick
2062e5dd7070Spatrick // Determine whether this is a lambda closure type, so that we can
2063e5dd7070Spatrick // allocate the appropriate DefinitionData structure.
2064e5dd7070Spatrick bool IsLambda = Record.readInt();
2065e5dd7070Spatrick if (IsLambda)
2066*12c85518Srobert DD = new (C) CXXRecordDecl::LambdaDefinitionData(
2067*12c85518Srobert D, nullptr, CXXRecordDecl::LDK_Unknown, false, LCD_None);
2068e5dd7070Spatrick else
2069e5dd7070Spatrick DD = new (C) struct CXXRecordDecl::DefinitionData(D);
2070e5dd7070Spatrick
2071e5dd7070Spatrick CXXRecordDecl *Canon = D->getCanonicalDecl();
2072e5dd7070Spatrick // Set decl definition data before reading it, so that during deserialization
2073e5dd7070Spatrick // when we read CXXRecordDecl, it already has definition data and we don't
2074e5dd7070Spatrick // set fake one.
2075e5dd7070Spatrick if (!Canon->DefinitionData)
2076e5dd7070Spatrick Canon->DefinitionData = DD;
2077e5dd7070Spatrick D->DefinitionData = Canon->DefinitionData;
2078e5dd7070Spatrick ReadCXXDefinitionData(*DD, D);
2079e5dd7070Spatrick
2080e5dd7070Spatrick // We might already have a different definition for this record. This can
2081e5dd7070Spatrick // happen either because we're reading an update record, or because we've
2082e5dd7070Spatrick // already done some merging. Either way, just merge into it.
2083e5dd7070Spatrick if (Canon->DefinitionData != DD) {
2084e5dd7070Spatrick MergeDefinitionData(Canon, std::move(*DD));
2085e5dd7070Spatrick return;
2086e5dd7070Spatrick }
2087e5dd7070Spatrick
2088e5dd7070Spatrick // Mark this declaration as being a definition.
2089e5dd7070Spatrick D->setCompleteDefinition(true);
2090e5dd7070Spatrick
2091e5dd7070Spatrick // If this is not the first declaration or is an update record, we can have
2092e5dd7070Spatrick // other redeclarations already. Make a note that we need to propagate the
2093e5dd7070Spatrick // DefinitionData pointer onto them.
2094e5dd7070Spatrick if (Update || Canon != D)
2095e5dd7070Spatrick Reader.PendingDefinitions.insert(D);
2096e5dd7070Spatrick }
2097e5dd7070Spatrick
2098e5dd7070Spatrick ASTDeclReader::RedeclarableResult
VisitCXXRecordDeclImpl(CXXRecordDecl * D)2099e5dd7070Spatrick ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
2100e5dd7070Spatrick RedeclarableResult Redecl = VisitRecordDeclImpl(D);
2101e5dd7070Spatrick
2102e5dd7070Spatrick ASTContext &C = Reader.getContext();
2103e5dd7070Spatrick
2104e5dd7070Spatrick enum CXXRecKind {
2105e5dd7070Spatrick CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
2106e5dd7070Spatrick };
2107e5dd7070Spatrick switch ((CXXRecKind)Record.readInt()) {
2108e5dd7070Spatrick case CXXRecNotTemplate:
2109e5dd7070Spatrick // Merged when we merge the folding set entry in the primary template.
2110e5dd7070Spatrick if (!isa<ClassTemplateSpecializationDecl>(D))
2111e5dd7070Spatrick mergeRedeclarable(D, Redecl);
2112e5dd7070Spatrick break;
2113e5dd7070Spatrick case CXXRecTemplate: {
2114e5dd7070Spatrick // Merged when we merge the template.
2115e5dd7070Spatrick auto *Template = readDeclAs<ClassTemplateDecl>();
2116e5dd7070Spatrick D->TemplateOrInstantiation = Template;
2117e5dd7070Spatrick if (!Template->getTemplatedDecl()) {
2118e5dd7070Spatrick // We've not actually loaded the ClassTemplateDecl yet, because we're
2119e5dd7070Spatrick // currently being loaded as its pattern. Rely on it to set up our
2120e5dd7070Spatrick // TypeForDecl (see VisitClassTemplateDecl).
2121e5dd7070Spatrick //
2122e5dd7070Spatrick // Beware: we do not yet know our canonical declaration, and may still
2123e5dd7070Spatrick // get merged once the surrounding class template has got off the ground.
2124e5dd7070Spatrick DeferredTypeID = 0;
2125e5dd7070Spatrick }
2126e5dd7070Spatrick break;
2127e5dd7070Spatrick }
2128e5dd7070Spatrick case CXXRecMemberSpecialization: {
2129e5dd7070Spatrick auto *RD = readDeclAs<CXXRecordDecl>();
2130e5dd7070Spatrick auto TSK = (TemplateSpecializationKind)Record.readInt();
2131e5dd7070Spatrick SourceLocation POI = readSourceLocation();
2132e5dd7070Spatrick MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
2133e5dd7070Spatrick MSI->setPointOfInstantiation(POI);
2134e5dd7070Spatrick D->TemplateOrInstantiation = MSI;
2135e5dd7070Spatrick mergeRedeclarable(D, Redecl);
2136e5dd7070Spatrick break;
2137e5dd7070Spatrick }
2138e5dd7070Spatrick }
2139e5dd7070Spatrick
2140e5dd7070Spatrick bool WasDefinition = Record.readInt();
2141e5dd7070Spatrick if (WasDefinition)
2142e5dd7070Spatrick ReadCXXRecordDefinition(D, /*Update*/false);
2143e5dd7070Spatrick else
2144e5dd7070Spatrick // Propagate DefinitionData pointer from the canonical declaration.
2145e5dd7070Spatrick D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
2146e5dd7070Spatrick
2147e5dd7070Spatrick // Lazily load the key function to avoid deserializing every method so we can
2148e5dd7070Spatrick // compute it.
2149e5dd7070Spatrick if (WasDefinition) {
2150e5dd7070Spatrick DeclID KeyFn = readDeclID();
2151e5dd7070Spatrick if (KeyFn && D->isCompleteDefinition())
2152e5dd7070Spatrick // FIXME: This is wrong for the ARM ABI, where some other module may have
2153e5dd7070Spatrick // made this function no longer be a key function. We need an update
2154e5dd7070Spatrick // record or similar for that case.
2155e5dd7070Spatrick C.KeyFunctions[D] = KeyFn;
2156e5dd7070Spatrick }
2157e5dd7070Spatrick
2158e5dd7070Spatrick return Redecl;
2159e5dd7070Spatrick }
2160e5dd7070Spatrick
VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl * D)2161e5dd7070Spatrick void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
2162e5dd7070Spatrick D->setExplicitSpecifier(Record.readExplicitSpec());
2163a9ac8606Spatrick D->Ctor = readDeclAs<CXXConstructorDecl>();
2164e5dd7070Spatrick VisitFunctionDecl(D);
2165e5dd7070Spatrick D->setIsCopyDeductionCandidate(Record.readInt());
2166e5dd7070Spatrick }
2167e5dd7070Spatrick
VisitCXXMethodDecl(CXXMethodDecl * D)2168e5dd7070Spatrick void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
2169e5dd7070Spatrick VisitFunctionDecl(D);
2170e5dd7070Spatrick
2171e5dd7070Spatrick unsigned NumOverridenMethods = Record.readInt();
2172e5dd7070Spatrick if (D->isCanonicalDecl()) {
2173e5dd7070Spatrick while (NumOverridenMethods--) {
2174e5dd7070Spatrick // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
2175e5dd7070Spatrick // MD may be initializing.
2176e5dd7070Spatrick if (auto *MD = readDeclAs<CXXMethodDecl>())
2177e5dd7070Spatrick Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
2178e5dd7070Spatrick }
2179e5dd7070Spatrick } else {
2180e5dd7070Spatrick // We don't care about which declarations this used to override; we get
2181e5dd7070Spatrick // the relevant information from the canonical declaration.
2182e5dd7070Spatrick Record.skipInts(NumOverridenMethods);
2183e5dd7070Spatrick }
2184e5dd7070Spatrick }
2185e5dd7070Spatrick
VisitCXXConstructorDecl(CXXConstructorDecl * D)2186e5dd7070Spatrick void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2187e5dd7070Spatrick // We need the inherited constructor information to merge the declaration,
2188e5dd7070Spatrick // so we have to read it before we call VisitCXXMethodDecl.
2189e5dd7070Spatrick D->setExplicitSpecifier(Record.readExplicitSpec());
2190e5dd7070Spatrick if (D->isInheritingConstructor()) {
2191e5dd7070Spatrick auto *Shadow = readDeclAs<ConstructorUsingShadowDecl>();
2192e5dd7070Spatrick auto *Ctor = readDeclAs<CXXConstructorDecl>();
2193e5dd7070Spatrick *D->getTrailingObjects<InheritedConstructor>() =
2194e5dd7070Spatrick InheritedConstructor(Shadow, Ctor);
2195e5dd7070Spatrick }
2196e5dd7070Spatrick
2197e5dd7070Spatrick VisitCXXMethodDecl(D);
2198e5dd7070Spatrick }
2199e5dd7070Spatrick
VisitCXXDestructorDecl(CXXDestructorDecl * D)2200e5dd7070Spatrick void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2201e5dd7070Spatrick VisitCXXMethodDecl(D);
2202e5dd7070Spatrick
2203e5dd7070Spatrick if (auto *OperatorDelete = readDeclAs<FunctionDecl>()) {
2204e5dd7070Spatrick CXXDestructorDecl *Canon = D->getCanonicalDecl();
2205e5dd7070Spatrick auto *ThisArg = Record.readExpr();
2206e5dd7070Spatrick // FIXME: Check consistency if we have an old and new operator delete.
2207e5dd7070Spatrick if (!Canon->OperatorDelete) {
2208e5dd7070Spatrick Canon->OperatorDelete = OperatorDelete;
2209e5dd7070Spatrick Canon->OperatorDeleteThisArg = ThisArg;
2210e5dd7070Spatrick }
2211e5dd7070Spatrick }
2212e5dd7070Spatrick }
2213e5dd7070Spatrick
VisitCXXConversionDecl(CXXConversionDecl * D)2214e5dd7070Spatrick void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
2215e5dd7070Spatrick D->setExplicitSpecifier(Record.readExplicitSpec());
2216e5dd7070Spatrick VisitCXXMethodDecl(D);
2217e5dd7070Spatrick }
2218e5dd7070Spatrick
VisitImportDecl(ImportDecl * D)2219e5dd7070Spatrick void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2220e5dd7070Spatrick VisitDecl(D);
2221ec727ea7Spatrick D->ImportedModule = readModule();
2222ec727ea7Spatrick D->setImportComplete(Record.readInt());
2223e5dd7070Spatrick auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
2224e5dd7070Spatrick for (unsigned I = 0, N = Record.back(); I != N; ++I)
2225e5dd7070Spatrick StoredLocs[I] = readSourceLocation();
2226e5dd7070Spatrick Record.skipInts(1); // The number of stored source locations.
2227e5dd7070Spatrick }
2228e5dd7070Spatrick
VisitAccessSpecDecl(AccessSpecDecl * D)2229e5dd7070Spatrick void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
2230e5dd7070Spatrick VisitDecl(D);
2231e5dd7070Spatrick D->setColonLoc(readSourceLocation());
2232e5dd7070Spatrick }
2233e5dd7070Spatrick
VisitFriendDecl(FriendDecl * D)2234e5dd7070Spatrick void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
2235e5dd7070Spatrick VisitDecl(D);
2236e5dd7070Spatrick if (Record.readInt()) // hasFriendDecl
2237e5dd7070Spatrick D->Friend = readDeclAs<NamedDecl>();
2238e5dd7070Spatrick else
2239e5dd7070Spatrick D->Friend = readTypeSourceInfo();
2240e5dd7070Spatrick for (unsigned i = 0; i != D->NumTPLists; ++i)
2241e5dd7070Spatrick D->getTrailingObjects<TemplateParameterList *>()[i] =
2242e5dd7070Spatrick Record.readTemplateParameterList();
2243e5dd7070Spatrick D->NextFriend = readDeclID();
2244e5dd7070Spatrick D->UnsupportedFriend = (Record.readInt() != 0);
2245e5dd7070Spatrick D->FriendLoc = readSourceLocation();
2246e5dd7070Spatrick }
2247e5dd7070Spatrick
VisitFriendTemplateDecl(FriendTemplateDecl * D)2248e5dd7070Spatrick void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
2249e5dd7070Spatrick VisitDecl(D);
2250e5dd7070Spatrick unsigned NumParams = Record.readInt();
2251e5dd7070Spatrick D->NumParams = NumParams;
2252*12c85518Srobert D->Params = new (Reader.getContext()) TemplateParameterList *[NumParams];
2253e5dd7070Spatrick for (unsigned i = 0; i != NumParams; ++i)
2254e5dd7070Spatrick D->Params[i] = Record.readTemplateParameterList();
2255e5dd7070Spatrick if (Record.readInt()) // HasFriendDecl
2256e5dd7070Spatrick D->Friend = readDeclAs<NamedDecl>();
2257e5dd7070Spatrick else
2258e5dd7070Spatrick D->Friend = readTypeSourceInfo();
2259e5dd7070Spatrick D->FriendLoc = readSourceLocation();
2260e5dd7070Spatrick }
2261e5dd7070Spatrick
VisitTemplateDecl(TemplateDecl * D)2262*12c85518Srobert void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
2263e5dd7070Spatrick VisitNamedDecl(D);
2264e5dd7070Spatrick
2265*12c85518Srobert assert(!D->TemplateParams && "TemplateParams already set!");
2266*12c85518Srobert D->TemplateParams = Record.readTemplateParameterList();
2267*12c85518Srobert D->init(readDeclAs<NamedDecl>());
2268e5dd7070Spatrick }
2269e5dd7070Spatrick
VisitConceptDecl(ConceptDecl * D)2270e5dd7070Spatrick void ASTDeclReader::VisitConceptDecl(ConceptDecl *D) {
2271e5dd7070Spatrick VisitTemplateDecl(D);
2272e5dd7070Spatrick D->ConstraintExpr = Record.readExpr();
2273e5dd7070Spatrick mergeMergeable(D);
2274e5dd7070Spatrick }
2275e5dd7070Spatrick
VisitImplicitConceptSpecializationDecl(ImplicitConceptSpecializationDecl * D)2276*12c85518Srobert void ASTDeclReader::VisitImplicitConceptSpecializationDecl(
2277*12c85518Srobert ImplicitConceptSpecializationDecl *D) {
2278*12c85518Srobert // The size of the template list was read during creation of the Decl, so we
2279*12c85518Srobert // don't have to re-read it here.
2280*12c85518Srobert VisitDecl(D);
2281*12c85518Srobert llvm::SmallVector<TemplateArgument, 4> Args;
2282*12c85518Srobert for (unsigned I = 0; I < D->NumTemplateArgs; ++I)
2283*12c85518Srobert Args.push_back(Record.readTemplateArgument(/*Canonicalize=*/true));
2284*12c85518Srobert D->setTemplateArguments(Args);
2285*12c85518Srobert }
2286*12c85518Srobert
VisitRequiresExprBodyDecl(RequiresExprBodyDecl * D)2287e5dd7070Spatrick void ASTDeclReader::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {
2288e5dd7070Spatrick }
2289e5dd7070Spatrick
2290e5dd7070Spatrick ASTDeclReader::RedeclarableResult
VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl * D)2291e5dd7070Spatrick ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
2292e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarable(D);
2293e5dd7070Spatrick
2294e5dd7070Spatrick // Make sure we've allocated the Common pointer first. We do this before
2295e5dd7070Spatrick // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2296e5dd7070Spatrick RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2297e5dd7070Spatrick if (!CanonD->Common) {
2298e5dd7070Spatrick CanonD->Common = CanonD->newCommon(Reader.getContext());
2299e5dd7070Spatrick Reader.PendingDefinitions.insert(CanonD);
2300e5dd7070Spatrick }
2301e5dd7070Spatrick D->Common = CanonD->Common;
2302e5dd7070Spatrick
2303e5dd7070Spatrick // If this is the first declaration of the template, fill in the information
2304e5dd7070Spatrick // for the 'common' pointer.
2305e5dd7070Spatrick if (ThisDeclID == Redecl.getFirstID()) {
2306e5dd7070Spatrick if (auto *RTD = readDeclAs<RedeclarableTemplateDecl>()) {
2307e5dd7070Spatrick assert(RTD->getKind() == D->getKind() &&
2308e5dd7070Spatrick "InstantiatedFromMemberTemplate kind mismatch");
2309e5dd7070Spatrick D->setInstantiatedFromMemberTemplate(RTD);
2310e5dd7070Spatrick if (Record.readInt())
2311e5dd7070Spatrick D->setMemberSpecialization();
2312e5dd7070Spatrick }
2313e5dd7070Spatrick }
2314e5dd7070Spatrick
2315*12c85518Srobert VisitTemplateDecl(D);
2316e5dd7070Spatrick D->IdentifierNamespace = Record.readInt();
2317e5dd7070Spatrick
2318e5dd7070Spatrick return Redecl;
2319e5dd7070Spatrick }
2320e5dd7070Spatrick
VisitClassTemplateDecl(ClassTemplateDecl * D)2321e5dd7070Spatrick void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
2322e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2323*12c85518Srobert mergeRedeclarableTemplate(D, Redecl);
2324e5dd7070Spatrick
2325e5dd7070Spatrick if (ThisDeclID == Redecl.getFirstID()) {
2326e5dd7070Spatrick // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2327e5dd7070Spatrick // the specializations.
2328e5dd7070Spatrick SmallVector<serialization::DeclID, 32> SpecIDs;
2329e5dd7070Spatrick readDeclIDList(SpecIDs);
2330e5dd7070Spatrick ASTDeclReader::AddLazySpecializations(D, SpecIDs);
2331e5dd7070Spatrick }
2332e5dd7070Spatrick
2333e5dd7070Spatrick if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2334e5dd7070Spatrick // We were loaded before our templated declaration was. We've not set up
2335e5dd7070Spatrick // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2336e5dd7070Spatrick // it now.
2337e5dd7070Spatrick Reader.getContext().getInjectedClassNameType(
2338e5dd7070Spatrick D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
2339e5dd7070Spatrick }
2340e5dd7070Spatrick }
2341e5dd7070Spatrick
VisitBuiltinTemplateDecl(BuiltinTemplateDecl * D)2342e5dd7070Spatrick void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2343e5dd7070Spatrick llvm_unreachable("BuiltinTemplates are not serialized");
2344e5dd7070Spatrick }
2345e5dd7070Spatrick
2346e5dd7070Spatrick /// TODO: Unify with ClassTemplateDecl version?
2347e5dd7070Spatrick /// May require unifying ClassTemplateDecl and
2348e5dd7070Spatrick /// VarTemplateDecl beyond TemplateDecl...
VisitVarTemplateDecl(VarTemplateDecl * D)2349e5dd7070Spatrick void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2350e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2351*12c85518Srobert mergeRedeclarableTemplate(D, Redecl);
2352e5dd7070Spatrick
2353e5dd7070Spatrick if (ThisDeclID == Redecl.getFirstID()) {
2354e5dd7070Spatrick // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
2355e5dd7070Spatrick // the specializations.
2356e5dd7070Spatrick SmallVector<serialization::DeclID, 32> SpecIDs;
2357e5dd7070Spatrick readDeclIDList(SpecIDs);
2358e5dd7070Spatrick ASTDeclReader::AddLazySpecializations(D, SpecIDs);
2359e5dd7070Spatrick }
2360e5dd7070Spatrick }
2361e5dd7070Spatrick
2362e5dd7070Spatrick ASTDeclReader::RedeclarableResult
VisitClassTemplateSpecializationDeclImpl(ClassTemplateSpecializationDecl * D)2363e5dd7070Spatrick ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2364e5dd7070Spatrick ClassTemplateSpecializationDecl *D) {
2365e5dd7070Spatrick RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
2366e5dd7070Spatrick
2367e5dd7070Spatrick ASTContext &C = Reader.getContext();
2368e5dd7070Spatrick if (Decl *InstD = readDecl()) {
2369e5dd7070Spatrick if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
2370e5dd7070Spatrick D->SpecializedTemplate = CTD;
2371e5dd7070Spatrick } else {
2372e5dd7070Spatrick SmallVector<TemplateArgument, 8> TemplArgs;
2373e5dd7070Spatrick Record.readTemplateArgumentList(TemplArgs);
2374e5dd7070Spatrick TemplateArgumentList *ArgList
2375e5dd7070Spatrick = TemplateArgumentList::CreateCopy(C, TemplArgs);
2376e5dd7070Spatrick auto *PS =
2377e5dd7070Spatrick new (C) ClassTemplateSpecializationDecl::
2378e5dd7070Spatrick SpecializedPartialSpecialization();
2379e5dd7070Spatrick PS->PartialSpecialization
2380e5dd7070Spatrick = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2381e5dd7070Spatrick PS->TemplateArgs = ArgList;
2382e5dd7070Spatrick D->SpecializedTemplate = PS;
2383e5dd7070Spatrick }
2384e5dd7070Spatrick }
2385e5dd7070Spatrick
2386e5dd7070Spatrick SmallVector<TemplateArgument, 8> TemplArgs;
2387e5dd7070Spatrick Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
2388e5dd7070Spatrick D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
2389e5dd7070Spatrick D->PointOfInstantiation = readSourceLocation();
2390e5dd7070Spatrick D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
2391e5dd7070Spatrick
2392e5dd7070Spatrick bool writtenAsCanonicalDecl = Record.readInt();
2393e5dd7070Spatrick if (writtenAsCanonicalDecl) {
2394e5dd7070Spatrick auto *CanonPattern = readDeclAs<ClassTemplateDecl>();
2395e5dd7070Spatrick if (D->isCanonicalDecl()) { // It's kept in the folding set.
2396e5dd7070Spatrick // Set this as, or find, the canonical declaration for this specialization
2397e5dd7070Spatrick ClassTemplateSpecializationDecl *CanonSpec;
2398e5dd7070Spatrick if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
2399e5dd7070Spatrick CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
2400e5dd7070Spatrick .GetOrInsertNode(Partial);
2401e5dd7070Spatrick } else {
2402e5dd7070Spatrick CanonSpec =
2403e5dd7070Spatrick CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2404e5dd7070Spatrick }
2405e5dd7070Spatrick // If there was already a canonical specialization, merge into it.
2406e5dd7070Spatrick if (CanonSpec != D) {
2407e5dd7070Spatrick mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2408e5dd7070Spatrick
2409e5dd7070Spatrick // This declaration might be a definition. Merge with any existing
2410e5dd7070Spatrick // definition.
2411e5dd7070Spatrick if (auto *DDD = D->DefinitionData) {
2412e5dd7070Spatrick if (CanonSpec->DefinitionData)
2413e5dd7070Spatrick MergeDefinitionData(CanonSpec, std::move(*DDD));
2414e5dd7070Spatrick else
2415e5dd7070Spatrick CanonSpec->DefinitionData = D->DefinitionData;
2416e5dd7070Spatrick }
2417e5dd7070Spatrick D->DefinitionData = CanonSpec->DefinitionData;
2418e5dd7070Spatrick }
2419e5dd7070Spatrick }
2420e5dd7070Spatrick }
2421e5dd7070Spatrick
2422e5dd7070Spatrick // Explicit info.
2423e5dd7070Spatrick if (TypeSourceInfo *TyInfo = readTypeSourceInfo()) {
2424e5dd7070Spatrick auto *ExplicitInfo =
2425e5dd7070Spatrick new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2426e5dd7070Spatrick ExplicitInfo->TypeAsWritten = TyInfo;
2427e5dd7070Spatrick ExplicitInfo->ExternLoc = readSourceLocation();
2428e5dd7070Spatrick ExplicitInfo->TemplateKeywordLoc = readSourceLocation();
2429e5dd7070Spatrick D->ExplicitInfo = ExplicitInfo;
2430e5dd7070Spatrick }
2431e5dd7070Spatrick
2432e5dd7070Spatrick return Redecl;
2433e5dd7070Spatrick }
2434e5dd7070Spatrick
VisitClassTemplatePartialSpecializationDecl(ClassTemplatePartialSpecializationDecl * D)2435e5dd7070Spatrick void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
2436e5dd7070Spatrick ClassTemplatePartialSpecializationDecl *D) {
2437e5dd7070Spatrick // We need to read the template params first because redeclarable is going to
2438e5dd7070Spatrick // need them for profiling
2439e5dd7070Spatrick TemplateParameterList *Params = Record.readTemplateParameterList();
2440e5dd7070Spatrick D->TemplateParams = Params;
2441e5dd7070Spatrick D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
2442e5dd7070Spatrick
2443e5dd7070Spatrick RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
2444e5dd7070Spatrick
2445e5dd7070Spatrick // These are read/set from/to the first declaration.
2446e5dd7070Spatrick if (ThisDeclID == Redecl.getFirstID()) {
2447e5dd7070Spatrick D->InstantiatedFromMember.setPointer(
2448e5dd7070Spatrick readDeclAs<ClassTemplatePartialSpecializationDecl>());
2449e5dd7070Spatrick D->InstantiatedFromMember.setInt(Record.readInt());
2450e5dd7070Spatrick }
2451e5dd7070Spatrick }
2452e5dd7070Spatrick
VisitClassScopeFunctionSpecializationDecl(ClassScopeFunctionSpecializationDecl * D)2453e5dd7070Spatrick void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2454e5dd7070Spatrick ClassScopeFunctionSpecializationDecl *D) {
2455e5dd7070Spatrick VisitDecl(D);
2456e5dd7070Spatrick D->Specialization = readDeclAs<CXXMethodDecl>();
2457e5dd7070Spatrick if (Record.readInt())
2458e5dd7070Spatrick D->TemplateArgs = Record.readASTTemplateArgumentListInfo();
2459e5dd7070Spatrick }
2460e5dd7070Spatrick
VisitFunctionTemplateDecl(FunctionTemplateDecl * D)2461e5dd7070Spatrick void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
2462e5dd7070Spatrick RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2463e5dd7070Spatrick
2464e5dd7070Spatrick if (ThisDeclID == Redecl.getFirstID()) {
2465e5dd7070Spatrick // This FunctionTemplateDecl owns a CommonPtr; read it.
2466e5dd7070Spatrick SmallVector<serialization::DeclID, 32> SpecIDs;
2467e5dd7070Spatrick readDeclIDList(SpecIDs);
2468e5dd7070Spatrick ASTDeclReader::AddLazySpecializations(D, SpecIDs);
2469e5dd7070Spatrick }
2470e5dd7070Spatrick }
2471e5dd7070Spatrick
2472e5dd7070Spatrick /// TODO: Unify with ClassTemplateSpecializationDecl version?
2473e5dd7070Spatrick /// May require unifying ClassTemplate(Partial)SpecializationDecl and
2474e5dd7070Spatrick /// VarTemplate(Partial)SpecializationDecl with a new data
2475e5dd7070Spatrick /// structure Template(Partial)SpecializationDecl, and
2476e5dd7070Spatrick /// using Template(Partial)SpecializationDecl as input type.
2477e5dd7070Spatrick ASTDeclReader::RedeclarableResult
VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl * D)2478e5dd7070Spatrick ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2479e5dd7070Spatrick VarTemplateSpecializationDecl *D) {
2480e5dd7070Spatrick ASTContext &C = Reader.getContext();
2481e5dd7070Spatrick if (Decl *InstD = readDecl()) {
2482e5dd7070Spatrick if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2483e5dd7070Spatrick D->SpecializedTemplate = VTD;
2484e5dd7070Spatrick } else {
2485e5dd7070Spatrick SmallVector<TemplateArgument, 8> TemplArgs;
2486e5dd7070Spatrick Record.readTemplateArgumentList(TemplArgs);
2487e5dd7070Spatrick TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
2488e5dd7070Spatrick C, TemplArgs);
2489e5dd7070Spatrick auto *PS =
2490e5dd7070Spatrick new (C)
2491e5dd7070Spatrick VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2492e5dd7070Spatrick PS->PartialSpecialization =
2493e5dd7070Spatrick cast<VarTemplatePartialSpecializationDecl>(InstD);
2494e5dd7070Spatrick PS->TemplateArgs = ArgList;
2495e5dd7070Spatrick D->SpecializedTemplate = PS;
2496e5dd7070Spatrick }
2497e5dd7070Spatrick }
2498e5dd7070Spatrick
2499e5dd7070Spatrick // Explicit info.
2500e5dd7070Spatrick if (TypeSourceInfo *TyInfo = readTypeSourceInfo()) {
2501e5dd7070Spatrick auto *ExplicitInfo =
2502e5dd7070Spatrick new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2503e5dd7070Spatrick ExplicitInfo->TypeAsWritten = TyInfo;
2504e5dd7070Spatrick ExplicitInfo->ExternLoc = readSourceLocation();
2505e5dd7070Spatrick ExplicitInfo->TemplateKeywordLoc = readSourceLocation();
2506e5dd7070Spatrick D->ExplicitInfo = ExplicitInfo;
2507e5dd7070Spatrick }
2508e5dd7070Spatrick
2509e5dd7070Spatrick SmallVector<TemplateArgument, 8> TemplArgs;
2510e5dd7070Spatrick Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
2511e5dd7070Spatrick D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
2512e5dd7070Spatrick D->PointOfInstantiation = readSourceLocation();
2513e5dd7070Spatrick D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
2514e5dd7070Spatrick D->IsCompleteDefinition = Record.readInt();
2515e5dd7070Spatrick
2516*12c85518Srobert RedeclarableResult Redecl = VisitVarDeclImpl(D);
2517*12c85518Srobert
2518e5dd7070Spatrick bool writtenAsCanonicalDecl = Record.readInt();
2519e5dd7070Spatrick if (writtenAsCanonicalDecl) {
2520e5dd7070Spatrick auto *CanonPattern = readDeclAs<VarTemplateDecl>();
2521e5dd7070Spatrick if (D->isCanonicalDecl()) { // It's kept in the folding set.
2522*12c85518Srobert VarTemplateSpecializationDecl *CanonSpec;
2523e5dd7070Spatrick if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
2524*12c85518Srobert CanonSpec = CanonPattern->getCommonPtr()
2525*12c85518Srobert ->PartialSpecializations.GetOrInsertNode(Partial);
2526e5dd7070Spatrick } else {
2527*12c85518Srobert CanonSpec =
2528e5dd7070Spatrick CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2529e5dd7070Spatrick }
2530*12c85518Srobert // If we already have a matching specialization, merge it.
2531*12c85518Srobert if (CanonSpec != D)
2532*12c85518Srobert mergeRedeclarable<VarDecl>(D, CanonSpec, Redecl);
2533e5dd7070Spatrick }
2534e5dd7070Spatrick }
2535e5dd7070Spatrick
2536e5dd7070Spatrick return Redecl;
2537e5dd7070Spatrick }
2538e5dd7070Spatrick
2539e5dd7070Spatrick /// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2540e5dd7070Spatrick /// May require unifying ClassTemplate(Partial)SpecializationDecl and
2541e5dd7070Spatrick /// VarTemplate(Partial)SpecializationDecl with a new data
2542e5dd7070Spatrick /// structure Template(Partial)SpecializationDecl, and
2543e5dd7070Spatrick /// using Template(Partial)SpecializationDecl as input type.
VisitVarTemplatePartialSpecializationDecl(VarTemplatePartialSpecializationDecl * D)2544e5dd7070Spatrick void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2545e5dd7070Spatrick VarTemplatePartialSpecializationDecl *D) {
2546e5dd7070Spatrick TemplateParameterList *Params = Record.readTemplateParameterList();
2547e5dd7070Spatrick D->TemplateParams = Params;
2548e5dd7070Spatrick D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
2549e5dd7070Spatrick
2550e5dd7070Spatrick RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2551e5dd7070Spatrick
2552e5dd7070Spatrick // These are read/set from/to the first declaration.
2553e5dd7070Spatrick if (ThisDeclID == Redecl.getFirstID()) {
2554e5dd7070Spatrick D->InstantiatedFromMember.setPointer(
2555e5dd7070Spatrick readDeclAs<VarTemplatePartialSpecializationDecl>());
2556e5dd7070Spatrick D->InstantiatedFromMember.setInt(Record.readInt());
2557e5dd7070Spatrick }
2558e5dd7070Spatrick }
2559e5dd7070Spatrick
VisitTemplateTypeParmDecl(TemplateTypeParmDecl * D)2560e5dd7070Spatrick void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
2561e5dd7070Spatrick VisitTypeDecl(D);
2562e5dd7070Spatrick
2563e5dd7070Spatrick D->setDeclaredWithTypename(Record.readInt());
2564e5dd7070Spatrick
2565e5dd7070Spatrick if (Record.readBool()) {
2566e5dd7070Spatrick NestedNameSpecifierLoc NNS = Record.readNestedNameSpecifierLoc();
2567e5dd7070Spatrick DeclarationNameInfo DN = Record.readDeclarationNameInfo();
2568e5dd7070Spatrick ConceptDecl *NamedConcept = Record.readDeclAs<ConceptDecl>();
2569e5dd7070Spatrick const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
2570e5dd7070Spatrick if (Record.readBool())
2571e5dd7070Spatrick ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
2572e5dd7070Spatrick Expr *ImmediatelyDeclaredConstraint = Record.readExpr();
2573e5dd7070Spatrick D->setTypeConstraint(NNS, DN, /*FoundDecl=*/nullptr, NamedConcept,
2574e5dd7070Spatrick ArgsAsWritten, ImmediatelyDeclaredConstraint);
2575e5dd7070Spatrick if ((D->ExpandedParameterPack = Record.readInt()))
2576e5dd7070Spatrick D->NumExpanded = Record.readInt();
2577e5dd7070Spatrick }
2578e5dd7070Spatrick
2579e5dd7070Spatrick if (Record.readInt())
2580e5dd7070Spatrick D->setDefaultArgument(readTypeSourceInfo());
2581e5dd7070Spatrick }
2582e5dd7070Spatrick
VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl * D)2583e5dd7070Spatrick void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
2584e5dd7070Spatrick VisitDeclaratorDecl(D);
2585e5dd7070Spatrick // TemplateParmPosition.
2586e5dd7070Spatrick D->setDepth(Record.readInt());
2587e5dd7070Spatrick D->setPosition(Record.readInt());
2588e5dd7070Spatrick if (D->hasPlaceholderTypeConstraint())
2589e5dd7070Spatrick D->setPlaceholderTypeConstraint(Record.readExpr());
2590e5dd7070Spatrick if (D->isExpandedParameterPack()) {
2591e5dd7070Spatrick auto TypesAndInfos =
2592e5dd7070Spatrick D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
2593e5dd7070Spatrick for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
2594e5dd7070Spatrick new (&TypesAndInfos[I].first) QualType(Record.readType());
2595e5dd7070Spatrick TypesAndInfos[I].second = readTypeSourceInfo();
2596e5dd7070Spatrick }
2597e5dd7070Spatrick } else {
2598e5dd7070Spatrick // Rest of NonTypeTemplateParmDecl.
2599e5dd7070Spatrick D->ParameterPack = Record.readInt();
2600e5dd7070Spatrick if (Record.readInt())
2601e5dd7070Spatrick D->setDefaultArgument(Record.readExpr());
2602e5dd7070Spatrick }
2603e5dd7070Spatrick }
2604e5dd7070Spatrick
VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl * D)2605e5dd7070Spatrick void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
2606e5dd7070Spatrick VisitTemplateDecl(D);
2607e5dd7070Spatrick // TemplateParmPosition.
2608e5dd7070Spatrick D->setDepth(Record.readInt());
2609e5dd7070Spatrick D->setPosition(Record.readInt());
2610e5dd7070Spatrick if (D->isExpandedParameterPack()) {
2611e5dd7070Spatrick auto **Data = D->getTrailingObjects<TemplateParameterList *>();
2612e5dd7070Spatrick for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2613e5dd7070Spatrick I != N; ++I)
2614e5dd7070Spatrick Data[I] = Record.readTemplateParameterList();
2615e5dd7070Spatrick } else {
2616e5dd7070Spatrick // Rest of TemplateTemplateParmDecl.
2617e5dd7070Spatrick D->ParameterPack = Record.readInt();
2618e5dd7070Spatrick if (Record.readInt())
2619e5dd7070Spatrick D->setDefaultArgument(Reader.getContext(),
2620e5dd7070Spatrick Record.readTemplateArgumentLoc());
2621e5dd7070Spatrick }
2622e5dd7070Spatrick }
2623e5dd7070Spatrick
VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl * D)2624e5dd7070Spatrick void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2625*12c85518Srobert RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2626*12c85518Srobert mergeRedeclarableTemplate(D, Redecl);
2627e5dd7070Spatrick }
2628e5dd7070Spatrick
VisitStaticAssertDecl(StaticAssertDecl * D)2629e5dd7070Spatrick void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
2630e5dd7070Spatrick VisitDecl(D);
2631e5dd7070Spatrick D->AssertExprAndFailed.setPointer(Record.readExpr());
2632e5dd7070Spatrick D->AssertExprAndFailed.setInt(Record.readInt());
2633e5dd7070Spatrick D->Message = cast_or_null<StringLiteral>(Record.readExpr());
2634e5dd7070Spatrick D->RParenLoc = readSourceLocation();
2635e5dd7070Spatrick }
2636e5dd7070Spatrick
VisitEmptyDecl(EmptyDecl * D)2637e5dd7070Spatrick void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2638e5dd7070Spatrick VisitDecl(D);
2639e5dd7070Spatrick }
2640e5dd7070Spatrick
VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl * D)2641e5dd7070Spatrick void ASTDeclReader::VisitLifetimeExtendedTemporaryDecl(
2642e5dd7070Spatrick LifetimeExtendedTemporaryDecl *D) {
2643e5dd7070Spatrick VisitDecl(D);
2644e5dd7070Spatrick D->ExtendingDecl = readDeclAs<ValueDecl>();
2645e5dd7070Spatrick D->ExprWithTemporary = Record.readStmt();
2646a9ac8606Spatrick if (Record.readInt()) {
2647e5dd7070Spatrick D->Value = new (D->getASTContext()) APValue(Record.readAPValue());
2648a9ac8606Spatrick D->getASTContext().addDestruction(D->Value);
2649a9ac8606Spatrick }
2650e5dd7070Spatrick D->ManglingNumber = Record.readInt();
2651e5dd7070Spatrick mergeMergeable(D);
2652e5dd7070Spatrick }
2653e5dd7070Spatrick
2654e5dd7070Spatrick std::pair<uint64_t, uint64_t>
VisitDeclContext(DeclContext * DC)2655e5dd7070Spatrick ASTDeclReader::VisitDeclContext(DeclContext *DC) {
2656e5dd7070Spatrick uint64_t LexicalOffset = ReadLocalOffset();
2657e5dd7070Spatrick uint64_t VisibleOffset = ReadLocalOffset();
2658e5dd7070Spatrick return std::make_pair(LexicalOffset, VisibleOffset);
2659e5dd7070Spatrick }
2660e5dd7070Spatrick
2661e5dd7070Spatrick template <typename T>
2662e5dd7070Spatrick ASTDeclReader::RedeclarableResult
VisitRedeclarable(Redeclarable<T> * D)2663e5dd7070Spatrick ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
2664e5dd7070Spatrick DeclID FirstDeclID = readDeclID();
2665e5dd7070Spatrick Decl *MergeWith = nullptr;
2666e5dd7070Spatrick
2667e5dd7070Spatrick bool IsKeyDecl = ThisDeclID == FirstDeclID;
2668e5dd7070Spatrick bool IsFirstLocalDecl = false;
2669e5dd7070Spatrick
2670e5dd7070Spatrick uint64_t RedeclOffset = 0;
2671e5dd7070Spatrick
2672e5dd7070Spatrick // 0 indicates that this declaration was the only declaration of its entity,
2673e5dd7070Spatrick // and is used for space optimization.
2674e5dd7070Spatrick if (FirstDeclID == 0) {
2675e5dd7070Spatrick FirstDeclID = ThisDeclID;
2676e5dd7070Spatrick IsKeyDecl = true;
2677e5dd7070Spatrick IsFirstLocalDecl = true;
2678e5dd7070Spatrick } else if (unsigned N = Record.readInt()) {
2679e5dd7070Spatrick // This declaration was the first local declaration, but may have imported
2680e5dd7070Spatrick // other declarations.
2681e5dd7070Spatrick IsKeyDecl = N == 1;
2682e5dd7070Spatrick IsFirstLocalDecl = true;
2683e5dd7070Spatrick
2684e5dd7070Spatrick // We have some declarations that must be before us in our redeclaration
2685e5dd7070Spatrick // chain. Read them now, and remember that we ought to merge with one of
2686e5dd7070Spatrick // them.
2687e5dd7070Spatrick // FIXME: Provide a known merge target to the second and subsequent such
2688e5dd7070Spatrick // declaration.
2689e5dd7070Spatrick for (unsigned I = 0; I != N - 1; ++I)
2690e5dd7070Spatrick MergeWith = readDecl();
2691e5dd7070Spatrick
2692e5dd7070Spatrick RedeclOffset = ReadLocalOffset();
2693e5dd7070Spatrick } else {
2694e5dd7070Spatrick // This declaration was not the first local declaration. Read the first
2695e5dd7070Spatrick // local declaration now, to trigger the import of other redeclarations.
2696e5dd7070Spatrick (void)readDecl();
2697e5dd7070Spatrick }
2698e5dd7070Spatrick
2699e5dd7070Spatrick auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2700e5dd7070Spatrick if (FirstDecl != D) {
2701e5dd7070Spatrick // We delay loading of the redeclaration chain to avoid deeply nested calls.
2702e5dd7070Spatrick // We temporarily set the first (canonical) declaration as the previous one
2703e5dd7070Spatrick // which is the one that matters and mark the real previous DeclID to be
2704e5dd7070Spatrick // loaded & attached later on.
2705e5dd7070Spatrick D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
2706e5dd7070Spatrick D->First = FirstDecl->getCanonicalDecl();
2707e5dd7070Spatrick }
2708e5dd7070Spatrick
2709e5dd7070Spatrick auto *DAsT = static_cast<T *>(D);
2710e5dd7070Spatrick
2711e5dd7070Spatrick // Note that we need to load local redeclarations of this decl and build a
2712e5dd7070Spatrick // decl chain for them. This must happen *after* we perform the preloading
2713e5dd7070Spatrick // above; this ensures that the redeclaration chain is built in the correct
2714e5dd7070Spatrick // order.
2715e5dd7070Spatrick if (IsFirstLocalDecl)
2716e5dd7070Spatrick Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
2717e5dd7070Spatrick
2718e5dd7070Spatrick return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
2719e5dd7070Spatrick }
2720e5dd7070Spatrick
2721e5dd7070Spatrick /// Attempts to merge the given declaration (D) with another declaration
2722e5dd7070Spatrick /// of the same entity.
2723e5dd7070Spatrick template <typename T>
mergeRedeclarable(Redeclarable<T> * DBase,RedeclarableResult & Redecl)2724e5dd7070Spatrick void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
2725*12c85518Srobert RedeclarableResult &Redecl) {
2726e5dd7070Spatrick // If modules are not available, there is no reason to perform this merge.
2727e5dd7070Spatrick if (!Reader.getContext().getLangOpts().Modules)
2728e5dd7070Spatrick return;
2729e5dd7070Spatrick
2730e5dd7070Spatrick // If we're not the canonical declaration, we don't need to merge.
2731e5dd7070Spatrick if (!DBase->isFirstDecl())
2732e5dd7070Spatrick return;
2733e5dd7070Spatrick
2734e5dd7070Spatrick auto *D = static_cast<T *>(DBase);
2735e5dd7070Spatrick
2736e5dd7070Spatrick if (auto *Existing = Redecl.getKnownMergeTarget())
2737e5dd7070Spatrick // We already know of an existing declaration we should merge with.
2738*12c85518Srobert mergeRedeclarable(D, cast<T>(Existing), Redecl);
2739e5dd7070Spatrick else if (FindExistingResult ExistingRes = findExisting(D))
2740e5dd7070Spatrick if (T *Existing = ExistingRes)
2741*12c85518Srobert mergeRedeclarable(D, Existing, Redecl);
2742*12c85518Srobert }
2743*12c85518Srobert
mergeRedeclarableTemplate(RedeclarableTemplateDecl * D,RedeclarableResult & Redecl)2744*12c85518Srobert void ASTDeclReader::mergeRedeclarableTemplate(RedeclarableTemplateDecl *D,
2745*12c85518Srobert RedeclarableResult &Redecl) {
2746*12c85518Srobert mergeRedeclarable(D, Redecl);
2747*12c85518Srobert // If we merged the template with a prior declaration chain, merge the
2748*12c85518Srobert // common pointer.
2749*12c85518Srobert // FIXME: Actually merge here, don't just overwrite.
2750*12c85518Srobert D->Common = D->getCanonicalDecl()->Common;
2751e5dd7070Spatrick }
2752e5dd7070Spatrick
2753e5dd7070Spatrick /// "Cast" to type T, asserting if we don't have an implicit conversion.
2754e5dd7070Spatrick /// We use this to put code in a template that will only be valid for certain
2755e5dd7070Spatrick /// instantiations.
assert_cast(T t)2756e5dd7070Spatrick template<typename T> static T assert_cast(T t) { return t; }
assert_cast(...)2757e5dd7070Spatrick template<typename T> static T assert_cast(...) {
2758e5dd7070Spatrick llvm_unreachable("bad assert_cast");
2759e5dd7070Spatrick }
2760e5dd7070Spatrick
2761e5dd7070Spatrick /// Merge together the pattern declarations from two template
2762e5dd7070Spatrick /// declarations.
mergeTemplatePattern(RedeclarableTemplateDecl * D,RedeclarableTemplateDecl * Existing,bool IsKeyDecl)2763e5dd7070Spatrick void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2764e5dd7070Spatrick RedeclarableTemplateDecl *Existing,
2765*12c85518Srobert bool IsKeyDecl) {
2766e5dd7070Spatrick auto *DPattern = D->getTemplatedDecl();
2767e5dd7070Spatrick auto *ExistingPattern = Existing->getTemplatedDecl();
2768e5dd7070Spatrick RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2769e5dd7070Spatrick DPattern->getCanonicalDecl()->getGlobalID(),
2770e5dd7070Spatrick IsKeyDecl);
2771e5dd7070Spatrick
2772e5dd7070Spatrick if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2773e5dd7070Spatrick // Merge with any existing definition.
2774e5dd7070Spatrick // FIXME: This is duplicated in several places. Refactor.
2775e5dd7070Spatrick auto *ExistingClass =
2776e5dd7070Spatrick cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
2777e5dd7070Spatrick if (auto *DDD = DClass->DefinitionData) {
2778e5dd7070Spatrick if (ExistingClass->DefinitionData) {
2779e5dd7070Spatrick MergeDefinitionData(ExistingClass, std::move(*DDD));
2780e5dd7070Spatrick } else {
2781e5dd7070Spatrick ExistingClass->DefinitionData = DClass->DefinitionData;
2782e5dd7070Spatrick // We may have skipped this before because we thought that DClass
2783e5dd7070Spatrick // was the canonical declaration.
2784e5dd7070Spatrick Reader.PendingDefinitions.insert(DClass);
2785e5dd7070Spatrick }
2786e5dd7070Spatrick }
2787e5dd7070Spatrick DClass->DefinitionData = ExistingClass->DefinitionData;
2788e5dd7070Spatrick
2789e5dd7070Spatrick return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2790e5dd7070Spatrick Result);
2791e5dd7070Spatrick }
2792e5dd7070Spatrick if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2793e5dd7070Spatrick return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2794e5dd7070Spatrick Result);
2795e5dd7070Spatrick if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2796e5dd7070Spatrick return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
2797e5dd7070Spatrick if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2798e5dd7070Spatrick return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2799e5dd7070Spatrick Result);
2800e5dd7070Spatrick llvm_unreachable("merged an unknown kind of redeclarable template");
2801e5dd7070Spatrick }
2802e5dd7070Spatrick
2803e5dd7070Spatrick /// Attempts to merge the given declaration (D) with another declaration
2804e5dd7070Spatrick /// of the same entity.
2805e5dd7070Spatrick template <typename T>
mergeRedeclarable(Redeclarable<T> * DBase,T * Existing,RedeclarableResult & Redecl)2806e5dd7070Spatrick void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2807*12c85518Srobert RedeclarableResult &Redecl) {
2808e5dd7070Spatrick auto *D = static_cast<T *>(DBase);
2809e5dd7070Spatrick T *ExistingCanon = Existing->getCanonicalDecl();
2810e5dd7070Spatrick T *DCanon = D->getCanonicalDecl();
2811e5dd7070Spatrick if (ExistingCanon != DCanon) {
2812e5dd7070Spatrick // Have our redeclaration link point back at the canonical declaration
2813e5dd7070Spatrick // of the existing declaration, so that this declaration has the
2814e5dd7070Spatrick // appropriate canonical declaration.
2815e5dd7070Spatrick D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
2816e5dd7070Spatrick D->First = ExistingCanon;
2817e5dd7070Spatrick ExistingCanon->Used |= D->Used;
2818e5dd7070Spatrick D->Used = false;
2819e5dd7070Spatrick
2820e5dd7070Spatrick // When we merge a namespace, update its pointer to the first namespace.
2821e5dd7070Spatrick // We cannot have loaded any redeclarations of this declaration yet, so
2822e5dd7070Spatrick // there's nothing else that needs to be updated.
2823e5dd7070Spatrick if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
2824*12c85518Srobert Namespace->AnonOrFirstNamespaceAndFlags.setPointer(
2825e5dd7070Spatrick assert_cast<NamespaceDecl *>(ExistingCanon));
2826e5dd7070Spatrick
2827e5dd7070Spatrick // When we merge a template, merge its pattern.
2828e5dd7070Spatrick if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2829e5dd7070Spatrick mergeTemplatePattern(
2830e5dd7070Spatrick DTemplate, assert_cast<RedeclarableTemplateDecl *>(ExistingCanon),
2831*12c85518Srobert Redecl.isKeyDecl());
2832e5dd7070Spatrick
2833e5dd7070Spatrick // If this declaration is a key declaration, make a note of that.
2834e5dd7070Spatrick if (Redecl.isKeyDecl())
2835e5dd7070Spatrick Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
2836e5dd7070Spatrick }
2837e5dd7070Spatrick }
2838e5dd7070Spatrick
2839e5dd7070Spatrick /// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2840e5dd7070Spatrick /// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2841e5dd7070Spatrick /// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2842e5dd7070Spatrick /// that some types are mergeable during deserialization, otherwise name
2843e5dd7070Spatrick /// lookup fails. This is the case for EnumConstantDecl.
allowODRLikeMergeInC(NamedDecl * ND)2844e5dd7070Spatrick static bool allowODRLikeMergeInC(NamedDecl *ND) {
2845e5dd7070Spatrick if (!ND)
2846e5dd7070Spatrick return false;
2847e5dd7070Spatrick // TODO: implement merge for other necessary decls.
2848*12c85518Srobert if (isa<EnumConstantDecl, FieldDecl, IndirectFieldDecl>(ND))
2849e5dd7070Spatrick return true;
2850e5dd7070Spatrick return false;
2851e5dd7070Spatrick }
2852e5dd7070Spatrick
2853e5dd7070Spatrick /// Attempts to merge LifetimeExtendedTemporaryDecl with
2854e5dd7070Spatrick /// identical class definitions from two different modules.
mergeMergeable(LifetimeExtendedTemporaryDecl * D)2855e5dd7070Spatrick void ASTDeclReader::mergeMergeable(LifetimeExtendedTemporaryDecl *D) {
2856e5dd7070Spatrick // If modules are not available, there is no reason to perform this merge.
2857e5dd7070Spatrick if (!Reader.getContext().getLangOpts().Modules)
2858e5dd7070Spatrick return;
2859e5dd7070Spatrick
2860e5dd7070Spatrick LifetimeExtendedTemporaryDecl *LETDecl = D;
2861e5dd7070Spatrick
2862e5dd7070Spatrick LifetimeExtendedTemporaryDecl *&LookupResult =
2863e5dd7070Spatrick Reader.LETemporaryForMerging[std::make_pair(
2864e5dd7070Spatrick LETDecl->getExtendingDecl(), LETDecl->getManglingNumber())];
2865e5dd7070Spatrick if (LookupResult)
2866e5dd7070Spatrick Reader.getContext().setPrimaryMergedDecl(LETDecl,
2867e5dd7070Spatrick LookupResult->getCanonicalDecl());
2868e5dd7070Spatrick else
2869e5dd7070Spatrick LookupResult = LETDecl;
2870e5dd7070Spatrick }
2871e5dd7070Spatrick
2872e5dd7070Spatrick /// Attempts to merge the given declaration (D) with another declaration
2873e5dd7070Spatrick /// of the same entity, for the case where the entity is not actually
2874e5dd7070Spatrick /// redeclarable. This happens, for instance, when merging the fields of
2875e5dd7070Spatrick /// identical class definitions from two different modules.
2876e5dd7070Spatrick template<typename T>
mergeMergeable(Mergeable<T> * D)2877e5dd7070Spatrick void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2878e5dd7070Spatrick // If modules are not available, there is no reason to perform this merge.
2879e5dd7070Spatrick if (!Reader.getContext().getLangOpts().Modules)
2880e5dd7070Spatrick return;
2881e5dd7070Spatrick
2882e5dd7070Spatrick // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2883e5dd7070Spatrick // Note that C identically-named things in different translation units are
2884e5dd7070Spatrick // not redeclarations, but may still have compatible types, where ODR-like
2885e5dd7070Spatrick // semantics may apply.
2886e5dd7070Spatrick if (!Reader.getContext().getLangOpts().CPlusPlus &&
2887e5dd7070Spatrick !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
2888e5dd7070Spatrick return;
2889e5dd7070Spatrick
2890e5dd7070Spatrick if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2891e5dd7070Spatrick if (T *Existing = ExistingRes)
2892e5dd7070Spatrick Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2893e5dd7070Spatrick Existing->getCanonicalDecl());
2894e5dd7070Spatrick }
2895e5dd7070Spatrick
VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl * D)2896e5dd7070Spatrick void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2897a9ac8606Spatrick Record.readOMPChildren(D->Data);
2898e5dd7070Spatrick VisitDecl(D);
2899e5dd7070Spatrick }
2900e5dd7070Spatrick
VisitOMPAllocateDecl(OMPAllocateDecl * D)2901e5dd7070Spatrick void ASTDeclReader::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
2902a9ac8606Spatrick Record.readOMPChildren(D->Data);
2903e5dd7070Spatrick VisitDecl(D);
2904e5dd7070Spatrick }
2905e5dd7070Spatrick
VisitOMPRequiresDecl(OMPRequiresDecl * D)2906e5dd7070Spatrick void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
2907a9ac8606Spatrick Record.readOMPChildren(D->Data);
2908e5dd7070Spatrick VisitDecl(D);
2909e5dd7070Spatrick }
2910e5dd7070Spatrick
VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl * D)2911e5dd7070Spatrick void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
2912e5dd7070Spatrick VisitValueDecl(D);
2913e5dd7070Spatrick D->setLocation(readSourceLocation());
2914e5dd7070Spatrick Expr *In = Record.readExpr();
2915e5dd7070Spatrick Expr *Out = Record.readExpr();
2916e5dd7070Spatrick D->setCombinerData(In, Out);
2917e5dd7070Spatrick Expr *Combiner = Record.readExpr();
2918e5dd7070Spatrick D->setCombiner(Combiner);
2919e5dd7070Spatrick Expr *Orig = Record.readExpr();
2920e5dd7070Spatrick Expr *Priv = Record.readExpr();
2921e5dd7070Spatrick D->setInitializerData(Orig, Priv);
2922e5dd7070Spatrick Expr *Init = Record.readExpr();
2923e5dd7070Spatrick auto IK = static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt());
2924e5dd7070Spatrick D->setInitializer(Init, IK);
2925e5dd7070Spatrick D->PrevDeclInScope = readDeclID();
2926e5dd7070Spatrick }
2927e5dd7070Spatrick
VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl * D)2928e5dd7070Spatrick void ASTDeclReader::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
2929a9ac8606Spatrick Record.readOMPChildren(D->Data);
2930e5dd7070Spatrick VisitValueDecl(D);
2931e5dd7070Spatrick D->VarName = Record.readDeclarationName();
2932e5dd7070Spatrick D->PrevDeclInScope = readDeclID();
2933e5dd7070Spatrick }
2934e5dd7070Spatrick
VisitOMPCapturedExprDecl(OMPCapturedExprDecl * D)2935e5dd7070Spatrick void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
2936e5dd7070Spatrick VisitVarDecl(D);
2937e5dd7070Spatrick }
2938e5dd7070Spatrick
2939e5dd7070Spatrick //===----------------------------------------------------------------------===//
2940e5dd7070Spatrick // Attribute Reading
2941e5dd7070Spatrick //===----------------------------------------------------------------------===//
2942e5dd7070Spatrick
2943e5dd7070Spatrick namespace {
2944e5dd7070Spatrick class AttrReader {
2945e5dd7070Spatrick ASTRecordReader &Reader;
2946e5dd7070Spatrick
2947e5dd7070Spatrick public:
AttrReader(ASTRecordReader & Reader)2948e5dd7070Spatrick AttrReader(ASTRecordReader &Reader) : Reader(Reader) {}
2949e5dd7070Spatrick
readInt()2950e5dd7070Spatrick uint64_t readInt() {
2951e5dd7070Spatrick return Reader.readInt();
2952e5dd7070Spatrick }
2953e5dd7070Spatrick
readBool()2954*12c85518Srobert bool readBool() { return Reader.readBool(); }
2955*12c85518Srobert
readSourceRange()2956e5dd7070Spatrick SourceRange readSourceRange() {
2957e5dd7070Spatrick return Reader.readSourceRange();
2958e5dd7070Spatrick }
2959e5dd7070Spatrick
readSourceLocation()2960e5dd7070Spatrick SourceLocation readSourceLocation() {
2961e5dd7070Spatrick return Reader.readSourceLocation();
2962e5dd7070Spatrick }
2963e5dd7070Spatrick
readExpr()2964e5dd7070Spatrick Expr *readExpr() { return Reader.readExpr(); }
2965e5dd7070Spatrick
readString()2966e5dd7070Spatrick std::string readString() {
2967e5dd7070Spatrick return Reader.readString();
2968e5dd7070Spatrick }
2969e5dd7070Spatrick
readTypeSourceInfo()2970e5dd7070Spatrick TypeSourceInfo *readTypeSourceInfo() {
2971e5dd7070Spatrick return Reader.readTypeSourceInfo();
2972e5dd7070Spatrick }
2973e5dd7070Spatrick
readIdentifier()2974e5dd7070Spatrick IdentifierInfo *readIdentifier() {
2975e5dd7070Spatrick return Reader.readIdentifier();
2976e5dd7070Spatrick }
2977e5dd7070Spatrick
readVersionTuple()2978e5dd7070Spatrick VersionTuple readVersionTuple() {
2979e5dd7070Spatrick return Reader.readVersionTuple();
2980e5dd7070Spatrick }
2981e5dd7070Spatrick
readOMPTraitInfo()2982ec727ea7Spatrick OMPTraitInfo *readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
2983ec727ea7Spatrick
GetLocalDeclAs(uint32_t LocalID)2984e5dd7070Spatrick template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
2985e5dd7070Spatrick return Reader.GetLocalDeclAs<T>(LocalID);
2986e5dd7070Spatrick }
2987e5dd7070Spatrick };
2988e5dd7070Spatrick }
2989e5dd7070Spatrick
readAttr()2990e5dd7070Spatrick Attr *ASTRecordReader::readAttr() {
2991e5dd7070Spatrick AttrReader Record(*this);
2992e5dd7070Spatrick auto V = Record.readInt();
2993e5dd7070Spatrick if (!V)
2994e5dd7070Spatrick return nullptr;
2995e5dd7070Spatrick
2996e5dd7070Spatrick Attr *New = nullptr;
2997e5dd7070Spatrick // Kind is stored as a 1-based integer because 0 is used to indicate a null
2998e5dd7070Spatrick // Attr pointer.
2999e5dd7070Spatrick auto Kind = static_cast<attr::Kind>(V - 1);
3000e5dd7070Spatrick ASTContext &Context = getContext();
3001e5dd7070Spatrick
3002e5dd7070Spatrick IdentifierInfo *AttrName = Record.readIdentifier();
3003e5dd7070Spatrick IdentifierInfo *ScopeName = Record.readIdentifier();
3004e5dd7070Spatrick SourceRange AttrRange = Record.readSourceRange();
3005e5dd7070Spatrick SourceLocation ScopeLoc = Record.readSourceLocation();
3006e5dd7070Spatrick unsigned ParsedKind = Record.readInt();
3007e5dd7070Spatrick unsigned Syntax = Record.readInt();
3008e5dd7070Spatrick unsigned SpellingIndex = Record.readInt();
3009e5dd7070Spatrick
3010e5dd7070Spatrick AttributeCommonInfo Info(AttrName, ScopeName, AttrRange, ScopeLoc,
3011e5dd7070Spatrick AttributeCommonInfo::Kind(ParsedKind),
3012e5dd7070Spatrick AttributeCommonInfo::Syntax(Syntax), SpellingIndex);
3013e5dd7070Spatrick
3014e5dd7070Spatrick #include "clang/Serialization/AttrPCHRead.inc"
3015e5dd7070Spatrick
3016e5dd7070Spatrick assert(New && "Unable to decode attribute?");
3017e5dd7070Spatrick return New;
3018e5dd7070Spatrick }
3019e5dd7070Spatrick
3020e5dd7070Spatrick /// Reads attributes from the current stream position.
readAttributes(AttrVec & Attrs)3021e5dd7070Spatrick void ASTRecordReader::readAttributes(AttrVec &Attrs) {
3022e5dd7070Spatrick for (unsigned I = 0, E = readInt(); I != E; ++I)
3023*12c85518Srobert if (auto *A = readAttr())
3024*12c85518Srobert Attrs.push_back(A);
3025e5dd7070Spatrick }
3026e5dd7070Spatrick
3027e5dd7070Spatrick //===----------------------------------------------------------------------===//
3028e5dd7070Spatrick // ASTReader Implementation
3029e5dd7070Spatrick //===----------------------------------------------------------------------===//
3030e5dd7070Spatrick
3031e5dd7070Spatrick /// Note that we have loaded the declaration with the given
3032e5dd7070Spatrick /// Index.
3033e5dd7070Spatrick ///
3034e5dd7070Spatrick /// This routine notes that this declaration has already been loaded,
3035e5dd7070Spatrick /// so that future GetDecl calls will return this declaration rather
3036e5dd7070Spatrick /// than trying to load a new declaration.
LoadedDecl(unsigned Index,Decl * D)3037e5dd7070Spatrick inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
3038e5dd7070Spatrick assert(!DeclsLoaded[Index] && "Decl loaded twice?");
3039e5dd7070Spatrick DeclsLoaded[Index] = D;
3040e5dd7070Spatrick }
3041e5dd7070Spatrick
3042e5dd7070Spatrick /// Determine whether the consumer will be interested in seeing
3043e5dd7070Spatrick /// this declaration (via HandleTopLevelDecl).
3044e5dd7070Spatrick ///
3045e5dd7070Spatrick /// This routine should return true for anything that might affect
3046e5dd7070Spatrick /// code generation, e.g., inline function definitions, Objective-C
3047e5dd7070Spatrick /// declarations with metadata, etc.
isConsumerInterestedIn(ASTContext & Ctx,Decl * D,bool HasBody)3048e5dd7070Spatrick static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
3049e5dd7070Spatrick // An ObjCMethodDecl is never considered as "interesting" because its
3050e5dd7070Spatrick // implementation container always is.
3051e5dd7070Spatrick
3052e5dd7070Spatrick // An ImportDecl or VarDecl imported from a module map module will get
3053e5dd7070Spatrick // emitted when we import the relevant module.
3054e5dd7070Spatrick if (isPartOfPerModuleInitializer(D)) {
3055e5dd7070Spatrick auto *M = D->getImportedOwningModule();
3056e5dd7070Spatrick if (M && M->Kind == Module::ModuleMapModule &&
3057e5dd7070Spatrick Ctx.DeclMustBeEmitted(D))
3058e5dd7070Spatrick return false;
3059e5dd7070Spatrick }
3060e5dd7070Spatrick
3061*12c85518Srobert if (isa<FileScopeAsmDecl, TopLevelStmtDecl, ObjCProtocolDecl, ObjCImplDecl,
3062*12c85518Srobert ImportDecl, PragmaCommentDecl, PragmaDetectMismatchDecl>(D))
3063e5dd7070Spatrick return true;
3064*12c85518Srobert if (isa<OMPThreadPrivateDecl, OMPDeclareReductionDecl, OMPDeclareMapperDecl,
3065*12c85518Srobert OMPAllocateDecl, OMPRequiresDecl>(D))
3066e5dd7070Spatrick return !D->getDeclContext()->isFunctionOrMethod();
3067e5dd7070Spatrick if (const auto *Var = dyn_cast<VarDecl>(D))
3068e5dd7070Spatrick return Var->isFileVarDecl() &&
3069e5dd7070Spatrick (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
3070e5dd7070Spatrick OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
3071e5dd7070Spatrick if (const auto *Func = dyn_cast<FunctionDecl>(D))
3072e5dd7070Spatrick return Func->doesThisDeclarationHaveABody() || HasBody;
3073e5dd7070Spatrick
3074e5dd7070Spatrick if (auto *ES = D->getASTContext().getExternalSource())
3075e5dd7070Spatrick if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
3076e5dd7070Spatrick return true;
3077e5dd7070Spatrick
3078e5dd7070Spatrick return false;
3079e5dd7070Spatrick }
3080e5dd7070Spatrick
3081e5dd7070Spatrick /// Get the correct cursor and offset for loading a declaration.
3082e5dd7070Spatrick ASTReader::RecordLocation
DeclCursorForID(DeclID ID,SourceLocation & Loc)3083e5dd7070Spatrick ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
3084e5dd7070Spatrick GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
3085e5dd7070Spatrick assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
3086e5dd7070Spatrick ModuleFile *M = I->second;
3087e5dd7070Spatrick const DeclOffset &DOffs =
3088e5dd7070Spatrick M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
3089e5dd7070Spatrick Loc = TranslateSourceLocation(*M, DOffs.getLocation());
3090ec727ea7Spatrick return RecordLocation(M, DOffs.getBitOffset(M->DeclsBlockStartOffset));
3091e5dd7070Spatrick }
3092e5dd7070Spatrick
getLocalBitOffset(uint64_t GlobalOffset)3093e5dd7070Spatrick ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
3094e5dd7070Spatrick auto I = GlobalBitOffsetsMap.find(GlobalOffset);
3095e5dd7070Spatrick
3096e5dd7070Spatrick assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
3097e5dd7070Spatrick return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
3098e5dd7070Spatrick }
3099e5dd7070Spatrick
getGlobalBitOffset(ModuleFile & M,uint64_t LocalOffset)3100ec727ea7Spatrick uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint64_t LocalOffset) {
3101e5dd7070Spatrick return LocalOffset + M.GlobalBitOffset;
3102e5dd7070Spatrick }
3103e5dd7070Spatrick
3104e5dd7070Spatrick /// Find the context in which we should search for previous declarations when
3105e5dd7070Spatrick /// looking for declarations to merge.
getPrimaryContextForMerging(ASTReader & Reader,DeclContext * DC)3106e5dd7070Spatrick DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3107e5dd7070Spatrick DeclContext *DC) {
3108e5dd7070Spatrick if (auto *ND = dyn_cast<NamespaceDecl>(DC))
3109e5dd7070Spatrick return ND->getOriginalNamespace();
3110e5dd7070Spatrick
3111e5dd7070Spatrick if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
3112e5dd7070Spatrick // Try to dig out the definition.
3113e5dd7070Spatrick auto *DD = RD->DefinitionData;
3114e5dd7070Spatrick if (!DD)
3115e5dd7070Spatrick DD = RD->getCanonicalDecl()->DefinitionData;
3116e5dd7070Spatrick
3117e5dd7070Spatrick // If there's no definition yet, then DC's definition is added by an update
3118e5dd7070Spatrick // record, but we've not yet loaded that update record. In this case, we
3119e5dd7070Spatrick // commit to DC being the canonical definition now, and will fix this when
3120e5dd7070Spatrick // we load the update record.
3121e5dd7070Spatrick if (!DD) {
3122e5dd7070Spatrick DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
3123e5dd7070Spatrick RD->setCompleteDefinition(true);
3124e5dd7070Spatrick RD->DefinitionData = DD;
3125e5dd7070Spatrick RD->getCanonicalDecl()->DefinitionData = DD;
3126e5dd7070Spatrick
3127e5dd7070Spatrick // Track that we did this horrible thing so that we can fix it later.
3128e5dd7070Spatrick Reader.PendingFakeDefinitionData.insert(
3129e5dd7070Spatrick std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
3130e5dd7070Spatrick }
3131e5dd7070Spatrick
3132e5dd7070Spatrick return DD->Definition;
3133e5dd7070Spatrick }
3134e5dd7070Spatrick
3135*12c85518Srobert if (auto *RD = dyn_cast<RecordDecl>(DC))
3136*12c85518Srobert return RD->getDefinition();
3137*12c85518Srobert
3138e5dd7070Spatrick if (auto *ED = dyn_cast<EnumDecl>(DC))
3139e5dd7070Spatrick return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3140e5dd7070Spatrick : nullptr;
3141e5dd7070Spatrick
3142*12c85518Srobert if (auto *OID = dyn_cast<ObjCInterfaceDecl>(DC))
3143*12c85518Srobert return OID->getDefinition();
3144*12c85518Srobert
3145e5dd7070Spatrick // We can see the TU here only if we have no Sema object. In that case,
3146e5dd7070Spatrick // there's no TU scope to look in, so using the DC alone is sufficient.
3147e5dd7070Spatrick if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3148e5dd7070Spatrick return TU;
3149e5dd7070Spatrick
3150e5dd7070Spatrick return nullptr;
3151e5dd7070Spatrick }
3152e5dd7070Spatrick
~FindExistingResult()3153e5dd7070Spatrick ASTDeclReader::FindExistingResult::~FindExistingResult() {
3154e5dd7070Spatrick // Record that we had a typedef name for linkage whether or not we merge
3155e5dd7070Spatrick // with that declaration.
3156e5dd7070Spatrick if (TypedefNameForLinkage) {
3157e5dd7070Spatrick DeclContext *DC = New->getDeclContext()->getRedeclContext();
3158e5dd7070Spatrick Reader.ImportedTypedefNamesForLinkage.insert(
3159e5dd7070Spatrick std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3160e5dd7070Spatrick return;
3161e5dd7070Spatrick }
3162e5dd7070Spatrick
3163e5dd7070Spatrick if (!AddResult || Existing)
3164e5dd7070Spatrick return;
3165e5dd7070Spatrick
3166e5dd7070Spatrick DeclarationName Name = New->getDeclName();
3167e5dd7070Spatrick DeclContext *DC = New->getDeclContext()->getRedeclContext();
3168e5dd7070Spatrick if (needsAnonymousDeclarationNumber(New)) {
3169e5dd7070Spatrick setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3170e5dd7070Spatrick AnonymousDeclNumber, New);
3171e5dd7070Spatrick } else if (DC->isTranslationUnit() &&
3172e5dd7070Spatrick !Reader.getContext().getLangOpts().CPlusPlus) {
3173e5dd7070Spatrick if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
3174e5dd7070Spatrick Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3175e5dd7070Spatrick .push_back(New);
3176e5dd7070Spatrick } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
3177e5dd7070Spatrick // Add the declaration to its redeclaration context so later merging
3178e5dd7070Spatrick // lookups will find it.
3179e5dd7070Spatrick MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
3180e5dd7070Spatrick }
3181e5dd7070Spatrick }
3182e5dd7070Spatrick
3183e5dd7070Spatrick /// Find the declaration that should be merged into, given the declaration found
3184e5dd7070Spatrick /// by name lookup. If we're merging an anonymous declaration within a typedef,
3185e5dd7070Spatrick /// we need a matching typedef, and we merge with the type inside it.
getDeclForMerging(NamedDecl * Found,bool IsTypedefNameForLinkage)3186e5dd7070Spatrick static NamedDecl *getDeclForMerging(NamedDecl *Found,
3187e5dd7070Spatrick bool IsTypedefNameForLinkage) {
3188e5dd7070Spatrick if (!IsTypedefNameForLinkage)
3189e5dd7070Spatrick return Found;
3190e5dd7070Spatrick
3191e5dd7070Spatrick // If we found a typedef declaration that gives a name to some other
3192e5dd7070Spatrick // declaration, then we want that inner declaration. Declarations from
3193e5dd7070Spatrick // AST files are handled via ImportedTypedefNamesForLinkage.
3194e5dd7070Spatrick if (Found->isFromASTFile())
3195e5dd7070Spatrick return nullptr;
3196e5dd7070Spatrick
3197e5dd7070Spatrick if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
3198e5dd7070Spatrick return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
3199e5dd7070Spatrick
3200e5dd7070Spatrick return nullptr;
3201e5dd7070Spatrick }
3202e5dd7070Spatrick
3203e5dd7070Spatrick /// Find the declaration to use to populate the anonymous declaration table
3204e5dd7070Spatrick /// for the given lexical DeclContext. We only care about finding local
3205e5dd7070Spatrick /// definitions of the context; we'll merge imported ones as we go.
3206e5dd7070Spatrick DeclContext *
getPrimaryDCForAnonymousDecl(DeclContext * LexicalDC)3207e5dd7070Spatrick ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3208e5dd7070Spatrick // For classes, we track the definition as we merge.
3209e5dd7070Spatrick if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3210e5dd7070Spatrick auto *DD = RD->getCanonicalDecl()->DefinitionData;
3211e5dd7070Spatrick return DD ? DD->Definition : nullptr;
3212*12c85518Srobert } else if (auto *OID = dyn_cast<ObjCInterfaceDecl>(LexicalDC)) {
3213*12c85518Srobert return OID->getCanonicalDecl()->getDefinition();
3214e5dd7070Spatrick }
3215e5dd7070Spatrick
3216e5dd7070Spatrick // For anything else, walk its merged redeclarations looking for a definition.
3217e5dd7070Spatrick // Note that we can't just call getDefinition here because the redeclaration
3218e5dd7070Spatrick // chain isn't wired up.
3219e5dd7070Spatrick for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3220e5dd7070Spatrick if (auto *FD = dyn_cast<FunctionDecl>(D))
3221e5dd7070Spatrick if (FD->isThisDeclarationADefinition())
3222e5dd7070Spatrick return FD;
3223e5dd7070Spatrick if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3224e5dd7070Spatrick if (MD->isThisDeclarationADefinition())
3225e5dd7070Spatrick return MD;
3226*12c85518Srobert if (auto *RD = dyn_cast<RecordDecl>(D))
3227*12c85518Srobert if (RD->isThisDeclarationADefinition())
3228*12c85518Srobert return RD;
3229e5dd7070Spatrick }
3230e5dd7070Spatrick
3231e5dd7070Spatrick // No merged definition yet.
3232e5dd7070Spatrick return nullptr;
3233e5dd7070Spatrick }
3234e5dd7070Spatrick
getAnonymousDeclForMerging(ASTReader & Reader,DeclContext * DC,unsigned Index)3235e5dd7070Spatrick NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3236e5dd7070Spatrick DeclContext *DC,
3237e5dd7070Spatrick unsigned Index) {
3238e5dd7070Spatrick // If the lexical context has been merged, look into the now-canonical
3239e5dd7070Spatrick // definition.
3240e5dd7070Spatrick auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
3241e5dd7070Spatrick
3242e5dd7070Spatrick // If we've seen this before, return the canonical declaration.
3243e5dd7070Spatrick auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
3244e5dd7070Spatrick if (Index < Previous.size() && Previous[Index])
3245e5dd7070Spatrick return Previous[Index];
3246e5dd7070Spatrick
3247e5dd7070Spatrick // If this is the first time, but we have parsed a declaration of the context,
3248e5dd7070Spatrick // build the anonymous declaration list from the parsed declaration.
3249e5dd7070Spatrick auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3250e5dd7070Spatrick if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3251e5dd7070Spatrick numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
3252e5dd7070Spatrick if (Previous.size() == Number)
3253e5dd7070Spatrick Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3254e5dd7070Spatrick else
3255e5dd7070Spatrick Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3256e5dd7070Spatrick });
3257e5dd7070Spatrick }
3258e5dd7070Spatrick
3259e5dd7070Spatrick return Index < Previous.size() ? Previous[Index] : nullptr;
3260e5dd7070Spatrick }
3261e5dd7070Spatrick
setAnonymousDeclForMerging(ASTReader & Reader,DeclContext * DC,unsigned Index,NamedDecl * D)3262e5dd7070Spatrick void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3263e5dd7070Spatrick DeclContext *DC, unsigned Index,
3264e5dd7070Spatrick NamedDecl *D) {
3265e5dd7070Spatrick auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
3266e5dd7070Spatrick
3267e5dd7070Spatrick auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
3268e5dd7070Spatrick if (Index >= Previous.size())
3269e5dd7070Spatrick Previous.resize(Index + 1);
3270e5dd7070Spatrick if (!Previous[Index])
3271e5dd7070Spatrick Previous[Index] = D;
3272e5dd7070Spatrick }
3273e5dd7070Spatrick
findExisting(NamedDecl * D)3274e5dd7070Spatrick ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
3275e5dd7070Spatrick DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3276e5dd7070Spatrick : D->getDeclName();
3277e5dd7070Spatrick
3278e5dd7070Spatrick if (!Name && !needsAnonymousDeclarationNumber(D)) {
3279e5dd7070Spatrick // Don't bother trying to find unnamed declarations that are in
3280e5dd7070Spatrick // unmergeable contexts.
3281e5dd7070Spatrick FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3282e5dd7070Spatrick AnonymousDeclNumber, TypedefNameForLinkage);
3283e5dd7070Spatrick Result.suppress();
3284e5dd7070Spatrick return Result;
3285e5dd7070Spatrick }
3286e5dd7070Spatrick
3287*12c85518Srobert ASTContext &C = Reader.getContext();
3288e5dd7070Spatrick DeclContext *DC = D->getDeclContext()->getRedeclContext();
3289e5dd7070Spatrick if (TypedefNameForLinkage) {
3290e5dd7070Spatrick auto It = Reader.ImportedTypedefNamesForLinkage.find(
3291e5dd7070Spatrick std::make_pair(DC, TypedefNameForLinkage));
3292e5dd7070Spatrick if (It != Reader.ImportedTypedefNamesForLinkage.end())
3293*12c85518Srobert if (C.isSameEntity(It->second, D))
3294e5dd7070Spatrick return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3295e5dd7070Spatrick TypedefNameForLinkage);
3296e5dd7070Spatrick // Go on to check in other places in case an existing typedef name
3297e5dd7070Spatrick // was not imported.
3298e5dd7070Spatrick }
3299e5dd7070Spatrick
3300e5dd7070Spatrick if (needsAnonymousDeclarationNumber(D)) {
3301e5dd7070Spatrick // This is an anonymous declaration that we may need to merge. Look it up
3302e5dd7070Spatrick // in its context by number.
3303e5dd7070Spatrick if (auto *Existing = getAnonymousDeclForMerging(
3304e5dd7070Spatrick Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3305*12c85518Srobert if (C.isSameEntity(Existing, D))
3306e5dd7070Spatrick return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3307e5dd7070Spatrick TypedefNameForLinkage);
3308e5dd7070Spatrick } else if (DC->isTranslationUnit() &&
3309e5dd7070Spatrick !Reader.getContext().getLangOpts().CPlusPlus) {
3310e5dd7070Spatrick IdentifierResolver &IdResolver = Reader.getIdResolver();
3311e5dd7070Spatrick
3312e5dd7070Spatrick // Temporarily consider the identifier to be up-to-date. We don't want to
3313e5dd7070Spatrick // cause additional lookups here.
3314e5dd7070Spatrick class UpToDateIdentifierRAII {
3315e5dd7070Spatrick IdentifierInfo *II;
3316e5dd7070Spatrick bool WasOutToDate = false;
3317e5dd7070Spatrick
3318e5dd7070Spatrick public:
3319e5dd7070Spatrick explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
3320e5dd7070Spatrick if (II) {
3321e5dd7070Spatrick WasOutToDate = II->isOutOfDate();
3322e5dd7070Spatrick if (WasOutToDate)
3323e5dd7070Spatrick II->setOutOfDate(false);
3324e5dd7070Spatrick }
3325e5dd7070Spatrick }
3326e5dd7070Spatrick
3327e5dd7070Spatrick ~UpToDateIdentifierRAII() {
3328e5dd7070Spatrick if (WasOutToDate)
3329e5dd7070Spatrick II->setOutOfDate(true);
3330e5dd7070Spatrick }
3331e5dd7070Spatrick } UpToDate(Name.getAsIdentifierInfo());
3332e5dd7070Spatrick
3333e5dd7070Spatrick for (IdentifierResolver::iterator I = IdResolver.begin(Name),
3334e5dd7070Spatrick IEnd = IdResolver.end();
3335e5dd7070Spatrick I != IEnd; ++I) {
3336e5dd7070Spatrick if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
3337*12c85518Srobert if (C.isSameEntity(Existing, D))
3338e5dd7070Spatrick return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3339e5dd7070Spatrick TypedefNameForLinkage);
3340e5dd7070Spatrick }
3341e5dd7070Spatrick } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
3342e5dd7070Spatrick DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
3343e5dd7070Spatrick for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
3344e5dd7070Spatrick if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
3345*12c85518Srobert if (C.isSameEntity(Existing, D))
3346e5dd7070Spatrick return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3347e5dd7070Spatrick TypedefNameForLinkage);
3348e5dd7070Spatrick }
3349e5dd7070Spatrick } else {
3350e5dd7070Spatrick // Not in a mergeable context.
3351e5dd7070Spatrick return FindExistingResult(Reader);
3352e5dd7070Spatrick }
3353e5dd7070Spatrick
3354e5dd7070Spatrick // If this declaration is from a merged context, make a note that we need to
3355e5dd7070Spatrick // check that the canonical definition of that context contains the decl.
3356e5dd7070Spatrick //
3357e5dd7070Spatrick // FIXME: We should do something similar if we merge two definitions of the
3358e5dd7070Spatrick // same template specialization into the same CXXRecordDecl.
3359e5dd7070Spatrick auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3360e5dd7070Spatrick if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3361e5dd7070Spatrick MergedDCIt->second == D->getDeclContext())
3362e5dd7070Spatrick Reader.PendingOdrMergeChecks.push_back(D);
3363e5dd7070Spatrick
3364e5dd7070Spatrick return FindExistingResult(Reader, D, /*Existing=*/nullptr,
3365e5dd7070Spatrick AnonymousDeclNumber, TypedefNameForLinkage);
3366e5dd7070Spatrick }
3367e5dd7070Spatrick
3368e5dd7070Spatrick template<typename DeclT>
getMostRecentDeclImpl(Redeclarable<DeclT> * D)3369e5dd7070Spatrick Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3370e5dd7070Spatrick return D->RedeclLink.getLatestNotUpdated();
3371e5dd7070Spatrick }
3372e5dd7070Spatrick
getMostRecentDeclImpl(...)3373e5dd7070Spatrick Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3374e5dd7070Spatrick llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3375e5dd7070Spatrick }
3376e5dd7070Spatrick
getMostRecentDecl(Decl * D)3377e5dd7070Spatrick Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3378e5dd7070Spatrick assert(D);
3379e5dd7070Spatrick
3380e5dd7070Spatrick switch (D->getKind()) {
3381e5dd7070Spatrick #define ABSTRACT_DECL(TYPE)
3382e5dd7070Spatrick #define DECL(TYPE, BASE) \
3383e5dd7070Spatrick case Decl::TYPE: \
3384e5dd7070Spatrick return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3385e5dd7070Spatrick #include "clang/AST/DeclNodes.inc"
3386e5dd7070Spatrick }
3387e5dd7070Spatrick llvm_unreachable("unknown decl kind");
3388e5dd7070Spatrick }
3389e5dd7070Spatrick
getMostRecentExistingDecl(Decl * D)3390e5dd7070Spatrick Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3391e5dd7070Spatrick return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3392e5dd7070Spatrick }
3393e5dd7070Spatrick
mergeInheritableAttributes(ASTReader & Reader,Decl * D,Decl * Previous)3394a9ac8606Spatrick void ASTDeclReader::mergeInheritableAttributes(ASTReader &Reader, Decl *D,
3395a9ac8606Spatrick Decl *Previous) {
3396a9ac8606Spatrick InheritableAttr *NewAttr = nullptr;
3397a9ac8606Spatrick ASTContext &Context = Reader.getContext();
3398a9ac8606Spatrick const auto *IA = Previous->getAttr<MSInheritanceAttr>();
3399a9ac8606Spatrick
3400a9ac8606Spatrick if (IA && !D->hasAttr<MSInheritanceAttr>()) {
3401a9ac8606Spatrick NewAttr = cast<InheritableAttr>(IA->clone(Context));
3402a9ac8606Spatrick NewAttr->setInherited(true);
3403a9ac8606Spatrick D->addAttr(NewAttr);
3404a9ac8606Spatrick }
3405*12c85518Srobert
3406*12c85518Srobert const auto *AA = Previous->getAttr<AvailabilityAttr>();
3407*12c85518Srobert if (AA && !D->hasAttr<AvailabilityAttr>()) {
3408*12c85518Srobert NewAttr = AA->clone(Context);
3409*12c85518Srobert NewAttr->setInherited(true);
3410*12c85518Srobert D->addAttr(NewAttr);
3411*12c85518Srobert }
3412a9ac8606Spatrick }
3413a9ac8606Spatrick
3414e5dd7070Spatrick template<typename DeclT>
attachPreviousDeclImpl(ASTReader & Reader,Redeclarable<DeclT> * D,Decl * Previous,Decl * Canon)3415e5dd7070Spatrick void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3416e5dd7070Spatrick Redeclarable<DeclT> *D,
3417e5dd7070Spatrick Decl *Previous, Decl *Canon) {
3418e5dd7070Spatrick D->RedeclLink.setPrevious(cast<DeclT>(Previous));
3419e5dd7070Spatrick D->First = cast<DeclT>(Previous)->First;
3420e5dd7070Spatrick }
3421e5dd7070Spatrick
3422e5dd7070Spatrick namespace clang {
3423e5dd7070Spatrick
3424e5dd7070Spatrick template<>
attachPreviousDeclImpl(ASTReader & Reader,Redeclarable<VarDecl> * D,Decl * Previous,Decl * Canon)3425e5dd7070Spatrick void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3426e5dd7070Spatrick Redeclarable<VarDecl> *D,
3427e5dd7070Spatrick Decl *Previous, Decl *Canon) {
3428e5dd7070Spatrick auto *VD = static_cast<VarDecl *>(D);
3429e5dd7070Spatrick auto *PrevVD = cast<VarDecl>(Previous);
3430e5dd7070Spatrick D->RedeclLink.setPrevious(PrevVD);
3431e5dd7070Spatrick D->First = PrevVD->First;
3432e5dd7070Spatrick
3433e5dd7070Spatrick // We should keep at most one definition on the chain.
3434e5dd7070Spatrick // FIXME: Cache the definition once we've found it. Building a chain with
3435e5dd7070Spatrick // N definitions currently takes O(N^2) time here.
3436e5dd7070Spatrick if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3437e5dd7070Spatrick for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3438e5dd7070Spatrick if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3439e5dd7070Spatrick Reader.mergeDefinitionVisibility(CurD, VD);
3440e5dd7070Spatrick VD->demoteThisDefinitionToDeclaration();
3441e5dd7070Spatrick break;
3442e5dd7070Spatrick }
3443e5dd7070Spatrick }
3444e5dd7070Spatrick }
3445e5dd7070Spatrick }
3446e5dd7070Spatrick
isUndeducedReturnType(QualType T)3447e5dd7070Spatrick static bool isUndeducedReturnType(QualType T) {
3448e5dd7070Spatrick auto *DT = T->getContainedDeducedType();
3449e5dd7070Spatrick return DT && !DT->isDeduced();
3450e5dd7070Spatrick }
3451e5dd7070Spatrick
3452e5dd7070Spatrick template<>
attachPreviousDeclImpl(ASTReader & Reader,Redeclarable<FunctionDecl> * D,Decl * Previous,Decl * Canon)3453e5dd7070Spatrick void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3454e5dd7070Spatrick Redeclarable<FunctionDecl> *D,
3455e5dd7070Spatrick Decl *Previous, Decl *Canon) {
3456e5dd7070Spatrick auto *FD = static_cast<FunctionDecl *>(D);
3457e5dd7070Spatrick auto *PrevFD = cast<FunctionDecl>(Previous);
3458e5dd7070Spatrick
3459e5dd7070Spatrick FD->RedeclLink.setPrevious(PrevFD);
3460e5dd7070Spatrick FD->First = PrevFD->First;
3461e5dd7070Spatrick
3462e5dd7070Spatrick // If the previous declaration is an inline function declaration, then this
3463e5dd7070Spatrick // declaration is too.
3464e5dd7070Spatrick if (PrevFD->isInlined() != FD->isInlined()) {
3465e5dd7070Spatrick // FIXME: [dcl.fct.spec]p4:
3466e5dd7070Spatrick // If a function with external linkage is declared inline in one
3467e5dd7070Spatrick // translation unit, it shall be declared inline in all translation
3468e5dd7070Spatrick // units in which it appears.
3469e5dd7070Spatrick //
3470e5dd7070Spatrick // Be careful of this case:
3471e5dd7070Spatrick //
3472e5dd7070Spatrick // module A:
3473e5dd7070Spatrick // template<typename T> struct X { void f(); };
3474e5dd7070Spatrick // template<typename T> inline void X<T>::f() {}
3475e5dd7070Spatrick //
3476e5dd7070Spatrick // module B instantiates the declaration of X<int>::f
3477e5dd7070Spatrick // module C instantiates the definition of X<int>::f
3478e5dd7070Spatrick //
3479e5dd7070Spatrick // If module B and C are merged, we do not have a violation of this rule.
3480e5dd7070Spatrick FD->setImplicitlyInline(true);
3481e5dd7070Spatrick }
3482e5dd7070Spatrick
3483e5dd7070Spatrick auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3484e5dd7070Spatrick auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
3485e5dd7070Spatrick if (FPT && PrevFPT) {
3486e5dd7070Spatrick // If we need to propagate an exception specification along the redecl
3487e5dd7070Spatrick // chain, make a note of that so that we can do so later.
3488e5dd7070Spatrick bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3489e5dd7070Spatrick bool WasUnresolved =
3490e5dd7070Spatrick isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3491e5dd7070Spatrick if (IsUnresolved != WasUnresolved)
3492e5dd7070Spatrick Reader.PendingExceptionSpecUpdates.insert(
3493e5dd7070Spatrick {Canon, IsUnresolved ? PrevFD : FD});
3494e5dd7070Spatrick
3495e5dd7070Spatrick // If we need to propagate a deduced return type along the redecl chain,
3496e5dd7070Spatrick // make a note of that so that we can do it later.
3497e5dd7070Spatrick bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3498e5dd7070Spatrick bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3499e5dd7070Spatrick if (IsUndeduced != WasUndeduced)
3500e5dd7070Spatrick Reader.PendingDeducedTypeUpdates.insert(
3501e5dd7070Spatrick {cast<FunctionDecl>(Canon),
3502e5dd7070Spatrick (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
3503e5dd7070Spatrick }
3504e5dd7070Spatrick }
3505e5dd7070Spatrick
3506e5dd7070Spatrick } // namespace clang
3507e5dd7070Spatrick
attachPreviousDeclImpl(ASTReader & Reader,...)3508e5dd7070Spatrick void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
3509e5dd7070Spatrick llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3510e5dd7070Spatrick }
3511e5dd7070Spatrick
3512e5dd7070Spatrick /// Inherit the default template argument from \p From to \p To. Returns
3513e5dd7070Spatrick /// \c false if there is no default template for \p From.
3514e5dd7070Spatrick template <typename ParmDecl>
inheritDefaultTemplateArgument(ASTContext & Context,ParmDecl * From,Decl * ToD)3515e5dd7070Spatrick static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3516e5dd7070Spatrick Decl *ToD) {
3517e5dd7070Spatrick auto *To = cast<ParmDecl>(ToD);
3518e5dd7070Spatrick if (!From->hasDefaultArgument())
3519e5dd7070Spatrick return false;
3520e5dd7070Spatrick To->setInheritedDefaultArgument(Context, From);
3521e5dd7070Spatrick return true;
3522e5dd7070Spatrick }
3523e5dd7070Spatrick
inheritDefaultTemplateArguments(ASTContext & Context,TemplateDecl * From,TemplateDecl * To)3524e5dd7070Spatrick static void inheritDefaultTemplateArguments(ASTContext &Context,
3525e5dd7070Spatrick TemplateDecl *From,
3526e5dd7070Spatrick TemplateDecl *To) {
3527e5dd7070Spatrick auto *FromTP = From->getTemplateParameters();
3528e5dd7070Spatrick auto *ToTP = To->getTemplateParameters();
3529e5dd7070Spatrick assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3530e5dd7070Spatrick
3531e5dd7070Spatrick for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3532e5dd7070Spatrick NamedDecl *FromParam = FromTP->getParam(I);
3533e5dd7070Spatrick NamedDecl *ToParam = ToTP->getParam(I);
3534e5dd7070Spatrick
3535e5dd7070Spatrick if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3536e5dd7070Spatrick inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3537e5dd7070Spatrick else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3538e5dd7070Spatrick inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3539e5dd7070Spatrick else
3540e5dd7070Spatrick inheritDefaultTemplateArgument(
3541e5dd7070Spatrick Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
3542e5dd7070Spatrick }
3543e5dd7070Spatrick }
3544e5dd7070Spatrick
attachPreviousDecl(ASTReader & Reader,Decl * D,Decl * Previous,Decl * Canon)3545e5dd7070Spatrick void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
3546e5dd7070Spatrick Decl *Previous, Decl *Canon) {
3547e5dd7070Spatrick assert(D && Previous);
3548e5dd7070Spatrick
3549e5dd7070Spatrick switch (D->getKind()) {
3550e5dd7070Spatrick #define ABSTRACT_DECL(TYPE)
3551e5dd7070Spatrick #define DECL(TYPE, BASE) \
3552e5dd7070Spatrick case Decl::TYPE: \
3553e5dd7070Spatrick attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
3554e5dd7070Spatrick break;
3555e5dd7070Spatrick #include "clang/AST/DeclNodes.inc"
3556e5dd7070Spatrick }
3557e5dd7070Spatrick
3558e5dd7070Spatrick // If the declaration was visible in one module, a redeclaration of it in
3559e5dd7070Spatrick // another module remains visible even if it wouldn't be visible by itself.
3560e5dd7070Spatrick //
3561e5dd7070Spatrick // FIXME: In this case, the declaration should only be visible if a module
3562e5dd7070Spatrick // that makes it visible has been imported.
3563e5dd7070Spatrick D->IdentifierNamespace |=
3564e5dd7070Spatrick Previous->IdentifierNamespace &
3565e5dd7070Spatrick (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
3566e5dd7070Spatrick
3567e5dd7070Spatrick // If the declaration declares a template, it may inherit default arguments
3568e5dd7070Spatrick // from the previous declaration.
3569e5dd7070Spatrick if (auto *TD = dyn_cast<TemplateDecl>(D))
3570e5dd7070Spatrick inheritDefaultTemplateArguments(Reader.getContext(),
3571e5dd7070Spatrick cast<TemplateDecl>(Previous), TD);
3572a9ac8606Spatrick
3573a9ac8606Spatrick // If any of the declaration in the chain contains an Inheritable attribute,
3574a9ac8606Spatrick // it needs to be added to all the declarations in the redeclarable chain.
3575a9ac8606Spatrick // FIXME: Only the logic of merging MSInheritableAttr is present, it should
3576a9ac8606Spatrick // be extended for all inheritable attributes.
3577a9ac8606Spatrick mergeInheritableAttributes(Reader, D, Previous);
3578e5dd7070Spatrick }
3579e5dd7070Spatrick
3580e5dd7070Spatrick template<typename DeclT>
attachLatestDeclImpl(Redeclarable<DeclT> * D,Decl * Latest)3581e5dd7070Spatrick void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
3582e5dd7070Spatrick D->RedeclLink.setLatest(cast<DeclT>(Latest));
3583e5dd7070Spatrick }
3584e5dd7070Spatrick
attachLatestDeclImpl(...)3585e5dd7070Spatrick void ASTDeclReader::attachLatestDeclImpl(...) {
3586e5dd7070Spatrick llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3587e5dd7070Spatrick }
3588e5dd7070Spatrick
attachLatestDecl(Decl * D,Decl * Latest)3589e5dd7070Spatrick void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3590e5dd7070Spatrick assert(D && Latest);
3591e5dd7070Spatrick
3592e5dd7070Spatrick switch (D->getKind()) {
3593e5dd7070Spatrick #define ABSTRACT_DECL(TYPE)
3594e5dd7070Spatrick #define DECL(TYPE, BASE) \
3595e5dd7070Spatrick case Decl::TYPE: \
3596e5dd7070Spatrick attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3597e5dd7070Spatrick break;
3598e5dd7070Spatrick #include "clang/AST/DeclNodes.inc"
3599e5dd7070Spatrick }
3600e5dd7070Spatrick }
3601e5dd7070Spatrick
3602e5dd7070Spatrick template<typename DeclT>
markIncompleteDeclChainImpl(Redeclarable<DeclT> * D)3603e5dd7070Spatrick void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3604e5dd7070Spatrick D->RedeclLink.markIncomplete();
3605e5dd7070Spatrick }
3606e5dd7070Spatrick
markIncompleteDeclChainImpl(...)3607e5dd7070Spatrick void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3608e5dd7070Spatrick llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3609e5dd7070Spatrick }
3610e5dd7070Spatrick
markIncompleteDeclChain(Decl * D)3611e5dd7070Spatrick void ASTReader::markIncompleteDeclChain(Decl *D) {
3612e5dd7070Spatrick switch (D->getKind()) {
3613e5dd7070Spatrick #define ABSTRACT_DECL(TYPE)
3614e5dd7070Spatrick #define DECL(TYPE, BASE) \
3615e5dd7070Spatrick case Decl::TYPE: \
3616e5dd7070Spatrick ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3617e5dd7070Spatrick break;
3618e5dd7070Spatrick #include "clang/AST/DeclNodes.inc"
3619e5dd7070Spatrick }
3620e5dd7070Spatrick }
3621e5dd7070Spatrick
3622e5dd7070Spatrick /// Read the declaration at the given offset from the AST file.
ReadDeclRecord(DeclID ID)3623e5dd7070Spatrick Decl *ASTReader::ReadDeclRecord(DeclID ID) {
3624e5dd7070Spatrick unsigned Index = ID - NUM_PREDEF_DECL_IDS;
3625e5dd7070Spatrick SourceLocation DeclLoc;
3626e5dd7070Spatrick RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
3627e5dd7070Spatrick llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
3628e5dd7070Spatrick // Keep track of where we are in the stream, then jump back there
3629e5dd7070Spatrick // after reading this declaration.
3630e5dd7070Spatrick SavedStreamPosition SavedPosition(DeclsCursor);
3631e5dd7070Spatrick
3632e5dd7070Spatrick ReadingKindTracker ReadingKind(Read_Decl, *this);
3633e5dd7070Spatrick
3634e5dd7070Spatrick // Note that we are loading a declaration record.
3635e5dd7070Spatrick Deserializing ADecl(this);
3636e5dd7070Spatrick
3637e5dd7070Spatrick auto Fail = [](const char *what, llvm::Error &&Err) {
3638e5dd7070Spatrick llvm::report_fatal_error(Twine("ASTReader::readDeclRecord failed ") + what +
3639e5dd7070Spatrick ": " + toString(std::move(Err)));
3640e5dd7070Spatrick };
3641e5dd7070Spatrick
3642e5dd7070Spatrick if (llvm::Error JumpFailed = DeclsCursor.JumpToBit(Loc.Offset))
3643e5dd7070Spatrick Fail("jumping", std::move(JumpFailed));
3644e5dd7070Spatrick ASTRecordReader Record(*this, *Loc.F);
3645e5dd7070Spatrick ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
3646e5dd7070Spatrick Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
3647e5dd7070Spatrick if (!MaybeCode)
3648e5dd7070Spatrick Fail("reading code", MaybeCode.takeError());
3649e5dd7070Spatrick unsigned Code = MaybeCode.get();
3650e5dd7070Spatrick
3651e5dd7070Spatrick ASTContext &Context = getContext();
3652e5dd7070Spatrick Decl *D = nullptr;
3653e5dd7070Spatrick Expected<unsigned> MaybeDeclCode = Record.readRecord(DeclsCursor, Code);
3654e5dd7070Spatrick if (!MaybeDeclCode)
3655e5dd7070Spatrick llvm::report_fatal_error(
3656*12c85518Srobert Twine("ASTReader::readDeclRecord failed reading decl code: ") +
3657e5dd7070Spatrick toString(MaybeDeclCode.takeError()));
3658e5dd7070Spatrick switch ((DeclCode)MaybeDeclCode.get()) {
3659e5dd7070Spatrick case DECL_CONTEXT_LEXICAL:
3660e5dd7070Spatrick case DECL_CONTEXT_VISIBLE:
3661e5dd7070Spatrick llvm_unreachable("Record cannot be de-serialized with readDeclRecord");
3662e5dd7070Spatrick case DECL_TYPEDEF:
3663e5dd7070Spatrick D = TypedefDecl::CreateDeserialized(Context, ID);
3664e5dd7070Spatrick break;
3665e5dd7070Spatrick case DECL_TYPEALIAS:
3666e5dd7070Spatrick D = TypeAliasDecl::CreateDeserialized(Context, ID);
3667e5dd7070Spatrick break;
3668e5dd7070Spatrick case DECL_ENUM:
3669e5dd7070Spatrick D = EnumDecl::CreateDeserialized(Context, ID);
3670e5dd7070Spatrick break;
3671e5dd7070Spatrick case DECL_RECORD:
3672e5dd7070Spatrick D = RecordDecl::CreateDeserialized(Context, ID);
3673e5dd7070Spatrick break;
3674e5dd7070Spatrick case DECL_ENUM_CONSTANT:
3675e5dd7070Spatrick D = EnumConstantDecl::CreateDeserialized(Context, ID);
3676e5dd7070Spatrick break;
3677e5dd7070Spatrick case DECL_FUNCTION:
3678e5dd7070Spatrick D = FunctionDecl::CreateDeserialized(Context, ID);
3679e5dd7070Spatrick break;
3680e5dd7070Spatrick case DECL_LINKAGE_SPEC:
3681e5dd7070Spatrick D = LinkageSpecDecl::CreateDeserialized(Context, ID);
3682e5dd7070Spatrick break;
3683e5dd7070Spatrick case DECL_EXPORT:
3684e5dd7070Spatrick D = ExportDecl::CreateDeserialized(Context, ID);
3685e5dd7070Spatrick break;
3686e5dd7070Spatrick case DECL_LABEL:
3687e5dd7070Spatrick D = LabelDecl::CreateDeserialized(Context, ID);
3688e5dd7070Spatrick break;
3689e5dd7070Spatrick case DECL_NAMESPACE:
3690e5dd7070Spatrick D = NamespaceDecl::CreateDeserialized(Context, ID);
3691e5dd7070Spatrick break;
3692e5dd7070Spatrick case DECL_NAMESPACE_ALIAS:
3693e5dd7070Spatrick D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
3694e5dd7070Spatrick break;
3695e5dd7070Spatrick case DECL_USING:
3696e5dd7070Spatrick D = UsingDecl::CreateDeserialized(Context, ID);
3697e5dd7070Spatrick break;
3698e5dd7070Spatrick case DECL_USING_PACK:
3699e5dd7070Spatrick D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
3700e5dd7070Spatrick break;
3701e5dd7070Spatrick case DECL_USING_SHADOW:
3702e5dd7070Spatrick D = UsingShadowDecl::CreateDeserialized(Context, ID);
3703e5dd7070Spatrick break;
3704a9ac8606Spatrick case DECL_USING_ENUM:
3705a9ac8606Spatrick D = UsingEnumDecl::CreateDeserialized(Context, ID);
3706a9ac8606Spatrick break;
3707e5dd7070Spatrick case DECL_CONSTRUCTOR_USING_SHADOW:
3708e5dd7070Spatrick D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3709e5dd7070Spatrick break;
3710e5dd7070Spatrick case DECL_USING_DIRECTIVE:
3711e5dd7070Spatrick D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
3712e5dd7070Spatrick break;
3713e5dd7070Spatrick case DECL_UNRESOLVED_USING_VALUE:
3714e5dd7070Spatrick D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
3715e5dd7070Spatrick break;
3716e5dd7070Spatrick case DECL_UNRESOLVED_USING_TYPENAME:
3717e5dd7070Spatrick D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
3718e5dd7070Spatrick break;
3719a9ac8606Spatrick case DECL_UNRESOLVED_USING_IF_EXISTS:
3720a9ac8606Spatrick D = UnresolvedUsingIfExistsDecl::CreateDeserialized(Context, ID);
3721a9ac8606Spatrick break;
3722e5dd7070Spatrick case DECL_CXX_RECORD:
3723e5dd7070Spatrick D = CXXRecordDecl::CreateDeserialized(Context, ID);
3724e5dd7070Spatrick break;
3725e5dd7070Spatrick case DECL_CXX_DEDUCTION_GUIDE:
3726e5dd7070Spatrick D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3727e5dd7070Spatrick break;
3728e5dd7070Spatrick case DECL_CXX_METHOD:
3729e5dd7070Spatrick D = CXXMethodDecl::CreateDeserialized(Context, ID);
3730e5dd7070Spatrick break;
3731e5dd7070Spatrick case DECL_CXX_CONSTRUCTOR:
3732e5dd7070Spatrick D = CXXConstructorDecl::CreateDeserialized(Context, ID, Record.readInt());
3733e5dd7070Spatrick break;
3734e5dd7070Spatrick case DECL_CXX_DESTRUCTOR:
3735e5dd7070Spatrick D = CXXDestructorDecl::CreateDeserialized(Context, ID);
3736e5dd7070Spatrick break;
3737e5dd7070Spatrick case DECL_CXX_CONVERSION:
3738e5dd7070Spatrick D = CXXConversionDecl::CreateDeserialized(Context, ID);
3739e5dd7070Spatrick break;
3740e5dd7070Spatrick case DECL_ACCESS_SPEC:
3741e5dd7070Spatrick D = AccessSpecDecl::CreateDeserialized(Context, ID);
3742e5dd7070Spatrick break;
3743e5dd7070Spatrick case DECL_FRIEND:
3744e5dd7070Spatrick D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
3745e5dd7070Spatrick break;
3746e5dd7070Spatrick case DECL_FRIEND_TEMPLATE:
3747e5dd7070Spatrick D = FriendTemplateDecl::CreateDeserialized(Context, ID);
3748e5dd7070Spatrick break;
3749e5dd7070Spatrick case DECL_CLASS_TEMPLATE:
3750e5dd7070Spatrick D = ClassTemplateDecl::CreateDeserialized(Context, ID);
3751e5dd7070Spatrick break;
3752e5dd7070Spatrick case DECL_CLASS_TEMPLATE_SPECIALIZATION:
3753e5dd7070Spatrick D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3754e5dd7070Spatrick break;
3755e5dd7070Spatrick case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
3756e5dd7070Spatrick D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3757e5dd7070Spatrick break;
3758e5dd7070Spatrick case DECL_VAR_TEMPLATE:
3759e5dd7070Spatrick D = VarTemplateDecl::CreateDeserialized(Context, ID);
3760e5dd7070Spatrick break;
3761e5dd7070Spatrick case DECL_VAR_TEMPLATE_SPECIALIZATION:
3762e5dd7070Spatrick D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3763e5dd7070Spatrick break;
3764e5dd7070Spatrick case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3765e5dd7070Spatrick D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3766e5dd7070Spatrick break;
3767e5dd7070Spatrick case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
3768e5dd7070Spatrick D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
3769e5dd7070Spatrick break;
3770e5dd7070Spatrick case DECL_FUNCTION_TEMPLATE:
3771e5dd7070Spatrick D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
3772e5dd7070Spatrick break;
3773e5dd7070Spatrick case DECL_TEMPLATE_TYPE_PARM: {
3774e5dd7070Spatrick bool HasTypeConstraint = Record.readInt();
3775e5dd7070Spatrick D = TemplateTypeParmDecl::CreateDeserialized(Context, ID,
3776e5dd7070Spatrick HasTypeConstraint);
3777e5dd7070Spatrick break;
3778e5dd7070Spatrick }
3779e5dd7070Spatrick case DECL_NON_TYPE_TEMPLATE_PARM: {
3780e5dd7070Spatrick bool HasTypeConstraint = Record.readInt();
3781e5dd7070Spatrick D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3782e5dd7070Spatrick HasTypeConstraint);
3783e5dd7070Spatrick break;
3784e5dd7070Spatrick }
3785e5dd7070Spatrick case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK: {
3786e5dd7070Spatrick bool HasTypeConstraint = Record.readInt();
3787e5dd7070Spatrick D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3788e5dd7070Spatrick Record.readInt(),
3789e5dd7070Spatrick HasTypeConstraint);
3790e5dd7070Spatrick break;
3791e5dd7070Spatrick }
3792e5dd7070Spatrick case DECL_TEMPLATE_TEMPLATE_PARM:
3793e5dd7070Spatrick D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
3794e5dd7070Spatrick break;
3795e5dd7070Spatrick case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3796e5dd7070Spatrick D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
3797e5dd7070Spatrick Record.readInt());
3798e5dd7070Spatrick break;
3799e5dd7070Spatrick case DECL_TYPE_ALIAS_TEMPLATE:
3800e5dd7070Spatrick D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
3801e5dd7070Spatrick break;
3802e5dd7070Spatrick case DECL_CONCEPT:
3803e5dd7070Spatrick D = ConceptDecl::CreateDeserialized(Context, ID);
3804e5dd7070Spatrick break;
3805e5dd7070Spatrick case DECL_REQUIRES_EXPR_BODY:
3806e5dd7070Spatrick D = RequiresExprBodyDecl::CreateDeserialized(Context, ID);
3807e5dd7070Spatrick break;
3808e5dd7070Spatrick case DECL_STATIC_ASSERT:
3809e5dd7070Spatrick D = StaticAssertDecl::CreateDeserialized(Context, ID);
3810e5dd7070Spatrick break;
3811e5dd7070Spatrick case DECL_OBJC_METHOD:
3812e5dd7070Spatrick D = ObjCMethodDecl::CreateDeserialized(Context, ID);
3813e5dd7070Spatrick break;
3814e5dd7070Spatrick case DECL_OBJC_INTERFACE:
3815e5dd7070Spatrick D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
3816e5dd7070Spatrick break;
3817e5dd7070Spatrick case DECL_OBJC_IVAR:
3818e5dd7070Spatrick D = ObjCIvarDecl::CreateDeserialized(Context, ID);
3819e5dd7070Spatrick break;
3820e5dd7070Spatrick case DECL_OBJC_PROTOCOL:
3821e5dd7070Spatrick D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
3822e5dd7070Spatrick break;
3823e5dd7070Spatrick case DECL_OBJC_AT_DEFS_FIELD:
3824e5dd7070Spatrick D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
3825e5dd7070Spatrick break;
3826e5dd7070Spatrick case DECL_OBJC_CATEGORY:
3827e5dd7070Spatrick D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
3828e5dd7070Spatrick break;
3829e5dd7070Spatrick case DECL_OBJC_CATEGORY_IMPL:
3830e5dd7070Spatrick D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
3831e5dd7070Spatrick break;
3832e5dd7070Spatrick case DECL_OBJC_IMPLEMENTATION:
3833e5dd7070Spatrick D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
3834e5dd7070Spatrick break;
3835e5dd7070Spatrick case DECL_OBJC_COMPATIBLE_ALIAS:
3836e5dd7070Spatrick D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
3837e5dd7070Spatrick break;
3838e5dd7070Spatrick case DECL_OBJC_PROPERTY:
3839e5dd7070Spatrick D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
3840e5dd7070Spatrick break;
3841e5dd7070Spatrick case DECL_OBJC_PROPERTY_IMPL:
3842e5dd7070Spatrick D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
3843e5dd7070Spatrick break;
3844e5dd7070Spatrick case DECL_FIELD:
3845e5dd7070Spatrick D = FieldDecl::CreateDeserialized(Context, ID);
3846e5dd7070Spatrick break;
3847e5dd7070Spatrick case DECL_INDIRECTFIELD:
3848e5dd7070Spatrick D = IndirectFieldDecl::CreateDeserialized(Context, ID);
3849e5dd7070Spatrick break;
3850e5dd7070Spatrick case DECL_VAR:
3851e5dd7070Spatrick D = VarDecl::CreateDeserialized(Context, ID);
3852e5dd7070Spatrick break;
3853e5dd7070Spatrick case DECL_IMPLICIT_PARAM:
3854e5dd7070Spatrick D = ImplicitParamDecl::CreateDeserialized(Context, ID);
3855e5dd7070Spatrick break;
3856e5dd7070Spatrick case DECL_PARM_VAR:
3857e5dd7070Spatrick D = ParmVarDecl::CreateDeserialized(Context, ID);
3858e5dd7070Spatrick break;
3859e5dd7070Spatrick case DECL_DECOMPOSITION:
3860e5dd7070Spatrick D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
3861e5dd7070Spatrick break;
3862e5dd7070Spatrick case DECL_BINDING:
3863e5dd7070Spatrick D = BindingDecl::CreateDeserialized(Context, ID);
3864e5dd7070Spatrick break;
3865e5dd7070Spatrick case DECL_FILE_SCOPE_ASM:
3866e5dd7070Spatrick D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
3867e5dd7070Spatrick break;
3868*12c85518Srobert case DECL_TOP_LEVEL_STMT_DECL:
3869*12c85518Srobert D = TopLevelStmtDecl::CreateDeserialized(Context, ID);
3870*12c85518Srobert break;
3871e5dd7070Spatrick case DECL_BLOCK:
3872e5dd7070Spatrick D = BlockDecl::CreateDeserialized(Context, ID);
3873e5dd7070Spatrick break;
3874e5dd7070Spatrick case DECL_MS_PROPERTY:
3875e5dd7070Spatrick D = MSPropertyDecl::CreateDeserialized(Context, ID);
3876e5dd7070Spatrick break;
3877ec727ea7Spatrick case DECL_MS_GUID:
3878ec727ea7Spatrick D = MSGuidDecl::CreateDeserialized(Context, ID);
3879ec727ea7Spatrick break;
3880*12c85518Srobert case DECL_UNNAMED_GLOBAL_CONSTANT:
3881*12c85518Srobert D = UnnamedGlobalConstantDecl::CreateDeserialized(Context, ID);
3882*12c85518Srobert break;
3883a9ac8606Spatrick case DECL_TEMPLATE_PARAM_OBJECT:
3884a9ac8606Spatrick D = TemplateParamObjectDecl::CreateDeserialized(Context, ID);
3885a9ac8606Spatrick break;
3886e5dd7070Spatrick case DECL_CAPTURED:
3887e5dd7070Spatrick D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
3888e5dd7070Spatrick break;
3889e5dd7070Spatrick case DECL_CXX_BASE_SPECIFIERS:
3890e5dd7070Spatrick Error("attempt to read a C++ base-specifier record as a declaration");
3891e5dd7070Spatrick return nullptr;
3892e5dd7070Spatrick case DECL_CXX_CTOR_INITIALIZERS:
3893e5dd7070Spatrick Error("attempt to read a C++ ctor initializer record as a declaration");
3894e5dd7070Spatrick return nullptr;
3895e5dd7070Spatrick case DECL_IMPORT:
3896e5dd7070Spatrick // Note: last entry of the ImportDecl record is the number of stored source
3897e5dd7070Spatrick // locations.
3898e5dd7070Spatrick D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
3899e5dd7070Spatrick break;
3900a9ac8606Spatrick case DECL_OMP_THREADPRIVATE: {
3901a9ac8606Spatrick Record.skipInts(1);
3902a9ac8606Spatrick unsigned NumChildren = Record.readInt();
3903a9ac8606Spatrick Record.skipInts(1);
3904a9ac8606Spatrick D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, NumChildren);
3905e5dd7070Spatrick break;
3906a9ac8606Spatrick }
3907e5dd7070Spatrick case DECL_OMP_ALLOCATE: {
3908e5dd7070Spatrick unsigned NumClauses = Record.readInt();
3909a9ac8606Spatrick unsigned NumVars = Record.readInt();
3910a9ac8606Spatrick Record.skipInts(1);
3911e5dd7070Spatrick D = OMPAllocateDecl::CreateDeserialized(Context, ID, NumVars, NumClauses);
3912e5dd7070Spatrick break;
3913e5dd7070Spatrick }
3914a9ac8606Spatrick case DECL_OMP_REQUIRES: {
3915a9ac8606Spatrick unsigned NumClauses = Record.readInt();
3916a9ac8606Spatrick Record.skipInts(2);
3917a9ac8606Spatrick D = OMPRequiresDecl::CreateDeserialized(Context, ID, NumClauses);
3918e5dd7070Spatrick break;
3919a9ac8606Spatrick }
3920e5dd7070Spatrick case DECL_OMP_DECLARE_REDUCTION:
3921e5dd7070Spatrick D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3922e5dd7070Spatrick break;
3923a9ac8606Spatrick case DECL_OMP_DECLARE_MAPPER: {
3924a9ac8606Spatrick unsigned NumClauses = Record.readInt();
3925a9ac8606Spatrick Record.skipInts(2);
3926a9ac8606Spatrick D = OMPDeclareMapperDecl::CreateDeserialized(Context, ID, NumClauses);
3927e5dd7070Spatrick break;
3928a9ac8606Spatrick }
3929e5dd7070Spatrick case DECL_OMP_CAPTUREDEXPR:
3930e5dd7070Spatrick D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
3931e5dd7070Spatrick break;
3932e5dd7070Spatrick case DECL_PRAGMA_COMMENT:
3933e5dd7070Spatrick D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
3934e5dd7070Spatrick break;
3935e5dd7070Spatrick case DECL_PRAGMA_DETECT_MISMATCH:
3936e5dd7070Spatrick D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
3937e5dd7070Spatrick Record.readInt());
3938e5dd7070Spatrick break;
3939e5dd7070Spatrick case DECL_EMPTY:
3940e5dd7070Spatrick D = EmptyDecl::CreateDeserialized(Context, ID);
3941e5dd7070Spatrick break;
3942e5dd7070Spatrick case DECL_LIFETIME_EXTENDED_TEMPORARY:
3943e5dd7070Spatrick D = LifetimeExtendedTemporaryDecl::CreateDeserialized(Context, ID);
3944e5dd7070Spatrick break;
3945e5dd7070Spatrick case DECL_OBJC_TYPE_PARAM:
3946e5dd7070Spatrick D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3947e5dd7070Spatrick break;
3948*12c85518Srobert case DECL_HLSL_BUFFER:
3949*12c85518Srobert D = HLSLBufferDecl::CreateDeserialized(Context, ID);
3950*12c85518Srobert break;
3951*12c85518Srobert case DECL_IMPLICIT_CONCEPT_SPECIALIZATION:
3952*12c85518Srobert D = ImplicitConceptSpecializationDecl::CreateDeserialized(Context, ID,
3953*12c85518Srobert Record.readInt());
3954*12c85518Srobert break;
3955e5dd7070Spatrick }
3956e5dd7070Spatrick
3957e5dd7070Spatrick assert(D && "Unknown declaration reading AST file");
3958e5dd7070Spatrick LoadedDecl(Index, D);
3959e5dd7070Spatrick // Set the DeclContext before doing any deserialization, to make sure internal
3960e5dd7070Spatrick // calls to Decl::getASTContext() by Decl's methods will find the
3961e5dd7070Spatrick // TranslationUnitDecl without crashing.
3962e5dd7070Spatrick D->setDeclContext(Context.getTranslationUnitDecl());
3963e5dd7070Spatrick Reader.Visit(D);
3964e5dd7070Spatrick
3965e5dd7070Spatrick // If this declaration is also a declaration context, get the
3966e5dd7070Spatrick // offsets for its tables of lexical and visible declarations.
3967e5dd7070Spatrick if (auto *DC = dyn_cast<DeclContext>(D)) {
3968e5dd7070Spatrick std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
3969e5dd7070Spatrick if (Offsets.first &&
3970e5dd7070Spatrick ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3971e5dd7070Spatrick return nullptr;
3972e5dd7070Spatrick if (Offsets.second &&
3973e5dd7070Spatrick ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3974e5dd7070Spatrick return nullptr;
3975e5dd7070Spatrick }
3976e5dd7070Spatrick assert(Record.getIdx() == Record.size());
3977e5dd7070Spatrick
3978e5dd7070Spatrick // Load any relevant update records.
3979e5dd7070Spatrick PendingUpdateRecords.push_back(
3980e5dd7070Spatrick PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
3981e5dd7070Spatrick
3982e5dd7070Spatrick // Load the categories after recursive loading is finished.
3983e5dd7070Spatrick if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
3984e5dd7070Spatrick // If we already have a definition when deserializing the ObjCInterfaceDecl,
3985e5dd7070Spatrick // we put the Decl in PendingDefinitions so we can pull the categories here.
3986e5dd7070Spatrick if (Class->isThisDeclarationADefinition() ||
3987e5dd7070Spatrick PendingDefinitions.count(Class))
3988e5dd7070Spatrick loadObjCCategories(ID, Class);
3989e5dd7070Spatrick
3990e5dd7070Spatrick // If we have deserialized a declaration that has a definition the
3991e5dd7070Spatrick // AST consumer might need to know about, queue it.
3992e5dd7070Spatrick // We don't pass it to the consumer immediately because we may be in recursive
3993e5dd7070Spatrick // loading, and some declarations may still be initializing.
3994e5dd7070Spatrick PotentiallyInterestingDecls.push_back(
3995e5dd7070Spatrick InterestingDecl(D, Reader.hasPendingBody()));
3996e5dd7070Spatrick
3997e5dd7070Spatrick return D;
3998e5dd7070Spatrick }
3999e5dd7070Spatrick
PassInterestingDeclsToConsumer()4000e5dd7070Spatrick void ASTReader::PassInterestingDeclsToConsumer() {
4001e5dd7070Spatrick assert(Consumer);
4002e5dd7070Spatrick
4003e5dd7070Spatrick if (PassingDeclsToConsumer)
4004e5dd7070Spatrick return;
4005e5dd7070Spatrick
4006e5dd7070Spatrick // Guard variable to avoid recursively redoing the process of passing
4007e5dd7070Spatrick // decls to consumer.
4008*12c85518Srobert SaveAndRestore GuardPassingDeclsToConsumer(PassingDeclsToConsumer, true);
4009e5dd7070Spatrick
4010e5dd7070Spatrick // Ensure that we've loaded all potentially-interesting declarations
4011e5dd7070Spatrick // that need to be eagerly loaded.
4012e5dd7070Spatrick for (auto ID : EagerlyDeserializedDecls)
4013e5dd7070Spatrick GetDecl(ID);
4014e5dd7070Spatrick EagerlyDeserializedDecls.clear();
4015e5dd7070Spatrick
4016e5dd7070Spatrick while (!PotentiallyInterestingDecls.empty()) {
4017e5dd7070Spatrick InterestingDecl D = PotentiallyInterestingDecls.front();
4018e5dd7070Spatrick PotentiallyInterestingDecls.pop_front();
4019e5dd7070Spatrick if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
4020e5dd7070Spatrick PassInterestingDeclToConsumer(D.getDecl());
4021e5dd7070Spatrick }
4022e5dd7070Spatrick }
4023e5dd7070Spatrick
loadDeclUpdateRecords(PendingUpdateRecord & Record)4024e5dd7070Spatrick void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
4025e5dd7070Spatrick // The declaration may have been modified by files later in the chain.
4026e5dd7070Spatrick // If this is the case, read the record containing the updates from each file
4027e5dd7070Spatrick // and pass it to ASTDeclReader to make the modifications.
4028e5dd7070Spatrick serialization::GlobalDeclID ID = Record.ID;
4029e5dd7070Spatrick Decl *D = Record.D;
4030e5dd7070Spatrick ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
4031e5dd7070Spatrick DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
4032e5dd7070Spatrick
4033e5dd7070Spatrick SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
4034e5dd7070Spatrick
4035e5dd7070Spatrick if (UpdI != DeclUpdateOffsets.end()) {
4036e5dd7070Spatrick auto UpdateOffsets = std::move(UpdI->second);
4037e5dd7070Spatrick DeclUpdateOffsets.erase(UpdI);
4038e5dd7070Spatrick
4039e5dd7070Spatrick // Check if this decl was interesting to the consumer. If we just loaded
4040e5dd7070Spatrick // the declaration, then we know it was interesting and we skip the call
4041e5dd7070Spatrick // to isConsumerInterestedIn because it is unsafe to call in the
4042e5dd7070Spatrick // current ASTReader state.
4043e5dd7070Spatrick bool WasInteresting =
4044e5dd7070Spatrick Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
4045e5dd7070Spatrick for (auto &FileAndOffset : UpdateOffsets) {
4046e5dd7070Spatrick ModuleFile *F = FileAndOffset.first;
4047e5dd7070Spatrick uint64_t Offset = FileAndOffset.second;
4048e5dd7070Spatrick llvm::BitstreamCursor &Cursor = F->DeclsCursor;
4049e5dd7070Spatrick SavedStreamPosition SavedPosition(Cursor);
4050e5dd7070Spatrick if (llvm::Error JumpFailed = Cursor.JumpToBit(Offset))
4051e5dd7070Spatrick // FIXME don't do a fatal error.
4052e5dd7070Spatrick llvm::report_fatal_error(
4053*12c85518Srobert Twine("ASTReader::loadDeclUpdateRecords failed jumping: ") +
4054e5dd7070Spatrick toString(std::move(JumpFailed)));
4055e5dd7070Spatrick Expected<unsigned> MaybeCode = Cursor.ReadCode();
4056e5dd7070Spatrick if (!MaybeCode)
4057e5dd7070Spatrick llvm::report_fatal_error(
4058*12c85518Srobert Twine("ASTReader::loadDeclUpdateRecords failed reading code: ") +
4059e5dd7070Spatrick toString(MaybeCode.takeError()));
4060e5dd7070Spatrick unsigned Code = MaybeCode.get();
4061e5dd7070Spatrick ASTRecordReader Record(*this, *F);
4062e5dd7070Spatrick if (Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code))
4063e5dd7070Spatrick assert(MaybeRecCode.get() == DECL_UPDATES &&
4064e5dd7070Spatrick "Expected DECL_UPDATES record!");
4065e5dd7070Spatrick else
4066e5dd7070Spatrick llvm::report_fatal_error(
4067*12c85518Srobert Twine("ASTReader::loadDeclUpdateRecords failed reading rec code: ") +
4068e5dd7070Spatrick toString(MaybeCode.takeError()));
4069e5dd7070Spatrick
4070e5dd7070Spatrick ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
4071e5dd7070Spatrick SourceLocation());
4072e5dd7070Spatrick Reader.UpdateDecl(D, PendingLazySpecializationIDs);
4073e5dd7070Spatrick
4074e5dd7070Spatrick // We might have made this declaration interesting. If so, remember that
4075e5dd7070Spatrick // we need to hand it off to the consumer.
4076e5dd7070Spatrick if (!WasInteresting &&
4077e5dd7070Spatrick isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
4078e5dd7070Spatrick PotentiallyInterestingDecls.push_back(
4079e5dd7070Spatrick InterestingDecl(D, Reader.hasPendingBody()));
4080e5dd7070Spatrick WasInteresting = true;
4081e5dd7070Spatrick }
4082e5dd7070Spatrick }
4083e5dd7070Spatrick }
4084e5dd7070Spatrick // Add the lazy specializations to the template.
4085e5dd7070Spatrick assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
4086*12c85518Srobert isa<FunctionTemplateDecl, VarTemplateDecl>(D)) &&
4087e5dd7070Spatrick "Must not have pending specializations");
4088e5dd7070Spatrick if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
4089e5dd7070Spatrick ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
4090e5dd7070Spatrick else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
4091e5dd7070Spatrick ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
4092e5dd7070Spatrick else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
4093e5dd7070Spatrick ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
4094e5dd7070Spatrick PendingLazySpecializationIDs.clear();
4095e5dd7070Spatrick
4096e5dd7070Spatrick // Load the pending visible updates for this decl context, if it has any.
4097e5dd7070Spatrick auto I = PendingVisibleUpdates.find(ID);
4098e5dd7070Spatrick if (I != PendingVisibleUpdates.end()) {
4099e5dd7070Spatrick auto VisibleUpdates = std::move(I->second);
4100e5dd7070Spatrick PendingVisibleUpdates.erase(I);
4101e5dd7070Spatrick
4102e5dd7070Spatrick auto *DC = cast<DeclContext>(D)->getPrimaryContext();
4103e5dd7070Spatrick for (const auto &Update : VisibleUpdates)
4104e5dd7070Spatrick Lookups[DC].Table.add(
4105e5dd7070Spatrick Update.Mod, Update.Data,
4106e5dd7070Spatrick reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
4107e5dd7070Spatrick DC->setHasExternalVisibleStorage(true);
4108e5dd7070Spatrick }
4109e5dd7070Spatrick }
4110e5dd7070Spatrick
loadPendingDeclChain(Decl * FirstLocal,uint64_t LocalOffset)4111e5dd7070Spatrick void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
4112e5dd7070Spatrick // Attach FirstLocal to the end of the decl chain.
4113e5dd7070Spatrick Decl *CanonDecl = FirstLocal->getCanonicalDecl();
4114e5dd7070Spatrick if (FirstLocal != CanonDecl) {
4115e5dd7070Spatrick Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
4116e5dd7070Spatrick ASTDeclReader::attachPreviousDecl(
4117e5dd7070Spatrick *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
4118e5dd7070Spatrick CanonDecl);
4119e5dd7070Spatrick }
4120e5dd7070Spatrick
4121e5dd7070Spatrick if (!LocalOffset) {
4122e5dd7070Spatrick ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
4123e5dd7070Spatrick return;
4124e5dd7070Spatrick }
4125e5dd7070Spatrick
4126e5dd7070Spatrick // Load the list of other redeclarations from this module file.
4127e5dd7070Spatrick ModuleFile *M = getOwningModuleFile(FirstLocal);
4128e5dd7070Spatrick assert(M && "imported decl from no module file");
4129e5dd7070Spatrick
4130e5dd7070Spatrick llvm::BitstreamCursor &Cursor = M->DeclsCursor;
4131e5dd7070Spatrick SavedStreamPosition SavedPosition(Cursor);
4132e5dd7070Spatrick if (llvm::Error JumpFailed = Cursor.JumpToBit(LocalOffset))
4133e5dd7070Spatrick llvm::report_fatal_error(
4134*12c85518Srobert Twine("ASTReader::loadPendingDeclChain failed jumping: ") +
4135e5dd7070Spatrick toString(std::move(JumpFailed)));
4136e5dd7070Spatrick
4137e5dd7070Spatrick RecordData Record;
4138e5dd7070Spatrick Expected<unsigned> MaybeCode = Cursor.ReadCode();
4139e5dd7070Spatrick if (!MaybeCode)
4140e5dd7070Spatrick llvm::report_fatal_error(
4141*12c85518Srobert Twine("ASTReader::loadPendingDeclChain failed reading code: ") +
4142e5dd7070Spatrick toString(MaybeCode.takeError()));
4143e5dd7070Spatrick unsigned Code = MaybeCode.get();
4144e5dd7070Spatrick if (Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record))
4145e5dd7070Spatrick assert(MaybeRecCode.get() == LOCAL_REDECLARATIONS &&
4146e5dd7070Spatrick "expected LOCAL_REDECLARATIONS record!");
4147e5dd7070Spatrick else
4148e5dd7070Spatrick llvm::report_fatal_error(
4149*12c85518Srobert Twine("ASTReader::loadPendingDeclChain failed reading rec code: ") +
4150e5dd7070Spatrick toString(MaybeCode.takeError()));
4151e5dd7070Spatrick
4152e5dd7070Spatrick // FIXME: We have several different dispatches on decl kind here; maybe
4153e5dd7070Spatrick // we should instead generate one loop per kind and dispatch up-front?
4154e5dd7070Spatrick Decl *MostRecent = FirstLocal;
4155e5dd7070Spatrick for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4156e5dd7070Spatrick auto *D = GetLocalDecl(*M, Record[N - I - 1]);
4157e5dd7070Spatrick ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
4158e5dd7070Spatrick MostRecent = D;
4159e5dd7070Spatrick }
4160e5dd7070Spatrick ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
4161e5dd7070Spatrick }
4162e5dd7070Spatrick
4163e5dd7070Spatrick namespace {
4164e5dd7070Spatrick
4165e5dd7070Spatrick /// Given an ObjC interface, goes through the modules and links to the
4166e5dd7070Spatrick /// interface all the categories for it.
4167e5dd7070Spatrick class ObjCCategoriesVisitor {
4168e5dd7070Spatrick ASTReader &Reader;
4169e5dd7070Spatrick ObjCInterfaceDecl *Interface;
4170e5dd7070Spatrick llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
4171e5dd7070Spatrick ObjCCategoryDecl *Tail = nullptr;
4172e5dd7070Spatrick llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
4173e5dd7070Spatrick serialization::GlobalDeclID InterfaceID;
4174e5dd7070Spatrick unsigned PreviousGeneration;
4175e5dd7070Spatrick
add(ObjCCategoryDecl * Cat)4176e5dd7070Spatrick void add(ObjCCategoryDecl *Cat) {
4177e5dd7070Spatrick // Only process each category once.
4178e5dd7070Spatrick if (!Deserialized.erase(Cat))
4179e5dd7070Spatrick return;
4180e5dd7070Spatrick
4181e5dd7070Spatrick // Check for duplicate categories.
4182e5dd7070Spatrick if (Cat->getDeclName()) {
4183e5dd7070Spatrick ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
4184e5dd7070Spatrick if (Existing &&
4185e5dd7070Spatrick Reader.getOwningModuleFile(Existing)
4186e5dd7070Spatrick != Reader.getOwningModuleFile(Cat)) {
4187e5dd7070Spatrick // FIXME: We should not warn for duplicates in diamond:
4188e5dd7070Spatrick //
4189e5dd7070Spatrick // MT //
4190e5dd7070Spatrick // / \ //
4191e5dd7070Spatrick // ML MR //
4192e5dd7070Spatrick // \ / //
4193e5dd7070Spatrick // MB //
4194e5dd7070Spatrick //
4195e5dd7070Spatrick // If there are duplicates in ML/MR, there will be warning when
4196e5dd7070Spatrick // creating MB *and* when importing MB. We should not warn when
4197e5dd7070Spatrick // importing.
4198e5dd7070Spatrick Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4199e5dd7070Spatrick << Interface->getDeclName() << Cat->getDeclName();
4200e5dd7070Spatrick Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4201e5dd7070Spatrick } else if (!Existing) {
4202e5dd7070Spatrick // Record this category.
4203e5dd7070Spatrick Existing = Cat;
4204e5dd7070Spatrick }
4205e5dd7070Spatrick }
4206e5dd7070Spatrick
4207e5dd7070Spatrick // Add this category to the end of the chain.
4208e5dd7070Spatrick if (Tail)
4209e5dd7070Spatrick ASTDeclReader::setNextObjCCategory(Tail, Cat);
4210e5dd7070Spatrick else
4211e5dd7070Spatrick Interface->setCategoryListRaw(Cat);
4212e5dd7070Spatrick Tail = Cat;
4213e5dd7070Spatrick }
4214e5dd7070Spatrick
4215e5dd7070Spatrick public:
ObjCCategoriesVisitor(ASTReader & Reader,ObjCInterfaceDecl * Interface,llvm::SmallPtrSetImpl<ObjCCategoryDecl * > & Deserialized,serialization::GlobalDeclID InterfaceID,unsigned PreviousGeneration)4216e5dd7070Spatrick ObjCCategoriesVisitor(ASTReader &Reader,
4217e5dd7070Spatrick ObjCInterfaceDecl *Interface,
4218e5dd7070Spatrick llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4219e5dd7070Spatrick serialization::GlobalDeclID InterfaceID,
4220e5dd7070Spatrick unsigned PreviousGeneration)
4221e5dd7070Spatrick : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4222e5dd7070Spatrick InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
4223e5dd7070Spatrick // Populate the name -> category map with the set of known categories.
4224e5dd7070Spatrick for (auto *Cat : Interface->known_categories()) {
4225e5dd7070Spatrick if (Cat->getDeclName())
4226e5dd7070Spatrick NameCategoryMap[Cat->getDeclName()] = Cat;
4227e5dd7070Spatrick
4228e5dd7070Spatrick // Keep track of the tail of the category list.
4229e5dd7070Spatrick Tail = Cat;
4230e5dd7070Spatrick }
4231e5dd7070Spatrick }
4232e5dd7070Spatrick
operator ()(ModuleFile & M)4233e5dd7070Spatrick bool operator()(ModuleFile &M) {
4234e5dd7070Spatrick // If we've loaded all of the category information we care about from
4235e5dd7070Spatrick // this module file, we're done.
4236e5dd7070Spatrick if (M.Generation <= PreviousGeneration)
4237e5dd7070Spatrick return true;
4238e5dd7070Spatrick
4239e5dd7070Spatrick // Map global ID of the definition down to the local ID used in this
4240e5dd7070Spatrick // module file. If there is no such mapping, we'll find nothing here
4241e5dd7070Spatrick // (or in any module it imports).
4242e5dd7070Spatrick DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4243e5dd7070Spatrick if (!LocalID)
4244e5dd7070Spatrick return true;
4245e5dd7070Spatrick
4246e5dd7070Spatrick // Perform a binary search to find the local redeclarations for this
4247e5dd7070Spatrick // declaration (if any).
4248e5dd7070Spatrick const ObjCCategoriesInfo Compare = { LocalID, 0 };
4249e5dd7070Spatrick const ObjCCategoriesInfo *Result
4250e5dd7070Spatrick = std::lower_bound(M.ObjCCategoriesMap,
4251e5dd7070Spatrick M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
4252e5dd7070Spatrick Compare);
4253e5dd7070Spatrick if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4254e5dd7070Spatrick Result->DefinitionID != LocalID) {
4255e5dd7070Spatrick // We didn't find anything. If the class definition is in this module
4256e5dd7070Spatrick // file, then the module files it depends on cannot have any categories,
4257e5dd7070Spatrick // so suppress further lookup.
4258e5dd7070Spatrick return Reader.isDeclIDFromModule(InterfaceID, M);
4259e5dd7070Spatrick }
4260e5dd7070Spatrick
4261e5dd7070Spatrick // We found something. Dig out all of the categories.
4262e5dd7070Spatrick unsigned Offset = Result->Offset;
4263e5dd7070Spatrick unsigned N = M.ObjCCategories[Offset];
4264e5dd7070Spatrick M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4265e5dd7070Spatrick for (unsigned I = 0; I != N; ++I)
4266e5dd7070Spatrick add(cast_or_null<ObjCCategoryDecl>(
4267e5dd7070Spatrick Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4268e5dd7070Spatrick return true;
4269e5dd7070Spatrick }
4270e5dd7070Spatrick };
4271e5dd7070Spatrick
4272e5dd7070Spatrick } // namespace
4273e5dd7070Spatrick
loadObjCCategories(serialization::GlobalDeclID ID,ObjCInterfaceDecl * D,unsigned PreviousGeneration)4274e5dd7070Spatrick void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4275e5dd7070Spatrick ObjCInterfaceDecl *D,
4276e5dd7070Spatrick unsigned PreviousGeneration) {
4277e5dd7070Spatrick ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
4278e5dd7070Spatrick PreviousGeneration);
4279e5dd7070Spatrick ModuleMgr.visit(Visitor);
4280e5dd7070Spatrick }
4281e5dd7070Spatrick
4282e5dd7070Spatrick template<typename DeclT, typename Fn>
forAllLaterRedecls(DeclT * D,Fn F)4283e5dd7070Spatrick static void forAllLaterRedecls(DeclT *D, Fn F) {
4284e5dd7070Spatrick F(D);
4285e5dd7070Spatrick
4286e5dd7070Spatrick // Check whether we've already merged D into its redeclaration chain.
4287e5dd7070Spatrick // MostRecent may or may not be nullptr if D has not been merged. If
4288e5dd7070Spatrick // not, walk the merged redecl chain and see if it's there.
4289e5dd7070Spatrick auto *MostRecent = D->getMostRecentDecl();
4290e5dd7070Spatrick bool Found = false;
4291e5dd7070Spatrick for (auto *Redecl = MostRecent; Redecl && !Found;
4292e5dd7070Spatrick Redecl = Redecl->getPreviousDecl())
4293e5dd7070Spatrick Found = (Redecl == D);
4294e5dd7070Spatrick
4295e5dd7070Spatrick // If this declaration is merged, apply the functor to all later decls.
4296e5dd7070Spatrick if (Found) {
4297e5dd7070Spatrick for (auto *Redecl = MostRecent; Redecl != D;
4298e5dd7070Spatrick Redecl = Redecl->getPreviousDecl())
4299e5dd7070Spatrick F(Redecl);
4300e5dd7070Spatrick }
4301e5dd7070Spatrick }
4302e5dd7070Spatrick
UpdateDecl(Decl * D,llvm::SmallVectorImpl<serialization::DeclID> & PendingLazySpecializationIDs)4303e5dd7070Spatrick void ASTDeclReader::UpdateDecl(Decl *D,
4304e5dd7070Spatrick llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
4305e5dd7070Spatrick while (Record.getIdx() < Record.size()) {
4306e5dd7070Spatrick switch ((DeclUpdateKind)Record.readInt()) {
4307e5dd7070Spatrick case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
4308e5dd7070Spatrick auto *RD = cast<CXXRecordDecl>(D);
4309e5dd7070Spatrick // FIXME: If we also have an update record for instantiating the
4310e5dd7070Spatrick // definition of D, we need that to happen before we get here.
4311e5dd7070Spatrick Decl *MD = Record.readDecl();
4312e5dd7070Spatrick assert(MD && "couldn't read decl from update record");
4313e5dd7070Spatrick // FIXME: We should call addHiddenDecl instead, to add the member
4314e5dd7070Spatrick // to its DeclContext.
4315e5dd7070Spatrick RD->addedMember(MD);
4316e5dd7070Spatrick break;
4317e5dd7070Spatrick }
4318e5dd7070Spatrick
4319e5dd7070Spatrick case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4320e5dd7070Spatrick // It will be added to the template's lazy specialization set.
4321e5dd7070Spatrick PendingLazySpecializationIDs.push_back(readDeclID());
4322e5dd7070Spatrick break;
4323e5dd7070Spatrick
4324e5dd7070Spatrick case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
4325e5dd7070Spatrick auto *Anon = readDeclAs<NamespaceDecl>();
4326e5dd7070Spatrick
4327e5dd7070Spatrick // Each module has its own anonymous namespace, which is disjoint from
4328e5dd7070Spatrick // any other module's anonymous namespaces, so don't attach the anonymous
4329e5dd7070Spatrick // namespace at all.
4330e5dd7070Spatrick if (!Record.isModule()) {
4331e5dd7070Spatrick if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
4332e5dd7070Spatrick TU->setAnonymousNamespace(Anon);
4333e5dd7070Spatrick else
4334e5dd7070Spatrick cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4335e5dd7070Spatrick }
4336e5dd7070Spatrick break;
4337e5dd7070Spatrick }
4338e5dd7070Spatrick
4339e5dd7070Spatrick case UPD_CXX_ADDED_VAR_DEFINITION: {
4340e5dd7070Spatrick auto *VD = cast<VarDecl>(D);
4341e5dd7070Spatrick VD->NonParmVarDeclBits.IsInline = Record.readInt();
4342e5dd7070Spatrick VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
4343e5dd7070Spatrick uint64_t Val = Record.readInt();
4344e5dd7070Spatrick if (Val && !VD->getInit()) {
4345e5dd7070Spatrick VD->setInit(Record.readExpr());
4346a9ac8606Spatrick if (Val != 1) {
4347e5dd7070Spatrick EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4348a9ac8606Spatrick Eval->HasConstantInitialization = (Val & 2) != 0;
4349a9ac8606Spatrick Eval->HasConstantDestruction = (Val & 4) != 0;
4350e5dd7070Spatrick }
4351e5dd7070Spatrick }
4352e5dd7070Spatrick break;
4353e5dd7070Spatrick }
4354e5dd7070Spatrick
4355e5dd7070Spatrick case UPD_CXX_POINT_OF_INSTANTIATION: {
4356e5dd7070Spatrick SourceLocation POI = Record.readSourceLocation();
4357e5dd7070Spatrick if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
4358e5dd7070Spatrick VTSD->setPointOfInstantiation(POI);
4359e5dd7070Spatrick } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4360e5dd7070Spatrick VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4361e5dd7070Spatrick } else {
4362e5dd7070Spatrick auto *FD = cast<FunctionDecl>(D);
4363e5dd7070Spatrick if (auto *FTSInfo = FD->TemplateOrSpecialization
4364e5dd7070Spatrick .dyn_cast<FunctionTemplateSpecializationInfo *>())
4365e5dd7070Spatrick FTSInfo->setPointOfInstantiation(POI);
4366e5dd7070Spatrick else
4367e5dd7070Spatrick FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4368e5dd7070Spatrick ->setPointOfInstantiation(POI);
4369e5dd7070Spatrick }
4370e5dd7070Spatrick break;
4371e5dd7070Spatrick }
4372e5dd7070Spatrick
4373e5dd7070Spatrick case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
4374e5dd7070Spatrick auto *Param = cast<ParmVarDecl>(D);
4375e5dd7070Spatrick
4376e5dd7070Spatrick // We have to read the default argument regardless of whether we use it
4377e5dd7070Spatrick // so that hypothetical further update records aren't messed up.
4378e5dd7070Spatrick // TODO: Add a function to skip over the next expr record.
4379e5dd7070Spatrick auto *DefaultArg = Record.readExpr();
4380e5dd7070Spatrick
4381e5dd7070Spatrick // Only apply the update if the parameter still has an uninstantiated
4382e5dd7070Spatrick // default argument.
4383e5dd7070Spatrick if (Param->hasUninstantiatedDefaultArg())
4384e5dd7070Spatrick Param->setDefaultArg(DefaultArg);
4385e5dd7070Spatrick break;
4386e5dd7070Spatrick }
4387e5dd7070Spatrick
4388e5dd7070Spatrick case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
4389e5dd7070Spatrick auto *FD = cast<FieldDecl>(D);
4390e5dd7070Spatrick auto *DefaultInit = Record.readExpr();
4391e5dd7070Spatrick
4392e5dd7070Spatrick // Only apply the update if the field still has an uninstantiated
4393e5dd7070Spatrick // default member initializer.
4394e5dd7070Spatrick if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4395e5dd7070Spatrick if (DefaultInit)
4396e5dd7070Spatrick FD->setInClassInitializer(DefaultInit);
4397e5dd7070Spatrick else
4398e5dd7070Spatrick // Instantiation failed. We can get here if we serialized an AST for
4399e5dd7070Spatrick // an invalid program.
4400e5dd7070Spatrick FD->removeInClassInitializer();
4401e5dd7070Spatrick }
4402e5dd7070Spatrick break;
4403e5dd7070Spatrick }
4404e5dd7070Spatrick
4405e5dd7070Spatrick case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
4406e5dd7070Spatrick auto *FD = cast<FunctionDecl>(D);
4407e5dd7070Spatrick if (Reader.PendingBodies[FD]) {
4408e5dd7070Spatrick // FIXME: Maybe check for ODR violations.
4409e5dd7070Spatrick // It's safe to stop now because this update record is always last.
4410e5dd7070Spatrick return;
4411e5dd7070Spatrick }
4412e5dd7070Spatrick
4413e5dd7070Spatrick if (Record.readInt()) {
4414e5dd7070Spatrick // Maintain AST consistency: any later redeclarations of this function
4415e5dd7070Spatrick // are inline if this one is. (We might have merged another declaration
4416e5dd7070Spatrick // into this one.)
4417e5dd7070Spatrick forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4418e5dd7070Spatrick FD->setImplicitlyInline();
4419e5dd7070Spatrick });
4420e5dd7070Spatrick }
4421e5dd7070Spatrick FD->setInnerLocStart(readSourceLocation());
4422e5dd7070Spatrick ReadFunctionDefinition(FD);
4423e5dd7070Spatrick assert(Record.getIdx() == Record.size() && "lazy body must be last");
4424e5dd7070Spatrick break;
4425e5dd7070Spatrick }
4426e5dd7070Spatrick
4427e5dd7070Spatrick case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4428e5dd7070Spatrick auto *RD = cast<CXXRecordDecl>(D);
4429e5dd7070Spatrick auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
4430e5dd7070Spatrick bool HadRealDefinition =
4431e5dd7070Spatrick OldDD && (OldDD->Definition != RD ||
4432e5dd7070Spatrick !Reader.PendingFakeDefinitionData.count(OldDD));
4433e5dd7070Spatrick RD->setParamDestroyedInCallee(Record.readInt());
4434e5dd7070Spatrick RD->setArgPassingRestrictions(
4435e5dd7070Spatrick (RecordDecl::ArgPassingKind)Record.readInt());
4436e5dd7070Spatrick ReadCXXRecordDefinition(RD, /*Update*/true);
4437e5dd7070Spatrick
4438e5dd7070Spatrick // Visible update is handled separately.
4439e5dd7070Spatrick uint64_t LexicalOffset = ReadLocalOffset();
4440e5dd7070Spatrick if (!HadRealDefinition && LexicalOffset) {
4441e5dd7070Spatrick Record.readLexicalDeclContextStorage(LexicalOffset, RD);
4442e5dd7070Spatrick Reader.PendingFakeDefinitionData.erase(OldDD);
4443e5dd7070Spatrick }
4444e5dd7070Spatrick
4445e5dd7070Spatrick auto TSK = (TemplateSpecializationKind)Record.readInt();
4446e5dd7070Spatrick SourceLocation POI = readSourceLocation();
4447e5dd7070Spatrick if (MemberSpecializationInfo *MSInfo =
4448e5dd7070Spatrick RD->getMemberSpecializationInfo()) {
4449e5dd7070Spatrick MSInfo->setTemplateSpecializationKind(TSK);
4450e5dd7070Spatrick MSInfo->setPointOfInstantiation(POI);
4451e5dd7070Spatrick } else {
4452e5dd7070Spatrick auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4453e5dd7070Spatrick Spec->setTemplateSpecializationKind(TSK);
4454e5dd7070Spatrick Spec->setPointOfInstantiation(POI);
4455e5dd7070Spatrick
4456e5dd7070Spatrick if (Record.readInt()) {
4457e5dd7070Spatrick auto *PartialSpec =
4458e5dd7070Spatrick readDeclAs<ClassTemplatePartialSpecializationDecl>();
4459e5dd7070Spatrick SmallVector<TemplateArgument, 8> TemplArgs;
4460e5dd7070Spatrick Record.readTemplateArgumentList(TemplArgs);
4461e5dd7070Spatrick auto *TemplArgList = TemplateArgumentList::CreateCopy(
4462e5dd7070Spatrick Reader.getContext(), TemplArgs);
4463e5dd7070Spatrick
4464e5dd7070Spatrick // FIXME: If we already have a partial specialization set,
4465e5dd7070Spatrick // check that it matches.
4466e5dd7070Spatrick if (!Spec->getSpecializedTemplateOrPartial()
4467e5dd7070Spatrick .is<ClassTemplatePartialSpecializationDecl *>())
4468e5dd7070Spatrick Spec->setInstantiationOf(PartialSpec, TemplArgList);
4469e5dd7070Spatrick }
4470e5dd7070Spatrick }
4471e5dd7070Spatrick
4472e5dd7070Spatrick RD->setTagKind((TagTypeKind)Record.readInt());
4473e5dd7070Spatrick RD->setLocation(readSourceLocation());
4474e5dd7070Spatrick RD->setLocStart(readSourceLocation());
4475e5dd7070Spatrick RD->setBraceRange(readSourceRange());
4476e5dd7070Spatrick
4477e5dd7070Spatrick if (Record.readInt()) {
4478e5dd7070Spatrick AttrVec Attrs;
4479e5dd7070Spatrick Record.readAttributes(Attrs);
4480e5dd7070Spatrick // If the declaration already has attributes, we assume that some other
4481e5dd7070Spatrick // AST file already loaded them.
4482e5dd7070Spatrick if (!D->hasAttrs())
4483e5dd7070Spatrick D->setAttrsImpl(Attrs, Reader.getContext());
4484e5dd7070Spatrick }
4485e5dd7070Spatrick break;
4486e5dd7070Spatrick }
4487e5dd7070Spatrick
4488e5dd7070Spatrick case UPD_CXX_RESOLVED_DTOR_DELETE: {
4489e5dd7070Spatrick // Set the 'operator delete' directly to avoid emitting another update
4490e5dd7070Spatrick // record.
4491e5dd7070Spatrick auto *Del = readDeclAs<FunctionDecl>();
4492e5dd7070Spatrick auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
4493e5dd7070Spatrick auto *ThisArg = Record.readExpr();
4494e5dd7070Spatrick // FIXME: Check consistency if we have an old and new operator delete.
4495e5dd7070Spatrick if (!First->OperatorDelete) {
4496e5dd7070Spatrick First->OperatorDelete = Del;
4497e5dd7070Spatrick First->OperatorDeleteThisArg = ThisArg;
4498e5dd7070Spatrick }
4499e5dd7070Spatrick break;
4500e5dd7070Spatrick }
4501e5dd7070Spatrick
4502e5dd7070Spatrick case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
4503e5dd7070Spatrick SmallVector<QualType, 8> ExceptionStorage;
4504e5dd7070Spatrick auto ESI = Record.readExceptionSpecInfo(ExceptionStorage);
4505e5dd7070Spatrick
4506e5dd7070Spatrick // Update this declaration's exception specification, if needed.
4507e5dd7070Spatrick auto *FD = cast<FunctionDecl>(D);
4508e5dd7070Spatrick auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4509e5dd7070Spatrick // FIXME: If the exception specification is already present, check that it
4510e5dd7070Spatrick // matches.
4511e5dd7070Spatrick if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
4512e5dd7070Spatrick FD->setType(Reader.getContext().getFunctionType(
4513e5dd7070Spatrick FPT->getReturnType(), FPT->getParamTypes(),
4514e5dd7070Spatrick FPT->getExtProtoInfo().withExceptionSpec(ESI)));
4515e5dd7070Spatrick
4516e5dd7070Spatrick // When we get to the end of deserializing, see if there are other decls
4517e5dd7070Spatrick // that we need to propagate this exception specification onto.
4518e5dd7070Spatrick Reader.PendingExceptionSpecUpdates.insert(
4519e5dd7070Spatrick std::make_pair(FD->getCanonicalDecl(), FD));
4520e5dd7070Spatrick }
4521e5dd7070Spatrick break;
4522e5dd7070Spatrick }
4523e5dd7070Spatrick
4524e5dd7070Spatrick case UPD_CXX_DEDUCED_RETURN_TYPE: {
4525e5dd7070Spatrick auto *FD = cast<FunctionDecl>(D);
4526e5dd7070Spatrick QualType DeducedResultType = Record.readType();
4527e5dd7070Spatrick Reader.PendingDeducedTypeUpdates.insert(
4528e5dd7070Spatrick {FD->getCanonicalDecl(), DeducedResultType});
4529e5dd7070Spatrick break;
4530e5dd7070Spatrick }
4531e5dd7070Spatrick
4532e5dd7070Spatrick case UPD_DECL_MARKED_USED:
4533e5dd7070Spatrick // Maintain AST consistency: any later redeclarations are used too.
4534e5dd7070Spatrick D->markUsed(Reader.getContext());
4535e5dd7070Spatrick break;
4536e5dd7070Spatrick
4537e5dd7070Spatrick case UPD_MANGLING_NUMBER:
4538e5dd7070Spatrick Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4539e5dd7070Spatrick Record.readInt());
4540e5dd7070Spatrick break;
4541e5dd7070Spatrick
4542e5dd7070Spatrick case UPD_STATIC_LOCAL_NUMBER:
4543e5dd7070Spatrick Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4544e5dd7070Spatrick Record.readInt());
4545e5dd7070Spatrick break;
4546e5dd7070Spatrick
4547e5dd7070Spatrick case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4548e5dd7070Spatrick D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
4549e5dd7070Spatrick Reader.getContext(), readSourceRange(),
4550e5dd7070Spatrick AttributeCommonInfo::AS_Pragma));
4551e5dd7070Spatrick break;
4552e5dd7070Spatrick
4553e5dd7070Spatrick case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
4554e5dd7070Spatrick auto AllocatorKind =
4555e5dd7070Spatrick static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(Record.readInt());
4556e5dd7070Spatrick Expr *Allocator = Record.readExpr();
4557*12c85518Srobert Expr *Alignment = Record.readExpr();
4558e5dd7070Spatrick SourceRange SR = readSourceRange();
4559e5dd7070Spatrick D->addAttr(OMPAllocateDeclAttr::CreateImplicit(
4560*12c85518Srobert Reader.getContext(), AllocatorKind, Allocator, Alignment, SR,
4561e5dd7070Spatrick AttributeCommonInfo::AS_Pragma));
4562e5dd7070Spatrick break;
4563e5dd7070Spatrick }
4564e5dd7070Spatrick
4565e5dd7070Spatrick case UPD_DECL_EXPORTED: {
4566e5dd7070Spatrick unsigned SubmoduleID = readSubmoduleID();
4567e5dd7070Spatrick auto *Exported = cast<NamedDecl>(D);
4568e5dd7070Spatrick Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
4569e5dd7070Spatrick Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
4570e5dd7070Spatrick Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
4571e5dd7070Spatrick break;
4572e5dd7070Spatrick }
4573e5dd7070Spatrick
4574e5dd7070Spatrick case UPD_DECL_MARKED_OPENMP_DECLARETARGET: {
4575a9ac8606Spatrick auto MapType = Record.readEnum<OMPDeclareTargetDeclAttr::MapTypeTy>();
4576a9ac8606Spatrick auto DevType = Record.readEnum<OMPDeclareTargetDeclAttr::DevTypeTy>();
4577*12c85518Srobert Expr *IndirectE = Record.readExpr();
4578*12c85518Srobert bool Indirect = Record.readBool();
4579a9ac8606Spatrick unsigned Level = Record.readInt();
4580e5dd7070Spatrick D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
4581*12c85518Srobert Reader.getContext(), MapType, DevType, IndirectE, Indirect, Level,
4582*12c85518Srobert readSourceRange(), AttributeCommonInfo::AS_Pragma));
4583e5dd7070Spatrick break;
4584e5dd7070Spatrick }
4585e5dd7070Spatrick
4586e5dd7070Spatrick case UPD_ADDED_ATTR_TO_RECORD:
4587e5dd7070Spatrick AttrVec Attrs;
4588e5dd7070Spatrick Record.readAttributes(Attrs);
4589e5dd7070Spatrick assert(Attrs.size() == 1);
4590e5dd7070Spatrick D->addAttr(Attrs[0]);
4591e5dd7070Spatrick break;
4592e5dd7070Spatrick }
4593e5dd7070Spatrick }
4594e5dd7070Spatrick }
4595