blob: 9c467055fe551e3e7b230bb613f39efce0537d3e [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===//
Chris Lattner487412d2009-04-27 05:27:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file implements the ASTReader::ReadDeclRecord method, which is the
Chris Lattner487412d2009-04-27 05:27:42 +000011// entrypoint for loading a decl.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000015#include "ASTCommon.h"
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +000016#include "ASTReaderInternals.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclGroup.h"
20#include "clang/AST/DeclTemplate.h"
21#include "clang/AST/DeclVisitor.h"
22#include "clang/AST/Expr.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000023#include "clang/Sema/IdentifierResolver.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000024#include "clang/Sema/SemaDiagnostic.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000025#include "clang/Serialization/ASTReader.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000026#include "llvm/Support/SaveAndRestore.h"
Hans Wennborgdcfba332015-10-06 23:40:43 +000027
Chris Lattner487412d2009-04-27 05:27:42 +000028using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000029using namespace clang::serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000030
31//===----------------------------------------------------------------------===//
32// Declaration deserialization
33//===----------------------------------------------------------------------===//
34
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000035namespace clang {
Sebastian Redlb3298c32010-08-18 23:56:48 +000036 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000037 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000038 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000039 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000040 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000041 const SourceLocation ThisDeclLoc;
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +000042 typedef ASTReader::RecordData RecordData;
Sebastian Redl539c5062010-08-18 23:57:32 +000043 TypeID TypeIDForTypeDecl;
Richard Smithd08aeb62014-08-28 01:33:39 +000044 unsigned AnonymousDeclNumber;
Richard Smith70d58502014-08-30 00:04:23 +000045 GlobalDeclID NamedDeclForTagDecl;
46 IdentifierInfo *TypedefNameForLinkage;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000047
Douglas Gregora6017bb2012-10-09 17:21:28 +000048 bool HasPendingBody;
49
Vassil Vassilev928c8252016-04-28 14:13:28 +000050 ///\brief A flag to carry the information for a decl from the entity is
51 /// used. We use it to delay the marking of the canonical decl as used until
52 /// the entire declaration is deserialized and merged.
53 bool IsDeclMarkedUsed;
54
Sebastian Redlc67764e2010-07-22 22:43:28 +000055 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000056
David L. Jonesc4808b9e2016-12-15 20:53:26 +000057 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +000058 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +000059 assert(LocalOffset < Loc.Offset && "offset point after current record");
60 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +000061 }
62
David L. Jonesc4808b9e2016-12-15 20:53:26 +000063 uint64_t ReadGlobalOffset() {
64 uint64_t Local = ReadLocalOffset();
65 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +000066 }
67
David L. Jonesc4808b9e2016-12-15 20:53:26 +000068 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000069 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +000070 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000071
David L. Jonesc4808b9e2016-12-15 20:53:26 +000072 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000073 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +000074 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000075
David L. Jonesc4808b9e2016-12-15 20:53:26 +000076 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000077 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +000078 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000079
David L. Jonesc4808b9e2016-12-15 20:53:26 +000080 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000081 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +000082 }
Richard Smith0b884372015-02-27 00:25:58 +000083
David L. Jonesc4808b9e2016-12-15 20:53:26 +000084 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000085 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +000086 }
87
Richard Smith0b884372015-02-27 00:25:58 +000088 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +000089 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +000090 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +000091 }
92
David L. Jonesc4808b9e2016-12-15 20:53:26 +000093 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000094 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +000095 }
96
97 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +000098 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000099 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000100 }
101
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000102 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000103 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000104 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000105
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000106 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000107 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000108 }
109
110 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000111 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000112 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000113
David L. Jonesbe1557a2016-12-21 00:17:49 +0000114 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000115 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000116
117 Module *readModule() {
118 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000119 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000120
Richard Smith2a9e5c52015-02-03 03:32:14 +0000121 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000122 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
123 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000124 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000125 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000126 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000127 void MergeDefinitionData(ObjCInterfaceDecl *D,
128 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000129
Richard Smithd08aeb62014-08-28 01:33:39 +0000130 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
131 DeclContext *DC,
132 unsigned Index);
133 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
134 unsigned Index, NamedDecl *D);
135
Richard Smith0144f512015-08-22 02:09:38 +0000136 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000137 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000138 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000139 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000140 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000141
Douglas Gregor022857e2011-12-22 01:48:48 +0000142 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000143 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
144 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000145
Douglas Gregor022857e2011-12-22 01:48:48 +0000146 /// \brief Retrieve the first ID.
147 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000148
Richard Smith0144f512015-08-22 02:09:38 +0000149 /// \brief Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000150 bool isKeyDecl() const { return IsKeyDecl; }
151
Richard Smith5a4737c2015-02-06 02:42:59 +0000152 /// \brief Get a known declaration that this should be merged with, if
153 /// any.
154 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000155 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000156
Douglas Gregor022857e2011-12-22 01:48:48 +0000157 /// \brief Class used to capture the result of searching for an existing
158 /// declaration of a specific kind and name, along with the ability
159 /// to update the place where this result was found (the declaration
160 /// chain hanging off an identifier or the DeclContext we searched in)
161 /// if requested.
162 class FindExistingResult {
163 ASTReader &Reader;
164 NamedDecl *New;
165 NamedDecl *Existing;
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000166 bool AddResult;
Richard Smith70d58502014-08-30 00:04:23 +0000167
Richard Smithd08aeb62014-08-28 01:33:39 +0000168 unsigned AnonymousDeclNumber;
Richard Smith70d58502014-08-30 00:04:23 +0000169 IdentifierInfo *TypedefNameForLinkage;
Richard Smithd08aeb62014-08-28 01:33:39 +0000170
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000171 void operator=(FindExistingResult &&) = delete;
Richard Smithd08aeb62014-08-28 01:33:39 +0000172
Douglas Gregor022857e2011-12-22 01:48:48 +0000173 public:
174 FindExistingResult(ASTReader &Reader)
Richard Smithd08aeb62014-08-28 01:33:39 +0000175 : Reader(Reader), New(nullptr), Existing(nullptr), AddResult(false),
Hans Wennborgdcfba332015-10-06 23:40:43 +0000176 AnonymousDeclNumber(0), TypedefNameForLinkage(nullptr) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000177
Richard Smithd08aeb62014-08-28 01:33:39 +0000178 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000179 unsigned AnonymousDeclNumber,
180 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000181 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000182 AnonymousDeclNumber(AnonymousDeclNumber),
183 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000184
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000185 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000186 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
187 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000188 AnonymousDeclNumber(Other.AnonymousDeclNumber),
189 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000190 Other.AddResult = false;
191 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000192
Douglas Gregor022857e2011-12-22 01:48:48 +0000193 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000194
Douglas Gregor2009cee2012-01-03 22:46:00 +0000195 /// \brief Suppress the addition of this result into the known set of
196 /// names.
197 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000198
Douglas Gregor022857e2011-12-22 01:48:48 +0000199 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000200
Douglas Gregor022857e2011-12-22 01:48:48 +0000201 template<typename T>
202 operator T*() const { return dyn_cast_or_null<T>(Existing); }
203 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000204
Richard Smith8a639892015-01-24 01:07:20 +0000205 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
206 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000207 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000208
Chris Lattner487412d2009-04-27 05:27:42 +0000209 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000210 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
211 ASTReader::RecordLocation Loc,
212 DeclID thisDeclID, SourceLocation ThisDeclLoc)
213 : Reader(Reader), Record(Record), Loc(Loc),
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000214 ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc),
David L. Jonesbe1557a2016-12-21 00:17:49 +0000215 TypeIDForTypeDecl(0), NamedDeclForTagDecl(0),
Vassil Vassilev928c8252016-04-28 14:13:28 +0000216 TypedefNameForLinkage(nullptr), HasPendingBody(false),
217 IsDeclMarkedUsed(false) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000218
Vassil Vassilev497a9952017-06-09 21:54:18 +0000219 template <typename T> static
220 void AddLazySpecializations(T *D,
221 SmallVectorImpl<serialization::DeclID>& IDs) {
222 if (IDs.empty())
223 return;
224
225 // FIXME: We should avoid this pattern of getting the ASTContext.
226 ASTContext &C = D->getASTContext();
227
228 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
229
230 if (auto &Old = LazySpecializations) {
231 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
232 std::sort(IDs.begin(), IDs.end());
233 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
234 }
235
236 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
237 *Result = IDs.size();
238 std::copy(IDs.begin(), IDs.end(), Result + 1);
239
240 LazySpecializations = Result;
241 }
242
Richard Smithb321ecb2014-05-13 01:15:00 +0000243 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000244 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
245 static Decl *getMostRecentDeclImpl(...);
246 static Decl *getMostRecentDecl(Decl *D);
247
248 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000249 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000250 Redeclarable<DeclT> *D, Decl *Previous,
251 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000252 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000253 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
254 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000255
256 template <typename DeclT>
257 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
258 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000259 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000260
Richard Smith851072e2014-05-19 20:59:20 +0000261 template <typename DeclT>
262 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
263 static void markIncompleteDeclChainImpl(...);
264
Douglas Gregora6017bb2012-10-09 17:21:28 +0000265 /// \brief Determine whether this declaration has a pending body.
266 bool hasPendingBody() const { return HasPendingBody; }
267
David Blaikieac4345c2017-02-12 18:45:31 +0000268 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000269 void Visit(Decl *D);
270
Vassil Vassilev497a9952017-06-09 21:54:18 +0000271 void UpdateDecl(Decl *D, llvm::SmallVectorImpl<serialization::DeclID>&);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000272
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000273 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
274 ObjCCategoryDecl *Next) {
275 Cat->NextClassCategory = Next;
276 }
277
Chris Lattner487412d2009-04-27 05:27:42 +0000278 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000279 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000280 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000281 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
282 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000283 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000284 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000285 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
286 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000287 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000288 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000289 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000290 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000291 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000292 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000293 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000294 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
295 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
296 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
297 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
298 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000299 ClassTemplateSpecializationDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000300 void VisitClassTemplateSpecializationDecl(
301 ClassTemplateSpecializationDecl *D) {
302 VisitClassTemplateSpecializationDeclImpl(D);
303 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000304 void VisitClassTemplatePartialSpecializationDecl(
305 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000306 void VisitClassScopeFunctionSpecializationDecl(
307 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000308 RedeclarableResult
309 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
310 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
311 VisitVarTemplateSpecializationDeclImpl(D);
312 }
313 void VisitVarTemplatePartialSpecializationDecl(
314 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000315 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000316 void VisitValueDecl(ValueDecl *VD);
317 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000318 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000319 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000320 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000321 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000322 void VisitCXXMethodDecl(CXXMethodDecl *D);
323 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
324 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
325 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000326 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000327 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000328 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000329 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
330 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000331 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
332 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000333 void VisitDecompositionDecl(DecompositionDecl *DD);
334 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000335 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000336 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000337 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000338 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000339 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000340 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000341 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000342 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000343 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000344 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000345 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000346 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000347 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000348 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000349 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000350 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000351 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000352 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000353 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000354 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
355 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000356 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000357 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000358 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000359
Chris Lattner487412d2009-04-27 05:27:42 +0000360 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000361
362 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000363 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000364
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000365 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000366 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
367 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000368
369 template<typename T>
370 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000371 RedeclarableResult &Redecl,
372 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000373
Richard Smith0b87e072013-10-07 08:02:11 +0000374 template<typename T>
375 void mergeMergeable(Mergeable<T> *D);
376
Richard Smithf17fdbd2014-04-24 02:25:27 +0000377 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
378 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000379 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000380
Douglas Gregor85f3f952015-07-07 03:57:15 +0000381 ObjCTypeParamList *ReadObjCTypeParamList();
382
Alexis Hunted053252010-05-30 07:21:58 +0000383 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000384 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000385 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000386 void VisitObjCContainerDecl(ObjCContainerDecl *D);
387 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
388 void VisitObjCIvarDecl(ObjCIvarDecl *D);
389 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
390 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000391 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
392 void VisitObjCImplDecl(ObjCImplDecl *D);
393 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
394 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
395 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
396 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
397 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000398 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000399 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000400 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000401 };
Hans Wennborgdcfba332015-10-06 23:40:43 +0000402} // end namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000403
Richard Smith86dfc1e2015-06-18 22:07:00 +0000404namespace {
405/// Iterator over the redeclarations of a declaration that have already
406/// been merged into the same redeclaration chain.
407template<typename DeclT>
408class MergedRedeclIterator {
409 DeclT *Start, *Canonical, *Current;
410public:
411 MergedRedeclIterator() : Current(nullptr) {}
412 MergedRedeclIterator(DeclT *Start)
413 : Start(Start), Canonical(nullptr), Current(Start) {}
414
415 DeclT *operator*() { return Current; }
416
417 MergedRedeclIterator &operator++() {
418 if (Current->isFirstDecl()) {
419 Canonical = Current;
420 Current = Current->getMostRecentDecl();
421 } else
422 Current = Current->getPreviousDecl();
423
424 // If we started in the merged portion, we'll reach our start position
425 // eventually. Otherwise, we'll never reach it, but the second declaration
426 // we reached was the canonical declaration, so stop when we see that one
427 // again.
428 if (Current == Start || Current == Canonical)
429 Current = nullptr;
430 return *this;
431 }
432
433 friend bool operator!=(const MergedRedeclIterator &A,
434 const MergedRedeclIterator &B) {
435 return A.Current != B.Current;
436 }
437};
Hans Wennborgdcfba332015-10-06 23:40:43 +0000438} // end anonymous namespace
439
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000440template <typename DeclT>
441static llvm::iterator_range<MergedRedeclIterator<DeclT>>
442merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000443 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
444 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000445}
446
Sebastian Redlb3298c32010-08-18 23:56:48 +0000447uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000448 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000449}
450
David Blaikieac4345c2017-02-12 18:45:31 +0000451void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
David Blaikiee6b7c282017-04-11 20:46:34 +0000452 if (Record.readInt())
453 Reader.BodySource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000454 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
455 CD->NumCtorInitializers = Record.readInt();
456 if (CD->NumCtorInitializers)
457 CD->CtorInitializers = ReadGlobalOffset();
458 }
459 // Store the offset of the body so we can lazily load it later.
460 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
461 HasPendingBody = true;
462}
463
Sebastian Redlb3298c32010-08-18 23:56:48 +0000464void ASTDeclReader::Visit(Decl *D) {
465 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000466
Vassil Vassilev928c8252016-04-28 14:13:28 +0000467 // At this point we have deserialized and merged the decl and it is safe to
468 // update its canonical decl to signal that the entire entity is used.
469 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
470 IsDeclMarkedUsed = false;
471
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000472 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
473 if (DD->DeclInfo) {
474 DeclaratorDecl::ExtInfo *Info =
475 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000476 Info->TInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000477 }
478 else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000479 DD->DeclInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000480 }
481 }
482
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000483 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000484 // We have a fully initialized TypeDecl. Read its type now.
Douglas Gregor0cdc8322010-12-10 17:03:06 +0000485 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000486
487 // If this is a tag declaration with a typedef name for linkage, it's safe
488 // to load that typedef now.
489 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000490 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
491 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000492 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
493 // if we have a fully initialized TypeDecl, we can safely read its type now.
494 ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull();
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000495 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
496 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000497 // We only read it if FD doesn't already have a body (e.g., from another
498 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000499 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000500 if (Record.readInt())
501 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000502 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000503}
504
Sebastian Redlb3298c32010-08-18 23:56:48 +0000505void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000506 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
507 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000508 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000509 // parameter or of a parameter of a function template immediately. These
510 // entities might be used in the formulation of its DeclContext (for
511 // example, a function parameter can be used in decltype() in trailing
512 // return type of the function). Use the translation unit DeclContext as a
513 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000514 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
515 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000516 if (!LexicalDCIDForTemplateParmDecl)
517 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000518 Reader.addPendingDeclContextInfo(D,
519 SemaDCIDForTemplateParmDecl,
520 LexicalDCIDForTemplateParmDecl);
Douglas Gregor4163aca2011-09-09 21:34:22 +0000521 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000522 } else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000523 DeclContext *SemaDC = ReadDeclAs<DeclContext>();
524 DeclContext *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000525 if (!LexicalDC)
526 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000527 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000528 // Avoid calling setLexicalDeclContext() directly because it uses
529 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000530 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
531 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000532 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000533 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000534 D->setInvalidDecl(Record.readInt());
535 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000536 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000537 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000538 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
539 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000540 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000541 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000542 D->setImplicit(Record.readInt());
543 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000544 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000545 D->setReferenced(Record.readInt());
546 D->setTopLevelDeclInObjCContainer(Record.readInt());
547 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000548 D->FromASTFile = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000549 D->setModulePrivate(Record.readInt());
Douglas Gregor781f7132012-01-06 16:59:53 +0000550 D->Hidden = D->isModulePrivate();
Richard Smith42413142015-05-15 20:05:43 +0000551
Douglas Gregorcf68c582011-12-01 22:20:10 +0000552 // Determine whether this declaration is part of a (sub)module. If so, it
553 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000554 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000555 // Store the owning submodule ID in the declaration.
556 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000557
558 if (D->Hidden) {
559 // Module-private declarations are never visible, so there is no work to do.
560 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
561 // If local visibility is being tracked, this declaration will become
562 // hidden and visible as the owning module does. Inform Sema that this
563 // declaration might not be visible.
564 D->Hidden = true;
565 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
566 if (Owner->NameVisibility != Module::AllVisible) {
567 // The owning module is not visible. Mark this declaration as hidden.
568 D->Hidden = true;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000569
Richard Smith42413142015-05-15 20:05:43 +0000570 // Note that this declaration was hidden because its owning module is
571 // not yet visible.
572 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000573 }
574 }
575 }
Chris Lattner487412d2009-04-27 05:27:42 +0000576}
577
Nico Weber66220292016-03-02 17:28:48 +0000578void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
579 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000580 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000581 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000582 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000583 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
584 D->getTrailingObjects<char>()[Arg.size()] = '\0';
585}
586
Nico Webercbbaeb12016-03-02 19:28:54 +0000587void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
588 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000589 D->setLocation(ReadSourceLocation());
590 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000591 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
592 D->getTrailingObjects<char>()[Name.size()] = '\0';
593
594 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000595 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000596 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
597 Value.size());
598 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
599}
600
Sebastian Redlb3298c32010-08-18 23:56:48 +0000601void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000602 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000603}
604
Sebastian Redlb3298c32010-08-18 23:56:48 +0000605void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000606 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000607 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000608 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000609}
610
Sebastian Redlb3298c32010-08-18 23:56:48 +0000611void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000612 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000613 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000614 // Delay type reading until after we have fully initialized the decl.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000615 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000616}
617
Richard Smith43ccec8e2014-08-26 03:52:16 +0000618ASTDeclReader::RedeclarableResult
619ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000620 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000621 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000622 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000623 if (Record.readInt()) { // isModed
624 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000625 TD->setModedTypeSourceInfo(TInfo, modedT);
626 } else
627 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000628 // Read and discard the declaration for which this is a typedef name for
629 // linkage, if it exists. We cannot rely on our type to pull in this decl,
630 // because it might have been merged with a type from another module and
631 // thus might not refer to our version of the declaration.
632 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000633 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000634}
635
636void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000637 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
638 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000639}
640
Richard Smithdda56e42011-04-15 14:24:37 +0000641void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000642 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000643 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000644 // Merged when we merge the template.
645 TD->setDescribedAliasTemplate(Template);
646 else
647 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000648}
649
Richard Smith1d209d02013-05-23 01:49:11 +0000650ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000651 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000652 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000653
David L. Jonesbe1557a2016-12-21 00:17:49 +0000654 TD->IdentifierNamespace = Record.readInt();
655 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000656 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000657 TD->setCompleteDefinition(Record.readInt());
658 TD->setEmbeddedInDeclarator(Record.readInt());
659 TD->setFreeStanding(Record.readInt());
660 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000661 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000662
David L. Jonesbe1557a2016-12-21 00:17:49 +0000663 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000664 case 0:
665 break;
666 case 1: { // ExtInfo
Douglas Gregor4163aca2011-09-09 21:34:22 +0000667 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000668 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000669 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000670 break;
671 }
672 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000673 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000674 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000675 break;
Richard Smith70d58502014-08-30 00:04:23 +0000676 default:
677 llvm_unreachable("unexpected tag info kind");
678 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000679
Richard Smithcd45dbc2014-04-19 03:48:30 +0000680 if (!isa<CXXRecordDecl>(TD))
681 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000682 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000683}
684
Sebastian Redlb3298c32010-08-18 23:56:48 +0000685void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000686 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000687 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000688 ED->setIntegerTypeSourceInfo(TI);
689 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000690 ED->setIntegerType(Record.readType());
691 ED->setPromotionType(Record.readType());
692 ED->setNumPositiveBits(Record.readInt());
693 ED->setNumNegativeBits(Record.readInt());
694 ED->IsScoped = Record.readInt();
695 ED->IsScopedUsingClassTag = Record.readInt();
696 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000697
Richard Smith01a73372013-10-15 22:02:41 +0000698 // If this is a definition subject to the ODR, and we already have a
699 // definition, merge this one into it.
700 if (ED->IsCompleteDefinition &&
701 Reader.getContext().getLangOpts().Modules &&
702 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000703 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
704 if (!OldDef) {
705 // This is the first time we've seen an imported definition. Look for a
706 // local definition before deciding that we are the first definition.
707 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
708 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
709 OldDef = D;
710 break;
711 }
712 }
713 }
714 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000715 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
716 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000717 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000718 } else {
719 OldDef = ED;
720 }
721 }
722
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000723 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000724 TemplateSpecializationKind TSK =
725 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000726 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000727 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
728 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
729 }
Chris Lattner487412d2009-04-27 05:27:42 +0000730}
731
Richard Smith1d209d02013-05-23 01:49:11 +0000732ASTDeclReader::RedeclarableResult
733ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
734 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000735 RD->setHasFlexibleArrayMember(Record.readInt());
736 RD->setAnonymousStructOrUnion(Record.readInt());
737 RD->setHasObjectMember(Record.readInt());
738 RD->setHasVolatileMember(Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000739 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000740}
741
Sebastian Redlb3298c32010-08-18 23:56:48 +0000742void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000743 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000744 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000745}
746
Sebastian Redlb3298c32010-08-18 23:56:48 +0000747void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000748 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000749 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000750 ECD->setInitExpr(Record.readExpr());
751 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000752 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000753}
754
Sebastian Redlb3298c32010-08-18 23:56:48 +0000755void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000756 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000757 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000758 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000759 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000760 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000761 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000762 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000763 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000764}
765
Sebastian Redlb3298c32010-08-18 23:56:48 +0000766void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000767 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000768 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000769
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000770 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000771 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000772
Douglas Gregorb2585692012-01-04 17:13:46 +0000773 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
774 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000775
David L. Jonesbe1557a2016-12-21 00:17:49 +0000776 FD->SClass = (StorageClass)Record.readInt();
777 FD->IsInline = Record.readInt();
778 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000779 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000780 FD->IsVirtualAsWritten = Record.readInt();
781 FD->IsPure = Record.readInt();
782 FD->HasInheritedPrototype = Record.readInt();
783 FD->HasWrittenPrototype = Record.readInt();
784 FD->IsDeleted = Record.readInt();
785 FD->IsTrivial = Record.readInt();
786 FD->IsDefaulted = Record.readInt();
787 FD->IsExplicitlyDefaulted = Record.readInt();
788 FD->HasImplicitReturnZero = Record.readInt();
789 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000790 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000791 FD->HasSkippedBody = Record.readInt();
792 FD->IsLateTemplateParsed = Record.readInt();
793 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000794 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000795
David L. Jonesbe1557a2016-12-21 00:17:49 +0000796 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000797 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000798 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000799 break;
800 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000801 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000802 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000803 break;
804 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000805 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000806 TemplateSpecializationKind TSK =
807 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000808 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000809 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000810 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000811 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000812 break;
813 }
814 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000815 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000816 TemplateSpecializationKind TSK =
817 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000818
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000819 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000820 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000821 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000822
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000823 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000824 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000825 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000826 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000827 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000828 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000829 TemplArgLocs.reserve(NumTemplateArgLocs);
830 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000831 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000832
833 LAngleLoc = ReadSourceLocation();
834 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000835 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000836
837 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000838
Douglas Gregor4163aca2011-09-09 21:34:22 +0000839 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000840 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000841 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000842 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000843 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000844 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000845 FunctionTemplateSpecializationInfo *FTInfo
846 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
847 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000848 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
849 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000850 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000851 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000852
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000853 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000854 // The template that contains the specializations set. It's not safe to
855 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000856 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000857 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
858 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
859 // FunctionTemplateSpecializationInfo's Profile().
860 // We avoid getASTContext because a decl in the parent hierarchy may
861 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000862 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000863 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000864 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000865 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000866 FunctionTemplateSpecializationInfo *ExistingInfo =
867 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000868 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000869 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
870 else {
871 assert(Reader.getContext().getLangOpts().Modules &&
872 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000873 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000874 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000875 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000876 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000877 }
878 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
879 // Templates.
880 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000881 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000882 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000883 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
884
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000885 // Templates args.
886 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000887 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000888 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000889 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000890 TemplArgs.setLAngleLoc(ReadSourceLocation());
891 TemplArgs.setRAngleLoc(ReadSourceLocation());
892
Douglas Gregor4163aca2011-09-09 21:34:22 +0000893 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000894 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000895 // These are not merged; we don't need to merge redeclarations of dependent
896 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000897 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000898 }
899 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000900
Chris Lattnerca025db2010-05-07 21:43:38 +0000901 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000902 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000903 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000904 Params.reserve(NumParams);
905 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000906 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000907 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000908}
909
Sebastian Redlb3298c32010-08-18 23:56:48 +0000910void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000911 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000912 if (Record.readInt()) {
Douglas Gregora6017bb2012-10-09 17:21:28 +0000913 // Load the body on-demand. Most clients won't care, because method
914 // definitions rarely show up in headers.
915 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
916 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000917 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
918 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000919 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000920 MD->setInstanceMethod(Record.readInt());
921 MD->setVariadic(Record.readInt());
922 MD->setPropertyAccessor(Record.readInt());
923 MD->setDefined(Record.readInt());
924 MD->IsOverriding = Record.readInt();
925 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000926
David L. Jonesbe1557a2016-12-21 00:17:49 +0000927 MD->IsRedeclaration = Record.readInt();
928 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000929 if (MD->HasRedeclaration)
930 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000931 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000932
David L. Jonesbe1557a2016-12-21 00:17:49 +0000933 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
934 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
935 MD->SetRelatedResultType(Record.readInt());
936 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000937 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
938 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000939 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000940 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000941 Params.reserve(NumParams);
942 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000943 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000944
David L. Jonesbe1557a2016-12-21 00:17:49 +0000945 MD->SelLocsKind = Record.readInt();
946 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000947 SmallVector<SourceLocation, 16> SelLocs;
948 SelLocs.reserve(NumStoredSelLocs);
949 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000950 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000951
952 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000953}
954
Douglas Gregor85f3f952015-07-07 03:57:15 +0000955void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000956 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000957
David L. Jonesbe1557a2016-12-21 00:17:49 +0000958 D->Variance = Record.readInt();
959 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000960 D->VarianceLoc = ReadSourceLocation();
961 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000962}
963
Sebastian Redlb3298c32010-08-18 23:56:48 +0000964void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000965 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000966 CD->setAtStartLoc(ReadSourceLocation());
967 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000968}
969
Douglas Gregor85f3f952015-07-07 03:57:15 +0000970ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000971 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000972 if (numParams == 0)
973 return nullptr;
974
975 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
976 typeParams.reserve(numParams);
977 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000978 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000979 if (!typeParam)
980 return nullptr;
981
982 typeParams.push_back(typeParam);
983 }
984
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000985 SourceLocation lAngleLoc = ReadSourceLocation();
986 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000987
988 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
989 typeParams, rAngleLoc);
990}
991
Manman Renec315f12016-09-09 23:48:27 +0000992void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +0000993 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +0000994 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000995 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +0000996
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000997 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000998 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000999
Manman Renec315f12016-09-09 23:48:27 +00001000 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001001 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001002 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1003 Protocols.reserve(NumProtocols);
1004 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001005 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001006 SmallVector<SourceLocation, 16> ProtoLocs;
1007 ProtoLocs.reserve(NumProtocols);
1008 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001009 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001010 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1011 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001012
Manman Renec315f12016-09-09 23:48:27 +00001013 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001014 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001015 Protocols.clear();
1016 Protocols.reserve(NumProtocols);
1017 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001018 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001019 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1020 Reader.getContext());
1021}
1022
1023void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1024 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1025 // FIXME: odr checking?
1026}
1027
Sebastian Redlb3298c32010-08-18 23:56:48 +00001028void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001029 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001030 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001031 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001032 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001033
1034 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001035 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001036 // Read the definition.
1037 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001038
David L. Jonesbe1557a2016-12-21 00:17:49 +00001039 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001040 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1041 if (Canon->Data.getPointer()) {
1042 // If we already have a definition, keep the definition invariant and
1043 // merge the data.
1044 MergeDefinitionData(Canon, std::move(ID->data()));
1045 ID->Data = Canon->Data;
1046 } else {
1047 // Set the definition data of the canonical declaration, so other
1048 // redeclarations will see it.
1049 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001050
Manman Renec315f12016-09-09 23:48:27 +00001051 // We will rebuild this list lazily.
1052 ID->setIvarList(nullptr);
1053 }
Craig Toppera13603a2014-05-22 05:54:18 +00001054
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001055 // Note that we have deserialized a definition.
1056 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001057
Douglas Gregor404cdde2012-01-27 01:47:08 +00001058 // Note that we've loaded this Objective-C class.
1059 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001060 } else {
1061 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001062 }
Chris Lattner487412d2009-04-27 05:27:42 +00001063}
1064
Sebastian Redlb3298c32010-08-18 23:56:48 +00001065void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001066 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001067 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001068 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001069 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001070 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001071 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001072}
1073
Sebastian Redlb3298c32010-08-18 23:56:48 +00001074void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
Douglas Gregorda389302012-01-01 21:47:52 +00001075 RedeclarableResult Redecl = VisitRedeclarable(PD);
Chris Lattner487412d2009-04-27 05:27:42 +00001076 VisitObjCContainerDecl(PD);
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001077 mergeRedeclarable(PD, Redecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001078
David L. Jonesbe1557a2016-12-21 00:17:49 +00001079 if (Record.readInt()) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00001080 // Read the definition.
1081 PD->allocateDefinitionData();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001082
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001083 // Set the definition data of the canonical declaration, so other
1084 // redeclarations will see it.
1085 PD->getCanonicalDecl()->Data = PD->Data;
1086
David L. Jonesbe1557a2016-12-21 00:17:49 +00001087 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001088 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1089 ProtoRefs.reserve(NumProtoRefs);
1090 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001091 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001092 SmallVector<SourceLocation, 16> ProtoLocs;
1093 ProtoLocs.reserve(NumProtoRefs);
1094 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001095 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001096 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
1097 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001098
Douglas Gregora715bff2012-01-01 19:51:50 +00001099 // Note that we have deserialized a definition.
1100 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001101 } else {
1102 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001103 }
Chris Lattner487412d2009-04-27 05:27:42 +00001104}
1105
Sebastian Redlb3298c32010-08-18 23:56:48 +00001106void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001107 VisitFieldDecl(FD);
1108}
1109
Sebastian Redlb3298c32010-08-18 23:56:48 +00001110void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001111 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001112 CD->setCategoryNameLoc(ReadSourceLocation());
1113 CD->setIvarLBraceLoc(ReadSourceLocation());
1114 CD->setIvarRBraceLoc(ReadSourceLocation());
1115
Douglas Gregor404cdde2012-01-27 01:47:08 +00001116 // Note that this category has been deserialized. We do this before
1117 // deserializing the interface declaration, so that it will consider this
1118 /// category.
1119 Reader.CategoriesDeserialized.insert(CD);
1120
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001121 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001122 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001123 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001124 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001125 ProtoRefs.reserve(NumProtoRefs);
1126 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001127 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001128 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001129 ProtoLocs.reserve(NumProtoRefs);
1130 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001131 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001132 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001133 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001134}
1135
Sebastian Redlb3298c32010-08-18 23:56:48 +00001136void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001137 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001138 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001139}
1140
Sebastian Redlb3298c32010-08-18 23:56:48 +00001141void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001142 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001143 D->setAtLoc(ReadSourceLocation());
1144 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001145 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001146 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001147 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001148 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001149 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001150 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001151 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001152 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001153 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001154 DeclarationName GetterName = Record.readDeclarationName();
1155 SourceLocation GetterLoc = ReadSourceLocation();
1156 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1157 DeclarationName SetterName = Record.readDeclarationName();
1158 SourceLocation SetterLoc = ReadSourceLocation();
1159 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001160 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1161 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1162 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001163}
1164
Sebastian Redlb3298c32010-08-18 23:56:48 +00001165void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001166 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001167 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001168}
1169
Sebastian Redlb3298c32010-08-18 23:56:48 +00001170void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001171 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001172 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001173}
1174
Sebastian Redlb3298c32010-08-18 23:56:48 +00001175void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001176 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001177 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1178 D->SuperLoc = ReadSourceLocation();
1179 D->setIvarLBraceLoc(ReadSourceLocation());
1180 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001181 D->setHasNonZeroConstructors(Record.readInt());
1182 D->setHasDestructors(Record.readInt());
1183 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001184 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001185 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001186}
1187
Sebastian Redlb3298c32010-08-18 23:56:48 +00001188void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001189 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001190 D->setAtLoc(ReadSourceLocation());
1191 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1192 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1193 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001194 D->setGetterCXXConstructor(Record.readExpr());
1195 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001196}
1197
Sebastian Redlb3298c32010-08-18 23:56:48 +00001198void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001199 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001200 FD->Mutable = Record.readInt();
1201 if (int BitWidthOrInitializer = Record.readInt()) {
John McCallc90c1492014-10-10 18:44:34 +00001202 FD->InitStorage.setInt(
1203 static_cast<FieldDecl::InitStorageKind>(BitWidthOrInitializer - 1));
1204 if (FD->InitStorage.getInt() == FieldDecl::ISK_CapturedVLAType) {
Alexey Bataev39c81e22014-08-28 04:28:19 +00001205 // Read captured variable length array.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001206 FD->InitStorage.setPointer(Record.readType().getAsOpaquePtr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001207 } else {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001208 FD->InitStorage.setPointer(Record.readExpr());
Alexey Bataev39c81e22014-08-28 04:28:19 +00001209 }
Richard Smith2b013182012-06-10 03:12:00 +00001210 }
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001211 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001212 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001213 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001214 }
Richard Smith0b87e072013-10-07 08:02:11 +00001215 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001216}
1217
John McCall5e77d762013-04-16 07:28:30 +00001218void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1219 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001220 PD->GetterId = Record.getIdentifierInfo();
1221 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001222}
1223
Francois Pichet783dd6e2010-11-21 06:08:52 +00001224void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1225 VisitValueDecl(FD);
1226
David L. Jonesbe1557a2016-12-21 00:17:49 +00001227 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001228 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001229 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001230
1231 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001232 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001233
1234 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001235}
1236
Larisse Voufo39a1e502013-08-06 01:03:05 +00001237ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001238 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001239 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001240
David L. Jonesbe1557a2016-12-21 00:17:49 +00001241 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1242 VD->VarDeclBits.TSCSpec = Record.readInt();
1243 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001244 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001245 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1246 Record.readInt();
1247 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1248 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1249 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
1250 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1251 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1252 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1253 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1254 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1255 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001256 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001257 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001258 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001259 VD->setCachedLinkage(VarLinkage);
1260
1261 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001262 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001263 VD->getLexicalDeclContext()->isFunctionOrMethod())
1264 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001265
David L. Jonesbe1557a2016-12-21 00:17:49 +00001266 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001267 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001268 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001269 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1270 Eval->CheckedICE = true;
1271 Eval->IsICE = Val == 3;
1272 }
1273 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001274
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001275 enum VarKind {
1276 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1277 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001278 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001279 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001280 // Only true variables (not parameters or implicit parameters) can be
1281 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001282 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1283 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001284 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001285 break;
1286 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001287 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001288 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001289 break;
1290 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001291 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001292 TemplateSpecializationKind TSK =
1293 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001294 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001295 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001296 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001297 break;
1298 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001299 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001300
1301 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001302}
1303
Sebastian Redlb3298c32010-08-18 23:56:48 +00001304void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001305 VisitVarDecl(PD);
1306}
1307
Sebastian Redlb3298c32010-08-18 23:56:48 +00001308void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001309 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001310 unsigned isObjCMethodParam = Record.readInt();
1311 unsigned scopeDepth = Record.readInt();
1312 unsigned scopeIndex = Record.readInt();
1313 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001314 if (isObjCMethodParam) {
1315 assert(scopeDepth == 0);
1316 PD->setObjCMethodScopeInfo(scopeIndex);
1317 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1318 } else {
1319 PD->setScopeInfo(scopeDepth, scopeIndex);
1320 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001321 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1322 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1323 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001324 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001325
1326 // FIXME: If this is a redeclaration of a function from another module, handle
1327 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001328}
1329
Richard Smith7b76d812016-08-12 02:21:25 +00001330void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1331 VisitVarDecl(DD);
1332 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1333 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001334 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001335}
1336
1337void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1338 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001339 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001340}
1341
Sebastian Redlb3298c32010-08-18 23:56:48 +00001342void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001343 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001344 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001345 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001346}
1347
Sebastian Redlb3298c32010-08-18 23:56:48 +00001348void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001349 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001350 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001351 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001352 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001353 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001354 Params.reserve(NumParams);
1355 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001356 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001357 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001358
David L. Jonesbe1557a2016-12-21 00:17:49 +00001359 BD->setIsVariadic(Record.readInt());
1360 BD->setBlockMissingReturnType(Record.readInt());
1361 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001362
David L. Jonesbe1557a2016-12-21 00:17:49 +00001363 bool capturesCXXThis = Record.readInt();
1364 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001365 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001366 captures.reserve(numCaptures);
1367 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001368 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001369 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001370 bool byRef = (flags & 1);
1371 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001372 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001373
1374 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1375 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001376 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001377}
1378
Ben Langmuirce914fc2013-05-03 19:20:19 +00001379void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1380 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001381 unsigned ContextParamPos = Record.readInt();
1382 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001383 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001384 for (unsigned I = 0; I < CD->NumParams; ++I) {
1385 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001386 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001387 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001388 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001389 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001390}
1391
Sebastian Redlb3298c32010-08-18 23:56:48 +00001392void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001393 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001394 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001395 D->setExternLoc(ReadSourceLocation());
1396 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001397}
1398
Richard Smith8df390f2016-09-08 23:14:54 +00001399void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1400 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001401 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001402}
1403
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001404void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1405 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001406 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001407}
1408
Sebastian Redlb3298c32010-08-18 23:56:48 +00001409void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001410 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001411 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001412 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001413 D->LocStart = ReadSourceLocation();
1414 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001415
Richard Smith15e32fd2015-03-17 02:23:11 +00001416 // Defer loading the anonymous namespace until we've finished merging
1417 // this namespace; loading it might load a later declaration of the
1418 // same namespace, and we have an invariant that older declarations
1419 // get merged before newer ones try to merge.
1420 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001421 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001422 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001423 } else {
1424 // Link this namespace back to the first declaration, which has already
1425 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001426 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001427 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001428
1429 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001430
1431 if (AnonNamespace) {
1432 // Each module has its own anonymous namespace, which is disjoint from
1433 // any other module's anonymous namespaces, so don't attach the anonymous
1434 // namespace at all.
1435 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001436 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001437 D->setAnonymousNamespace(Anon);
1438 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001439}
1440
Sebastian Redlb3298c32010-08-18 23:56:48 +00001441void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001442 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001443 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001444 D->NamespaceLoc = ReadSourceLocation();
1445 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001446 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001447 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001448 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001449}
1450
Sebastian Redlb3298c32010-08-18 23:56:48 +00001451void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001452 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001453 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001454 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001455 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1456 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001457 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001458 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001459 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001460 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001461}
1462
Richard Smith151c4562016-12-20 21:35:28 +00001463void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1464 VisitNamedDecl(D);
1465 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1466 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1467 for (unsigned I = 0; I != D->NumExpansions; ++I)
1468 Expansions[I] = ReadDeclAs<NamedDecl>();
1469 mergeMergeable(D);
1470}
1471
Sebastian Redlb3298c32010-08-18 23:56:48 +00001472void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001473 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001474 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001475 D->setTargetDecl(ReadDeclAs<NamedDecl>());
1476 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1477 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001478 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001479 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001480 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001481}
1482
Richard Smith5179eb72016-06-28 19:03:57 +00001483void ASTDeclReader::VisitConstructorUsingShadowDecl(
1484 ConstructorUsingShadowDecl *D) {
1485 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001486 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1487 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001488 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001489}
1490
Sebastian Redlb3298c32010-08-18 23:56:48 +00001491void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001492 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001493 D->UsingLoc = ReadSourceLocation();
1494 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001495 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001496 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1497 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001498}
1499
Sebastian Redlb3298c32010-08-18 23:56:48 +00001500void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001501 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001502 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001503 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001504 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001505 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001506 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001507}
1508
Sebastian Redlb3298c32010-08-18 23:56:48 +00001509void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001510 UnresolvedUsingTypenameDecl *D) {
1511 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001512 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001513 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001514 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001515 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001516}
1517
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001518void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001519 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001520 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001521 Data.UserDeclaredConstructor = Record.readInt();
1522 Data.UserDeclaredSpecialMembers = Record.readInt();
1523 Data.Aggregate = Record.readInt();
1524 Data.PlainOldData = Record.readInt();
1525 Data.Empty = Record.readInt();
1526 Data.Polymorphic = Record.readInt();
1527 Data.Abstract = Record.readInt();
1528 Data.IsStandardLayout = Record.readInt();
1529 Data.HasNoNonEmptyBases = Record.readInt();
1530 Data.HasPrivateFields = Record.readInt();
1531 Data.HasProtectedFields = Record.readInt();
1532 Data.HasPublicFields = Record.readInt();
1533 Data.HasMutableFields = Record.readInt();
1534 Data.HasVariantMembers = Record.readInt();
1535 Data.HasOnlyCMembers = Record.readInt();
1536 Data.HasInClassInitializer = Record.readInt();
1537 Data.HasUninitializedReferenceMember = Record.readInt();
1538 Data.HasUninitializedFields = Record.readInt();
1539 Data.HasInheritedConstructor = Record.readInt();
1540 Data.HasInheritedAssignment = Record.readInt();
1541 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1542 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1543 Data.NeedOverloadResolutionForDestructor = Record.readInt();
1544 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1545 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1546 Data.DefaultedDestructorIsDeleted = Record.readInt();
1547 Data.HasTrivialSpecialMembers = Record.readInt();
1548 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
1549 Data.HasIrrelevantDestructor = Record.readInt();
1550 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1551 Data.HasDefaultedDefaultConstructor = Record.readInt();
1552 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1553 Data.HasConstexprDefaultConstructor = Record.readInt();
1554 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1555 Data.ComputedVisibleConversions = Record.readInt();
1556 Data.UserProvidedDefaultConstructor = Record.readInt();
1557 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001558 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1559 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001560 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1561 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1562 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001563 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001564 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001565
David Blaikie1ac9c982017-04-11 21:13:37 +00001566 if (Record.readInt()) {
1567 Reader.BodySource[D] = Loc.F->Kind == ModuleKind::MK_MainFile
1568 ? ExternalASTSource::EK_Never
1569 : ExternalASTSource::EK_Always;
1570 }
1571
David L. Jonesbe1557a2016-12-21 00:17:49 +00001572 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001573 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001574 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001575 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001576 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001577 Data.VBases = ReadGlobalOffset();
1578
David L. Jonesb6a8f022016-12-21 04:34:52 +00001579 Record.readUnresolvedSet(Data.Conversions);
1580 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001581 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001582 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001583
Douglas Gregor99ae8062012-02-14 17:54:36 +00001584 if (Data.IsLambda) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001585 typedef LambdaCapture Capture;
Douglas Gregor99ae8062012-02-14 17:54:36 +00001586 CXXRecordDecl::LambdaDefinitionData &Lambda
1587 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001588 Lambda.Dependent = Record.readInt();
1589 Lambda.IsGenericLambda = Record.readInt();
1590 Lambda.CaptureDefault = Record.readInt();
1591 Lambda.NumCaptures = Record.readInt();
1592 Lambda.NumExplicitCaptures = Record.readInt();
1593 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001594 Lambda.ContextDecl = ReadDeclID();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001595 Lambda.Captures
1596 = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures);
1597 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001598 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001599 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001600 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001601 bool IsImplicit = Record.readInt();
1602 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001603 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001604 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001605 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001606 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001607 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001608 break;
1609 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001610 case LCK_ByRef:
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001611 VarDecl *Var = ReadDeclAs<VarDecl>();
1612 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001613 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1614 break;
1615 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001616 }
1617 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001618}
1619
Richard Smithcd45dbc2014-04-19 03:48:30 +00001620void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001621 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001622 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001623 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001624 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001625
Richard Smith02793752015-03-27 21:16:39 +00001626 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001627 // Track that we merged the definitions.
1628 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1629 DD.Definition));
1630 Reader.PendingDefinitions.erase(MergeDD.Definition);
1631 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001632 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001633 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1634 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001635 }
1636
Richard Smith2a9e5c52015-02-03 03:32:14 +00001637 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1638 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1639 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001640 // We faked up this definition data because we found a class for which we'd
1641 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001642 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001643 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001644
1645 // Don't change which declaration is the definition; that is required
1646 // to be invariant once we select it.
1647 auto *Def = DD.Definition;
1648 DD = std::move(MergeDD);
1649 DD.Definition = Def;
1650 return;
1651 }
1652
Richard Smithcd45dbc2014-04-19 03:48:30 +00001653 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001654 bool DetectedOdrViolation = false;
1655#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1656#define MATCH_FIELD(Field) \
1657 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1658 OR_FIELD(Field)
1659 MATCH_FIELD(UserDeclaredConstructor)
1660 MATCH_FIELD(UserDeclaredSpecialMembers)
1661 MATCH_FIELD(Aggregate)
1662 MATCH_FIELD(PlainOldData)
1663 MATCH_FIELD(Empty)
1664 MATCH_FIELD(Polymorphic)
1665 MATCH_FIELD(Abstract)
1666 MATCH_FIELD(IsStandardLayout)
1667 MATCH_FIELD(HasNoNonEmptyBases)
1668 MATCH_FIELD(HasPrivateFields)
1669 MATCH_FIELD(HasProtectedFields)
1670 MATCH_FIELD(HasPublicFields)
1671 MATCH_FIELD(HasMutableFields)
1672 MATCH_FIELD(HasVariantMembers)
1673 MATCH_FIELD(HasOnlyCMembers)
1674 MATCH_FIELD(HasInClassInitializer)
1675 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001676 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001677 MATCH_FIELD(HasInheritedConstructor)
1678 MATCH_FIELD(HasInheritedAssignment)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001679 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1680 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1681 MATCH_FIELD(NeedOverloadResolutionForDestructor)
1682 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1683 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1684 MATCH_FIELD(DefaultedDestructorIsDeleted)
1685 OR_FIELD(HasTrivialSpecialMembers)
1686 OR_FIELD(DeclaredNonTrivialSpecialMembers)
1687 MATCH_FIELD(HasIrrelevantDestructor)
1688 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001689 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001690 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1691 OR_FIELD(HasConstexprDefaultConstructor)
1692 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1693 // ComputedVisibleConversions is handled below.
1694 MATCH_FIELD(UserProvidedDefaultConstructor)
1695 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001696 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1697 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001698 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1699 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1700 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1701 MATCH_FIELD(IsLambda)
1702#undef OR_FIELD
1703#undef MATCH_FIELD
1704
1705 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1706 DetectedOdrViolation = true;
1707 // FIXME: Issue a diagnostic if the base classes don't match when we come
1708 // to lazily load them.
1709
1710 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1711 // match when we come to lazily load them.
1712 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1713 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1714 DD.ComputedVisibleConversions = true;
1715 }
1716
1717 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1718 // lazily load it.
1719
1720 if (DD.IsLambda) {
1721 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1722 // when they occur within the body of a function template specialization).
1723 }
1724
Richard Trieufd1acbb2017-04-11 21:31:00 +00001725 if (D->getODRHash() != MergeDD.ODRHash) {
1726 DetectedOdrViolation = true;
1727 }
1728
Richard Smithcd45dbc2014-04-19 03:48:30 +00001729 if (DetectedOdrViolation)
1730 Reader.PendingOdrMergeFailures[DD.Definition].push_back(MergeDD.Definition);
1731}
1732
Richard Smith2a9e5c52015-02-03 03:32:14 +00001733void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001734 struct CXXRecordDecl::DefinitionData *DD;
1735 ASTContext &C = Reader.getContext();
1736
1737 // Determine whether this is a lambda closure type, so that we can
1738 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001739 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001740 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001741 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001742 LCD_None);
1743 else
1744 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1745
David Blaikie1ac9c982017-04-11 21:13:37 +00001746 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001747
Richard Smith5156c382015-01-22 01:41:56 +00001748 // We might already have a definition for this record. This can happen either
1749 // because we're reading an update record, or because we've already done some
1750 // merging. Either way, just merge into it.
Richard Smith8a639892015-01-24 01:07:20 +00001751 CXXRecordDecl *Canon = D->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00001752 if (Canon->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00001753 MergeDefinitionData(Canon, std::move(*DD));
1754 D->DefinitionData = Canon->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001755 return;
1756 }
1757
Richard Smith97100e32015-06-20 00:22:34 +00001758 // Mark this declaration as being a definition.
1759 D->IsCompleteDefinition = true;
1760 D->DefinitionData = DD;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001761
Richard Smith97100e32015-06-20 00:22:34 +00001762 // If this is not the first declaration or is an update record, we can have
1763 // other redeclarations already. Make a note that we need to propagate the
1764 // DefinitionData pointer onto them.
1765 if (Update || Canon != D) {
1766 Canon->DefinitionData = D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001767 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001768 }
1769}
1770
Richard Smith1d209d02013-05-23 01:49:11 +00001771ASTDeclReader::RedeclarableResult
1772ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1773 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001774
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001775 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001776
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001777 enum CXXRecKind {
1778 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1779 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001780 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001781 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001782 // Merged when we merge the folding set entry in the primary template.
1783 if (!isa<ClassTemplateSpecializationDecl>(D))
1784 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001785 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001786 case CXXRecTemplate: {
1787 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001788 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001789 D->TemplateOrInstantiation = Template;
1790 if (!Template->getTemplatedDecl()) {
1791 // We've not actually loaded the ClassTemplateDecl yet, because we're
1792 // currently being loaded as its pattern. Rely on it to set up our
1793 // TypeForDecl (see VisitClassTemplateDecl).
1794 //
1795 // Beware: we do not yet know our canonical declaration, and may still
1796 // get merged once the surrounding class template has got off the ground.
1797 TypeIDForTypeDecl = 0;
1798 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001799 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001800 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001801 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001802 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001803 TemplateSpecializationKind TSK =
1804 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001805 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001806 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1807 MSI->setPointOfInstantiation(POI);
1808 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001809 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001810 break;
1811 }
1812 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001813
David L. Jonesbe1557a2016-12-21 00:17:49 +00001814 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001815 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001816 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001817 else
1818 // Propagate DefinitionData pointer from the canonical declaration.
1819 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1820
Richard Smith676c4042013-08-29 23:59:27 +00001821 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001822 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001823 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001824 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001825 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001826 // FIXME: This is wrong for the ARM ABI, where some other module may have
1827 // made this function no longer be a key function. We need an update
1828 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001829 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001830 }
Richard Smith1d209d02013-05-23 01:49:11 +00001831
1832 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001833}
1834
Richard Smithbc491202017-02-17 20:05:37 +00001835void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1836 VisitFunctionDecl(D);
1837}
1838
Sebastian Redlb3298c32010-08-18 23:56:48 +00001839void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001840 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001841
David L. Jonesbe1557a2016-12-21 00:17:49 +00001842 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001843 if (D->isCanonicalDecl()) {
1844 while (NumOverridenMethods--) {
1845 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1846 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001847 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001848 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1849 }
1850 } else {
1851 // We don't care about which declarations this used to override; we get
1852 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001853 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001854 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001855}
1856
Sebastian Redlb3298c32010-08-18 23:56:48 +00001857void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001858 // We need the inherited constructor information to merge the declaration,
1859 // so we have to read it before we call VisitCXXMethodDecl.
1860 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001861 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1862 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001863 *D->getTrailingObjects<InheritedConstructor>() =
1864 InheritedConstructor(Shadow, Ctor);
1865 }
1866
Chris Lattnerca025db2010-05-07 21:43:38 +00001867 VisitCXXMethodDecl(D);
1868}
1869
Sebastian Redlb3298c32010-08-18 23:56:48 +00001870void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001871 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001872
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001873 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
Richard Smithf8134002015-03-10 01:41:22 +00001874 auto *Canon = cast<CXXDestructorDecl>(D->getCanonicalDecl());
1875 // FIXME: Check consistency if we have an old and new operator delete.
1876 if (!Canon->OperatorDelete)
1877 Canon->OperatorDelete = OperatorDelete;
1878 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001879}
1880
Sebastian Redlb3298c32010-08-18 23:56:48 +00001881void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001882 VisitCXXMethodDecl(D);
1883}
1884
Douglas Gregorba345522011-12-02 23:23:56 +00001885void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1886 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001887 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001888 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001889 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001890 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001891 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001892 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001893}
1894
Sebastian Redlb3298c32010-08-18 23:56:48 +00001895void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001896 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001897 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001898}
1899
Sebastian Redlb3298c32010-08-18 23:56:48 +00001900void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001901 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001902 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001903 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001904 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001905 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001906 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001907 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001908 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001909 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001910 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001911 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001912}
1913
Sebastian Redlb3298c32010-08-18 23:56:48 +00001914void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001915 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001916 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001917 D->NumParams = NumParams;
1918 D->Params = new TemplateParameterList*[NumParams];
1919 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001920 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001921 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001922 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001923 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001924 D->Friend = GetTypeSourceInfo();
1925 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001926}
1927
Richard Smithf17fdbd2014-04-24 02:25:27 +00001928DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001929 VisitNamedDecl(D);
1930
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001931 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001932 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001933 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00001934 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001935 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001936
Richard Smithf17fdbd2014-04-24 02:25:27 +00001937 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001938}
1939
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001940ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001941ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001942 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001943
Douglas Gregor68444de2012-01-14 15:13:49 +00001944 // Make sure we've allocated the Common pointer first. We do this before
1945 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1946 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1947 if (!CanonD->Common) {
1948 CanonD->Common = CanonD->newCommon(Reader.getContext());
1949 Reader.PendingDefinitions.insert(CanonD);
1950 }
1951 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00001952
Douglas Gregor68444de2012-01-14 15:13:49 +00001953 // If this is the first declaration of the template, fill in the information
1954 // for the 'common' pointer.
1955 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001956 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001957 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001958 assert(RTD->getKind() == D->getKind() &&
1959 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00001960 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001961 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001962 D->setMemberSpecialization();
1963 }
1964 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00001965
Richard Smithf17fdbd2014-04-24 02:25:27 +00001966 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001967 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00001968
Richard Smithf17fdbd2014-04-24 02:25:27 +00001969 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00001970
Richard Smithd46d6de2013-10-13 23:50:45 +00001971 // If we merged the template with a prior declaration chain, merge the common
1972 // pointer.
1973 // FIXME: Actually merge here, don't just overwrite.
1974 D->Common = D->getCanonicalDecl()->Common;
1975
Douglas Gregor68444de2012-01-14 15:13:49 +00001976 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001977}
1978
Sebastian Redlb3298c32010-08-18 23:56:48 +00001979void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001980 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001981
Douglas Gregor68444de2012-01-14 15:13:49 +00001982 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00001983 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1984 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00001985 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00001986 ReadDeclIDList(SpecIDs);
Vassil Vassilev497a9952017-06-09 21:54:18 +00001987 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001988 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00001989
1990 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
1991 // We were loaded before our templated declaration was. We've not set up
1992 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
1993 // it now.
1994 Reader.Context.getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00001995 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00001996 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001997}
1998
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001999void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2000 llvm_unreachable("BuiltinTemplates are not serialized");
2001}
2002
Larisse Voufo30616382013-08-23 22:21:36 +00002003/// TODO: Unify with ClassTemplateDecl version?
2004/// May require unifying ClassTemplateDecl and
2005/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002006void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2007 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2008
2009 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002010 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002011 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002012 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002013 ReadDeclIDList(SpecIDs);
Vassil Vassilev497a9952017-06-09 21:54:18 +00002014 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002015 }
2016}
2017
Richard Smith1d209d02013-05-23 01:49:11 +00002018ASTDeclReader::RedeclarableResult
2019ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2020 ClassTemplateSpecializationDecl *D) {
2021 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002022
Douglas Gregor4163aca2011-09-09 21:34:22 +00002023 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002024 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002025 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002026 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002027 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002028 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002029 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002030 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002031 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002032 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
2033 = new (C) ClassTemplateSpecializationDecl::
2034 SpecializedPartialSpecialization();
2035 PS->PartialSpecialization
2036 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2037 PS->TemplateArgs = ArgList;
2038 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002039 }
2040 }
2041
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002042 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002043 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002044 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002045 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002046 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002047
David L. Jonesbe1557a2016-12-21 00:17:49 +00002048 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002049 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002050 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002051 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002052 // Set this as, or find, the canonical declaration for this specialization
2053 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002054 if (ClassTemplatePartialSpecializationDecl *Partial =
2055 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002056 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002057 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002058 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002059 CanonSpec =
2060 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2061 }
2062 // If there was already a canonical specialization, merge into it.
2063 if (CanonSpec != D) {
2064 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2065
2066 // This declaration might be a definition. Merge with any existing
2067 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002068 if (auto *DDD = D->DefinitionData) {
2069 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002070 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002071 else
Richard Smith053f6c62014-05-16 23:01:30 +00002072 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002073 }
Richard Smith547864d2014-07-11 18:22:58 +00002074 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002075 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002076 }
2077 }
Richard Smith1d209d02013-05-23 01:49:11 +00002078
Richard Smithd55889a2013-09-09 16:55:27 +00002079 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002080 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002081 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2082 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2083 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002084 ExplicitInfo->ExternLoc = ReadSourceLocation();
2085 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002086 D->ExplicitInfo = ExplicitInfo;
2087 }
2088
Richard Smith1d209d02013-05-23 01:49:11 +00002089 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002090}
2091
Sebastian Redlb3298c32010-08-18 23:56:48 +00002092void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002093 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002094 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002095
David L. Jonesb6a8f022016-12-21 04:34:52 +00002096 D->TemplateParams = Record.readTemplateParameterList();
2097 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002098
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002099 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002100 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002101 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002102 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002103 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002104 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002105}
2106
Sebastian Redlb7448632011-08-31 13:59:56 +00002107void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2108 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002109 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002110 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002111}
2112
Sebastian Redlb3298c32010-08-18 23:56:48 +00002113void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002114 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002115
Douglas Gregor68444de2012-01-14 15:13:49 +00002116 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002117 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002118 SmallVector<serialization::DeclID, 32> SpecIDs;
2119 ReadDeclIDList(SpecIDs);
Vassil Vassilev497a9952017-06-09 21:54:18 +00002120 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002121 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002122}
2123
Larisse Voufo30616382013-08-23 22:21:36 +00002124/// TODO: Unify with ClassTemplateSpecializationDecl version?
2125/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2126/// VarTemplate(Partial)SpecializationDecl with a new data
2127/// structure Template(Partial)SpecializationDecl, and
2128/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002129ASTDeclReader::RedeclarableResult
2130ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2131 VarTemplateSpecializationDecl *D) {
2132 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2133
2134 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002135 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002136 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2137 D->SpecializedTemplate = VTD;
2138 } else {
2139 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002140 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002141 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002142 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002143 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2144 new (C)
2145 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2146 PS->PartialSpecialization =
2147 cast<VarTemplatePartialSpecializationDecl>(InstD);
2148 PS->TemplateArgs = ArgList;
2149 D->SpecializedTemplate = PS;
2150 }
2151 }
2152
2153 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002154 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002155 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2156 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2157 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002158 ExplicitInfo->ExternLoc = ReadSourceLocation();
2159 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002160 D->ExplicitInfo = ExplicitInfo;
2161 }
2162
2163 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002164 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002165 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002166 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002167 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002168
David L. Jonesbe1557a2016-12-21 00:17:49 +00002169 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002170 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002171 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002172 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002173 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002174 if (VarTemplatePartialSpecializationDecl *Partial =
2175 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002176 CanonPattern->getCommonPtr()->PartialSpecializations
2177 .GetOrInsertNode(Partial);
2178 } else {
2179 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2180 }
2181 }
2182 }
2183
2184 return Redecl;
2185}
2186
Larisse Voufo30616382013-08-23 22:21:36 +00002187/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2188/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2189/// VarTemplate(Partial)SpecializationDecl with a new data
2190/// structure Template(Partial)SpecializationDecl, and
2191/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002192void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2193 VarTemplatePartialSpecializationDecl *D) {
2194 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2195
David L. Jonesb6a8f022016-12-21 04:34:52 +00002196 D->TemplateParams = Record.readTemplateParameterList();
2197 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002198
2199 // These are read/set from/to the first declaration.
2200 if (ThisDeclID == Redecl.getFirstID()) {
2201 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002202 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002203 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002204 }
2205}
2206
Sebastian Redlb3298c32010-08-18 23:56:48 +00002207void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002208 VisitTypeDecl(D);
2209
David L. Jonesbe1557a2016-12-21 00:17:49 +00002210 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002211
David L. Jonesbe1557a2016-12-21 00:17:49 +00002212 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002213 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002214}
2215
Sebastian Redlb3298c32010-08-18 23:56:48 +00002216void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002217 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002218 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002219 D->setDepth(Record.readInt());
2220 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002221 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002222 auto TypesAndInfos =
2223 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002224 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002225 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002226 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002227 }
2228 } else {
2229 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002230 D->ParameterPack = Record.readInt();
2231 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002232 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002233 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002234}
2235
Sebastian Redlb3298c32010-08-18 23:56:48 +00002236void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002237 VisitTemplateDecl(D);
2238 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002239 D->setDepth(Record.readInt());
2240 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002241 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002242 TemplateParameterList **Data =
2243 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002244 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2245 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002246 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002247 } else {
2248 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002249 D->ParameterPack = Record.readInt();
2250 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002251 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002252 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002253 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002254}
2255
Richard Smith3f1b5d02011-05-05 21:57:07 +00002256void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2257 VisitRedeclarableTemplateDecl(D);
2258}
2259
Sebastian Redlb3298c32010-08-18 23:56:48 +00002260void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002261 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002262 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002263 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002264 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002265 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002266}
2267
Michael Han84324352013-02-22 17:15:32 +00002268void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2269 VisitDecl(D);
2270}
2271
Mike Stump11289f42009-09-09 15:08:12 +00002272std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002273ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002274 uint64_t LexicalOffset = ReadLocalOffset();
2275 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002276 return std::make_pair(LexicalOffset, VisibleOffset);
2277}
2278
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002279template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002280ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002281ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002282 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002283 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002284
Richard Smith5fc18a92015-07-12 23:43:21 +00002285 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002286 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002287
Richard Smithd61d4ac2015-08-22 20:13:39 +00002288 uint64_t RedeclOffset = 0;
2289
Douglas Gregor358cd442012-01-15 16:58:34 +00002290 // 0 indicates that this declaration was the only declaration of its entity,
2291 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002292 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002293 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002294 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002295 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002296 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002297 // This declaration was the first local declaration, but may have imported
2298 // other declarations.
2299 IsKeyDecl = N == 1;
2300 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002301
Richard Smithfe620d22015-03-05 23:24:12 +00002302 // We have some declarations that must be before us in our redeclaration
2303 // chain. Read them now, and remember that we ought to merge with one of
2304 // them.
2305 // FIXME: Provide a known merge target to the second and subsequent such
2306 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002307 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002308 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002309
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002310 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002311 } else {
2312 // This declaration was not the first local declaration. Read the first
2313 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002314 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002315 }
2316
Douglas Gregor358cd442012-01-15 16:58:34 +00002317 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2318 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002319 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2320 // We temporarily set the first (canonical) declaration as the previous one
2321 // which is the one that matters and mark the real previous DeclID to be
2322 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002323 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002324 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002325 }
Richard Smithd8a83712015-08-22 01:47:18 +00002326
Richard Smithd8a83712015-08-22 01:47:18 +00002327 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002328
2329 // Note that we need to load local redeclarations of this decl and build a
2330 // decl chain for them. This must happen *after* we perform the preloading
2331 // above; this ensures that the redeclaration chain is built in the correct
2332 // order.
2333 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002334 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002335
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002336 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002337}
2338
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002339/// \brief Attempts to merge the given declaration (D) with another declaration
2340/// of the same entity.
2341template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002342void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002343 RedeclarableResult &Redecl,
2344 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002345 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002346 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002347 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002348
Richard Smith202850a2015-03-10 02:57:50 +00002349 // If we're not the canonical declaration, we don't need to merge.
2350 if (!DBase->isFirstDecl())
2351 return;
2352
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002353 T *D = static_cast<T*>(DBase);
2354
Richard Smith5a4737c2015-02-06 02:42:59 +00002355 if (auto *Existing = Redecl.getKnownMergeTarget())
2356 // We already know of an existing declaration we should merge with.
2357 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2358 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002359 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002360 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2361}
2362
2363/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2364/// We use this to put code in a template that will only be valid for certain
2365/// instantiations.
2366template<typename T> static T assert_cast(T t) { return t; }
2367template<typename T> static T assert_cast(...) {
2368 llvm_unreachable("bad assert_cast");
2369}
2370
2371/// \brief Merge together the pattern declarations from two template
2372/// declarations.
2373void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2374 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002375 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002376 auto *DPattern = D->getTemplatedDecl();
2377 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002378 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2379 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002380 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002381
Richard Smith547864d2014-07-11 18:22:58 +00002382 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2383 // Merge with any existing definition.
2384 // FIXME: This is duplicated in several places. Refactor.
2385 auto *ExistingClass =
2386 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002387 if (auto *DDD = DClass->DefinitionData) {
2388 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002389 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002390 } else {
2391 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002392 // We may have skipped this before because we thought that DClass
2393 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002394 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002395 }
2396 }
2397 DClass->DefinitionData = ExistingClass->DefinitionData;
2398
Richard Smithf17fdbd2014-04-24 02:25:27 +00002399 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2400 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002401 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002402 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2403 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2404 Result);
2405 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2406 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002407 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2408 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2409 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002410 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002411}
2412
2413/// \brief Attempts to merge the given declaration (D) with another declaration
2414/// of the same entity.
2415template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002416void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2417 RedeclarableResult &Redecl,
2418 DeclID TemplatePatternID) {
2419 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002420 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002421 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002422 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002423 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2424 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002425
Richard Smithd55889a2013-09-09 16:55:27 +00002426 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002427 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002428 // appropriate canonical declaration.
2429 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002430 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002431 ExistingCanon->Used |= D->Used;
2432 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002433
2434 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002435 // We cannot have loaded any redeclarations of this declaration yet, so
2436 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002437 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002438 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002439 assert_cast<NamespaceDecl*>(ExistingCanon));
2440
2441 // When we merge a template, merge its pattern.
2442 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2443 mergeTemplatePattern(
2444 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002445 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002446
Richard Smith5fc18a92015-07-12 23:43:21 +00002447 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002448 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002449 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002450 }
2451}
2452
Richard Smith0b87e072013-10-07 08:02:11 +00002453/// \brief Attempts to merge the given declaration (D) with another declaration
2454/// of the same entity, for the case where the entity is not actually
2455/// redeclarable. This happens, for instance, when merging the fields of
2456/// identical class definitions from two different modules.
2457template<typename T>
2458void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2459 // If modules are not available, there is no reason to perform this merge.
2460 if (!Reader.getContext().getLangOpts().Modules)
2461 return;
2462
Richard Smith01a73372013-10-15 22:02:41 +00002463 // ODR-based merging is only performed in C++. In C, identically-named things
2464 // in different translation units are not redeclarations (but may still have
2465 // compatible types).
2466 if (!Reader.getContext().getLangOpts().CPlusPlus)
2467 return;
2468
Richard Smith0b87e072013-10-07 08:02:11 +00002469 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2470 if (T *Existing = ExistingRes)
2471 Reader.Context.setPrimaryMergedDecl(static_cast<T*>(D),
2472 Existing->getCanonicalDecl());
2473}
2474
Alexey Bataeva769e072013-03-22 06:34:35 +00002475void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2476 VisitDecl(D);
2477 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002478 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002479 Vars.reserve(NumVars);
2480 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002481 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002482 }
2483 D->setVars(Vars);
2484}
2485
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002486void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002487 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002488 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002489 D->setCombiner(Record.readExpr());
2490 D->setInitializer(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002491 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002492}
2493
Alexey Bataev4244be22016-02-11 05:35:55 +00002494void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002495 VisitVarDecl(D);
2496}
2497
Chris Lattner487412d2009-04-27 05:27:42 +00002498//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002499// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002500//===----------------------------------------------------------------------===//
2501
Chris Lattner8f63ab52009-04-27 06:01:06 +00002502/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002503void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2504 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002505 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002506 attr::Kind Kind = (attr::Kind)Record.readInt();
2507 SourceRange Range = Record.readSourceRange();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002508
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002509#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002510
2511 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002512 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002513 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002514}
2515
2516//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002517// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002518//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002519
2520/// \brief Note that we have loaded the declaration with the given
2521/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002522///
Chris Lattner487412d2009-04-27 05:27:42 +00002523/// This routine notes that this declaration has already been loaded,
2524/// so that future GetDecl calls will return this declaration rather
2525/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002526inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002527 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2528 DeclsLoaded[Index] = D;
2529}
2530
2531
2532/// \brief Determine whether the consumer will be interested in seeing
2533/// this declaration (via HandleTopLevelDecl).
2534///
2535/// This routine should return true for anything that might affect
2536/// code generation, e.g., inline function definitions, Objective-C
2537/// declarations with metadata, etc.
Richard Smithdc1f0422016-07-20 19:10:16 +00002538static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002539 // An ObjCMethodDecl is never considered as "interesting" because its
2540 // implementation container always is.
2541
Richard Smithdc1f0422016-07-20 19:10:16 +00002542 // An ImportDecl or VarDecl imported from a module will get emitted when
2543 // we import the relevant module.
Bruno Cardoso Lopes84df6e99b2017-03-01 19:18:42 +00002544 if ((isa<ImportDecl>(D) || isa<VarDecl>(D)) && D->getImportedOwningModule() &&
2545 Ctx.DeclMustBeEmitted(D))
Richard Smithdc1f0422016-07-20 19:10:16 +00002546 return false;
2547
Douglas Gregor87d81242011-09-10 00:22:34 +00002548 if (isa<FileScopeAsmDecl>(D) ||
2549 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002550 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002551 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002552 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002553 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002554 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002555 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2556 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002557 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002558 return Var->isFileVarDecl() &&
2559 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002560 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Douglas Gregora6017bb2012-10-09 17:21:28 +00002561 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002562
2563 if (auto *ES = D->getASTContext().getExternalSource())
2564 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2565 return true;
2566
Douglas Gregor87d81242011-09-10 00:22:34 +00002567 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002568}
2569
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002570/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002571ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002572ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002573 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2574 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002575 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002576 const DeclOffset &DOffs =
2577 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002578 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002579 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002580}
2581
Douglas Gregord32f0352011-07-22 06:10:01 +00002582ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002583 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002584 = GlobalBitOffsetsMap.find(GlobalOffset);
2585
2586 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2587 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2588}
2589
Douglas Gregorde3ef502011-11-30 23:21:26 +00002590uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002591 return LocalOffset + M.GlobalBitOffset;
2592}
2593
Richard Smithbf78e642013-06-24 22:51:00 +00002594static bool isSameTemplateParameterList(const TemplateParameterList *X,
2595 const TemplateParameterList *Y);
2596
2597/// \brief Determine whether two template parameters are similar enough
2598/// that they may be used in declarations of the same template.
2599static bool isSameTemplateParameter(const NamedDecl *X,
2600 const NamedDecl *Y) {
2601 if (X->getKind() != Y->getKind())
2602 return false;
2603
2604 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2605 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2606 return TX->isParameterPack() == TY->isParameterPack();
2607 }
2608
2609 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2610 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2611 return TX->isParameterPack() == TY->isParameterPack() &&
2612 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2613 }
2614
2615 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2616 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2617 return TX->isParameterPack() == TY->isParameterPack() &&
2618 isSameTemplateParameterList(TX->getTemplateParameters(),
2619 TY->getTemplateParameters());
2620}
2621
Richard Smith32952e12014-10-14 02:00:47 +00002622static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2623 if (auto *NS = X->getAsNamespace())
2624 return NS;
2625 if (auto *NAS = X->getAsNamespaceAlias())
2626 return NAS->getNamespace();
2627 return nullptr;
2628}
2629
2630static bool isSameQualifier(const NestedNameSpecifier *X,
2631 const NestedNameSpecifier *Y) {
2632 if (auto *NSX = getNamespace(X)) {
2633 auto *NSY = getNamespace(Y);
2634 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2635 return false;
2636 } else if (X->getKind() != Y->getKind())
2637 return false;
2638
2639 // FIXME: For namespaces and types, we're permitted to check that the entity
2640 // is named via the same tokens. We should probably do so.
2641 switch (X->getKind()) {
2642 case NestedNameSpecifier::Identifier:
2643 if (X->getAsIdentifier() != Y->getAsIdentifier())
2644 return false;
2645 break;
2646 case NestedNameSpecifier::Namespace:
2647 case NestedNameSpecifier::NamespaceAlias:
2648 // We've already checked that we named the same namespace.
2649 break;
2650 case NestedNameSpecifier::TypeSpec:
2651 case NestedNameSpecifier::TypeSpecWithTemplate:
2652 if (X->getAsType()->getCanonicalTypeInternal() !=
2653 Y->getAsType()->getCanonicalTypeInternal())
2654 return false;
2655 break;
2656 case NestedNameSpecifier::Global:
2657 case NestedNameSpecifier::Super:
2658 return true;
2659 }
2660
2661 // Recurse into earlier portion of NNS, if any.
2662 auto *PX = X->getPrefix();
2663 auto *PY = Y->getPrefix();
2664 if (PX && PY)
2665 return isSameQualifier(PX, PY);
2666 return !PX && !PY;
2667}
2668
Richard Smithbf78e642013-06-24 22:51:00 +00002669/// \brief Determine whether two template parameter lists are similar enough
2670/// that they may be used in declarations of the same template.
2671static bool isSameTemplateParameterList(const TemplateParameterList *X,
2672 const TemplateParameterList *Y) {
2673 if (X->size() != Y->size())
2674 return false;
2675
2676 for (unsigned I = 0, N = X->size(); I != N; ++I)
2677 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2678 return false;
2679
2680 return true;
2681}
2682
George Burgess IV95845082017-02-15 22:43:27 +00002683/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002684/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002685static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2686 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002687 // Note that pass_object_size attributes are represented in the function's
2688 // ExtParameterInfo, so we don't need to check them here.
2689
George Burgess IV95845082017-02-15 22:43:27 +00002690 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2691 // Since this is an equality check, we can ignore that enable_if attrs show up
2692 // in reverse order.
2693 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2694 AEnableIfs.push_back(EIA);
2695
2696 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2697 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2698 BEnableIfs.push_back(EIA);
2699
2700 // Two very common cases: either we have 0 enable_if attrs, or we have an
2701 // unequal number of enable_if attrs.
2702 if (AEnableIfs.empty() && BEnableIfs.empty())
2703 return true;
2704
2705 if (AEnableIfs.size() != BEnableIfs.size())
2706 return false;
2707
2708 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
2709 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
2710 Cand1ID.clear();
2711 Cand2ID.clear();
2712
2713 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2714 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2715 if (Cand1ID != Cand2ID)
2716 return false;
2717 }
2718
George Burgess IV95845082017-02-15 22:43:27 +00002719 return true;
2720}
2721
Douglas Gregor022857e2011-12-22 01:48:48 +00002722/// \brief Determine whether the two declarations refer to the same entity.
2723static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2724 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002725
Douglas Gregor022857e2011-12-22 01:48:48 +00002726 if (X == Y)
2727 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002728
Douglas Gregor022857e2011-12-22 01:48:48 +00002729 // Must be in the same context.
2730 if (!X->getDeclContext()->getRedeclContext()->Equals(
2731 Y->getDeclContext()->getRedeclContext()))
2732 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002733
2734 // Two typedefs refer to the same entity if they have the same underlying
2735 // type.
2736 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2737 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2738 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2739 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002740
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002741 // Must have the same kind.
2742 if (X->getKind() != Y->getKind())
2743 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002744
Douglas Gregorda389302012-01-01 21:47:52 +00002745 // Objective-C classes and protocols with the same name always match.
2746 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002747 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002748
2749 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002750 // No need to handle these here: we merge them when adding them to the
2751 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002752 return false;
2753 }
Richard Smithd55889a2013-09-09 16:55:27 +00002754
Douglas Gregor2009cee2012-01-03 22:46:00 +00002755 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002756 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2757 TagDecl *TagY = cast<TagDecl>(Y);
2758 return (TagX->getTagKind() == TagY->getTagKind()) ||
2759 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2760 TagX->getTagKind() == TTK_Interface) &&
2761 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2762 TagY->getTagKind() == TTK_Interface));
2763 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002764
Joao Matose9a3ed42012-08-31 22:18:20 +00002765 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002766 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2767 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002768 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2769 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002770 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2771 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2772 if (CtorX->getInheritedConstructor() &&
2773 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2774 CtorY->getInheritedConstructor().getConstructor()))
2775 return false;
2776 }
Richard Smitha54d3242017-03-08 23:00:26 +00002777 ASTContext &C = FuncX->getASTContext();
2778 if (!C.hasSameType(FuncX->getType(), FuncY->getType())) {
2779 // We can get functions with different types on the redecl chain in C++17
2780 // if they have differing exception specifications and at least one of
2781 // the excpetion specs is unresolved.
2782 // FIXME: Do we need to check for C++14 deduced return types here too?
2783 auto *XFPT = FuncX->getType()->getAs<FunctionProtoType>();
2784 auto *YFPT = FuncY->getType()->getAs<FunctionProtoType>();
2785 if (C.getLangOpts().CPlusPlus1z && XFPT && YFPT &&
2786 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2787 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
2788 C.hasSameFunctionTypeIgnoringExceptionSpec(FuncX->getType(),
2789 FuncY->getType()))
2790 return true;
2791 return false;
2792 }
George Burgess IV95845082017-02-15 22:43:27 +00002793 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002794 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002795 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002796
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002797 // Variables with the same type and linkage match.
2798 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2799 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002800 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2801 ASTContext &C = VarX->getASTContext();
2802 if (C.hasSameType(VarX->getType(), VarY->getType()))
2803 return true;
2804
2805 // We can get decls with different types on the redecl chain. Eg.
2806 // template <typename T> struct S { static T Var[]; }; // #1
2807 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2808 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002809 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002810 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2811 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2812 if (!VarXTy || !VarYTy)
2813 return false;
2814 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2815 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2816 }
2817 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002818 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002819
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002820 // Namespaces with the same name and inlinedness match.
2821 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2822 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2823 return NamespaceX->isInline() == NamespaceY->isInline();
2824 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002825
Richard Smith8f8f05c2013-06-24 04:45:28 +00002826 // Identical template names and kinds match if their template parameter lists
2827 // and patterns match.
2828 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002829 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002830 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002831 TemplateY->getTemplatedDecl()) &&
2832 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2833 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002834 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002835
Richard Smith0b87e072013-10-07 08:02:11 +00002836 // Fields with the same name and the same type match.
2837 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2838 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002839 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002840 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2841 }
2842
Richard Smith8cbd8952015-08-04 02:05:09 +00002843 // Indirect fields with the same target field match.
2844 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2845 auto *IFDY = cast<IndirectFieldDecl>(Y);
2846 return IFDX->getAnonField()->getCanonicalDecl() ==
2847 IFDY->getAnonField()->getCanonicalDecl();
2848 }
2849
Richard Smith01a73372013-10-15 22:02:41 +00002850 // Enumerators with the same name match.
2851 if (isa<EnumConstantDecl>(X))
2852 // FIXME: Also check the value is odr-equivalent.
2853 return true;
2854
Richard Smithfd8634a2013-10-23 02:17:46 +00002855 // Using shadow declarations with the same target match.
2856 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2857 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2858 return USX->getTargetDecl() == USY->getTargetDecl();
2859 }
2860
Richard Smith32952e12014-10-14 02:00:47 +00002861 // Using declarations with the same qualifier match. (We already know that
2862 // the name matches.)
2863 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2864 auto *UY = cast<UsingDecl>(Y);
2865 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2866 UX->hasTypename() == UY->hasTypename() &&
2867 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2868 }
2869 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2870 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2871 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2872 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2873 }
2874 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2875 return isSameQualifier(
2876 UX->getQualifier(),
2877 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2878
Richard Smithf4634362014-09-03 23:11:22 +00002879 // Namespace alias definitions with the same target match.
2880 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2881 auto *NAY = cast<NamespaceAliasDecl>(Y);
2882 return NAX->getNamespace()->Equals(NAY->getNamespace());
2883 }
2884
Douglas Gregor022857e2011-12-22 01:48:48 +00002885 return false;
2886}
2887
Richard Smithd55889a2013-09-09 16:55:27 +00002888/// Find the context in which we should search for previous declarations when
2889/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002890DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2891 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002892 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2893 return ND->getOriginalNamespace();
2894
Richard Smith8a639892015-01-24 01:07:20 +00002895 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2896 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002897 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002898 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002899 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002900
2901 // If there's no definition yet, then DC's definition is added by an update
2902 // record, but we've not yet loaded that update record. In this case, we
2903 // commit to DC being the canonical definition now, and will fix this when
2904 // we load the update record.
2905 if (!DD) {
2906 DD = new (Reader.Context) struct CXXRecordDecl::DefinitionData(RD);
2907 RD->IsCompleteDefinition = true;
2908 RD->DefinitionData = DD;
2909 RD->getCanonicalDecl()->DefinitionData = DD;
2910
2911 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002912 Reader.PendingFakeDefinitionData.insert(
2913 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002914 }
2915
2916 return DD->Definition;
2917 }
Richard Smithd55889a2013-09-09 16:55:27 +00002918
Richard Smith01a73372013-10-15 22:02:41 +00002919 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002920 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2921 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002922
Richard Smith4eca9b92015-02-04 23:37:59 +00002923 // We can see the TU here only if we have no Sema object. In that case,
2924 // there's no TU scope to look in, so using the DC alone is sufficient.
2925 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2926 return TU;
2927
Craig Toppera13603a2014-05-22 05:54:18 +00002928 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00002929}
2930
Douglas Gregor022857e2011-12-22 01:48:48 +00002931ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00002932 // Record that we had a typedef name for linkage whether or not we merge
2933 // with that declaration.
2934 if (TypedefNameForLinkage) {
2935 DeclContext *DC = New->getDeclContext()->getRedeclContext();
2936 Reader.ImportedTypedefNamesForLinkage.insert(
2937 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
2938 return;
2939 }
2940
Douglas Gregor9b47f942012-01-09 17:38:47 +00002941 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00002942 return;
Richard Smith95d99302013-07-13 02:00:19 +00002943
Richard Smith70d58502014-08-30 00:04:23 +00002944 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00002945 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00002946 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00002947 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
2948 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00002949 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00002950 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00002951 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00002952 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
2953 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00002954 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00002955 // Add the declaration to its redeclaration context so later merging
2956 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00002957 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00002958 }
2959}
2960
Richard Smith88ebade2014-08-23 01:45:27 +00002961/// Find the declaration that should be merged into, given the declaration found
2962/// by name lookup. If we're merging an anonymous declaration within a typedef,
2963/// we need a matching typedef, and we merge with the type inside it.
2964static NamedDecl *getDeclForMerging(NamedDecl *Found,
2965 bool IsTypedefNameForLinkage) {
2966 if (!IsTypedefNameForLinkage)
2967 return Found;
2968
2969 // If we found a typedef declaration that gives a name to some other
2970 // declaration, then we want that inner declaration. Declarations from
2971 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00002972 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00002973 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00002974
2975 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00002976 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00002977
Hans Wennborgdcfba332015-10-06 23:40:43 +00002978 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00002979}
2980
Richard Smithd08aeb62014-08-28 01:33:39 +00002981NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
2982 DeclContext *DC,
2983 unsigned Index) {
2984 // If the lexical context has been merged, look into the now-canonical
2985 // definition.
2986 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
2987 DC = Merged;
2988
2989 // If we've seen this before, return the canonical declaration.
2990 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
2991 if (Index < Previous.size() && Previous[Index])
2992 return Previous[Index];
2993
2994 // If this is the first time, but we have parsed a declaration of the context,
2995 // build the anonymous declaration list from the parsed declaration.
2996 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00002997 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
2998 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00002999 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3000 else
Richard Smith2b560572015-02-07 03:11:11 +00003001 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3002 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003003 }
3004
3005 return Index < Previous.size() ? Previous[Index] : nullptr;
3006}
3007
3008void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3009 DeclContext *DC, unsigned Index,
3010 NamedDecl *D) {
3011 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3012 DC = Merged;
3013
3014 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3015 if (Index >= Previous.size())
3016 Previous.resize(Index + 1);
3017 if (!Previous[Index])
3018 Previous[Index] = D;
3019}
3020
Douglas Gregor022857e2011-12-22 01:48:48 +00003021ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003022 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3023 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003024
Richard Smithd08aeb62014-08-28 01:33:39 +00003025 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3026 // Don't bother trying to find unnamed declarations that are in
3027 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003028 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3029 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003030 Result.suppress();
3031 return Result;
3032 }
Richard Smith95d99302013-07-13 02:00:19 +00003033
Douglas Gregor022857e2011-12-22 01:48:48 +00003034 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003035 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003036 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003037 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003038 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3039 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003040 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3041 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003042 // Go on to check in other places in case an existing typedef name
3043 // was not imported.
3044 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003045
Richard Smith2b560572015-02-07 03:11:11 +00003046 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003047 // This is an anonymous declaration that we may need to merge. Look it up
3048 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003049 if (auto *Existing = getAnonymousDeclForMerging(
3050 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3051 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003052 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3053 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003054 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003055 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003056 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003057
3058 // Temporarily consider the identifier to be up-to-date. We don't want to
3059 // cause additional lookups here.
3060 class UpToDateIdentifierRAII {
3061 IdentifierInfo *II;
3062 bool WasOutToDate;
3063
3064 public:
3065 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
3066 : II(II), WasOutToDate(false)
3067 {
3068 if (II) {
3069 WasOutToDate = II->isOutOfDate();
3070 if (WasOutToDate)
3071 II->setOutOfDate(false);
3072 }
3073 }
3074
3075 ~UpToDateIdentifierRAII() {
3076 if (WasOutToDate)
3077 II->setOutOfDate(true);
3078 }
3079 } UpToDate(Name.getAsIdentifierInfo());
3080
Douglas Gregor2009cee2012-01-03 22:46:00 +00003081 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003082 IEnd = IdResolver.end();
3083 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003084 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003085 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003086 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3087 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003088 }
Richard Smith8a639892015-01-24 01:07:20 +00003089 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003090 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003091 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003092 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003093 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003094 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3095 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003096 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003097 } else {
3098 // Not in a mergeable context.
3099 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003100 }
Richard Smithd55889a2013-09-09 16:55:27 +00003101
Richard Smith2b9e3e32013-10-18 06:05:18 +00003102 // If this declaration is from a merged context, make a note that we need to
3103 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003104 //
3105 // FIXME: We should do something similar if we merge two definitions of the
3106 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003107 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3108 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3109 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003110 Reader.PendingOdrMergeChecks.push_back(D);
3111
Richard Smithd08aeb62014-08-28 01:33:39 +00003112 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003113 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003114}
3115
Richard Smithb321ecb2014-05-13 01:15:00 +00003116template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003117Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3118 return D->RedeclLink.getLatestNotUpdated();
3119}
3120Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3121 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3122}
3123
3124Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3125 assert(D);
3126
3127 switch (D->getKind()) {
3128#define ABSTRACT_DECL(TYPE)
3129#define DECL(TYPE, BASE) \
3130 case Decl::TYPE: \
3131 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3132#include "clang/AST/DeclNodes.inc"
3133 }
3134 llvm_unreachable("unknown decl kind");
3135}
3136
Richard Smith8ce51082015-03-11 01:44:51 +00003137Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3138 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3139}
3140
Richard Smithc3a53252015-02-28 05:57:02 +00003141template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003142void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3143 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003144 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003145 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003146 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003147}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003148
Richard Smith24d166c2014-07-31 23:52:38 +00003149namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003150template<>
3151void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003152 Redeclarable<VarDecl> *D,
3153 Decl *Previous, Decl *Canon) {
3154 VarDecl *VD = static_cast<VarDecl*>(D);
3155 VarDecl *PrevVD = cast<VarDecl>(Previous);
3156 D->RedeclLink.setPrevious(PrevVD);
3157 D->First = PrevVD->First;
3158
3159 // We should keep at most one definition on the chain.
3160 // FIXME: Cache the definition once we've found it. Building a chain with
3161 // N definitions currently takes O(N^2) time here.
3162 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3163 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3164 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3165 Reader.mergeDefinitionVisibility(CurD, VD);
3166 VD->demoteThisDefinitionToDeclaration();
3167 break;
3168 }
3169 }
3170 }
3171}
3172
3173template<>
3174void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003175 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003176 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003177 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3178 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3179
3180 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003181 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003182
3183 // If the previous declaration is an inline function declaration, then this
3184 // declaration is too.
3185 if (PrevFD->IsInline != FD->IsInline) {
3186 // FIXME: [dcl.fct.spec]p4:
3187 // If a function with external linkage is declared inline in one
3188 // translation unit, it shall be declared inline in all translation
3189 // units in which it appears.
3190 //
3191 // Be careful of this case:
3192 //
3193 // module A:
3194 // template<typename T> struct X { void f(); };
3195 // template<typename T> inline void X<T>::f() {}
3196 //
3197 // module B instantiates the declaration of X<int>::f
3198 // module C instantiates the definition of X<int>::f
3199 //
3200 // If module B and C are merged, we do not have a violation of this rule.
3201 FD->IsInline = true;
3202 }
3203
Richard Smith9e2341d2015-03-23 03:25:59 +00003204 // If we need to propagate an exception specification along the redecl
3205 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003206 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3207 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003208 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003209 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3210 bool WasUnresolved =
3211 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3212 if (IsUnresolved != WasUnresolved)
3213 Reader.PendingExceptionSpecUpdates.insert(
3214 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003215 }
3216}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003217} // end namespace clang
3218
Richard Smith6de7a242014-07-31 23:46:44 +00003219void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003220 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3221}
3222
Richard Smith8346e522015-06-10 01:47:58 +00003223/// Inherit the default template argument from \p From to \p To. Returns
3224/// \c false if there is no default template for \p From.
3225template <typename ParmDecl>
3226static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3227 Decl *ToD) {
3228 auto *To = cast<ParmDecl>(ToD);
3229 if (!From->hasDefaultArgument())
3230 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003231 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003232 return true;
3233}
3234
3235static void inheritDefaultTemplateArguments(ASTContext &Context,
3236 TemplateDecl *From,
3237 TemplateDecl *To) {
3238 auto *FromTP = From->getTemplateParameters();
3239 auto *ToTP = To->getTemplateParameters();
3240 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3241
3242 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3243 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003244 if (FromParam->isParameterPack())
3245 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003246 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3247
3248 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003249 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003250 break;
3251 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003252 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003253 break;
3254 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003255 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003256 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3257 break;
3258 }
3259 }
3260}
3261
Richard Smith6de7a242014-07-31 23:46:44 +00003262void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003263 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003264 assert(D && Previous);
3265
3266 switch (D->getKind()) {
3267#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003268#define DECL(TYPE, BASE) \
3269 case Decl::TYPE: \
3270 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003271 break;
3272#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003273 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003274
3275 // If the declaration was visible in one module, a redeclaration of it in
3276 // another module remains visible even if it wouldn't be visible by itself.
3277 //
3278 // FIXME: In this case, the declaration should only be visible if a module
3279 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003280 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003281 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003282 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003283
Richard Smith8346e522015-06-10 01:47:58 +00003284 // If the declaration declares a template, it may inherit default arguments
3285 // from the previous declaration.
3286 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3287 inheritDefaultTemplateArguments(Reader.getContext(),
3288 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003289}
3290
Richard Smithb321ecb2014-05-13 01:15:00 +00003291template<typename DeclT>
3292void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003293 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003294}
3295void ASTDeclReader::attachLatestDeclImpl(...) {
3296 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3297}
3298
Douglas Gregor05f10352011-12-17 23:38:30 +00003299void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3300 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003301
3302 switch (D->getKind()) {
3303#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003304#define DECL(TYPE, BASE) \
3305 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003306 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3307 break;
3308#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003309 }
3310}
3311
Richard Smith851072e2014-05-19 20:59:20 +00003312template<typename DeclT>
3313void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3314 D->RedeclLink.markIncomplete();
3315}
3316void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3317 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3318}
3319
3320void ASTReader::markIncompleteDeclChain(Decl *D) {
3321 switch (D->getKind()) {
3322#define ABSTRACT_DECL(TYPE)
3323#define DECL(TYPE, BASE) \
3324 case Decl::TYPE: \
3325 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3326 break;
3327#include "clang/AST/DeclNodes.inc"
3328 }
3329}
3330
Sebastian Redlb3298c32010-08-18 23:56:48 +00003331/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003332Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003333 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003334 SourceLocation DeclLoc;
3335 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003336 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003337 // Keep track of where we are in the stream, then jump back there
3338 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003339 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003340
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003341 ReadingKindTracker ReadingKind(Read_Decl, *this);
3342
Douglas Gregor1342e842009-07-06 18:54:52 +00003343 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003344 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003345
Sebastian Redl2c373b92010-10-05 15:59:54 +00003346 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003347 ASTRecordReader Record(*this, *Loc.F);
3348 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003349 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003350
Craig Toppera13603a2014-05-22 05:54:18 +00003351 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003352 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003353 case DECL_CONTEXT_LEXICAL:
3354 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003355 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003356 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003357 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003358 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003359 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003360 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003361 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003362 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003363 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003364 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003365 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003366 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003367 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003368 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003369 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003370 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003371 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003372 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003373 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003374 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003375 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003376 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003377 case DECL_EXPORT:
3378 D = ExportDecl::CreateDeserialized(Context, ID);
3379 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003380 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003381 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003382 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003383 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003384 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003385 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003386 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003387 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003388 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003389 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003390 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003391 break;
Richard Smith151c4562016-12-20 21:35:28 +00003392 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003393 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003394 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003395 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003396 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003397 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003398 case DECL_CONSTRUCTOR_USING_SHADOW:
3399 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3400 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003401 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003402 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003403 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003404 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003405 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003406 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003407 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003408 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003409 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003410 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003411 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003412 break;
Richard Smithbc491202017-02-17 20:05:37 +00003413 case DECL_CXX_DEDUCTION_GUIDE:
3414 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3415 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003416 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003417 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003418 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003419 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003420 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3421 break;
3422 case DECL_CXX_INHERITED_CONSTRUCTOR:
3423 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003424 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003425 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003426 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003427 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003428 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003429 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003430 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003431 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003432 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003433 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003434 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003435 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003436 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003437 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003438 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003439 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003440 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003441 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003442 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003443 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003444 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003445 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003446 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003447 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003448 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003449 case DECL_VAR_TEMPLATE:
3450 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3451 break;
3452 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3453 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3454 break;
3455 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3456 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3457 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003458 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003459 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003460 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003461 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003462 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003463 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003464 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003465 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003466 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003467 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003468 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003469 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003470 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003471 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3472 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003473 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003474 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003475 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003476 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003477 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3478 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003479 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003480 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003481 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003482 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003483 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003484 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003485 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003486 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003487 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003488 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003489 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003490 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003491 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003492 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003493 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003494 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003495 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003496 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003497 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003498 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003499 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003500 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003501 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003502 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003503 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003504 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003505 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003506 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003507 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003508 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003509 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003510 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003511 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003512 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003513 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003514 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003515 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003516 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003517 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003518 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003519 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003520 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003521 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003522 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003523 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003524 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003525 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003526 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003527 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003528 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003529 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003530 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003531 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003532 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003533 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003534 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003535 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003536 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003537 break;
3538 case DECL_BINDING:
3539 D = BindingDecl::CreateDeserialized(Context, ID);
3540 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003541 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003542 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003543 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003544 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003545 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003546 break;
John McCall5e77d762013-04-16 07:28:30 +00003547 case DECL_MS_PROPERTY:
3548 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3549 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003550 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003551 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003552 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003553 case DECL_CXX_BASE_SPECIFIERS:
3554 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003555 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003556 case DECL_CXX_CTOR_INITIALIZERS:
3557 Error("attempt to read a C++ ctor initializer record as a declaration");
3558 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003559 case DECL_IMPORT:
3560 // Note: last entry of the ImportDecl record is the number of stored source
3561 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003562 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003563 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003564 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003565 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003566 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003567 case DECL_OMP_DECLARE_REDUCTION:
3568 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3569 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003570 case DECL_OMP_CAPTUREDEXPR:
3571 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003572 break;
Nico Weber66220292016-03-02 17:28:48 +00003573 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003574 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003575 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003576 case DECL_PRAGMA_DETECT_MISMATCH:
3577 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003578 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003579 break;
Michael Han84324352013-02-22 17:15:32 +00003580 case DECL_EMPTY:
3581 D = EmptyDecl::CreateDeserialized(Context, ID);
3582 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003583 case DECL_OBJC_TYPE_PARAM:
3584 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3585 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003586 }
Chris Lattner487412d2009-04-27 05:27:42 +00003587
Sebastian Redlb3298c32010-08-18 23:56:48 +00003588 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003589 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003590 // Set the DeclContext before doing any deserialization, to make sure internal
3591 // calls to Decl::getASTContext() by Decl's methods will find the
3592 // TranslationUnitDecl without crashing.
3593 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003594 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003595
3596 // If this declaration is also a declaration context, get the
3597 // offsets for its tables of lexical and visible declarations.
3598 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3599 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003600 if (Offsets.first &&
3601 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3602 return nullptr;
3603 if (Offsets.second &&
3604 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3605 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003606 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003607 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003608
Douglas Gregordab42432011-08-12 00:15:20 +00003609 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003610 PendingUpdateRecords.push_back(
3611 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003612
Douglas Gregor404cdde2012-01-27 01:47:08 +00003613 // Load the categories after recursive loading is finished.
3614 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003615 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3616 // we put the Decl in PendingDefinitions so we can pull the categories here.
3617 if (Class->isThisDeclarationADefinition() ||
3618 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003619 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003620
Richard Smith13fb8602016-07-15 21:33:46 +00003621 // If we have deserialized a declaration that has a definition the
3622 // AST consumer might need to know about, queue it.
3623 // We don't pass it to the consumer immediately because we may be in recursive
3624 // loading, and some declarations may still be initializing.
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003625 PotentiallyInterestingDecls.push_back(
3626 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003627
Douglas Gregordab42432011-08-12 00:15:20 +00003628 return D;
3629}
3630
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003631void ASTReader::PassInterestingDeclsToConsumer() {
3632 assert(Consumer);
3633
3634 if (PassingDeclsToConsumer)
3635 return;
3636
3637 // Guard variable to avoid recursively redoing the process of passing
3638 // decls to consumer.
3639 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3640 true);
3641
3642 // Ensure that we've loaded all potentially-interesting declarations
3643 // that need to be eagerly loaded.
3644 for (auto ID : EagerlyDeserializedDecls)
3645 GetDecl(ID);
3646 EagerlyDeserializedDecls.clear();
3647
3648 while (!PotentiallyInterestingDecls.empty()) {
3649 InterestingDecl D = PotentiallyInterestingDecls.front();
3650 PotentiallyInterestingDecls.pop_front();
3651 if (isConsumerInterestedIn(Context, D.getDecl(), D.hasPendingBody()))
3652 PassInterestingDeclToConsumer(D.getDecl());
3653 }
3654}
3655
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003656void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003657 // The declaration may have been modified by files later in the chain.
3658 // If this is the case, read the record containing the updates from each file
3659 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003660 serialization::GlobalDeclID ID = Record.ID;
3661 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003662 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003663 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev497a9952017-06-09 21:54:18 +00003664
3665 llvm::SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
3666
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003667 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003668 auto UpdateOffsets = std::move(UpdI->second);
3669 DeclUpdateOffsets.erase(UpdI);
3670
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003671 // Check if this decl was interesting to the consumer. If we just loaded
3672 // the declaration, then we know it was interesting and we skip the call
3673 // to isConsumerInterestedIn because it is unsafe to call in the
3674 // current ASTReader state.
3675 bool WasInteresting =
3676 Record.JustLoaded || isConsumerInterestedIn(Context, D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003677 for (auto &FileAndOffset : UpdateOffsets) {
3678 ModuleFile *F = FileAndOffset.first;
3679 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003680 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3681 SavedStreamPosition SavedPosition(Cursor);
3682 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003683 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003684 ASTRecordReader Record(*this, *F);
3685 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003686 (void)RecCode;
3687 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003688
David L. Jonesbe1557a2016-12-21 00:17:49 +00003689 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3690 SourceLocation());
Vassil Vassilev497a9952017-06-09 21:54:18 +00003691 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003692
3693 // We might have made this declaration interesting. If so, remember that
3694 // we need to hand it off to the consumer.
3695 if (!WasInteresting &&
Richard Smithdc1f0422016-07-20 19:10:16 +00003696 isConsumerInterestedIn(Context, D, Reader.hasPendingBody())) {
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003697 PotentiallyInterestingDecls.push_back(
3698 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003699 WasInteresting = true;
3700 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003701 }
3702 }
Vassil Vassilev497a9952017-06-09 21:54:18 +00003703 // Add the lazy specializations to the template.
3704 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3705 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3706 "Must not have pending specializations");
3707 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3708 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3709 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3710 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3711 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3712 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
3713 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00003714
3715 // Load the pending visible updates for this decl context, if it has any.
3716 auto I = PendingVisibleUpdates.find(ID);
3717 if (I != PendingVisibleUpdates.end()) {
3718 auto VisibleUpdates = std::move(I->second);
3719 PendingVisibleUpdates.erase(I);
3720
3721 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3722 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3723 Lookups[DC].Table.add(
3724 Update.Mod, Update.Data,
3725 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3726 DC->setHasExternalVisibleStorage(true);
3727 }
Chris Lattner487412d2009-04-27 05:27:42 +00003728}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003729
Richard Smithd61d4ac2015-08-22 20:13:39 +00003730void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3731 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003732 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003733 if (FirstLocal != CanonDecl) {
3734 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3735 ASTDeclReader::attachPreviousDecl(
3736 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3737 CanonDecl);
3738 }
3739
3740 if (!LocalOffset) {
3741 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3742 return;
3743 }
3744
3745 // Load the list of other redeclarations from this module file.
3746 ModuleFile *M = getOwningModuleFile(FirstLocal);
3747 assert(M && "imported decl from no module file");
3748
3749 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3750 SavedStreamPosition SavedPosition(Cursor);
3751 Cursor.JumpToBit(LocalOffset);
3752
3753 RecordData Record;
3754 unsigned Code = Cursor.ReadCode();
3755 unsigned RecCode = Cursor.readRecord(Code, Record);
3756 (void)RecCode;
3757 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3758
3759 // FIXME: We have several different dispatches on decl kind here; maybe
3760 // we should instead generate one loop per kind and dispatch up-front?
3761 Decl *MostRecent = FirstLocal;
3762 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3763 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003764 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3765 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003766 }
Richard Smithc3a53252015-02-28 05:57:02 +00003767 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003768}
3769
3770namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003771 /// \brief Given an ObjC interface, goes through the modules and links to the
3772 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003773 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003774 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003775 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003776 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003777 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003778 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003779 serialization::GlobalDeclID InterfaceID;
3780 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003781
3782 void add(ObjCCategoryDecl *Cat) {
3783 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003784 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003785 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003786
3787 // Check for duplicate categories.
3788 if (Cat->getDeclName()) {
3789 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3790 if (Existing &&
3791 Reader.getOwningModuleFile(Existing)
3792 != Reader.getOwningModuleFile(Cat)) {
3793 // FIXME: We should not warn for duplicates in diamond:
3794 //
3795 // MT //
3796 // / \ //
3797 // ML MR //
3798 // \ / //
3799 // MB //
3800 //
3801 // If there are duplicates in ML/MR, there will be warning when
3802 // creating MB *and* when importing MB. We should not warn when
3803 // importing.
3804 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3805 << Interface->getDeclName() << Cat->getDeclName();
3806 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3807 } else if (!Existing) {
3808 // Record this category.
3809 Existing = Cat;
3810 }
3811 }
3812
3813 // Add this category to the end of the chain.
3814 if (Tail)
3815 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3816 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003817 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003818 Tail = Cat;
3819 }
3820
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003821 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003822 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003823 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003824 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3825 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003826 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003827 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3828 Tail(nullptr), InterfaceID(InterfaceID),
3829 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003830 {
3831 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003832 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003833 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003834 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003835
3836 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003837 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003838 }
3839 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003840
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003841 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003842 // If we've loaded all of the category information we care about from
3843 // this module file, we're done.
3844 if (M.Generation <= PreviousGeneration)
3845 return true;
3846
3847 // Map global ID of the definition down to the local ID used in this
3848 // module file. If there is no such mapping, we'll find nothing here
3849 // (or in any module it imports).
3850 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3851 if (!LocalID)
3852 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003853
Douglas Gregor404cdde2012-01-27 01:47:08 +00003854 // Perform a binary search to find the local redeclarations for this
3855 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003856 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003857 const ObjCCategoriesInfo *Result
3858 = std::lower_bound(M.ObjCCategoriesMap,
3859 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003860 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003861 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3862 Result->DefinitionID != LocalID) {
3863 // We didn't find anything. If the class definition is in this module
3864 // file, then the module files it depends on cannot have any categories,
3865 // so suppress further lookup.
3866 return Reader.isDeclIDFromModule(InterfaceID, M);
3867 }
3868
3869 // We found something. Dig out all of the categories.
3870 unsigned Offset = Result->Offset;
3871 unsigned N = M.ObjCCategories[Offset];
3872 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3873 for (unsigned I = 0; I != N; ++I)
3874 add(cast_or_null<ObjCCategoryDecl>(
3875 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3876 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003877 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003878 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003879} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003880
Douglas Gregor404cdde2012-01-27 01:47:08 +00003881void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3882 ObjCInterfaceDecl *D,
3883 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003884 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003885 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003886 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003887}
Douglas Gregordab42432011-08-12 00:15:20 +00003888
Richard Smithd6db68c2014-08-07 20:58:41 +00003889template<typename DeclT, typename Fn>
3890static void forAllLaterRedecls(DeclT *D, Fn F) {
3891 F(D);
3892
3893 // Check whether we've already merged D into its redeclaration chain.
3894 // MostRecent may or may not be nullptr if D has not been merged. If
3895 // not, walk the merged redecl chain and see if it's there.
3896 auto *MostRecent = D->getMostRecentDecl();
3897 bool Found = false;
3898 for (auto *Redecl = MostRecent; Redecl && !Found;
3899 Redecl = Redecl->getPreviousDecl())
3900 Found = (Redecl == D);
3901
3902 // If this declaration is merged, apply the functor to all later decls.
3903 if (Found) {
3904 for (auto *Redecl = MostRecent; Redecl != D;
3905 Redecl = Redecl->getPreviousDecl())
3906 F(Redecl);
3907 }
3908}
3909
Vassil Vassilev497a9952017-06-09 21:54:18 +00003910void ASTDeclReader::UpdateDecl(Decl *D,
3911 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003912 while (Record.getIdx() < Record.size()) {
3913 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003914 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003915 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003916 // FIXME: If we also have an update record for instantiating the
3917 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003918 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003919 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003920 // FIXME: We should call addHiddenDecl instead, to add the member
3921 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003922 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003923 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003924 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003925
3926 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev497a9952017-06-09 21:54:18 +00003927 // It will be added to the template's lazy specialization set.
3928 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003929 break;
3930
3931 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003932 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
3933
Douglas Gregor540fd812012-01-09 18:07:24 +00003934 // Each module has its own anonymous namespace, which is disjoint from
3935 // any other module's anonymous namespaces, so don't attach the anonymous
3936 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003937 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00003938 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
3939 TU->setAnonymousNamespace(Anon);
3940 else
3941 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
3942 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00003943 break;
3944 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003945
3946 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3947 cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003948 ReadSourceLocation());
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003949 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00003950
John McCall32791cc2016-01-06 22:34:54 +00003951 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
3952 auto Param = cast<ParmVarDecl>(D);
3953
3954 // We have to read the default argument regardless of whether we use it
3955 // so that hypothetical further update records aren't messed up.
3956 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003957 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00003958
3959 // Only apply the update if the parameter still has an uninstantiated
3960 // default argument.
3961 if (Param->hasUninstantiatedDefaultArg())
3962 Param->setDefaultArg(DefaultArg);
3963 break;
3964 }
3965
Richard Smith4b054b22016-08-24 21:25:37 +00003966 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
3967 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00003968 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00003969
3970 // Only apply the update if the field still has an uninstantiated
3971 // default member initializer.
3972 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
3973 if (DefaultInit)
3974 FD->setInClassInitializer(DefaultInit);
3975 else
3976 // Instantiation failed. We can get here if we serialized an AST for
3977 // an invalid program.
3978 FD->removeInClassInitializer();
3979 }
3980 break;
3981 }
3982
Richard Smith4d235792014-08-07 18:53:08 +00003983 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003984 FunctionDecl *FD = cast<FunctionDecl>(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00003985 if (Reader.PendingBodies[FD]) {
Richard Smithd28ac5b2014-03-22 23:33:22 +00003986 // FIXME: Maybe check for ODR violations.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003987 // It's safe to stop now because this update record is always last.
3988 return;
3989 }
Richard Smithd28ac5b2014-03-22 23:33:22 +00003990
David L. Jonesbe1557a2016-12-21 00:17:49 +00003991 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00003992 // Maintain AST consistency: any later redeclarations of this function
3993 // are inline if this one is. (We might have merged another declaration
3994 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00003995 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
3996 FD->setImplicitlyInline();
3997 });
Richard Smith195d8ef2014-05-29 03:15:31 +00003998 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00003999 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004000 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004001 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004002 break;
4003 }
4004
Richard Smithcd45dbc2014-04-19 03:48:30 +00004005 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4006 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004007 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004008 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004009 OldDD && (OldDD->Definition != RD ||
4010 !Reader.PendingFakeDefinitionData.count(OldDD));
Richard Smith2a9e5c52015-02-03 03:32:14 +00004011 ReadCXXRecordDefinition(RD, /*Update*/true);
4012
Richard Smithcd45dbc2014-04-19 03:48:30 +00004013 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004014 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004015 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004016 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004017 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004018 }
4019
David L. Jonesbe1557a2016-12-21 00:17:49 +00004020 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004021 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004022 if (MemberSpecializationInfo *MSInfo =
4023 RD->getMemberSpecializationInfo()) {
4024 MSInfo->setTemplateSpecializationKind(TSK);
4025 MSInfo->setPointOfInstantiation(POI);
4026 } else {
4027 ClassTemplateSpecializationDecl *Spec =
4028 cast<ClassTemplateSpecializationDecl>(RD);
4029 Spec->setTemplateSpecializationKind(TSK);
4030 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004031
David L. Jonesbe1557a2016-12-21 00:17:49 +00004032 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00004033 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004034 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004035 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004036 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004037 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004038 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004039
4040 // FIXME: If we already have a partial specialization set,
4041 // check that it matches.
4042 if (!Spec->getSpecializedTemplateOrPartial()
4043 .is<ClassTemplatePartialSpecializationDecl *>())
4044 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004045 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004046 }
4047
David L. Jonesbe1557a2016-12-21 00:17:49 +00004048 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004049 RD->setLocation(ReadSourceLocation());
4050 RD->setLocStart(ReadSourceLocation());
4051 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004052
David L. Jonesbe1557a2016-12-21 00:17:49 +00004053 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004054 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004055 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004056 // If the declaration already has attributes, we assume that some other
4057 // AST file already loaded them.
4058 if (!D->hasAttrs())
4059 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004060 }
4061 break;
4062 }
4063
Richard Smithf8134002015-03-10 01:41:22 +00004064 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4065 // Set the 'operator delete' directly to avoid emitting another update
4066 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004067 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004068 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
4069 // FIXME: Check consistency if we have an old and new operator delete.
4070 if (!First->OperatorDelete)
4071 First->OperatorDelete = Del;
4072 break;
4073 }
4074
Richard Smith564417a2014-03-20 21:47:22 +00004075 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004076 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004077 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004078 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004079
4080 // Update this declaration's exception specification, if needed.
4081 auto *FD = cast<FunctionDecl>(D);
4082 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4083 // FIXME: If the exception specification is already present, check that it
4084 // matches.
4085 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smith6de7a242014-07-31 23:46:44 +00004086 FD->setType(Reader.Context.getFunctionType(
4087 FPT->getReturnType(), FPT->getParamTypes(),
4088 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004089
4090 // When we get to the end of deserializing, see if there are other decls
4091 // that we need to propagate this exception specification onto.
4092 Reader.PendingExceptionSpecUpdates.insert(
4093 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004094 }
Richard Smith564417a2014-03-20 21:47:22 +00004095 break;
4096 }
4097
Richard Smith1fa5d642013-05-11 05:45:24 +00004098 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004099 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004100 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004101 for (auto *Redecl : merged_redecls(D)) {
4102 // FIXME: If the return type is already deduced, check that it matches.
4103 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
4104 Reader.Context.adjustDeducedFunctionResultType(FD, DeducedResultType);
4105 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004106 break;
4107 }
Eli Friedman276dd182013-09-05 00:02:25 +00004108
4109 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00004110 // Maintain AST consistency: any later redeclarations are used too.
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004111 D->markUsed(Reader.Context);
Eli Friedman276dd182013-09-05 00:02:25 +00004112 break;
4113 }
Richard Smith5652c0f2014-03-21 01:48:23 +00004114
4115 case UPD_MANGLING_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004116 Reader.Context.setManglingNumber(cast<NamedDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004117 break;
4118
4119 case UPD_STATIC_LOCAL_NUMBER:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004120 Reader.Context.setStaticLocalNumber(cast<VarDecl>(D), Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004121 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004122
Alexey Bataev97720002014-11-11 04:05:39 +00004123 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4124 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004125 Reader.Context, ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004126 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004127
Alex Denisovfde64952015-06-26 05:28:36 +00004128 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004129 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004130 auto *Exported = cast<NamedDecl>(D);
4131 if (auto *TD = dyn_cast<TagDecl>(Exported))
4132 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004133 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004134 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004135 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4136 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004137 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4138 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004139 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004140 // If Owner is made visible at some later point, make this declaration
4141 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004142 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004143 } else {
4144 // The declaration is now visible.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004145 Exported->Hidden = false;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004146 }
4147 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004148 }
Alex Denisovfde64952015-06-26 05:28:36 +00004149
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004150 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004151 case UPD_ADDED_ATTR_TO_RECORD:
4152 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004153 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004154 assert(Attrs.size() == 1);
4155 D->addAttr(Attrs[0]);
4156 break;
4157 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004158 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004159}