blob: 47484f8b38b0b0eb81ca4d32eda291d94c433f4e [file] [log] [blame]
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001//===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
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"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000018#include "clang/AST/Attr.h"
19#include "clang/AST/AttrIterator.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/AST/DeclCXX.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000023#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclOpenMP.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/AST/DeclTemplate.h"
27#include "clang/AST/DeclVisitor.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000028#include "clang/AST/DeclarationName.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "clang/AST/Expr.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000030#include "clang/AST/ExternalASTSource.h"
31#include "clang/AST/LambdaCapture.h"
32#include "clang/AST/NestedNameSpecifier.h"
33#include "clang/AST/Redeclarable.h"
34#include "clang/AST/Stmt.h"
35#include "clang/AST/TemplateBase.h"
36#include "clang/AST/Type.h"
37#include "clang/AST/UnresolvedSet.h"
38#include "clang/Basic/AttrKinds.h"
39#include "clang/Basic/ExceptionSpecificationType.h"
40#include "clang/Basic/IdentifierTable.h"
41#include "clang/Basic/LLVM.h"
42#include "clang/Basic/Lambda.h"
43#include "clang/Basic/LangOptions.h"
44#include "clang/Basic/Linkage.h"
45#include "clang/Basic/Module.h"
46#include "clang/Basic/PragmaKinds.h"
47#include "clang/Basic/SourceLocation.h"
48#include "clang/Basic/Specifiers.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000049#include "clang/Sema/IdentifierResolver.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000050#include "clang/Sema/SemaDiagnostic.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000051#include "clang/Serialization/ASTBitCodes.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000052#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000053#include "clang/Serialization/ContinuousRangeMap.h"
54#include "clang/Serialization/Module.h"
55#include "llvm/ADT/DenseMap.h"
56#include "llvm/ADT/FoldingSet.h"
57#include "llvm/ADT/STLExtras.h"
58#include "llvm/ADT/SmallPtrSet.h"
59#include "llvm/ADT/SmallVector.h"
60#include "llvm/ADT/iterator_range.h"
61#include "llvm/Bitcode/BitstreamReader.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000064#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000065#include <algorithm>
66#include <cassert>
67#include <cstdint>
68#include <cstring>
69#include <string>
70#include <utility>
Hans Wennborgdcfba332015-10-06 23:40:43 +000071
Chris Lattner487412d2009-04-27 05:27:42 +000072using namespace clang;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000073using namespace serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000074
75//===----------------------------------------------------------------------===//
76// Declaration deserialization
77//===----------------------------------------------------------------------===//
78
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000079namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000080
Sebastian Redlb3298c32010-08-18 23:56:48 +000081 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000082 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000083 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000084 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000085 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000086 const SourceLocation ThisDeclLoc;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000087
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000088 using RecordData = ASTReader::RecordData;
89
Richard Smith600adef2018-07-04 02:25:38 +000090 TypeID DeferredTypeID = 0;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000091 unsigned AnonymousDeclNumber;
92 GlobalDeclID NamedDeclForTagDecl = 0;
93 IdentifierInfo *TypedefNameForLinkage = nullptr;
94
95 bool HasPendingBody = false;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +000096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000097 ///A flag to carry the information for a decl from the entity is
Vassil Vassilev928c8252016-04-28 14:13:28 +000098 /// used. We use it to delay the marking of the canonical decl as used until
99 /// the entire declaration is deserialized and merged.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000100 bool IsDeclMarkedUsed = false;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000101
Sebastian Redlc67764e2010-07-22 22:43:28 +0000102 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000103
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000104 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000105 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000106 assert(LocalOffset < Loc.Offset && "offset point after current record");
107 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +0000108 }
109
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000110 uint64_t ReadGlobalOffset() {
111 uint64_t Local = ReadLocalOffset();
112 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +0000113 }
114
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000115 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000116 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000117 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000118
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000119 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000120 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000121 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000122
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000123 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000124 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000125 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000126
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000127 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000128 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000129 }
Richard Smith0b884372015-02-27 00:25:58 +0000130
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000131 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000132 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +0000133 }
134
Richard Smith0b884372015-02-27 00:25:58 +0000135 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000136 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000137 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +0000138 }
139
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000140 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000141 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000142 }
143
144 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000145 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000146 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000147 }
148
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000149 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000150 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000151 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000152
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000153 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000154 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000155 }
156
157 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000158 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000159 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000160
David L. Jonesbe1557a2016-12-21 00:17:49 +0000161 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000162 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000163
164 Module *readModule() {
165 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000166 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000167
Richard Smith2a9e5c52015-02-03 03:32:14 +0000168 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000169 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
170 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000171 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000172 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000173 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000174 void MergeDefinitionData(ObjCInterfaceDecl *D,
175 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000176 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
177 void MergeDefinitionData(ObjCProtocolDecl *D,
178 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000179
Richard Smith600adef2018-07-04 02:25:38 +0000180 static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
181
Richard Smithd08aeb62014-08-28 01:33:39 +0000182 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
183 DeclContext *DC,
184 unsigned Index);
185 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
186 unsigned Index, NamedDecl *D);
187
Richard Smith0144f512015-08-22 02:09:38 +0000188 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000189 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000190 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000191 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000192 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000193
Douglas Gregor022857e2011-12-22 01:48:48 +0000194 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000195 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000196 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000198 /// Retrieve the first ID.
Douglas Gregor022857e2011-12-22 01:48:48 +0000199 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000200
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000201 /// Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000202 bool isKeyDecl() const { return IsKeyDecl; }
203
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000204 /// Get a known declaration that this should be merged with, if
Richard Smith5a4737c2015-02-06 02:42:59 +0000205 /// any.
206 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000207 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000209 /// Class used to capture the result of searching for an existing
Douglas Gregor022857e2011-12-22 01:48:48 +0000210 /// declaration of a specific kind and name, along with the ability
211 /// to update the place where this result was found (the declaration
212 /// chain hanging off an identifier or the DeclContext we searched in)
213 /// if requested.
214 class FindExistingResult {
215 ASTReader &Reader;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000216 NamedDecl *New = nullptr;
217 NamedDecl *Existing = nullptr;
218 bool AddResult = false;
219 unsigned AnonymousDeclNumber = 0;
220 IdentifierInfo *TypedefNameForLinkage = nullptr;
Richard Smithd08aeb62014-08-28 01:33:39 +0000221
Douglas Gregor022857e2011-12-22 01:48:48 +0000222 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000223 FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000224
Richard Smithd08aeb62014-08-28 01:33:39 +0000225 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000226 unsigned AnonymousDeclNumber,
227 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000228 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000229 AnonymousDeclNumber(AnonymousDeclNumber),
230 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000231
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000232 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000233 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
234 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000235 AnonymousDeclNumber(Other.AnonymousDeclNumber),
236 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000237 Other.AddResult = false;
238 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000239
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000240 FindExistingResult &operator=(FindExistingResult &&) = delete;
Douglas Gregor022857e2011-12-22 01:48:48 +0000241 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000242
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000243 /// Suppress the addition of this result into the known set of
Douglas Gregor2009cee2012-01-03 22:46:00 +0000244 /// names.
245 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000246
Douglas Gregor022857e2011-12-22 01:48:48 +0000247 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000248
Douglas Gregor022857e2011-12-22 01:48:48 +0000249 template<typename T>
250 operator T*() const { return dyn_cast_or_null<T>(Existing); }
251 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000252
Richard Smith8a639892015-01-24 01:07:20 +0000253 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
254 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000255 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000256
Chris Lattner487412d2009-04-27 05:27:42 +0000257 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000258 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
259 ASTReader::RecordLocation Loc,
260 DeclID thisDeclID, SourceLocation ThisDeclLoc)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000261 : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
262 ThisDeclLoc(ThisDeclLoc) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000263
Vassil Vassilev7d264622017-06-30 22:40:17 +0000264 template <typename T> static
265 void AddLazySpecializations(T *D,
266 SmallVectorImpl<serialization::DeclID>& IDs) {
267 if (IDs.empty())
268 return;
269
270 // FIXME: We should avoid this pattern of getting the ASTContext.
271 ASTContext &C = D->getASTContext();
272
273 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
274
275 if (auto &Old = LazySpecializations) {
276 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +0000277 llvm::sort(IDs.begin(), IDs.end());
Vassil Vassilev7d264622017-06-30 22:40:17 +0000278 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
279 }
280
281 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
282 *Result = IDs.size();
283 std::copy(IDs.begin(), IDs.end(), Result + 1);
284
285 LazySpecializations = Result;
286 }
287
Richard Smithb321ecb2014-05-13 01:15:00 +0000288 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000289 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
290 static Decl *getMostRecentDeclImpl(...);
291 static Decl *getMostRecentDecl(Decl *D);
292
293 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000294 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000295 Redeclarable<DeclT> *D, Decl *Previous,
296 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000297 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000298 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
299 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000300
301 template <typename DeclT>
302 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
303 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000304 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000305
Richard Smith851072e2014-05-19 20:59:20 +0000306 template <typename DeclT>
307 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
308 static void markIncompleteDeclChainImpl(...);
309
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000310 /// Determine whether this declaration has a pending body.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000311 bool hasPendingBody() const { return HasPendingBody; }
312
David Blaikieac4345c2017-02-12 18:45:31 +0000313 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000314 void Visit(Decl *D);
315
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000316 void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000317
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000318 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
319 ObjCCategoryDecl *Next) {
320 Cat->NextClassCategory = Next;
321 }
322
Chris Lattner487412d2009-04-27 05:27:42 +0000323 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000324 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000325 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000326 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
327 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000328 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000329 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000330 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
331 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000332 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000333 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000334 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000335 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000336 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000337 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000338 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000339 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
340 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
341 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
342 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
343 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000344 ClassTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000345
Richard Smith1d209d02013-05-23 01:49:11 +0000346 void VisitClassTemplateSpecializationDecl(
347 ClassTemplateSpecializationDecl *D) {
348 VisitClassTemplateSpecializationDeclImpl(D);
349 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000350
Chris Lattnerca025db2010-05-07 21:43:38 +0000351 void VisitClassTemplatePartialSpecializationDecl(
352 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000353 void VisitClassScopeFunctionSpecializationDecl(
354 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000355 RedeclarableResult
356 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000357
Larisse Voufo39a1e502013-08-06 01:03:05 +0000358 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
359 VisitVarTemplateSpecializationDeclImpl(D);
360 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000361
Larisse Voufo39a1e502013-08-06 01:03:05 +0000362 void VisitVarTemplatePartialSpecializationDecl(
363 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000364 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000365 void VisitValueDecl(ValueDecl *VD);
366 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000367 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000368 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000369 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000370 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000371 void VisitCXXMethodDecl(CXXMethodDecl *D);
372 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
373 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
374 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000375 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000376 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000377 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000378 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
379 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000380 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
381 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000382 void VisitDecompositionDecl(DecompositionDecl *DD);
383 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000384 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000385 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000386 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000387 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000388 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000389 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000390 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000391 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000392 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000393 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000394 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000395 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000396 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000397 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000398 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000399 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000400 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000401 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000402 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000403 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
404 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000405 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000406 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000407 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000408
Chris Lattner487412d2009-04-27 05:27:42 +0000409 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000410
411 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000412 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000413
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000414 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000415 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
416 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000417
418 template<typename T>
419 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000420 RedeclarableResult &Redecl,
421 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000422
Richard Smith0b87e072013-10-07 08:02:11 +0000423 template<typename T>
424 void mergeMergeable(Mergeable<T> *D);
425
Richard Smithf17fdbd2014-04-24 02:25:27 +0000426 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
427 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000428 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000429
Douglas Gregor85f3f952015-07-07 03:57:15 +0000430 ObjCTypeParamList *ReadObjCTypeParamList();
431
Alexis Hunted053252010-05-30 07:21:58 +0000432 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000433 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000434 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000435 void VisitObjCContainerDecl(ObjCContainerDecl *D);
436 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
437 void VisitObjCIvarDecl(ObjCIvarDecl *D);
438 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
439 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000440 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
441 void VisitObjCImplDecl(ObjCImplDecl *D);
442 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
443 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
444 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
445 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
446 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000447 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000448 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000449 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000450 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000451
452} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000453
Richard Smith86dfc1e2015-06-18 22:07:00 +0000454namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000455
Richard Smith86dfc1e2015-06-18 22:07:00 +0000456/// Iterator over the redeclarations of a declaration that have already
457/// been merged into the same redeclaration chain.
458template<typename DeclT>
459class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000460 DeclT *Start;
461 DeclT *Canonical = nullptr;
462 DeclT *Current = nullptr;
463
Richard Smith86dfc1e2015-06-18 22:07:00 +0000464public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000465 MergedRedeclIterator() = default;
466 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000467
468 DeclT *operator*() { return Current; }
469
470 MergedRedeclIterator &operator++() {
471 if (Current->isFirstDecl()) {
472 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000473 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000474 } else
475 Current = Current->getPreviousDecl();
476
477 // If we started in the merged portion, we'll reach our start position
478 // eventually. Otherwise, we'll never reach it, but the second declaration
479 // we reached was the canonical declaration, so stop when we see that one
480 // again.
481 if (Current == Start || Current == Canonical)
482 Current = nullptr;
483 return *this;
484 }
485
486 friend bool operator!=(const MergedRedeclIterator &A,
487 const MergedRedeclIterator &B) {
488 return A.Current != B.Current;
489 }
490};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000491
492} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000493
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000494template <typename DeclT>
495static llvm::iterator_range<MergedRedeclIterator<DeclT>>
496merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000497 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
498 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000499}
500
Sebastian Redlb3298c32010-08-18 23:56:48 +0000501uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000502 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000503}
504
David Blaikieac4345c2017-02-12 18:45:31 +0000505void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000506 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000507 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000508 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Erich Keane9c665062018-08-01 21:02:40 +0000509 CD->setNumCtorInitializers(Record.readInt());
510 if (CD->getNumCtorInitializers())
David Blaikieac4345c2017-02-12 18:45:31 +0000511 CD->CtorInitializers = ReadGlobalOffset();
512 }
513 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000514 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
515 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000516}
517
Sebastian Redlb3298c32010-08-18 23:56:48 +0000518void ASTDeclReader::Visit(Decl *D) {
519 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000520
Vassil Vassilev928c8252016-04-28 14:13:28 +0000521 // At this point we have deserialized and merged the decl and it is safe to
522 // update its canonical decl to signal that the entire entity is used.
523 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
524 IsDeclMarkedUsed = false;
525
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000526 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000527 if (auto *TInfo = DD->getTypeSourceInfo())
528 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000529 }
530
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000531 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000532 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000533 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000534
535 // If this is a tag declaration with a typedef name for linkage, it's safe
536 // to load that typedef now.
537 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000538 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
539 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000540 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000541 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000542 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000543 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000544 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000545 // We only read it if FD doesn't already have a body (e.g., from another
546 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000547 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000548 if (Record.readInt())
549 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000550 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000551}
552
Sebastian Redlb3298c32010-08-18 23:56:48 +0000553void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000554 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
555 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000556 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000557 // parameter or of a parameter of a function template immediately. These
558 // entities might be used in the formulation of its DeclContext (for
559 // example, a function parameter can be used in decltype() in trailing
560 // return type of the function). Use the translation unit DeclContext as a
561 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000562 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
563 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000564 if (!LexicalDCIDForTemplateParmDecl)
565 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000566 Reader.addPendingDeclContextInfo(D,
567 SemaDCIDForTemplateParmDecl,
568 LexicalDCIDForTemplateParmDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000569 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000570 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000571 auto *SemaDC = ReadDeclAs<DeclContext>();
572 auto *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000573 if (!LexicalDC)
574 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000575 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000576 // Avoid calling setLexicalDeclContext() directly because it uses
577 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000578 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
579 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000580 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000581 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000582 D->setInvalidDecl(Record.readInt());
583 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000584 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000585 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000586 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
587 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000588 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000589 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000590 D->setImplicit(Record.readInt());
591 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000592 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000593 D->setReferenced(Record.readInt());
594 D->setTopLevelDeclInObjCContainer(Record.readInt());
595 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000596 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000597 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000598
Douglas Gregorcf68c582011-12-01 22:20:10 +0000599 // Determine whether this declaration is part of a (sub)module. If so, it
600 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000601 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000602 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000603 D->setModuleOwnershipKind(
604 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
605 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000606 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000607
Richard Smith90dc5252017-06-23 01:04:34 +0000608 if (ModulePrivate) {
609 // Module-private declarations are never visible, so there is no work to
610 // do.
Richard Smith42413142015-05-15 20:05:43 +0000611 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
612 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000613 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000614 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000615 // Mark the declaration as visible when its owning module becomes visible.
616 if (Owner->NameVisibility == Module::AllVisible)
617 D->setVisibleDespiteOwningModule();
618 else
Richard Smith42413142015-05-15 20:05:43 +0000619 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000620 }
Richard Smithd19389a2017-07-05 07:47:11 +0000621 } else if (ModulePrivate) {
622 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000623 }
Chris Lattner487412d2009-04-27 05:27:42 +0000624}
625
Nico Weber66220292016-03-02 17:28:48 +0000626void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
627 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000628 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000629 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000630 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000631 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
632 D->getTrailingObjects<char>()[Arg.size()] = '\0';
633}
634
Nico Webercbbaeb12016-03-02 19:28:54 +0000635void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
636 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000637 D->setLocation(ReadSourceLocation());
638 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000639 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
640 D->getTrailingObjects<char>()[Name.size()] = '\0';
641
642 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000643 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000644 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
645 Value.size());
646 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
647}
648
Sebastian Redlb3298c32010-08-18 23:56:48 +0000649void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000650 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000651}
652
Sebastian Redlb3298c32010-08-18 23:56:48 +0000653void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000654 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000655 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000656 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000657}
658
Sebastian Redlb3298c32010-08-18 23:56:48 +0000659void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000660 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000661 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000662 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000663 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000664}
665
Richard Smith43ccec8e2014-08-26 03:52:16 +0000666ASTDeclReader::RedeclarableResult
667ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000668 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000669 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000670 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000671 if (Record.readInt()) { // isModed
672 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000673 TD->setModedTypeSourceInfo(TInfo, modedT);
674 } else
675 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000676 // Read and discard the declaration for which this is a typedef name for
677 // linkage, if it exists. We cannot rely on our type to pull in this decl,
678 // because it might have been merged with a type from another module and
679 // thus might not refer to our version of the declaration.
680 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000681 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000682}
683
684void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000685 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
686 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000687}
688
Richard Smithdda56e42011-04-15 14:24:37 +0000689void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000690 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000691 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000692 // Merged when we merge the template.
693 TD->setDescribedAliasTemplate(Template);
694 else
695 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000696}
697
Richard Smith1d209d02013-05-23 01:49:11 +0000698ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000699 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000700 VisitTypeDecl(TD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000701
David L. Jonesbe1557a2016-12-21 00:17:49 +0000702 TD->IdentifierNamespace = Record.readInt();
703 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000704 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000705 TD->setCompleteDefinition(Record.readInt());
706 TD->setEmbeddedInDeclarator(Record.readInt());
707 TD->setFreeStanding(Record.readInt());
708 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000709 TD->setBraceRange(ReadSourceRange());
Fangrui Song6907ce22018-07-30 19:24:48 +0000710
David L. Jonesbe1557a2016-12-21 00:17:49 +0000711 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000712 case 0:
713 break;
714 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000715 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000716 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000717 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000718 break;
719 }
720 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000721 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000722 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000723 break;
Richard Smith70d58502014-08-30 00:04:23 +0000724 default:
725 llvm_unreachable("unexpected tag info kind");
726 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000727
Richard Smithcd45dbc2014-04-19 03:48:30 +0000728 if (!isa<CXXRecordDecl>(TD))
729 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000730 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000731}
732
Sebastian Redlb3298c32010-08-18 23:56:48 +0000733void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000734 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000735 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000736 ED->setIntegerTypeSourceInfo(TI);
737 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000738 ED->setIntegerType(Record.readType());
739 ED->setPromotionType(Record.readType());
740 ED->setNumPositiveBits(Record.readInt());
741 ED->setNumNegativeBits(Record.readInt());
Erich Keanef92f31c2018-08-01 20:48:16 +0000742 ED->setScoped(Record.readInt());
743 ED->setScopedUsingClassTag(Record.readInt());
744 ED->setFixed(Record.readInt());
Richard Smith4b38ded2012-03-14 23:13:10 +0000745
Erich Keanef92f31c2018-08-01 20:48:16 +0000746 ED->setHasODRHash(true);
Richard Trieuab4d7302018-07-25 22:52:05 +0000747 ED->ODRHash = Record.readInt();
748
Richard Smith01a73372013-10-15 22:02:41 +0000749 // If this is a definition subject to the ODR, and we already have a
750 // definition, merge this one into it.
Erich Keanef92f31c2018-08-01 20:48:16 +0000751 if (ED->isCompleteDefinition() &&
Richard Smith01a73372013-10-15 22:02:41 +0000752 Reader.getContext().getLangOpts().Modules &&
753 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000754 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
755 if (!OldDef) {
756 // This is the first time we've seen an imported definition. Look for a
757 // local definition before deciding that we are the first definition.
758 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
759 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
760 OldDef = D;
761 break;
762 }
763 }
764 }
765 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000766 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
Erich Keanef92f31c2018-08-01 20:48:16 +0000767 ED->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +0000768 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000769 if (OldDef->getODRHash() != ED->getODRHash())
770 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000771 } else {
772 OldDef = ED;
773 }
774 }
775
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000776 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
777 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000778 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000779 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
780 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
781 }
Chris Lattner487412d2009-04-27 05:27:42 +0000782}
783
Richard Smith1d209d02013-05-23 01:49:11 +0000784ASTDeclReader::RedeclarableResult
785ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
786 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000787 RD->setHasFlexibleArrayMember(Record.readInt());
788 RD->setAnonymousStructOrUnion(Record.readInt());
789 RD->setHasObjectMember(Record.readInt());
790 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000791 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
792 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
793 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000794 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000795 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000796 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000797}
798
Sebastian Redlb3298c32010-08-18 23:56:48 +0000799void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000800 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000801 // For function declarations, defer reading the type in case the function has
802 // a deduced return type that references an entity declared within the
803 // function.
804 if (isa<FunctionDecl>(VD))
805 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
806 else
807 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000808}
809
Sebastian Redlb3298c32010-08-18 23:56:48 +0000810void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000811 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000812 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000813 ECD->setInitExpr(Record.readExpr());
814 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000815 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000816}
817
Sebastian Redlb3298c32010-08-18 23:56:48 +0000818void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000819 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000820 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000821 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000822 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000823 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000824 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000825 }
Richard Smithc23d7342018-06-29 20:46:25 +0000826 QualType TSIType = Record.readType();
827 DD->setTypeSourceInfo(
828 TSIType.isNull() ? nullptr
829 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000830}
831
Sebastian Redlb3298c32010-08-18 23:56:48 +0000832void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000833 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000834 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000835
Richard Smith600adef2018-07-04 02:25:38 +0000836 // Attach a type to this function. Use the real type if possible, but fall
837 // back to the type as written if it involves a deduced return type.
838 if (FD->getTypeSourceInfo() &&
839 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
840 ->getReturnType()->getContainedAutoType()) {
841 // We'll set up the real type in Visit, once we've finished loading the
842 // function.
843 FD->setType(FD->getTypeSourceInfo()->getType());
Richard Smitha62d1982018-08-03 01:00:01 +0000844 Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
Richard Smith600adef2018-07-04 02:25:38 +0000845 } else {
846 FD->setType(Reader.GetType(DeferredTypeID));
Richard Smith600adef2018-07-04 02:25:38 +0000847 }
Richard Smitha62d1982018-08-03 01:00:01 +0000848 DeferredTypeID = 0;
Richard Smith600adef2018-07-04 02:25:38 +0000849
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000850 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000851 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000852
Douglas Gregorb2585692012-01-04 17:13:46 +0000853 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
854 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000855
Erich Keane9c665062018-08-01 21:02:40 +0000856 FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
857 FD->setInlineSpecified(Record.readInt());
858 FD->setImplicitlyInline(Record.readInt());
859 FD->setExplicitSpecified(Record.readInt());
860 FD->setVirtualAsWritten(Record.readInt());
861 FD->setPure(Record.readInt());
862 FD->setHasInheritedPrototype(Record.readInt());
863 FD->setHasWrittenPrototype(Record.readInt());
864 FD->setDeletedAsWritten(Record.readInt());
865 FD->setTrivial(Record.readInt());
866 FD->setTrivialForCall(Record.readInt());
867 FD->setDefaulted(Record.readInt());
868 FD->setExplicitlyDefaulted(Record.readInt());
869 FD->setHasImplicitReturnZero(Record.readInt());
870 FD->setConstexpr(Record.readInt());
871 FD->setUsesSEHTry(Record.readInt());
872 FD->setHasSkippedBody(Record.readInt());
873 FD->setIsMultiVersion(Record.readInt());
874 FD->setLateTemplateParsed(Record.readInt());
875
876 FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000877 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000878
Richard Trieue6caa262017-12-23 00:41:01 +0000879 FD->ODRHash = Record.readInt();
Erich Keane9c665062018-08-01 21:02:40 +0000880 FD->setHasODRHash(true);
Richard Trieue6caa262017-12-23 00:41:01 +0000881
David L. Jonesbe1557a2016-12-21 00:17:49 +0000882 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000883 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000884 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000885 break;
886 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000887 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000888 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000889 break;
890 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000891 auto *InstFD = ReadDeclAs<FunctionDecl>();
892 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000893 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000894 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000895 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000896 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000897 break;
898 }
899 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000900 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
901 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000902
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000903 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000904 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000905 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000906
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000907 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000908 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000909 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000910 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000911 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000912 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000913 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000914 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000915 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000916
917 LAngleLoc = ReadSourceLocation();
918 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000919 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000920
921 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000922
Douglas Gregor4163aca2011-09-09 21:34:22 +0000923 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000924 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000925 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000926 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000927 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000928 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000929 FunctionTemplateSpecializationInfo *FTInfo
930 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
931 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000932 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
933 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000934 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000935 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000936
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000937 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000938 // The template that contains the specializations set. It's not safe to
939 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000940 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000941 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
942 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
943 // FunctionTemplateSpecializationInfo's Profile().
944 // We avoid getASTContext because a decl in the parent hierarchy may
945 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000946 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000947 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000948 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000949 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000950 FunctionTemplateSpecializationInfo *ExistingInfo =
951 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000952 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000953 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
954 else {
955 assert(Reader.getContext().getLangOpts().Modules &&
956 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000957 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000958 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000959 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000960 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000961 }
962 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
963 // Templates.
964 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000965 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000966 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000967 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
968
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000969 // Templates args.
970 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000971 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000972 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000973 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000974 TemplArgs.setLAngleLoc(ReadSourceLocation());
975 TemplArgs.setRAngleLoc(ReadSourceLocation());
976
Douglas Gregor4163aca2011-09-09 21:34:22 +0000977 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000978 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000979 // These are not merged; we don't need to merge redeclarations of dependent
980 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000981 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000982 }
983 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000984
Chris Lattnerca025db2010-05-07 21:43:38 +0000985 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000986 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000987 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000988 Params.reserve(NumParams);
989 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000990 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000991 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000992}
993
Sebastian Redlb3298c32010-08-18 23:56:48 +0000994void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000995 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000996 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000997 // Load the body on-demand. Most clients won't care, because method
998 // definitions rarely show up in headers.
999 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1000 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001001 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1002 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001003 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001004 MD->setInstanceMethod(Record.readInt());
1005 MD->setVariadic(Record.readInt());
1006 MD->setPropertyAccessor(Record.readInt());
1007 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001008 MD->setOverriding(Record.readInt());
1009 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001010
Erich Keane9b18eca2018-08-01 21:31:08 +00001011 MD->setIsRedeclaration(Record.readInt());
1012 MD->setHasRedeclaration(Record.readInt());
1013 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001014 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001015 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001016
David L. Jonesbe1557a2016-12-21 00:17:49 +00001017 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1018 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001019 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001020 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001021 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1022 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001023 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001024 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001025 Params.reserve(NumParams);
1026 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001027 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001028
Erich Keane9b18eca2018-08-01 21:31:08 +00001029 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001030 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001031 SmallVector<SourceLocation, 16> SelLocs;
1032 SelLocs.reserve(NumStoredSelLocs);
1033 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001034 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001035
1036 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001037}
1038
Douglas Gregor85f3f952015-07-07 03:57:15 +00001039void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001040 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001041
David L. Jonesbe1557a2016-12-21 00:17:49 +00001042 D->Variance = Record.readInt();
1043 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001044 D->VarianceLoc = ReadSourceLocation();
1045 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001046}
1047
Sebastian Redlb3298c32010-08-18 23:56:48 +00001048void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001049 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001050 CD->setAtStartLoc(ReadSourceLocation());
1051 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001052}
1053
Douglas Gregor85f3f952015-07-07 03:57:15 +00001054ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001055 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001056 if (numParams == 0)
1057 return nullptr;
1058
1059 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1060 typeParams.reserve(numParams);
1061 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001062 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001063 if (!typeParam)
1064 return nullptr;
1065
1066 typeParams.push_back(typeParam);
1067 }
1068
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001069 SourceLocation lAngleLoc = ReadSourceLocation();
1070 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001071
1072 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1073 typeParams, rAngleLoc);
1074}
1075
Manman Renec315f12016-09-09 23:48:27 +00001076void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001077 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001078 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001079 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001080
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001081 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001082 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001083
Manman Renec315f12016-09-09 23:48:27 +00001084 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001085 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001086 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1087 Protocols.reserve(NumProtocols);
1088 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001089 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001090 SmallVector<SourceLocation, 16> ProtoLocs;
1091 ProtoLocs.reserve(NumProtocols);
1092 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001093 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001094 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1095 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001096
Manman Renec315f12016-09-09 23:48:27 +00001097 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001098 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001099 Protocols.clear();
1100 Protocols.reserve(NumProtocols);
1101 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001102 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001103 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1104 Reader.getContext());
1105}
1106
1107void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1108 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1109 // FIXME: odr checking?
1110}
1111
Sebastian Redlb3298c32010-08-18 23:56:48 +00001112void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001113 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001114 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001115 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001116 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001117
1118 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001119 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001120 // Read the definition.
1121 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001122
David L. Jonesbe1557a2016-12-21 00:17:49 +00001123 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001124 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1125 if (Canon->Data.getPointer()) {
1126 // If we already have a definition, keep the definition invariant and
1127 // merge the data.
1128 MergeDefinitionData(Canon, std::move(ID->data()));
1129 ID->Data = Canon->Data;
1130 } else {
1131 // Set the definition data of the canonical declaration, so other
1132 // redeclarations will see it.
1133 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001134
Manman Renec315f12016-09-09 23:48:27 +00001135 // We will rebuild this list lazily.
1136 ID->setIvarList(nullptr);
1137 }
Craig Toppera13603a2014-05-22 05:54:18 +00001138
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001139 // Note that we have deserialized a definition.
1140 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001141
Douglas Gregor404cdde2012-01-27 01:47:08 +00001142 // Note that we've loaded this Objective-C class.
1143 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001144 } else {
1145 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001146 }
Chris Lattner487412d2009-04-27 05:27:42 +00001147}
1148
Sebastian Redlb3298c32010-08-18 23:56:48 +00001149void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001150 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001151 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001152 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001153 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001154 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001155 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001156}
1157
Graydon Hoaree0a68352017-06-28 18:36:27 +00001158void ASTDeclReader::ReadObjCDefinitionData(
1159 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001160 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001161 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1162 ProtoRefs.reserve(NumProtoRefs);
1163 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001164 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001165 SmallVector<SourceLocation, 16> ProtoLocs;
1166 ProtoLocs.reserve(NumProtoRefs);
1167 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001168 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001169 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1170 ProtoLocs.data(), Reader.getContext());
1171}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001172
Graydon Hoaree0a68352017-06-28 18:36:27 +00001173void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1174 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1175 // FIXME: odr checking?
1176}
1177
1178void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1179 RedeclarableResult Redecl = VisitRedeclarable(PD);
1180 VisitObjCContainerDecl(PD);
1181 mergeRedeclarable(PD, Redecl);
1182
1183 if (Record.readInt()) {
1184 // Read the definition.
1185 PD->allocateDefinitionData();
1186
1187 ReadObjCDefinitionData(PD->data());
1188
1189 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1190 if (Canon->Data.getPointer()) {
1191 // If we already have a definition, keep the definition invariant and
1192 // merge the data.
1193 MergeDefinitionData(Canon, std::move(PD->data()));
1194 PD->Data = Canon->Data;
1195 } else {
1196 // Set the definition data of the canonical declaration, so other
1197 // redeclarations will see it.
1198 PD->getCanonicalDecl()->Data = PD->Data;
1199 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001200 // Note that we have deserialized a definition.
1201 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001202 } else {
1203 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001204 }
Chris Lattner487412d2009-04-27 05:27:42 +00001205}
1206
Sebastian Redlb3298c32010-08-18 23:56:48 +00001207void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001208 VisitFieldDecl(FD);
1209}
1210
Sebastian Redlb3298c32010-08-18 23:56:48 +00001211void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001212 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001213 CD->setCategoryNameLoc(ReadSourceLocation());
1214 CD->setIvarLBraceLoc(ReadSourceLocation());
1215 CD->setIvarRBraceLoc(ReadSourceLocation());
1216
Douglas Gregor404cdde2012-01-27 01:47:08 +00001217 // Note that this category has been deserialized. We do this before
1218 // deserializing the interface declaration, so that it will consider this
1219 /// category.
1220 Reader.CategoriesDeserialized.insert(CD);
1221
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001222 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001223 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001224 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001225 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001226 ProtoRefs.reserve(NumProtoRefs);
1227 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001228 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001229 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001230 ProtoLocs.reserve(NumProtoRefs);
1231 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001232 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001233 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001234 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001235
1236 // Protocols in the class extension belong to the class.
1237 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1238 CD->ClassInterface->mergeClassExtensionProtocolList(
1239 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1240 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001241}
1242
Sebastian Redlb3298c32010-08-18 23:56:48 +00001243void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001244 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001245 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001246}
1247
Sebastian Redlb3298c32010-08-18 23:56:48 +00001248void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001249 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001250 D->setAtLoc(ReadSourceLocation());
1251 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001252 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001253 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001254 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001255 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001256 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001257 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001258 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001259 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001260 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001261 DeclarationName GetterName = Record.readDeclarationName();
1262 SourceLocation GetterLoc = ReadSourceLocation();
1263 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1264 DeclarationName SetterName = Record.readDeclarationName();
1265 SourceLocation SetterLoc = ReadSourceLocation();
1266 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001267 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1268 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1269 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001270}
1271
Sebastian Redlb3298c32010-08-18 23:56:48 +00001272void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001273 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001274 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001275}
1276
Sebastian Redlb3298c32010-08-18 23:56:48 +00001277void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001278 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001279 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001280}
1281
Sebastian Redlb3298c32010-08-18 23:56:48 +00001282void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001283 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001284 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1285 D->SuperLoc = ReadSourceLocation();
1286 D->setIvarLBraceLoc(ReadSourceLocation());
1287 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001288 D->setHasNonZeroConstructors(Record.readInt());
1289 D->setHasDestructors(Record.readInt());
1290 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001291 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001292 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001293}
1294
Sebastian Redlb3298c32010-08-18 23:56:48 +00001295void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001296 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001297 D->setAtLoc(ReadSourceLocation());
1298 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1299 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1300 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001301 D->setGetterCXXConstructor(Record.readExpr());
1302 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001303}
1304
Sebastian Redlb3298c32010-08-18 23:56:48 +00001305void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001306 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001307 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001308
1309 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1310 FD->InitStorage.setInt(ISK);
1311 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1312 ? Record.readType().getAsOpaquePtr()
1313 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001314 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001315
1316 if (auto *BW = Record.readExpr())
1317 FD->setBitWidth(BW);
1318
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001319 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001320 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001321 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001322 }
Richard Smith0b87e072013-10-07 08:02:11 +00001323 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001324}
1325
John McCall5e77d762013-04-16 07:28:30 +00001326void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1327 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001328 PD->GetterId = Record.getIdentifierInfo();
1329 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001330}
1331
Francois Pichet783dd6e2010-11-21 06:08:52 +00001332void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1333 VisitValueDecl(FD);
1334
David L. Jonesbe1557a2016-12-21 00:17:49 +00001335 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001336 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001337 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001338
1339 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001340 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001341
1342 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001343}
1344
Larisse Voufo39a1e502013-08-06 01:03:05 +00001345ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001346 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001347 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001348
David L. Jonesbe1557a2016-12-21 00:17:49 +00001349 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1350 VD->VarDeclBits.TSCSpec = Record.readInt();
1351 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001352 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001353 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1354 Record.readInt();
1355 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001356 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001357 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001358 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001359 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1360 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1361 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1362 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1363 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1364 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001365 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001366 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001367 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001368 VD->setCachedLinkage(VarLinkage);
1369
1370 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001371 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001372 VD->getLexicalDeclContext()->isFunctionOrMethod())
1373 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001374
David L. Jonesbe1557a2016-12-21 00:17:49 +00001375 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001376 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001377 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001378 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1379 Eval->CheckedICE = true;
1380 Eval->IsICE = Val == 3;
1381 }
1382 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001383
Akira Hatanaka9978da32018-08-10 15:09:24 +00001384 if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1385 Expr *CopyExpr = Record.readExpr();
1386 if (CopyExpr)
1387 Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1388 }
1389
Richard Smitha4653622017-09-06 20:01:14 +00001390 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1391 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1392
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001393 enum VarKind {
1394 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1395 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001396 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001397 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001398 // Only true variables (not parameters or implicit parameters) can be
1399 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001400 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1401 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001402 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001403 break;
1404 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001405 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001406 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001407 break;
1408 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001409 auto *Tmpl = ReadDeclAs<VarDecl>();
1410 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001411 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001412 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001413 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001414 break;
1415 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001416 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001417
1418 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001419}
1420
Sebastian Redlb3298c32010-08-18 23:56:48 +00001421void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001422 VisitVarDecl(PD);
1423}
1424
Sebastian Redlb3298c32010-08-18 23:56:48 +00001425void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001426 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001427 unsigned isObjCMethodParam = Record.readInt();
1428 unsigned scopeDepth = Record.readInt();
1429 unsigned scopeIndex = Record.readInt();
1430 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001431 if (isObjCMethodParam) {
1432 assert(scopeDepth == 0);
1433 PD->setObjCMethodScopeInfo(scopeIndex);
1434 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1435 } else {
1436 PD->setScopeInfo(scopeDepth, scopeIndex);
1437 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001438 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1439 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1440 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001441 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001442
1443 // FIXME: If this is a redeclaration of a function from another module, handle
1444 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001445}
1446
Richard Smith7b76d812016-08-12 02:21:25 +00001447void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1448 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001449 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith7b76d812016-08-12 02:21:25 +00001450 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001451 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001452}
1453
1454void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1455 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001456 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001457}
1458
Sebastian Redlb3298c32010-08-18 23:56:48 +00001459void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001460 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001461 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001462 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001463}
1464
Sebastian Redlb3298c32010-08-18 23:56:48 +00001465void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001466 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001467 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001468 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001469 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001470 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001471 Params.reserve(NumParams);
1472 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001473 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001474 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001475
David L. Jonesbe1557a2016-12-21 00:17:49 +00001476 BD->setIsVariadic(Record.readInt());
1477 BD->setBlockMissingReturnType(Record.readInt());
1478 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001479 BD->setDoesNotEscape(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001480
David L. Jonesbe1557a2016-12-21 00:17:49 +00001481 bool capturesCXXThis = Record.readInt();
1482 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001483 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001484 captures.reserve(numCaptures);
1485 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001486 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001487 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001488 bool byRef = (flags & 1);
1489 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001490 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001491
1492 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1493 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001494 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001495}
1496
Ben Langmuirce914fc2013-05-03 19:20:19 +00001497void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1498 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001499 unsigned ContextParamPos = Record.readInt();
1500 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001501 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001502 for (unsigned I = 0; I < CD->NumParams; ++I) {
1503 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001504 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001505 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001506 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001507 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001508}
1509
Sebastian Redlb3298c32010-08-18 23:56:48 +00001510void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001511 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001512 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001513 D->setExternLoc(ReadSourceLocation());
1514 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001515}
1516
Richard Smith8df390f2016-09-08 23:14:54 +00001517void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1518 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001519 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001520}
1521
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001522void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1523 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001524 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001525}
1526
Sebastian Redlb3298c32010-08-18 23:56:48 +00001527void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001528 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001529 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001530 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001531 D->LocStart = ReadSourceLocation();
1532 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001533
Richard Smith15e32fd2015-03-17 02:23:11 +00001534 // Defer loading the anonymous namespace until we've finished merging
1535 // this namespace; loading it might load a later declaration of the
1536 // same namespace, and we have an invariant that older declarations
1537 // get merged before newer ones try to merge.
1538 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001539 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001540 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001541 } else {
1542 // Link this namespace back to the first declaration, which has already
1543 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001544 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001545 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001546
1547 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001548
1549 if (AnonNamespace) {
1550 // Each module has its own anonymous namespace, which is disjoint from
1551 // any other module's anonymous namespaces, so don't attach the anonymous
1552 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001553 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001554 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001555 D->setAnonymousNamespace(Anon);
1556 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001557}
1558
Sebastian Redlb3298c32010-08-18 23:56:48 +00001559void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001560 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001561 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001562 D->NamespaceLoc = ReadSourceLocation();
1563 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001564 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001565 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001566 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001567}
1568
Sebastian Redlb3298c32010-08-18 23:56:48 +00001569void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001570 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001571 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001572 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001573 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1574 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001575 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001576 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001577 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001578 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001579}
1580
Richard Smith151c4562016-12-20 21:35:28 +00001581void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1582 VisitNamedDecl(D);
1583 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001584 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001585 for (unsigned I = 0; I != D->NumExpansions; ++I)
1586 Expansions[I] = ReadDeclAs<NamedDecl>();
1587 mergeMergeable(D);
1588}
1589
Sebastian Redlb3298c32010-08-18 23:56:48 +00001590void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001591 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001592 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001593 D->Underlying = ReadDeclAs<NamedDecl>();
1594 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001595 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001596 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001597 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001598 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001599 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001600}
1601
Richard Smith5179eb72016-06-28 19:03:57 +00001602void ASTDeclReader::VisitConstructorUsingShadowDecl(
1603 ConstructorUsingShadowDecl *D) {
1604 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001605 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1606 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001607 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001608}
1609
Sebastian Redlb3298c32010-08-18 23:56:48 +00001610void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001611 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001612 D->UsingLoc = ReadSourceLocation();
1613 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001614 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001615 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1616 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001617}
1618
Sebastian Redlb3298c32010-08-18 23:56:48 +00001619void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001620 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001621 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001622 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001623 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001624 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001625 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001626}
1627
Sebastian Redlb3298c32010-08-18 23:56:48 +00001628void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001629 UnresolvedUsingTypenameDecl *D) {
1630 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001631 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001632 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001633 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001634 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001635}
1636
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001637void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001638 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001639 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001640 Data.UserDeclaredConstructor = Record.readInt();
1641 Data.UserDeclaredSpecialMembers = Record.readInt();
1642 Data.Aggregate = Record.readInt();
1643 Data.PlainOldData = Record.readInt();
1644 Data.Empty = Record.readInt();
1645 Data.Polymorphic = Record.readInt();
1646 Data.Abstract = Record.readInt();
1647 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001648 Data.IsCXX11StandardLayout = Record.readInt();
1649 Data.HasBasesWithFields = Record.readInt();
1650 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001651 Data.HasPrivateFields = Record.readInt();
1652 Data.HasProtectedFields = Record.readInt();
1653 Data.HasPublicFields = Record.readInt();
1654 Data.HasMutableFields = Record.readInt();
1655 Data.HasVariantMembers = Record.readInt();
1656 Data.HasOnlyCMembers = Record.readInt();
1657 Data.HasInClassInitializer = Record.readInt();
1658 Data.HasUninitializedReferenceMember = Record.readInt();
1659 Data.HasUninitializedFields = Record.readInt();
1660 Data.HasInheritedConstructor = Record.readInt();
1661 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001662 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001663 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1664 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1665 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001666 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001667 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1668 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1669 Data.DefaultedDestructorIsDeleted = Record.readInt();
1670 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001671 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001672 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001673 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001674 Data.HasIrrelevantDestructor = Record.readInt();
1675 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1676 Data.HasDefaultedDefaultConstructor = Record.readInt();
1677 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1678 Data.HasConstexprDefaultConstructor = Record.readInt();
1679 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1680 Data.ComputedVisibleConversions = Record.readInt();
1681 Data.UserProvidedDefaultConstructor = Record.readInt();
1682 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001683 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1684 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001685 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1686 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1687 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001688 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001689 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001690
Richard Smithcd4a7a42017-09-07 00:55:55 +00001691 if (Record.readInt())
1692 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001693
David L. Jonesbe1557a2016-12-21 00:17:49 +00001694 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001695 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001696 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001697 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001698 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001699 Data.VBases = ReadGlobalOffset();
1700
David L. Jonesb6a8f022016-12-21 04:34:52 +00001701 Record.readUnresolvedSet(Data.Conversions);
1702 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001703 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001704 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001705
Douglas Gregor99ae8062012-02-14 17:54:36 +00001706 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001707 using Capture = LambdaCapture;
1708
1709 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001710 Lambda.Dependent = Record.readInt();
1711 Lambda.IsGenericLambda = Record.readInt();
1712 Lambda.CaptureDefault = Record.readInt();
1713 Lambda.NumCaptures = Record.readInt();
1714 Lambda.NumExplicitCaptures = Record.readInt();
1715 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001716 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001717 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1718 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001719 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001720 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001721 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001722 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001723 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001724 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001725 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001726 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001727 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001728 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001729 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001730 break;
1731 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001732 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001733 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001734 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001735 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1736 break;
1737 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001738 }
1739 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001740}
1741
Richard Smithcd45dbc2014-04-19 03:48:30 +00001742void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001743 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001744 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001745 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001746 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001747
Richard Smith02793752015-03-27 21:16:39 +00001748 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001749 // Track that we merged the definitions.
1750 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1751 DD.Definition));
1752 Reader.PendingDefinitions.erase(MergeDD.Definition);
Erich Keanef92f31c2018-08-01 20:48:16 +00001753 MergeDD.Definition->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +00001754 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001755 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1756 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001757 }
1758
Richard Smith2a9e5c52015-02-03 03:32:14 +00001759 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1760 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1761 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001762 // We faked up this definition data because we found a class for which we'd
1763 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001764 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001765 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001766
1767 // Don't change which declaration is the definition; that is required
1768 // to be invariant once we select it.
1769 auto *Def = DD.Definition;
1770 DD = std::move(MergeDD);
1771 DD.Definition = Def;
1772 return;
1773 }
1774
Richard Smithcd45dbc2014-04-19 03:48:30 +00001775 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001776 bool DetectedOdrViolation = false;
1777#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1778#define MATCH_FIELD(Field) \
1779 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1780 OR_FIELD(Field)
1781 MATCH_FIELD(UserDeclaredConstructor)
1782 MATCH_FIELD(UserDeclaredSpecialMembers)
1783 MATCH_FIELD(Aggregate)
1784 MATCH_FIELD(PlainOldData)
1785 MATCH_FIELD(Empty)
1786 MATCH_FIELD(Polymorphic)
1787 MATCH_FIELD(Abstract)
1788 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001789 MATCH_FIELD(IsCXX11StandardLayout)
1790 MATCH_FIELD(HasBasesWithFields)
1791 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001792 MATCH_FIELD(HasPrivateFields)
1793 MATCH_FIELD(HasProtectedFields)
1794 MATCH_FIELD(HasPublicFields)
1795 MATCH_FIELD(HasMutableFields)
1796 MATCH_FIELD(HasVariantMembers)
1797 MATCH_FIELD(HasOnlyCMembers)
1798 MATCH_FIELD(HasInClassInitializer)
1799 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001800 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001801 MATCH_FIELD(HasInheritedConstructor)
1802 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001803 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001804 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1805 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1806 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001807 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001808 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1809 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1810 MATCH_FIELD(DefaultedDestructorIsDeleted)
1811 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001812 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001813 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001814 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001815 MATCH_FIELD(HasIrrelevantDestructor)
1816 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001817 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001818 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1819 OR_FIELD(HasConstexprDefaultConstructor)
1820 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1821 // ComputedVisibleConversions is handled below.
1822 MATCH_FIELD(UserProvidedDefaultConstructor)
1823 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001824 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1825 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001826 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1827 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1828 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1829 MATCH_FIELD(IsLambda)
1830#undef OR_FIELD
1831#undef MATCH_FIELD
1832
1833 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1834 DetectedOdrViolation = true;
1835 // FIXME: Issue a diagnostic if the base classes don't match when we come
1836 // to lazily load them.
1837
1838 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1839 // match when we come to lazily load them.
1840 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1841 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1842 DD.ComputedVisibleConversions = true;
1843 }
1844
1845 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1846 // lazily load it.
1847
1848 if (DD.IsLambda) {
1849 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1850 // when they occur within the body of a function template specialization).
1851 }
1852
Richard Trieufd1acbb2017-04-11 21:31:00 +00001853 if (D->getODRHash() != MergeDD.ODRHash) {
1854 DetectedOdrViolation = true;
1855 }
1856
Richard Smithcd45dbc2014-04-19 03:48:30 +00001857 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001858 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1859 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001860}
1861
Richard Smith2a9e5c52015-02-03 03:32:14 +00001862void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001863 struct CXXRecordDecl::DefinitionData *DD;
1864 ASTContext &C = Reader.getContext();
1865
1866 // Determine whether this is a lambda closure type, so that we can
1867 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001868 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001869 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001870 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001871 LCD_None);
1872 else
1873 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1874
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001875 CXXRecordDecl *Canon = D->getCanonicalDecl();
1876 // Set decl definition data before reading it, so that during deserialization
1877 // when we read CXXRecordDecl, it already has definition data and we don't
1878 // set fake one.
1879 if (!Canon->DefinitionData)
1880 Canon->DefinitionData = DD;
1881 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001882 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001883
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001884 // We might already have a different definition for this record. This can
1885 // happen either because we're reading an update record, or because we've
1886 // already done some merging. Either way, just merge into it.
1887 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001888 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001889 return;
1890 }
1891
Richard Smith97100e32015-06-20 00:22:34 +00001892 // Mark this declaration as being a definition.
Erich Keanef92f31c2018-08-01 20:48:16 +00001893 D->setCompleteDefinition(true);
Richard Smith2a9e5c52015-02-03 03:32:14 +00001894
Richard Smith97100e32015-06-20 00:22:34 +00001895 // If this is not the first declaration or is an update record, we can have
1896 // other redeclarations already. Make a note that we need to propagate the
1897 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001898 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001899 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001900}
1901
Richard Smith1d209d02013-05-23 01:49:11 +00001902ASTDeclReader::RedeclarableResult
1903ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1904 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001905
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001906 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001907
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001908 enum CXXRecKind {
1909 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1910 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001911 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001912 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001913 // Merged when we merge the folding set entry in the primary template.
1914 if (!isa<ClassTemplateSpecializationDecl>(D))
1915 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001916 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001917 case CXXRecTemplate: {
1918 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001919 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001920 D->TemplateOrInstantiation = Template;
1921 if (!Template->getTemplatedDecl()) {
1922 // We've not actually loaded the ClassTemplateDecl yet, because we're
1923 // currently being loaded as its pattern. Rely on it to set up our
1924 // TypeForDecl (see VisitClassTemplateDecl).
1925 //
1926 // Beware: we do not yet know our canonical declaration, and may still
1927 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001928 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001929 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001930 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001931 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001932 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001933 auto *RD = ReadDeclAs<CXXRecordDecl>();
1934 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001935 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001936 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1937 MSI->setPointOfInstantiation(POI);
1938 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001939 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001940 break;
1941 }
1942 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001943
David L. Jonesbe1557a2016-12-21 00:17:49 +00001944 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001945 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001946 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001947 else
1948 // Propagate DefinitionData pointer from the canonical declaration.
1949 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1950
Richard Smith676c4042013-08-29 23:59:27 +00001951 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001952 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001953 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001954 DeclID KeyFn = ReadDeclID();
Erich Keanef92f31c2018-08-01 20:48:16 +00001955 if (KeyFn && D->isCompleteDefinition())
Richard Smitha9a1c682014-07-07 06:38:20 +00001956 // FIXME: This is wrong for the ARM ABI, where some other module may have
1957 // made this function no longer be a key function. We need an update
1958 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001959 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001960 }
Richard Smith1d209d02013-05-23 01:49:11 +00001961
1962 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001963}
1964
Richard Smithbc491202017-02-17 20:05:37 +00001965void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1966 VisitFunctionDecl(D);
Erich Keane9c665062018-08-01 21:02:40 +00001967 D->setIsCopyDeductionCandidate(Record.readInt());
Richard Smithbc491202017-02-17 20:05:37 +00001968}
1969
Sebastian Redlb3298c32010-08-18 23:56:48 +00001970void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001971 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001972
David L. Jonesbe1557a2016-12-21 00:17:49 +00001973 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001974 if (D->isCanonicalDecl()) {
1975 while (NumOverridenMethods--) {
1976 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1977 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001978 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001979 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1980 }
1981 } else {
1982 // We don't care about which declarations this used to override; we get
1983 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001984 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001985 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001986}
1987
Sebastian Redlb3298c32010-08-18 23:56:48 +00001988void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001989 // We need the inherited constructor information to merge the declaration,
1990 // so we have to read it before we call VisitCXXMethodDecl.
1991 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001992 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1993 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001994 *D->getTrailingObjects<InheritedConstructor>() =
1995 InheritedConstructor(Shadow, Ctor);
1996 }
1997
Chris Lattnerca025db2010-05-07 21:43:38 +00001998 VisitCXXMethodDecl(D);
1999}
2000
Sebastian Redlb3298c32010-08-18 23:56:48 +00002001void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002002 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00002003
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002004 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002005 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00002006 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00002007 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00002008 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00002009 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00002010 Canon->OperatorDeleteThisArg = ThisArg;
2011 }
Richard Smithf8134002015-03-10 01:41:22 +00002012 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002013}
2014
Sebastian Redlb3298c32010-08-18 23:56:48 +00002015void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002016 VisitCXXMethodDecl(D);
2017}
2018
Douglas Gregorba345522011-12-02 23:23:56 +00002019void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2020 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002021 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002022 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002023 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002024 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002025 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002026 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002027}
2028
Sebastian Redlb3298c32010-08-18 23:56:48 +00002029void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002030 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002031 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002032}
2033
Sebastian Redlb3298c32010-08-18 23:56:48 +00002034void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002035 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002036 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002037 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002038 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002039 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002040 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002041 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002042 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002043 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002044 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002045 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002046}
2047
Sebastian Redlb3298c32010-08-18 23:56:48 +00002048void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002049 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002050 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002051 D->NumParams = NumParams;
2052 D->Params = new TemplateParameterList*[NumParams];
2053 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002054 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002055 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002056 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002057 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002058 D->Friend = GetTypeSourceInfo();
2059 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002060}
2061
Richard Smithf17fdbd2014-04-24 02:25:27 +00002062DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002063 VisitNamedDecl(D);
2064
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002065 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002066 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002067 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002068 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002069 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002070
Richard Smithf17fdbd2014-04-24 02:25:27 +00002071 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002072}
2073
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002074ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002075ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002076 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002077
Douglas Gregor68444de2012-01-14 15:13:49 +00002078 // Make sure we've allocated the Common pointer first. We do this before
2079 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2080 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2081 if (!CanonD->Common) {
2082 CanonD->Common = CanonD->newCommon(Reader.getContext());
2083 Reader.PendingDefinitions.insert(CanonD);
2084 }
2085 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002086
Douglas Gregor68444de2012-01-14 15:13:49 +00002087 // If this is the first declaration of the template, fill in the information
2088 // for the 'common' pointer.
2089 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002090 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002091 assert(RTD->getKind() == D->getKind() &&
2092 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002093 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002094 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002095 D->setMemberSpecialization();
2096 }
2097 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002098
Richard Smithf17fdbd2014-04-24 02:25:27 +00002099 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002100 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002101
Richard Smithf17fdbd2014-04-24 02:25:27 +00002102 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002103
Richard Smithd46d6de2013-10-13 23:50:45 +00002104 // If we merged the template with a prior declaration chain, merge the common
2105 // pointer.
2106 // FIXME: Actually merge here, don't just overwrite.
2107 D->Common = D->getCanonicalDecl()->Common;
2108
Douglas Gregor68444de2012-01-14 15:13:49 +00002109 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002110}
2111
Sebastian Redlb3298c32010-08-18 23:56:48 +00002112void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002113 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002114
Douglas Gregor68444de2012-01-14 15:13:49 +00002115 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002116 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2117 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002118 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002119 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002120 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002121 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002122
2123 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2124 // We were loaded before our templated declaration was. We've not set up
2125 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2126 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002127 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002128 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002129 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002130}
2131
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002132void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2133 llvm_unreachable("BuiltinTemplates are not serialized");
2134}
2135
Larisse Voufo30616382013-08-23 22:21:36 +00002136/// TODO: Unify with ClassTemplateDecl version?
2137/// May require unifying ClassTemplateDecl and
2138/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002139void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2140 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2141
2142 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002143 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002144 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002145 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002146 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002147 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002148 }
2149}
2150
Richard Smith1d209d02013-05-23 01:49:11 +00002151ASTDeclReader::RedeclarableResult
2152ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2153 ClassTemplateSpecializationDecl *D) {
2154 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002155
Douglas Gregor4163aca2011-09-09 21:34:22 +00002156 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002157 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002158 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002159 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002160 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002161 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002162 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002163 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002164 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002165 auto *PS =
2166 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002167 SpecializedPartialSpecialization();
2168 PS->PartialSpecialization
2169 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2170 PS->TemplateArgs = ArgList;
2171 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002172 }
2173 }
2174
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002175 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002176 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002177 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002178 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002179 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002180
David L. Jonesbe1557a2016-12-21 00:17:49 +00002181 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002182 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002183 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002184 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002185 // Set this as, or find, the canonical declaration for this specialization
2186 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002187 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002188 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002189 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002190 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002191 CanonSpec =
2192 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2193 }
2194 // If there was already a canonical specialization, merge into it.
2195 if (CanonSpec != D) {
2196 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2197
2198 // This declaration might be a definition. Merge with any existing
2199 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002200 if (auto *DDD = D->DefinitionData) {
2201 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002202 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002203 else
Richard Smith053f6c62014-05-16 23:01:30 +00002204 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002205 }
Richard Smith547864d2014-07-11 18:22:58 +00002206 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002207 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002208 }
2209 }
Richard Smith1d209d02013-05-23 01:49:11 +00002210
Richard Smithd55889a2013-09-09 16:55:27 +00002211 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002212 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002213 auto *ExplicitInfo =
2214 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002215 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002216 ExplicitInfo->ExternLoc = ReadSourceLocation();
2217 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002218 D->ExplicitInfo = ExplicitInfo;
2219 }
2220
Richard Smith1d209d02013-05-23 01:49:11 +00002221 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002222}
2223
Sebastian Redlb3298c32010-08-18 23:56:48 +00002224void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002225 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002226 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002227
David L. Jonesb6a8f022016-12-21 04:34:52 +00002228 D->TemplateParams = Record.readTemplateParameterList();
2229 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002230
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002231 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002232 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002233 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002234 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002235 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002236 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002237}
2238
Sebastian Redlb7448632011-08-31 13:59:56 +00002239void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2240 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002241 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002242 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002243}
2244
Sebastian Redlb3298c32010-08-18 23:56:48 +00002245void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002246 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002247
Douglas Gregor68444de2012-01-14 15:13:49 +00002248 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002249 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002250 SmallVector<serialization::DeclID, 32> SpecIDs;
2251 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002252 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002253 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002254}
2255
Larisse Voufo30616382013-08-23 22:21:36 +00002256/// TODO: Unify with ClassTemplateSpecializationDecl version?
2257/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2258/// VarTemplate(Partial)SpecializationDecl with a new data
2259/// structure Template(Partial)SpecializationDecl, and
2260/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002261ASTDeclReader::RedeclarableResult
2262ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2263 VarTemplateSpecializationDecl *D) {
2264 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2265
2266 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002267 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002268 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002269 D->SpecializedTemplate = VTD;
2270 } else {
2271 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002272 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002273 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002274 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002275 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002276 new (C)
2277 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2278 PS->PartialSpecialization =
2279 cast<VarTemplatePartialSpecializationDecl>(InstD);
2280 PS->TemplateArgs = ArgList;
2281 D->SpecializedTemplate = PS;
2282 }
2283 }
2284
2285 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002286 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002287 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002288 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2289 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002290 ExplicitInfo->ExternLoc = ReadSourceLocation();
2291 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002292 D->ExplicitInfo = ExplicitInfo;
2293 }
2294
2295 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002296 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002297 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002298 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002299 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002300 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002301
David L. Jonesbe1557a2016-12-21 00:17:49 +00002302 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002303 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002304 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002305 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002306 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002307 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002308 CanonPattern->getCommonPtr()->PartialSpecializations
2309 .GetOrInsertNode(Partial);
2310 } else {
2311 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2312 }
2313 }
2314 }
2315
2316 return Redecl;
2317}
2318
Larisse Voufo30616382013-08-23 22:21:36 +00002319/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2320/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2321/// VarTemplate(Partial)SpecializationDecl with a new data
2322/// structure Template(Partial)SpecializationDecl, and
2323/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002324void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2325 VarTemplatePartialSpecializationDecl *D) {
2326 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2327
David L. Jonesb6a8f022016-12-21 04:34:52 +00002328 D->TemplateParams = Record.readTemplateParameterList();
2329 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002330
2331 // These are read/set from/to the first declaration.
2332 if (ThisDeclID == Redecl.getFirstID()) {
2333 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002334 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002335 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002336 }
2337}
2338
Sebastian Redlb3298c32010-08-18 23:56:48 +00002339void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002340 VisitTypeDecl(D);
2341
David L. Jonesbe1557a2016-12-21 00:17:49 +00002342 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002343
David L. Jonesbe1557a2016-12-21 00:17:49 +00002344 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002345 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002346}
2347
Sebastian Redlb3298c32010-08-18 23:56:48 +00002348void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002349 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002350 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002351 D->setDepth(Record.readInt());
2352 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002353 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002354 auto TypesAndInfos =
2355 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002356 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002357 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002358 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002359 }
2360 } else {
2361 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002362 D->ParameterPack = Record.readInt();
2363 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002364 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002365 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002366}
2367
Sebastian Redlb3298c32010-08-18 23:56:48 +00002368void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002369 VisitTemplateDecl(D);
2370 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002371 D->setDepth(Record.readInt());
2372 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002373 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002374 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002375 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2376 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002377 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002378 } else {
2379 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002380 D->ParameterPack = Record.readInt();
2381 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002382 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002383 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002384 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002385}
2386
Richard Smith3f1b5d02011-05-05 21:57:07 +00002387void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2388 VisitRedeclarableTemplateDecl(D);
2389}
2390
Sebastian Redlb3298c32010-08-18 23:56:48 +00002391void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002392 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002393 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002394 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002395 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002396 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002397}
2398
Michael Han84324352013-02-22 17:15:32 +00002399void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2400 VisitDecl(D);
2401}
2402
Mike Stump11289f42009-09-09 15:08:12 +00002403std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002404ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002405 uint64_t LexicalOffset = ReadLocalOffset();
2406 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002407 return std::make_pair(LexicalOffset, VisibleOffset);
2408}
2409
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002410template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002411ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002412ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002413 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002414 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002415
Richard Smith5fc18a92015-07-12 23:43:21 +00002416 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002417 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002418
Richard Smithd61d4ac2015-08-22 20:13:39 +00002419 uint64_t RedeclOffset = 0;
2420
Douglas Gregor358cd442012-01-15 16:58:34 +00002421 // 0 indicates that this declaration was the only declaration of its entity,
2422 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002423 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002424 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002425 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002426 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002427 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002428 // This declaration was the first local declaration, but may have imported
2429 // other declarations.
2430 IsKeyDecl = N == 1;
2431 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002432
Richard Smithfe620d22015-03-05 23:24:12 +00002433 // We have some declarations that must be before us in our redeclaration
2434 // chain. Read them now, and remember that we ought to merge with one of
2435 // them.
2436 // FIXME: Provide a known merge target to the second and subsequent such
2437 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002438 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002439 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002440
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002441 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002442 } else {
2443 // This declaration was not the first local declaration. Read the first
2444 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002445 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002446 }
2447
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002448 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002449 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002450 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2451 // We temporarily set the first (canonical) declaration as the previous one
2452 // which is the one that matters and mark the real previous DeclID to be
2453 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002454 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002455 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002456 }
Richard Smithd8a83712015-08-22 01:47:18 +00002457
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002458 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002459
2460 // Note that we need to load local redeclarations of this decl and build a
2461 // decl chain for them. This must happen *after* we perform the preloading
2462 // above; this ensures that the redeclaration chain is built in the correct
2463 // order.
2464 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002465 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002466
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002467 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002468}
2469
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002470/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002471/// of the same entity.
2472template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002473void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002474 RedeclarableResult &Redecl,
2475 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002476 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002477 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002478 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002479
Richard Smith202850a2015-03-10 02:57:50 +00002480 // If we're not the canonical declaration, we don't need to merge.
2481 if (!DBase->isFirstDecl())
2482 return;
2483
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002484 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002485
Richard Smith5a4737c2015-02-06 02:42:59 +00002486 if (auto *Existing = Redecl.getKnownMergeTarget())
2487 // We already know of an existing declaration we should merge with.
2488 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2489 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002490 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002491 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2492}
2493
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002494/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002495/// We use this to put code in a template that will only be valid for certain
2496/// instantiations.
2497template<typename T> static T assert_cast(T t) { return t; }
2498template<typename T> static T assert_cast(...) {
2499 llvm_unreachable("bad assert_cast");
2500}
2501
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002502/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002503/// declarations.
2504void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2505 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002506 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002507 auto *DPattern = D->getTemplatedDecl();
2508 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002509 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2510 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002511 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002512
Richard Smith547864d2014-07-11 18:22:58 +00002513 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2514 // Merge with any existing definition.
2515 // FIXME: This is duplicated in several places. Refactor.
2516 auto *ExistingClass =
2517 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002518 if (auto *DDD = DClass->DefinitionData) {
2519 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002520 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002521 } else {
2522 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002523 // We may have skipped this before because we thought that DClass
2524 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002525 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002526 }
2527 }
2528 DClass->DefinitionData = ExistingClass->DefinitionData;
2529
Richard Smithf17fdbd2014-04-24 02:25:27 +00002530 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2531 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002532 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002533 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2534 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2535 Result);
2536 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2537 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002538 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2539 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2540 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002541 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002542}
2543
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002544/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002545/// of the same entity.
2546template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002547void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2548 RedeclarableResult &Redecl,
2549 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002550 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002551 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002552 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002553 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002554 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2555 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002556
Richard Smithd55889a2013-09-09 16:55:27 +00002557 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002558 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002559 // appropriate canonical declaration.
2560 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002561 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002562 ExistingCanon->Used |= D->Used;
2563 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002564
2565 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002566 // We cannot have loaded any redeclarations of this declaration yet, so
2567 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002568 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002569 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002570 assert_cast<NamespaceDecl*>(ExistingCanon));
2571
2572 // When we merge a template, merge its pattern.
2573 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2574 mergeTemplatePattern(
2575 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002576 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002577
Richard Smith5fc18a92015-07-12 23:43:21 +00002578 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002579 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002580 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002581 }
2582}
2583
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002584/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2585/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2586/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2587/// that some types are mergeable during deserialization, otherwise name
2588/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002589static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002590 if (!ND)
2591 return false;
2592 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002593 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002594 return true;
2595 return false;
2596}
2597
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002598/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002599/// of the same entity, for the case where the entity is not actually
2600/// redeclarable. This happens, for instance, when merging the fields of
2601/// identical class definitions from two different modules.
2602template<typename T>
2603void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2604 // If modules are not available, there is no reason to perform this merge.
2605 if (!Reader.getContext().getLangOpts().Modules)
2606 return;
2607
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002608 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2609 // Note that C identically-named things in different translation units are
2610 // not redeclarations, but may still have compatible types, where ODR-like
2611 // semantics may apply.
2612 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2613 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002614 return;
2615
Richard Smith0b87e072013-10-07 08:02:11 +00002616 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2617 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002618 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2619 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002620}
2621
Alexey Bataeva769e072013-03-22 06:34:35 +00002622void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2623 VisitDecl(D);
2624 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002625 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002626 Vars.reserve(NumVars);
2627 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002628 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002629 }
2630 D->setVars(Vars);
2631}
2632
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002633void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002634 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002635 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002636 D->setCombiner(Record.readExpr());
Alexey Bataev070f43a2017-09-06 14:49:58 +00002637 D->setInitializer(
2638 Record.readExpr(),
2639 static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002640 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002641}
2642
Alexey Bataev4244be22016-02-11 05:35:55 +00002643void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002644 VisitVarDecl(D);
2645}
2646
Chris Lattner487412d2009-04-27 05:27:42 +00002647//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002648// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002649//===----------------------------------------------------------------------===//
2650
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00002651/// Reads attributes from the current stream position.
2652void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2653 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
2654 Attr *New = nullptr;
2655 auto Kind = (attr::Kind)Record.readInt();
2656 SourceRange Range = Record.readSourceRange();
2657 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002658
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002659#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002660
Reid Kleckner11f9f8a2018-08-14 01:55:37 +00002661 assert(New && "Unable to decode attribute?");
2662 Attrs.push_back(New);
2663 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002664}
2665
2666//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002667// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002668//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002669
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002670/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002671/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002672///
Chris Lattner487412d2009-04-27 05:27:42 +00002673/// This routine notes that this declaration has already been loaded,
2674/// so that future GetDecl calls will return this declaration rather
2675/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002676inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002677 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2678 DeclsLoaded[Index] = D;
2679}
2680
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002681/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002682/// this declaration (via HandleTopLevelDecl).
2683///
2684/// This routine should return true for anything that might affect
2685/// code generation, e.g., inline function definitions, Objective-C
2686/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002687static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002688 // An ObjCMethodDecl is never considered as "interesting" because its
2689 // implementation container always is.
2690
Richard Smithcd4a7a42017-09-07 00:55:55 +00002691 // An ImportDecl or VarDecl imported from a module map module will get
2692 // emitted when we import the relevant module.
2693 if (isa<ImportDecl>(D) || isa<VarDecl>(D)) {
2694 auto *M = D->getImportedOwningModule();
2695 if (M && M->Kind == Module::ModuleMapModule &&
2696 Ctx.DeclMustBeEmitted(D))
2697 return false;
2698 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002699
Fangrui Song6907ce22018-07-30 19:24:48 +00002700 if (isa<FileScopeAsmDecl>(D) ||
2701 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002702 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002703 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002704 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002705 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002706 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002707 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2708 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002709 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002710 return Var->isFileVarDecl() &&
Alexey Bataevd01b7492018-08-15 19:45:12 +00002711 (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
2712 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002713 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002714 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002715
2716 if (auto *ES = D->getASTContext().getExternalSource())
2717 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2718 return true;
2719
Douglas Gregor87d81242011-09-10 00:22:34 +00002720 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002721}
2722
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002723/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002724ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002725ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002726 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2727 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002728 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002729 const DeclOffset &DOffs =
2730 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002731 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002732 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002733}
2734
Douglas Gregord32f0352011-07-22 06:10:01 +00002735ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002736 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002737
2738 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2739 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2740}
2741
Douglas Gregorde3ef502011-11-30 23:21:26 +00002742uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002743 return LocalOffset + M.GlobalBitOffset;
2744}
2745
Richard Smithbf78e642013-06-24 22:51:00 +00002746static bool isSameTemplateParameterList(const TemplateParameterList *X,
2747 const TemplateParameterList *Y);
2748
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002749/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002750/// that they may be used in declarations of the same template.
2751static bool isSameTemplateParameter(const NamedDecl *X,
2752 const NamedDecl *Y) {
2753 if (X->getKind() != Y->getKind())
2754 return false;
2755
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002756 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2757 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002758 return TX->isParameterPack() == TY->isParameterPack();
2759 }
2760
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002761 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2762 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002763 return TX->isParameterPack() == TY->isParameterPack() &&
2764 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2765 }
2766
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002767 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2768 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002769 return TX->isParameterPack() == TY->isParameterPack() &&
2770 isSameTemplateParameterList(TX->getTemplateParameters(),
2771 TY->getTemplateParameters());
2772}
2773
Richard Smith32952e12014-10-14 02:00:47 +00002774static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2775 if (auto *NS = X->getAsNamespace())
2776 return NS;
2777 if (auto *NAS = X->getAsNamespaceAlias())
2778 return NAS->getNamespace();
2779 return nullptr;
2780}
2781
2782static bool isSameQualifier(const NestedNameSpecifier *X,
2783 const NestedNameSpecifier *Y) {
2784 if (auto *NSX = getNamespace(X)) {
2785 auto *NSY = getNamespace(Y);
2786 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2787 return false;
2788 } else if (X->getKind() != Y->getKind())
2789 return false;
2790
2791 // FIXME: For namespaces and types, we're permitted to check that the entity
2792 // is named via the same tokens. We should probably do so.
2793 switch (X->getKind()) {
2794 case NestedNameSpecifier::Identifier:
2795 if (X->getAsIdentifier() != Y->getAsIdentifier())
2796 return false;
2797 break;
2798 case NestedNameSpecifier::Namespace:
2799 case NestedNameSpecifier::NamespaceAlias:
2800 // We've already checked that we named the same namespace.
2801 break;
2802 case NestedNameSpecifier::TypeSpec:
2803 case NestedNameSpecifier::TypeSpecWithTemplate:
2804 if (X->getAsType()->getCanonicalTypeInternal() !=
2805 Y->getAsType()->getCanonicalTypeInternal())
2806 return false;
2807 break;
2808 case NestedNameSpecifier::Global:
2809 case NestedNameSpecifier::Super:
2810 return true;
2811 }
2812
2813 // Recurse into earlier portion of NNS, if any.
2814 auto *PX = X->getPrefix();
2815 auto *PY = Y->getPrefix();
2816 if (PX && PY)
2817 return isSameQualifier(PX, PY);
2818 return !PX && !PY;
2819}
2820
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002821/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002822/// that they may be used in declarations of the same template.
2823static bool isSameTemplateParameterList(const TemplateParameterList *X,
2824 const TemplateParameterList *Y) {
2825 if (X->size() != Y->size())
2826 return false;
2827
2828 for (unsigned I = 0, N = X->size(); I != N; ++I)
2829 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2830 return false;
2831
2832 return true;
2833}
2834
George Burgess IV95845082017-02-15 22:43:27 +00002835/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002836/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002837static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2838 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002839 // Note that pass_object_size attributes are represented in the function's
2840 // ExtParameterInfo, so we don't need to check them here.
2841
Michael Krusedc5ce722018-08-03 01:21:16 +00002842 // Return false if any of the enable_if expressions of A and B are different.
George Burgess IV95845082017-02-15 22:43:27 +00002843 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00002844 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
2845 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
2846 auto AEnableIf = AEnableIfAttrs.begin();
2847 auto BEnableIf = BEnableIfAttrs.begin();
2848 for (; AEnableIf != AEnableIfAttrs.end() && BEnableIf != BEnableIfAttrs.end();
2849 ++BEnableIf, ++AEnableIf) {
George Burgess IV95845082017-02-15 22:43:27 +00002850 Cand1ID.clear();
2851 Cand2ID.clear();
2852
Michael Krusedc5ce722018-08-03 01:21:16 +00002853 AEnableIf->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2854 BEnableIf->getCond()->Profile(Cand2ID, B->getASTContext(), true);
George Burgess IV95845082017-02-15 22:43:27 +00002855 if (Cand1ID != Cand2ID)
2856 return false;
2857 }
2858
Michael Krusedc5ce722018-08-03 01:21:16 +00002859 // Return false if the number of enable_if attributes was different.
2860 return AEnableIf == AEnableIfAttrs.end() && BEnableIf == BEnableIfAttrs.end();
George Burgess IV95845082017-02-15 22:43:27 +00002861}
2862
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002863/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002864static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2865 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002866
Douglas Gregor022857e2011-12-22 01:48:48 +00002867 if (X == Y)
2868 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002869
Douglas Gregor022857e2011-12-22 01:48:48 +00002870 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00002871 //
2872 // Note that we can't use DeclContext::Equals here, because the DeclContexts
2873 // could be two different declarations of the same function. (We will fix the
2874 // semantic DC to refer to the primary definition after merging.)
2875 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
2876 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00002877 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002878
2879 // Two typedefs refer to the same entity if they have the same underlying
2880 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002881 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
2882 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002883 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2884 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002885
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002886 // Must have the same kind.
2887 if (X->getKind() != Y->getKind())
2888 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002889
Douglas Gregorda389302012-01-01 21:47:52 +00002890 // Objective-C classes and protocols with the same name always match.
2891 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002892 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002893
2894 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002895 // No need to handle these here: we merge them when adding them to the
2896 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002897 return false;
2898 }
Richard Smithd55889a2013-09-09 16:55:27 +00002899
Douglas Gregor2009cee2012-01-03 22:46:00 +00002900 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002901 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
2902 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00002903 return (TagX->getTagKind() == TagY->getTagKind()) ||
2904 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2905 TagX->getTagKind() == TTK_Interface) &&
2906 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2907 TagY->getTagKind() == TTK_Interface));
2908 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002909
Joao Matose9a3ed42012-08-31 22:18:20 +00002910 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002911 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2912 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002913 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
2914 const auto *FuncY = cast<FunctionDecl>(Y);
2915 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2916 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002917 if (CtorX->getInheritedConstructor() &&
2918 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2919 CtorY->getInheritedConstructor().getConstructor()))
2920 return false;
2921 }
Erich Keane281d20b2018-01-08 21:34:17 +00002922
2923 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
2924 return false;
2925
2926 // Multiversioned functions with different feature strings are represented
2927 // as separate declarations.
2928 if (FuncX->isMultiVersion()) {
2929 const auto *TAX = FuncX->getAttr<TargetAttr>();
2930 const auto *TAY = FuncY->getAttr<TargetAttr>();
2931 assert(TAX && TAY && "Multiversion Function without target attribute");
2932
2933 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
2934 return false;
2935 }
2936
Richard Smitha54d3242017-03-08 23:00:26 +00002937 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00002938 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
2939 // Map to the first declaration that we've already merged into this one.
2940 // The TSI of redeclarations might not match (due to calling conventions
2941 // being inherited onto the type but not the TSI), but the TSI type of
2942 // the first declaration of the function should match across modules.
2943 FD = FD->getCanonicalDecl();
2944 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
2945 : FD->getType();
2946 };
2947 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
2948 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00002949 // We can get functions with different types on the redecl chain in C++17
2950 // if they have differing exception specifications and at least one of
2951 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00002952 auto *XFPT = XT->getAs<FunctionProtoType>();
2953 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002954 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00002955 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2956 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00002957 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00002958 return true;
2959 return false;
2960 }
George Burgess IV95845082017-02-15 22:43:27 +00002961 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002962 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002963 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002964
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002965 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002966 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
2967 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002968 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2969 ASTContext &C = VarX->getASTContext();
2970 if (C.hasSameType(VarX->getType(), VarY->getType()))
2971 return true;
2972
2973 // We can get decls with different types on the redecl chain. Eg.
2974 // template <typename T> struct S { static T Var[]; }; // #1
2975 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2976 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002977 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002978 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2979 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2980 if (!VarXTy || !VarYTy)
2981 return false;
2982 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2983 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2984 }
2985 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002986 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002987
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002988 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002989 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2990 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002991 return NamespaceX->isInline() == NamespaceY->isInline();
2992 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002993
Richard Smith8f8f05c2013-06-24 04:45:28 +00002994 // Identical template names and kinds match if their template parameter lists
2995 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002996 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
2997 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002998 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002999 TemplateY->getTemplatedDecl()) &&
3000 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3001 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003002 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003003
Richard Smith0b87e072013-10-07 08:02:11 +00003004 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003005 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3006 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003007 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003008 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3009 }
3010
Richard Smith8cbd8952015-08-04 02:05:09 +00003011 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003012 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3013 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003014 return IFDX->getAnonField()->getCanonicalDecl() ==
3015 IFDY->getAnonField()->getCanonicalDecl();
3016 }
3017
Richard Smith01a73372013-10-15 22:02:41 +00003018 // Enumerators with the same name match.
3019 if (isa<EnumConstantDecl>(X))
3020 // FIXME: Also check the value is odr-equivalent.
3021 return true;
3022
Richard Smithfd8634a2013-10-23 02:17:46 +00003023 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003024 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3025 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003026 return USX->getTargetDecl() == USY->getTargetDecl();
3027 }
3028
Richard Smith32952e12014-10-14 02:00:47 +00003029 // Using declarations with the same qualifier match. (We already know that
3030 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003031 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3032 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003033 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3034 UX->hasTypename() == UY->hasTypename() &&
3035 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3036 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003037 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3038 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003039 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3040 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3041 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003042 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003043 return isSameQualifier(
3044 UX->getQualifier(),
3045 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3046
Richard Smithf4634362014-09-03 23:11:22 +00003047 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003048 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3049 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003050 return NAX->getNamespace()->Equals(NAY->getNamespace());
3051 }
3052
Douglas Gregor022857e2011-12-22 01:48:48 +00003053 return false;
3054}
3055
Richard Smithd55889a2013-09-09 16:55:27 +00003056/// Find the context in which we should search for previous declarations when
3057/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003058DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3059 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003060 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003061 return ND->getOriginalNamespace();
3062
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003063 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003064 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003065 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003066 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003067 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003068
3069 // If there's no definition yet, then DC's definition is added by an update
3070 // record, but we've not yet loaded that update record. In this case, we
3071 // commit to DC being the canonical definition now, and will fix this when
3072 // we load the update record.
3073 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003074 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003075 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003076 RD->DefinitionData = DD;
3077 RD->getCanonicalDecl()->DefinitionData = DD;
3078
3079 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003080 Reader.PendingFakeDefinitionData.insert(
3081 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003082 }
3083
3084 return DD->Definition;
3085 }
Richard Smithd55889a2013-09-09 16:55:27 +00003086
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003087 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003088 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3089 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003090
Richard Smith4eca9b92015-02-04 23:37:59 +00003091 // We can see the TU here only if we have no Sema object. In that case,
3092 // there's no TU scope to look in, so using the DC alone is sufficient.
3093 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3094 return TU;
3095
Craig Toppera13603a2014-05-22 05:54:18 +00003096 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003097}
3098
Douglas Gregor022857e2011-12-22 01:48:48 +00003099ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003100 // Record that we had a typedef name for linkage whether or not we merge
3101 // with that declaration.
3102 if (TypedefNameForLinkage) {
3103 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3104 Reader.ImportedTypedefNamesForLinkage.insert(
3105 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3106 return;
3107 }
3108
Douglas Gregor9b47f942012-01-09 17:38:47 +00003109 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003110 return;
Richard Smith95d99302013-07-13 02:00:19 +00003111
Richard Smith70d58502014-08-30 00:04:23 +00003112 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003113 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003114 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003115 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3116 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003117 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003118 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003119 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003120 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3121 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003122 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003123 // Add the declaration to its redeclaration context so later merging
3124 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003125 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003126 }
3127}
3128
Richard Smith88ebade2014-08-23 01:45:27 +00003129/// Find the declaration that should be merged into, given the declaration found
3130/// by name lookup. If we're merging an anonymous declaration within a typedef,
3131/// we need a matching typedef, and we merge with the type inside it.
3132static NamedDecl *getDeclForMerging(NamedDecl *Found,
3133 bool IsTypedefNameForLinkage) {
3134 if (!IsTypedefNameForLinkage)
3135 return Found;
3136
3137 // If we found a typedef declaration that gives a name to some other
3138 // declaration, then we want that inner declaration. Declarations from
3139 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003140 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003141 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003142
3143 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003144 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003145
Hans Wennborgdcfba332015-10-06 23:40:43 +00003146 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003147}
3148
Richard Smith600adef2018-07-04 02:25:38 +00003149/// Find the declaration to use to populate the anonymous declaration table
3150/// for the given lexical DeclContext. We only care about finding local
3151/// definitions of the context; we'll merge imported ones as we go.
3152DeclContext *
3153ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3154 // For classes, we track the definition as we merge.
3155 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3156 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3157 return DD ? DD->Definition : nullptr;
3158 }
3159
3160 // For anything else, walk its merged redeclarations looking for a definition.
3161 // Note that we can't just call getDefinition here because the redeclaration
3162 // chain isn't wired up.
3163 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3164 if (auto *FD = dyn_cast<FunctionDecl>(D))
3165 if (FD->isThisDeclarationADefinition())
3166 return FD;
3167 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3168 if (MD->isThisDeclarationADefinition())
3169 return MD;
3170 }
3171
3172 // No merged definition yet.
3173 return nullptr;
3174}
3175
Richard Smithd08aeb62014-08-28 01:33:39 +00003176NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3177 DeclContext *DC,
3178 unsigned Index) {
3179 // If the lexical context has been merged, look into the now-canonical
3180 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003181 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003182
3183 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003184 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003185 if (Index < Previous.size() && Previous[Index])
3186 return Previous[Index];
3187
3188 // If this is the first time, but we have parsed a declaration of the context,
3189 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003190 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3191 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3192 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003193 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003194 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3195 else
Richard Smith2b560572015-02-07 03:11:11 +00003196 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3197 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003198 }
3199
3200 return Index < Previous.size() ? Previous[Index] : nullptr;
3201}
3202
3203void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3204 DeclContext *DC, unsigned Index,
3205 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003206 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003207
Richard Smith600adef2018-07-04 02:25:38 +00003208 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003209 if (Index >= Previous.size())
3210 Previous.resize(Index + 1);
3211 if (!Previous[Index])
3212 Previous[Index] = D;
3213}
3214
Douglas Gregor022857e2011-12-22 01:48:48 +00003215ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003216 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3217 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003218
Richard Smithd08aeb62014-08-28 01:33:39 +00003219 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3220 // Don't bother trying to find unnamed declarations that are in
3221 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003222 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3223 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003224 Result.suppress();
3225 return Result;
3226 }
Richard Smith95d99302013-07-13 02:00:19 +00003227
Douglas Gregor022857e2011-12-22 01:48:48 +00003228 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003229 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003230 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003231 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003232 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3233 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003234 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3235 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003236 // Go on to check in other places in case an existing typedef name
3237 // was not imported.
3238 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003239
Richard Smith2b560572015-02-07 03:11:11 +00003240 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003241 // This is an anonymous declaration that we may need to merge. Look it up
3242 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003243 if (auto *Existing = getAnonymousDeclForMerging(
3244 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3245 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003246 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3247 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003248 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003249 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003250 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003251
3252 // Temporarily consider the identifier to be up-to-date. We don't want to
3253 // cause additional lookups here.
3254 class UpToDateIdentifierRAII {
3255 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003256 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003257
3258 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003259 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003260 if (II) {
3261 WasOutToDate = II->isOutOfDate();
3262 if (WasOutToDate)
3263 II->setOutOfDate(false);
3264 }
3265 }
3266
3267 ~UpToDateIdentifierRAII() {
3268 if (WasOutToDate)
3269 II->setOutOfDate(true);
3270 }
3271 } UpToDate(Name.getAsIdentifierInfo());
3272
Fangrui Song6907ce22018-07-30 19:24:48 +00003273 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003274 IEnd = IdResolver.end();
3275 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003276 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003277 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003278 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3279 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003280 }
Richard Smith8a639892015-01-24 01:07:20 +00003281 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003282 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003283 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003284 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003285 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003286 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3287 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003288 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003289 } else {
3290 // Not in a mergeable context.
3291 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003292 }
Richard Smithd55889a2013-09-09 16:55:27 +00003293
Richard Smith2b9e3e32013-10-18 06:05:18 +00003294 // If this declaration is from a merged context, make a note that we need to
3295 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003296 //
3297 // FIXME: We should do something similar if we merge two definitions of the
3298 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003299 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3300 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3301 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003302 Reader.PendingOdrMergeChecks.push_back(D);
3303
Richard Smithd08aeb62014-08-28 01:33:39 +00003304 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003305 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003306}
3307
Richard Smithb321ecb2014-05-13 01:15:00 +00003308template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003309Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3310 return D->RedeclLink.getLatestNotUpdated();
3311}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003312
Richard Smithc3a53252015-02-28 05:57:02 +00003313Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3314 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3315}
3316
3317Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3318 assert(D);
3319
3320 switch (D->getKind()) {
3321#define ABSTRACT_DECL(TYPE)
3322#define DECL(TYPE, BASE) \
3323 case Decl::TYPE: \
3324 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3325#include "clang/AST/DeclNodes.inc"
3326 }
3327 llvm_unreachable("unknown decl kind");
3328}
3329
Richard Smith8ce51082015-03-11 01:44:51 +00003330Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3331 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3332}
3333
Richard Smithc3a53252015-02-28 05:57:02 +00003334template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003335void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3336 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003337 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003338 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003339 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003340}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003341
Richard Smith24d166c2014-07-31 23:52:38 +00003342namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003343
Richard Smith6de7a242014-07-31 23:46:44 +00003344template<>
3345void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003346 Redeclarable<VarDecl> *D,
3347 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003348 auto *VD = static_cast<VarDecl *>(D);
3349 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003350 D->RedeclLink.setPrevious(PrevVD);
3351 D->First = PrevVD->First;
3352
3353 // We should keep at most one definition on the chain.
3354 // FIXME: Cache the definition once we've found it. Building a chain with
3355 // N definitions currently takes O(N^2) time here.
3356 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3357 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3358 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3359 Reader.mergeDefinitionVisibility(CurD, VD);
3360 VD->demoteThisDefinitionToDeclaration();
3361 break;
3362 }
3363 }
3364 }
3365}
3366
Richard Smitha62d1982018-08-03 01:00:01 +00003367static bool isUndeducedReturnType(QualType T) {
3368 auto *DT = T->getContainedDeducedType();
3369 return DT && !DT->isDeduced();
3370}
3371
Richard Smithedbc6e92016-10-14 21:41:24 +00003372template<>
3373void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003374 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003375 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003376 auto *FD = static_cast<FunctionDecl *>(D);
3377 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003378
3379 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003380 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003381
3382 // If the previous declaration is an inline function declaration, then this
3383 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003384 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003385 // FIXME: [dcl.fct.spec]p4:
3386 // If a function with external linkage is declared inline in one
3387 // translation unit, it shall be declared inline in all translation
3388 // units in which it appears.
3389 //
3390 // Be careful of this case:
3391 //
3392 // module A:
3393 // template<typename T> struct X { void f(); };
3394 // template<typename T> inline void X<T>::f() {}
3395 //
3396 // module B instantiates the declaration of X<int>::f
3397 // module C instantiates the definition of X<int>::f
3398 //
3399 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003400 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003401 }
3402
Richard Smith6de7a242014-07-31 23:46:44 +00003403 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3404 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003405 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003406 // If we need to propagate an exception specification along the redecl
3407 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003408 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3409 bool WasUnresolved =
3410 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3411 if (IsUnresolved != WasUnresolved)
3412 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003413 {Canon, IsUnresolved ? PrevFD : FD});
3414
3415 // If we need to propagate a deduced return type along the redecl chain,
3416 // make a note of that so that we can do it later.
3417 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3418 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3419 if (IsUndeduced != WasUndeduced)
3420 Reader.PendingDeducedTypeUpdates.insert(
3421 {cast<FunctionDecl>(Canon),
3422 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003423 }
3424}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003425
3426} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003427
Richard Smith6de7a242014-07-31 23:46:44 +00003428void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003429 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3430}
3431
Richard Smith8346e522015-06-10 01:47:58 +00003432/// Inherit the default template argument from \p From to \p To. Returns
3433/// \c false if there is no default template for \p From.
3434template <typename ParmDecl>
3435static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3436 Decl *ToD) {
3437 auto *To = cast<ParmDecl>(ToD);
3438 if (!From->hasDefaultArgument())
3439 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003440 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003441 return true;
3442}
3443
3444static void inheritDefaultTemplateArguments(ASTContext &Context,
3445 TemplateDecl *From,
3446 TemplateDecl *To) {
3447 auto *FromTP = From->getTemplateParameters();
3448 auto *ToTP = To->getTemplateParameters();
3449 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3450
3451 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003452 NamedDecl *FromParam = FromTP->getParam(I);
3453 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003454
Richard Smith559cc692018-07-31 21:01:53 +00003455 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3456 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3457 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3458 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3459 else
3460 inheritDefaultTemplateArgument(
3461 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003462 }
3463}
3464
Richard Smith6de7a242014-07-31 23:46:44 +00003465void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003466 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003467 assert(D && Previous);
3468
3469 switch (D->getKind()) {
3470#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003471#define DECL(TYPE, BASE) \
3472 case Decl::TYPE: \
3473 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003474 break;
3475#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003476 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003477
3478 // If the declaration was visible in one module, a redeclaration of it in
3479 // another module remains visible even if it wouldn't be visible by itself.
3480 //
3481 // FIXME: In this case, the declaration should only be visible if a module
3482 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003483 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003484 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003485 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003486
Richard Smith8346e522015-06-10 01:47:58 +00003487 // If the declaration declares a template, it may inherit default arguments
3488 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003489 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003490 inheritDefaultTemplateArguments(Reader.getContext(),
3491 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003492}
3493
Richard Smithb321ecb2014-05-13 01:15:00 +00003494template<typename DeclT>
3495void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003496 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003497}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003498
Richard Smithb321ecb2014-05-13 01:15:00 +00003499void ASTDeclReader::attachLatestDeclImpl(...) {
3500 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3501}
3502
Douglas Gregor05f10352011-12-17 23:38:30 +00003503void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3504 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003505
3506 switch (D->getKind()) {
3507#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003508#define DECL(TYPE, BASE) \
3509 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003510 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3511 break;
3512#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003513 }
3514}
3515
Richard Smith851072e2014-05-19 20:59:20 +00003516template<typename DeclT>
3517void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3518 D->RedeclLink.markIncomplete();
3519}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003520
Richard Smith851072e2014-05-19 20:59:20 +00003521void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3522 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3523}
3524
3525void ASTReader::markIncompleteDeclChain(Decl *D) {
3526 switch (D->getKind()) {
3527#define ABSTRACT_DECL(TYPE)
3528#define DECL(TYPE, BASE) \
3529 case Decl::TYPE: \
3530 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3531 break;
3532#include "clang/AST/DeclNodes.inc"
3533 }
3534}
3535
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003536/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003537Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003538 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003539 SourceLocation DeclLoc;
3540 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003541 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003542 // Keep track of where we are in the stream, then jump back there
3543 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003544 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003545
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003546 ReadingKindTracker ReadingKind(Read_Decl, *this);
3547
Douglas Gregor1342e842009-07-06 18:54:52 +00003548 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003549 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003550
Sebastian Redl2c373b92010-10-05 15:59:54 +00003551 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003552 ASTRecordReader Record(*this, *Loc.F);
3553 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003554 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003555
Richard Smithdbafb6c2017-06-29 23:23:46 +00003556 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003557 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003558 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003559 case DECL_CONTEXT_LEXICAL:
3560 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003561 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003562 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003563 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003564 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003565 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003566 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003567 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003568 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003569 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003570 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003571 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003572 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003573 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003574 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003575 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003576 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003577 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003578 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003579 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003580 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003581 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003582 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003583 case DECL_EXPORT:
3584 D = ExportDecl::CreateDeserialized(Context, ID);
3585 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003586 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003587 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003588 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003589 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003590 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003591 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003592 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003593 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003594 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003595 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003596 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003597 break;
Richard Smith151c4562016-12-20 21:35:28 +00003598 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003599 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003600 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003601 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003602 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003603 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003604 case DECL_CONSTRUCTOR_USING_SHADOW:
3605 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3606 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003607 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003608 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003609 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003610 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003611 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003612 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003613 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003614 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003615 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003616 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003617 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003618 break;
Richard Smithbc491202017-02-17 20:05:37 +00003619 case DECL_CXX_DEDUCTION_GUIDE:
3620 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3621 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003622 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003623 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003624 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003625 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003626 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3627 break;
3628 case DECL_CXX_INHERITED_CONSTRUCTOR:
3629 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003630 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003631 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003632 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003633 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003634 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003635 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003636 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003637 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003638 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003639 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003640 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003641 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003642 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003643 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003644 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003645 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003646 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003647 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003648 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003649 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003650 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003651 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003652 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003653 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003654 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003655 case DECL_VAR_TEMPLATE:
3656 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3657 break;
3658 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3659 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3660 break;
3661 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3662 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3663 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003664 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003665 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003666 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003667 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003668 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003669 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003670 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003671 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003672 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003673 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003674 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003675 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003676 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003677 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3678 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003679 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003680 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003681 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003682 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003683 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3684 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003685 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003686 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003687 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003688 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003689 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003690 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003691 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003692 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003693 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003694 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003695 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003696 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003697 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003698 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003699 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003700 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003701 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003702 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003703 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003704 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003705 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003706 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003707 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003708 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003709 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003710 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003711 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003712 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003713 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003714 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003715 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003716 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003717 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003718 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003719 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003720 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003721 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003722 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003723 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003724 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003725 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003726 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003727 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003728 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003729 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003730 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003731 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003732 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003733 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003734 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003735 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003736 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003737 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003738 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003739 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003740 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003741 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003742 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003743 break;
3744 case DECL_BINDING:
3745 D = BindingDecl::CreateDeserialized(Context, ID);
3746 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003747 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003748 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003749 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003750 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003751 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003752 break;
John McCall5e77d762013-04-16 07:28:30 +00003753 case DECL_MS_PROPERTY:
3754 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3755 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003756 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003757 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003758 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003759 case DECL_CXX_BASE_SPECIFIERS:
3760 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003761 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003762 case DECL_CXX_CTOR_INITIALIZERS:
3763 Error("attempt to read a C++ ctor initializer record as a declaration");
3764 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003765 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003766 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003767 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003768 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003769 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003770 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003771 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003772 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003773 case DECL_OMP_DECLARE_REDUCTION:
3774 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3775 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003776 case DECL_OMP_CAPTUREDEXPR:
3777 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003778 break;
Nico Weber66220292016-03-02 17:28:48 +00003779 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003780 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003781 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003782 case DECL_PRAGMA_DETECT_MISMATCH:
3783 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003784 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003785 break;
Michael Han84324352013-02-22 17:15:32 +00003786 case DECL_EMPTY:
3787 D = EmptyDecl::CreateDeserialized(Context, ID);
3788 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003789 case DECL_OBJC_TYPE_PARAM:
3790 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3791 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003792 }
Chris Lattner487412d2009-04-27 05:27:42 +00003793
Sebastian Redlb3298c32010-08-18 23:56:48 +00003794 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003795 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003796 // Set the DeclContext before doing any deserialization, to make sure internal
3797 // calls to Decl::getASTContext() by Decl's methods will find the
3798 // TranslationUnitDecl without crashing.
3799 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003800 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003801
3802 // If this declaration is also a declaration context, get the
3803 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003804 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003805 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003806 if (Offsets.first &&
3807 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3808 return nullptr;
3809 if (Offsets.second &&
3810 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3811 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003812 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003813 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003814
Douglas Gregordab42432011-08-12 00:15:20 +00003815 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003816 PendingUpdateRecords.push_back(
3817 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003818
Douglas Gregor404cdde2012-01-27 01:47:08 +00003819 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003820 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003821 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3822 // we put the Decl in PendingDefinitions so we can pull the categories here.
3823 if (Class->isThisDeclarationADefinition() ||
3824 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003825 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00003826
Richard Smith13fb8602016-07-15 21:33:46 +00003827 // If we have deserialized a declaration that has a definition the
3828 // AST consumer might need to know about, queue it.
3829 // We don't pass it to the consumer immediately because we may be in recursive
3830 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003831 PotentiallyInterestingDecls.push_back(
3832 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003833
Douglas Gregordab42432011-08-12 00:15:20 +00003834 return D;
3835}
3836
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003837void ASTReader::PassInterestingDeclsToConsumer() {
3838 assert(Consumer);
3839
3840 if (PassingDeclsToConsumer)
3841 return;
3842
3843 // Guard variable to avoid recursively redoing the process of passing
3844 // decls to consumer.
3845 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3846 true);
3847
3848 // Ensure that we've loaded all potentially-interesting declarations
3849 // that need to be eagerly loaded.
3850 for (auto ID : EagerlyDeserializedDecls)
3851 GetDecl(ID);
3852 EagerlyDeserializedDecls.clear();
3853
3854 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003855 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003856 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003857 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3858 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003859 }
3860}
3861
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003862void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003863 // The declaration may have been modified by files later in the chain.
3864 // If this is the case, read the record containing the updates from each file
3865 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003866 serialization::GlobalDeclID ID = Record.ID;
3867 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003868 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003869 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003870
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003871 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00003872
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003873 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003874 auto UpdateOffsets = std::move(UpdI->second);
3875 DeclUpdateOffsets.erase(UpdI);
3876
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003877 // Check if this decl was interesting to the consumer. If we just loaded
3878 // the declaration, then we know it was interesting and we skip the call
3879 // to isConsumerInterestedIn because it is unsafe to call in the
3880 // current ASTReader state.
3881 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003882 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003883 for (auto &FileAndOffset : UpdateOffsets) {
3884 ModuleFile *F = FileAndOffset.first;
3885 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003886 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3887 SavedStreamPosition SavedPosition(Cursor);
3888 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003889 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003890 ASTRecordReader Record(*this, *F);
3891 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003892 (void)RecCode;
3893 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003894
David L. Jonesbe1557a2016-12-21 00:17:49 +00003895 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3896 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003897 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003898
3899 // We might have made this declaration interesting. If so, remember that
3900 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003901 if (!WasInteresting &&
3902 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
3903 PotentiallyInterestingDecls.push_back(
3904 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003905 WasInteresting = true;
3906 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003907 }
3908 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00003909 // Add the lazy specializations to the template.
3910 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3911 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3912 "Must not have pending specializations");
3913 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3914 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3915 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3916 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3917 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3918 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
3919 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00003920
3921 // Load the pending visible updates for this decl context, if it has any.
3922 auto I = PendingVisibleUpdates.find(ID);
3923 if (I != PendingVisibleUpdates.end()) {
3924 auto VisibleUpdates = std::move(I->second);
3925 PendingVisibleUpdates.erase(I);
3926
3927 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003928 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00003929 Lookups[DC].Table.add(
3930 Update.Mod, Update.Data,
3931 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3932 DC->setHasExternalVisibleStorage(true);
3933 }
Chris Lattner487412d2009-04-27 05:27:42 +00003934}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003935
Richard Smithd61d4ac2015-08-22 20:13:39 +00003936void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3937 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003938 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003939 if (FirstLocal != CanonDecl) {
3940 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3941 ASTDeclReader::attachPreviousDecl(
3942 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3943 CanonDecl);
3944 }
3945
3946 if (!LocalOffset) {
3947 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3948 return;
3949 }
3950
3951 // Load the list of other redeclarations from this module file.
3952 ModuleFile *M = getOwningModuleFile(FirstLocal);
3953 assert(M && "imported decl from no module file");
3954
3955 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3956 SavedStreamPosition SavedPosition(Cursor);
3957 Cursor.JumpToBit(LocalOffset);
3958
3959 RecordData Record;
3960 unsigned Code = Cursor.ReadCode();
3961 unsigned RecCode = Cursor.readRecord(Code, Record);
3962 (void)RecCode;
3963 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3964
3965 // FIXME: We have several different dispatches on decl kind here; maybe
3966 // we should instead generate one loop per kind and dispatch up-front?
3967 Decl *MostRecent = FirstLocal;
3968 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3969 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003970 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3971 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003972 }
Richard Smithc3a53252015-02-28 05:57:02 +00003973 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003974}
3975
3976namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003977
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003978 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003979 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003980 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003981 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003982 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003983 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003984 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003985 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003986 serialization::GlobalDeclID InterfaceID;
3987 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00003988
Douglas Gregor404cdde2012-01-27 01:47:08 +00003989 void add(ObjCCategoryDecl *Cat) {
3990 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003991 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003992 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003993
Douglas Gregor404cdde2012-01-27 01:47:08 +00003994 // Check for duplicate categories.
3995 if (Cat->getDeclName()) {
3996 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00003997 if (Existing &&
3998 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003999 != Reader.getOwningModuleFile(Cat)) {
4000 // FIXME: We should not warn for duplicates in diamond:
4001 //
4002 // MT //
4003 // / \ //
4004 // ML MR //
4005 // \ / //
4006 // MB //
4007 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004008 // If there are duplicates in ML/MR, there will be warning when
4009 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004010 // importing.
4011 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4012 << Interface->getDeclName() << Cat->getDeclName();
4013 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4014 } else if (!Existing) {
4015 // Record this category.
4016 Existing = Cat;
4017 }
4018 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004019
Douglas Gregor404cdde2012-01-27 01:47:08 +00004020 // Add this category to the end of the chain.
4021 if (Tail)
4022 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4023 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004024 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004025 Tail = Cat;
4026 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004027
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004028 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004029 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004030 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004031 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4032 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004033 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004034 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4035 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004036 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004037 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004038 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004039 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004040
Douglas Gregor404cdde2012-01-27 01:47:08 +00004041 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004042 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004043 }
4044 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004045
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004046 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004047 // If we've loaded all of the category information we care about from
4048 // this module file, we're done.
4049 if (M.Generation <= PreviousGeneration)
4050 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004051
4052 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004053 // module file. If there is no such mapping, we'll find nothing here
4054 // (or in any module it imports).
4055 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4056 if (!LocalID)
4057 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004058
Douglas Gregor404cdde2012-01-27 01:47:08 +00004059 // Perform a binary search to find the local redeclarations for this
4060 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004061 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004062 const ObjCCategoriesInfo *Result
4063 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004064 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004065 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004066 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4067 Result->DefinitionID != LocalID) {
4068 // We didn't find anything. If the class definition is in this module
4069 // file, then the module files it depends on cannot have any categories,
4070 // so suppress further lookup.
4071 return Reader.isDeclIDFromModule(InterfaceID, M);
4072 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004073
Douglas Gregor404cdde2012-01-27 01:47:08 +00004074 // We found something. Dig out all of the categories.
4075 unsigned Offset = Result->Offset;
4076 unsigned N = M.ObjCCategories[Offset];
4077 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4078 for (unsigned I = 0; I != N; ++I)
4079 add(cast_or_null<ObjCCategoryDecl>(
4080 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4081 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004082 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004083 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004084
4085} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004086
Douglas Gregor404cdde2012-01-27 01:47:08 +00004087void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4088 ObjCInterfaceDecl *D,
4089 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004090 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004091 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004092 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004093}
Douglas Gregordab42432011-08-12 00:15:20 +00004094
Richard Smithd6db68c2014-08-07 20:58:41 +00004095template<typename DeclT, typename Fn>
4096static void forAllLaterRedecls(DeclT *D, Fn F) {
4097 F(D);
4098
4099 // Check whether we've already merged D into its redeclaration chain.
4100 // MostRecent may or may not be nullptr if D has not been merged. If
4101 // not, walk the merged redecl chain and see if it's there.
4102 auto *MostRecent = D->getMostRecentDecl();
4103 bool Found = false;
4104 for (auto *Redecl = MostRecent; Redecl && !Found;
4105 Redecl = Redecl->getPreviousDecl())
4106 Found = (Redecl == D);
4107
4108 // If this declaration is merged, apply the functor to all later decls.
4109 if (Found) {
4110 for (auto *Redecl = MostRecent; Redecl != D;
4111 Redecl = Redecl->getPreviousDecl())
4112 F(Redecl);
4113 }
4114}
4115
Vassil Vassilev7d264622017-06-30 22:40:17 +00004116void ASTDeclReader::UpdateDecl(Decl *D,
4117 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004118 while (Record.getIdx() < Record.size()) {
4119 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004120 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004121 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004122 // FIXME: If we also have an update record for instantiating the
4123 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004124 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004125 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004126 // FIXME: We should call addHiddenDecl instead, to add the member
4127 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004128 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004129 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004130 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004131
4132 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004133 // It will be added to the template's lazy specialization set.
4134 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004135 break;
4136
4137 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004138 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004139
Douglas Gregor540fd812012-01-09 18:07:24 +00004140 // Each module has its own anonymous namespace, which is disjoint from
4141 // any other module's anonymous namespaces, so don't attach the anonymous
4142 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004143 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004144 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004145 TU->setAnonymousNamespace(Anon);
4146 else
4147 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4148 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004149 break;
4150 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004151
Richard Smith891fc7f2017-12-05 01:31:47 +00004152 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004153 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004154 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4155 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004156 uint64_t Val = Record.readInt();
4157 if (Val && !VD->getInit()) {
4158 VD->setInit(Record.readExpr());
4159 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4160 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4161 Eval->CheckedICE = true;
4162 Eval->IsICE = Val == 3;
4163 }
4164 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004165 break;
Richard Smith05a21352017-06-22 22:18:46 +00004166 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004167
Richard Smith891fc7f2017-12-05 01:31:47 +00004168 case UPD_CXX_POINT_OF_INSTANTIATION: {
4169 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004170 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004171 VTSD->setPointOfInstantiation(POI);
4172 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4173 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4174 } else {
4175 auto *FD = cast<FunctionDecl>(D);
4176 if (auto *FTSInfo = FD->TemplateOrSpecialization
4177 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4178 FTSInfo->setPointOfInstantiation(POI);
4179 else
4180 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4181 ->setPointOfInstantiation(POI);
4182 }
4183 break;
4184 }
4185
John McCall32791cc2016-01-06 22:34:54 +00004186 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004187 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004188
4189 // We have to read the default argument regardless of whether we use it
4190 // so that hypothetical further update records aren't messed up.
4191 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004192 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004193
4194 // Only apply the update if the parameter still has an uninstantiated
4195 // default argument.
4196 if (Param->hasUninstantiatedDefaultArg())
4197 Param->setDefaultArg(DefaultArg);
4198 break;
4199 }
4200
Richard Smith4b054b22016-08-24 21:25:37 +00004201 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004202 auto *FD = cast<FieldDecl>(D);
4203 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004204
4205 // Only apply the update if the field still has an uninstantiated
4206 // default member initializer.
4207 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4208 if (DefaultInit)
4209 FD->setInClassInitializer(DefaultInit);
4210 else
4211 // Instantiation failed. We can get here if we serialized an AST for
4212 // an invalid program.
4213 FD->removeInClassInitializer();
4214 }
4215 break;
4216 }
4217
Richard Smith4d235792014-08-07 18:53:08 +00004218 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004219 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004220 if (Reader.PendingBodies[FD]) {
4221 // FIXME: Maybe check for ODR violations.
4222 // It's safe to stop now because this update record is always last.
4223 return;
4224 }
4225
David L. Jonesbe1557a2016-12-21 00:17:49 +00004226 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004227 // Maintain AST consistency: any later redeclarations of this function
4228 // are inline if this one is. (We might have merged another declaration
4229 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004230 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4231 FD->setImplicitlyInline();
4232 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004233 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004234 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004235 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004236 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004237 break;
4238 }
4239
Richard Smithcd45dbc2014-04-19 03:48:30 +00004240 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4241 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004242 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004243 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004244 OldDD && (OldDD->Definition != RD ||
4245 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004246 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004247 RD->setArgPassingRestrictions(
4248 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004249 ReadCXXRecordDefinition(RD, /*Update*/true);
4250
Richard Smithcd45dbc2014-04-19 03:48:30 +00004251 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004252 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004253 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004254 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004255 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004256 }
4257
David L. Jonesbe1557a2016-12-21 00:17:49 +00004258 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004259 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004260 if (MemberSpecializationInfo *MSInfo =
4261 RD->getMemberSpecializationInfo()) {
4262 MSInfo->setTemplateSpecializationKind(TSK);
4263 MSInfo->setPointOfInstantiation(POI);
4264 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004265 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004266 Spec->setTemplateSpecializationKind(TSK);
4267 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004268
David L. Jonesbe1557a2016-12-21 00:17:49 +00004269 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004270 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004271 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004272 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004273 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004274 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004275 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004276
4277 // FIXME: If we already have a partial specialization set,
4278 // check that it matches.
4279 if (!Spec->getSpecializedTemplateOrPartial()
4280 .is<ClassTemplatePartialSpecializationDecl *>())
4281 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004282 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004283 }
4284
David L. Jonesbe1557a2016-12-21 00:17:49 +00004285 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004286 RD->setLocation(ReadSourceLocation());
4287 RD->setLocStart(ReadSourceLocation());
4288 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004289
David L. Jonesbe1557a2016-12-21 00:17:49 +00004290 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004291 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004292 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004293 // If the declaration already has attributes, we assume that some other
4294 // AST file already loaded them.
4295 if (!D->hasAttrs())
4296 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004297 }
4298 break;
4299 }
4300
Richard Smithf8134002015-03-10 01:41:22 +00004301 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4302 // Set the 'operator delete' directly to avoid emitting another update
4303 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004304 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004305 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004306 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004307 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004308 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004309 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004310 First->OperatorDeleteThisArg = ThisArg;
4311 }
Richard Smithf8134002015-03-10 01:41:22 +00004312 break;
4313 }
4314
Richard Smith564417a2014-03-20 21:47:22 +00004315 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004316 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004317 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004318 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004319
4320 // Update this declaration's exception specification, if needed.
4321 auto *FD = cast<FunctionDecl>(D);
4322 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4323 // FIXME: If the exception specification is already present, check that it
4324 // matches.
4325 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004326 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004327 FPT->getReturnType(), FPT->getParamTypes(),
4328 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004329
4330 // When we get to the end of deserializing, see if there are other decls
4331 // that we need to propagate this exception specification onto.
4332 Reader.PendingExceptionSpecUpdates.insert(
4333 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004334 }
Richard Smith564417a2014-03-20 21:47:22 +00004335 break;
4336 }
4337
Richard Smith1fa5d642013-05-11 05:45:24 +00004338 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004339 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004340 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004341 Reader.PendingDeducedTypeUpdates.insert(
4342 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004343 break;
4344 }
Eli Friedman276dd182013-09-05 00:02:25 +00004345
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004346 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004347 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004348 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004349 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004350
4351 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004352 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4353 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004354 break;
4355
4356 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004357 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4358 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004359 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004360
Alexey Bataev97720002014-11-11 04:05:39 +00004361 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004362 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4363 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004364 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004365
Alex Denisovfde64952015-06-26 05:28:36 +00004366 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004367 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004368 auto *Exported = cast<NamedDecl>(D);
4369 if (auto *TD = dyn_cast<TagDecl>(Exported))
4370 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004371 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004372 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004373 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4374 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004375 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4376 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004377 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004378 // If Owner is made visible at some later point, make this declaration
4379 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004380 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004381 } else {
4382 // The declaration is now visible.
Richard Smith90dc5252017-06-23 01:04:34 +00004383 Exported->setVisibleDespiteOwningModule();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004384 }
4385 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004386 }
Alex Denisovfde64952015-06-26 05:28:36 +00004387
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004388 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00004389 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
4390 Reader.getContext(),
4391 static_cast<OMPDeclareTargetDeclAttr::MapTypeTy>(Record.readInt()),
4392 ReadSourceRange()));
4393 break;
4394
Alex Denisovfde64952015-06-26 05:28:36 +00004395 case UPD_ADDED_ATTR_TO_RECORD:
4396 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004397 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004398 assert(Attrs.size() == 1);
4399 D->addAttr(Attrs[0]);
4400 break;
4401 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004402 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004403}