blob: 763ab527570d7b6c7bfc29e6b3f2400f49b5f097 [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"
Kelvin Li1408f912018-09-26 04:28:39 +000033#include "clang/AST/OpenMPClause.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000034#include "clang/AST/Redeclarable.h"
35#include "clang/AST/Stmt.h"
36#include "clang/AST/TemplateBase.h"
37#include "clang/AST/Type.h"
38#include "clang/AST/UnresolvedSet.h"
39#include "clang/Basic/AttrKinds.h"
40#include "clang/Basic/ExceptionSpecificationType.h"
41#include "clang/Basic/IdentifierTable.h"
42#include "clang/Basic/LLVM.h"
43#include "clang/Basic/Lambda.h"
44#include "clang/Basic/LangOptions.h"
45#include "clang/Basic/Linkage.h"
46#include "clang/Basic/Module.h"
47#include "clang/Basic/PragmaKinds.h"
48#include "clang/Basic/SourceLocation.h"
49#include "clang/Basic/Specifiers.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000050#include "clang/Sema/IdentifierResolver.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]);
Fangrui Song55fab262018-09-26 22:16:28 +0000277 llvm::sort(IDs);
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);
Kelvin Li1408f912018-09-26 04:28:39 +0000449 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000450 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000451 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000452
453} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000454
Richard Smith86dfc1e2015-06-18 22:07:00 +0000455namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000456
Richard Smith86dfc1e2015-06-18 22:07:00 +0000457/// Iterator over the redeclarations of a declaration that have already
458/// been merged into the same redeclaration chain.
459template<typename DeclT>
460class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000461 DeclT *Start;
462 DeclT *Canonical = nullptr;
463 DeclT *Current = nullptr;
464
Richard Smith86dfc1e2015-06-18 22:07:00 +0000465public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000466 MergedRedeclIterator() = default;
467 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000468
469 DeclT *operator*() { return Current; }
470
471 MergedRedeclIterator &operator++() {
472 if (Current->isFirstDecl()) {
473 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000474 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000475 } else
476 Current = Current->getPreviousDecl();
477
478 // If we started in the merged portion, we'll reach our start position
479 // eventually. Otherwise, we'll never reach it, but the second declaration
480 // we reached was the canonical declaration, so stop when we see that one
481 // again.
482 if (Current == Start || Current == Canonical)
483 Current = nullptr;
484 return *this;
485 }
486
487 friend bool operator!=(const MergedRedeclIterator &A,
488 const MergedRedeclIterator &B) {
489 return A.Current != B.Current;
490 }
491};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000492
493} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000494
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000495template <typename DeclT>
496static llvm::iterator_range<MergedRedeclIterator<DeclT>>
497merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000498 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
499 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000500}
501
Sebastian Redlb3298c32010-08-18 23:56:48 +0000502uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000503 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000504}
505
David Blaikieac4345c2017-02-12 18:45:31 +0000506void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000507 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000508 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000509 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Erich Keane9c665062018-08-01 21:02:40 +0000510 CD->setNumCtorInitializers(Record.readInt());
511 if (CD->getNumCtorInitializers())
David Blaikieac4345c2017-02-12 18:45:31 +0000512 CD->CtorInitializers = ReadGlobalOffset();
513 }
514 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000515 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
516 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000517}
518
Sebastian Redlb3298c32010-08-18 23:56:48 +0000519void ASTDeclReader::Visit(Decl *D) {
520 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000521
Vassil Vassilev928c8252016-04-28 14:13:28 +0000522 // At this point we have deserialized and merged the decl and it is safe to
523 // update its canonical decl to signal that the entire entity is used.
524 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
525 IsDeclMarkedUsed = false;
526
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000527 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000528 if (auto *TInfo = DD->getTypeSourceInfo())
529 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000530 }
531
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000532 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000533 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000534 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000535
536 // If this is a tag declaration with a typedef name for linkage, it's safe
537 // to load that typedef now.
538 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000539 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
540 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000541 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000542 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000543 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000544 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000545 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000546 // We only read it if FD doesn't already have a body (e.g., from another
547 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000548 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000549 if (Record.readInt())
550 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000551 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000552}
553
Sebastian Redlb3298c32010-08-18 23:56:48 +0000554void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000555 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
556 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000557 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000558 // parameter or of a parameter of a function template immediately. These
559 // entities might be used in the formulation of its DeclContext (for
560 // example, a function parameter can be used in decltype() in trailing
561 // return type of the function). Use the translation unit DeclContext as a
562 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000563 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
564 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000565 if (!LexicalDCIDForTemplateParmDecl)
566 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000567 Reader.addPendingDeclContextInfo(D,
568 SemaDCIDForTemplateParmDecl,
569 LexicalDCIDForTemplateParmDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000570 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000571 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000572 auto *SemaDC = ReadDeclAs<DeclContext>();
573 auto *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000574 if (!LexicalDC)
575 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000576 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000577 // Avoid calling setLexicalDeclContext() directly because it uses
578 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000579 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
580 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000581 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000582 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000583 D->setInvalidDecl(Record.readInt());
584 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000585 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000586 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000587 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
588 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000589 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000590 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000591 D->setImplicit(Record.readInt());
592 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000593 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000594 D->setReferenced(Record.readInt());
595 D->setTopLevelDeclInObjCContainer(Record.readInt());
596 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000597 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000598 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000599
Douglas Gregorcf68c582011-12-01 22:20:10 +0000600 // Determine whether this declaration is part of a (sub)module. If so, it
601 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000602 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000603 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000604 D->setModuleOwnershipKind(
605 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
606 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000607 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000608
Richard Smith90dc5252017-06-23 01:04:34 +0000609 if (ModulePrivate) {
610 // Module-private declarations are never visible, so there is no work to
611 // do.
Richard Smith42413142015-05-15 20:05:43 +0000612 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
613 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000614 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000615 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000616 // Mark the declaration as visible when its owning module becomes visible.
617 if (Owner->NameVisibility == Module::AllVisible)
618 D->setVisibleDespiteOwningModule();
619 else
Richard Smith42413142015-05-15 20:05:43 +0000620 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000621 }
Richard Smithd19389a2017-07-05 07:47:11 +0000622 } else if (ModulePrivate) {
623 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000624 }
Chris Lattner487412d2009-04-27 05:27:42 +0000625}
626
Nico Weber66220292016-03-02 17:28:48 +0000627void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
628 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000629 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000630 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000631 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000632 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
633 D->getTrailingObjects<char>()[Arg.size()] = '\0';
634}
635
Nico Webercbbaeb12016-03-02 19:28:54 +0000636void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
637 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000638 D->setLocation(ReadSourceLocation());
639 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000640 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
641 D->getTrailingObjects<char>()[Name.size()] = '\0';
642
643 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000644 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000645 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
646 Value.size());
647 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
648}
649
Sebastian Redlb3298c32010-08-18 23:56:48 +0000650void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000651 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000652}
653
Sebastian Redlb3298c32010-08-18 23:56:48 +0000654void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000655 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000656 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000657 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000658}
659
Sebastian Redlb3298c32010-08-18 23:56:48 +0000660void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000661 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000662 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000663 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000664 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000665}
666
Richard Smith43ccec8e2014-08-26 03:52:16 +0000667ASTDeclReader::RedeclarableResult
668ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000669 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000670 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000671 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000672 if (Record.readInt()) { // isModed
673 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000674 TD->setModedTypeSourceInfo(TInfo, modedT);
675 } else
676 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000677 // Read and discard the declaration for which this is a typedef name for
678 // linkage, if it exists. We cannot rely on our type to pull in this decl,
679 // because it might have been merged with a type from another module and
680 // thus might not refer to our version of the declaration.
681 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000682 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000683}
684
685void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000686 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
687 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000688}
689
Richard Smithdda56e42011-04-15 14:24:37 +0000690void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000691 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000692 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000693 // Merged when we merge the template.
694 TD->setDescribedAliasTemplate(Template);
695 else
696 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000697}
698
Richard Smith1d209d02013-05-23 01:49:11 +0000699ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000700 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000701 VisitTypeDecl(TD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000702
David L. Jonesbe1557a2016-12-21 00:17:49 +0000703 TD->IdentifierNamespace = Record.readInt();
704 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000705 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000706 TD->setCompleteDefinition(Record.readInt());
707 TD->setEmbeddedInDeclarator(Record.readInt());
708 TD->setFreeStanding(Record.readInt());
709 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000710 TD->setBraceRange(ReadSourceRange());
Fangrui Song6907ce22018-07-30 19:24:48 +0000711
David L. Jonesbe1557a2016-12-21 00:17:49 +0000712 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000713 case 0:
714 break;
715 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000716 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000717 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000718 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000719 break;
720 }
721 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000722 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000723 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000724 break;
Richard Smith70d58502014-08-30 00:04:23 +0000725 default:
726 llvm_unreachable("unexpected tag info kind");
727 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000728
Richard Smithcd45dbc2014-04-19 03:48:30 +0000729 if (!isa<CXXRecordDecl>(TD))
730 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000731 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000732}
733
Sebastian Redlb3298c32010-08-18 23:56:48 +0000734void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000735 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000736 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000737 ED->setIntegerTypeSourceInfo(TI);
738 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000739 ED->setIntegerType(Record.readType());
740 ED->setPromotionType(Record.readType());
741 ED->setNumPositiveBits(Record.readInt());
742 ED->setNumNegativeBits(Record.readInt());
Erich Keanef92f31c2018-08-01 20:48:16 +0000743 ED->setScoped(Record.readInt());
744 ED->setScopedUsingClassTag(Record.readInt());
745 ED->setFixed(Record.readInt());
Richard Smith4b38ded2012-03-14 23:13:10 +0000746
Erich Keanef92f31c2018-08-01 20:48:16 +0000747 ED->setHasODRHash(true);
Richard Trieuab4d7302018-07-25 22:52:05 +0000748 ED->ODRHash = Record.readInt();
749
Richard Smith01a73372013-10-15 22:02:41 +0000750 // If this is a definition subject to the ODR, and we already have a
751 // definition, merge this one into it.
Erich Keanef92f31c2018-08-01 20:48:16 +0000752 if (ED->isCompleteDefinition() &&
Richard Smith01a73372013-10-15 22:02:41 +0000753 Reader.getContext().getLangOpts().Modules &&
754 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000755 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
756 if (!OldDef) {
757 // This is the first time we've seen an imported definition. Look for a
758 // local definition before deciding that we are the first definition.
759 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
760 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
761 OldDef = D;
762 break;
763 }
764 }
765 }
766 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000767 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
Erich Keanef92f31c2018-08-01 20:48:16 +0000768 ED->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +0000769 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000770 if (OldDef->getODRHash() != ED->getODRHash())
771 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000772 } else {
773 OldDef = ED;
774 }
775 }
776
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000777 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
778 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000779 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000780 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
781 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
782 }
Chris Lattner487412d2009-04-27 05:27:42 +0000783}
784
Richard Smith1d209d02013-05-23 01:49:11 +0000785ASTDeclReader::RedeclarableResult
786ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
787 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000788 RD->setHasFlexibleArrayMember(Record.readInt());
789 RD->setAnonymousStructOrUnion(Record.readInt());
790 RD->setHasObjectMember(Record.readInt());
791 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000792 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
793 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
794 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000795 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000796 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000797 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000798}
799
Sebastian Redlb3298c32010-08-18 23:56:48 +0000800void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000801 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000802 // For function declarations, defer reading the type in case the function has
803 // a deduced return type that references an entity declared within the
804 // function.
805 if (isa<FunctionDecl>(VD))
806 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
807 else
808 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000809}
810
Sebastian Redlb3298c32010-08-18 23:56:48 +0000811void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000812 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000813 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000814 ECD->setInitExpr(Record.readExpr());
815 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000816 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000817}
818
Sebastian Redlb3298c32010-08-18 23:56:48 +0000819void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000820 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000821 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000822 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000823 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000824 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000825 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000826 }
Richard Smithc23d7342018-06-29 20:46:25 +0000827 QualType TSIType = Record.readType();
828 DD->setTypeSourceInfo(
829 TSIType.isNull() ? nullptr
830 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000831}
832
Sebastian Redlb3298c32010-08-18 23:56:48 +0000833void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000834 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000835 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000836
Richard Smith600adef2018-07-04 02:25:38 +0000837 // Attach a type to this function. Use the real type if possible, but fall
838 // back to the type as written if it involves a deduced return type.
839 if (FD->getTypeSourceInfo() &&
840 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
841 ->getReturnType()->getContainedAutoType()) {
842 // We'll set up the real type in Visit, once we've finished loading the
843 // function.
844 FD->setType(FD->getTypeSourceInfo()->getType());
Richard Smitha62d1982018-08-03 01:00:01 +0000845 Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
Richard Smith600adef2018-07-04 02:25:38 +0000846 } else {
847 FD->setType(Reader.GetType(DeferredTypeID));
Richard Smith600adef2018-07-04 02:25:38 +0000848 }
Richard Smitha62d1982018-08-03 01:00:01 +0000849 DeferredTypeID = 0;
Richard Smith600adef2018-07-04 02:25:38 +0000850
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000851 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000852 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000853
Douglas Gregorb2585692012-01-04 17:13:46 +0000854 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
855 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000856
Erich Keane9c665062018-08-01 21:02:40 +0000857 FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
858 FD->setInlineSpecified(Record.readInt());
859 FD->setImplicitlyInline(Record.readInt());
860 FD->setExplicitSpecified(Record.readInt());
861 FD->setVirtualAsWritten(Record.readInt());
862 FD->setPure(Record.readInt());
863 FD->setHasInheritedPrototype(Record.readInt());
864 FD->setHasWrittenPrototype(Record.readInt());
865 FD->setDeletedAsWritten(Record.readInt());
866 FD->setTrivial(Record.readInt());
867 FD->setTrivialForCall(Record.readInt());
868 FD->setDefaulted(Record.readInt());
869 FD->setExplicitlyDefaulted(Record.readInt());
870 FD->setHasImplicitReturnZero(Record.readInt());
871 FD->setConstexpr(Record.readInt());
872 FD->setUsesSEHTry(Record.readInt());
873 FD->setHasSkippedBody(Record.readInt());
874 FD->setIsMultiVersion(Record.readInt());
875 FD->setLateTemplateParsed(Record.readInt());
876
877 FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000878 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000879
Richard Trieue6caa262017-12-23 00:41:01 +0000880 FD->ODRHash = Record.readInt();
Erich Keane9c665062018-08-01 21:02:40 +0000881 FD->setHasODRHash(true);
Richard Trieue6caa262017-12-23 00:41:01 +0000882
David L. Jonesbe1557a2016-12-21 00:17:49 +0000883 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000884 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000885 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000886 break;
887 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000888 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000889 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000890 break;
891 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000892 auto *InstFD = ReadDeclAs<FunctionDecl>();
893 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000894 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000895 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000896 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000897 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000898 break;
899 }
900 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000901 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
902 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000903
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000904 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000905 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000906 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000907
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000908 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000909 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000910 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000911 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000912 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000913 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000914 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000915 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000916 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000917
918 LAngleLoc = ReadSourceLocation();
919 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000920 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000921
922 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000923
Douglas Gregor4163aca2011-09-09 21:34:22 +0000924 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000925 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000926 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000927 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000928 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000929 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000930 FunctionTemplateSpecializationInfo *FTInfo
931 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
932 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000933 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
934 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000935 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000936 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000937
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000938 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000939 // The template that contains the specializations set. It's not safe to
940 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000941 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000942 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
943 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
944 // FunctionTemplateSpecializationInfo's Profile().
945 // We avoid getASTContext because a decl in the parent hierarchy may
946 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000947 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000948 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000949 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000950 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000951 FunctionTemplateSpecializationInfo *ExistingInfo =
952 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000953 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000954 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
955 else {
956 assert(Reader.getContext().getLangOpts().Modules &&
957 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000958 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000959 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000960 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000961 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000962 }
963 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
964 // Templates.
965 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000966 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000967 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000968 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
969
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000970 // Templates args.
971 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000972 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000973 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000974 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000975 TemplArgs.setLAngleLoc(ReadSourceLocation());
976 TemplArgs.setRAngleLoc(ReadSourceLocation());
977
Douglas Gregor4163aca2011-09-09 21:34:22 +0000978 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000979 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000980 // These are not merged; we don't need to merge redeclarations of dependent
981 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000982 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000983 }
984 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000985
Chris Lattnerca025db2010-05-07 21:43:38 +0000986 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000987 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000988 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000989 Params.reserve(NumParams);
990 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000991 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000992 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000993}
994
Sebastian Redlb3298c32010-08-18 23:56:48 +0000995void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000996 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000997 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000998 // Load the body on-demand. Most clients won't care, because method
999 // definitions rarely show up in headers.
1000 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1001 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001002 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1003 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001004 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001005 MD->setInstanceMethod(Record.readInt());
1006 MD->setVariadic(Record.readInt());
1007 MD->setPropertyAccessor(Record.readInt());
1008 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001009 MD->setOverriding(Record.readInt());
1010 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001011
Erich Keane9b18eca2018-08-01 21:31:08 +00001012 MD->setIsRedeclaration(Record.readInt());
1013 MD->setHasRedeclaration(Record.readInt());
1014 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001015 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001016 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001017
David L. Jonesbe1557a2016-12-21 00:17:49 +00001018 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1019 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001020 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001021 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001022 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1023 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001024 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001025 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001026 Params.reserve(NumParams);
1027 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001028 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001029
Erich Keane9b18eca2018-08-01 21:31:08 +00001030 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001031 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001032 SmallVector<SourceLocation, 16> SelLocs;
1033 SelLocs.reserve(NumStoredSelLocs);
1034 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001035 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001036
1037 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001038}
1039
Douglas Gregor85f3f952015-07-07 03:57:15 +00001040void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001041 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001042
David L. Jonesbe1557a2016-12-21 00:17:49 +00001043 D->Variance = Record.readInt();
1044 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001045 D->VarianceLoc = ReadSourceLocation();
1046 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001047}
1048
Sebastian Redlb3298c32010-08-18 23:56:48 +00001049void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001050 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001051 CD->setAtStartLoc(ReadSourceLocation());
1052 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001053}
1054
Douglas Gregor85f3f952015-07-07 03:57:15 +00001055ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001056 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001057 if (numParams == 0)
1058 return nullptr;
1059
1060 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1061 typeParams.reserve(numParams);
1062 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001063 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001064 if (!typeParam)
1065 return nullptr;
1066
1067 typeParams.push_back(typeParam);
1068 }
1069
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001070 SourceLocation lAngleLoc = ReadSourceLocation();
1071 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001072
1073 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1074 typeParams, rAngleLoc);
1075}
1076
Manman Renec315f12016-09-09 23:48:27 +00001077void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001078 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001079 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001080 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001081
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001082 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001083 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001084
Manman Renec315f12016-09-09 23:48:27 +00001085 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001086 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001087 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1088 Protocols.reserve(NumProtocols);
1089 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001090 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001091 SmallVector<SourceLocation, 16> ProtoLocs;
1092 ProtoLocs.reserve(NumProtocols);
1093 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001094 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001095 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1096 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001097
Manman Renec315f12016-09-09 23:48:27 +00001098 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001099 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001100 Protocols.clear();
1101 Protocols.reserve(NumProtocols);
1102 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001103 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001104 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1105 Reader.getContext());
1106}
1107
1108void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1109 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1110 // FIXME: odr checking?
1111}
1112
Sebastian Redlb3298c32010-08-18 23:56:48 +00001113void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001114 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001115 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001116 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001117 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001118
1119 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001120 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001121 // Read the definition.
1122 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001123
David L. Jonesbe1557a2016-12-21 00:17:49 +00001124 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001125 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1126 if (Canon->Data.getPointer()) {
1127 // If we already have a definition, keep the definition invariant and
1128 // merge the data.
1129 MergeDefinitionData(Canon, std::move(ID->data()));
1130 ID->Data = Canon->Data;
1131 } else {
1132 // Set the definition data of the canonical declaration, so other
1133 // redeclarations will see it.
1134 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001135
Manman Renec315f12016-09-09 23:48:27 +00001136 // We will rebuild this list lazily.
1137 ID->setIvarList(nullptr);
1138 }
Craig Toppera13603a2014-05-22 05:54:18 +00001139
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001140 // Note that we have deserialized a definition.
1141 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001142
Douglas Gregor404cdde2012-01-27 01:47:08 +00001143 // Note that we've loaded this Objective-C class.
1144 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001145 } else {
1146 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001147 }
Chris Lattner487412d2009-04-27 05:27:42 +00001148}
1149
Sebastian Redlb3298c32010-08-18 23:56:48 +00001150void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001151 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001152 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001153 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001154 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001155 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001156 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001157}
1158
Graydon Hoaree0a68352017-06-28 18:36:27 +00001159void ASTDeclReader::ReadObjCDefinitionData(
1160 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001161 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001162 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1163 ProtoRefs.reserve(NumProtoRefs);
1164 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001165 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001166 SmallVector<SourceLocation, 16> ProtoLocs;
1167 ProtoLocs.reserve(NumProtoRefs);
1168 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001169 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001170 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1171 ProtoLocs.data(), Reader.getContext());
1172}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001173
Graydon Hoaree0a68352017-06-28 18:36:27 +00001174void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1175 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1176 // FIXME: odr checking?
1177}
1178
1179void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1180 RedeclarableResult Redecl = VisitRedeclarable(PD);
1181 VisitObjCContainerDecl(PD);
1182 mergeRedeclarable(PD, Redecl);
1183
1184 if (Record.readInt()) {
1185 // Read the definition.
1186 PD->allocateDefinitionData();
1187
1188 ReadObjCDefinitionData(PD->data());
1189
1190 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1191 if (Canon->Data.getPointer()) {
1192 // If we already have a definition, keep the definition invariant and
1193 // merge the data.
1194 MergeDefinitionData(Canon, std::move(PD->data()));
1195 PD->Data = Canon->Data;
1196 } else {
1197 // Set the definition data of the canonical declaration, so other
1198 // redeclarations will see it.
1199 PD->getCanonicalDecl()->Data = PD->Data;
1200 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001201 // Note that we have deserialized a definition.
1202 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001203 } else {
1204 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001205 }
Chris Lattner487412d2009-04-27 05:27:42 +00001206}
1207
Sebastian Redlb3298c32010-08-18 23:56:48 +00001208void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001209 VisitFieldDecl(FD);
1210}
1211
Sebastian Redlb3298c32010-08-18 23:56:48 +00001212void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001213 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001214 CD->setCategoryNameLoc(ReadSourceLocation());
1215 CD->setIvarLBraceLoc(ReadSourceLocation());
1216 CD->setIvarRBraceLoc(ReadSourceLocation());
1217
Douglas Gregor404cdde2012-01-27 01:47:08 +00001218 // Note that this category has been deserialized. We do this before
1219 // deserializing the interface declaration, so that it will consider this
1220 /// category.
1221 Reader.CategoriesDeserialized.insert(CD);
1222
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001223 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001224 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001225 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001226 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001227 ProtoRefs.reserve(NumProtoRefs);
1228 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001229 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001230 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001231 ProtoLocs.reserve(NumProtoRefs);
1232 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001233 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001234 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001235 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001236
1237 // Protocols in the class extension belong to the class.
1238 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1239 CD->ClassInterface->mergeClassExtensionProtocolList(
1240 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1241 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001242}
1243
Sebastian Redlb3298c32010-08-18 23:56:48 +00001244void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001245 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001246 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001247}
1248
Sebastian Redlb3298c32010-08-18 23:56:48 +00001249void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001250 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001251 D->setAtLoc(ReadSourceLocation());
1252 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001253 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001254 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001255 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001256 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001257 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001258 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001259 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001260 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001261 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001262 DeclarationName GetterName = Record.readDeclarationName();
1263 SourceLocation GetterLoc = ReadSourceLocation();
1264 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1265 DeclarationName SetterName = Record.readDeclarationName();
1266 SourceLocation SetterLoc = ReadSourceLocation();
1267 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001268 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1269 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1270 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001271}
1272
Sebastian Redlb3298c32010-08-18 23:56:48 +00001273void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001274 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001275 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001276}
1277
Sebastian Redlb3298c32010-08-18 23:56:48 +00001278void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001279 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001280 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001281}
1282
Sebastian Redlb3298c32010-08-18 23:56:48 +00001283void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001284 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001285 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1286 D->SuperLoc = ReadSourceLocation();
1287 D->setIvarLBraceLoc(ReadSourceLocation());
1288 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001289 D->setHasNonZeroConstructors(Record.readInt());
1290 D->setHasDestructors(Record.readInt());
1291 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001292 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001293 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001294}
1295
Sebastian Redlb3298c32010-08-18 23:56:48 +00001296void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001297 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001298 D->setAtLoc(ReadSourceLocation());
1299 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1300 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1301 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001302 D->setGetterCXXConstructor(Record.readExpr());
1303 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001304}
1305
Sebastian Redlb3298c32010-08-18 23:56:48 +00001306void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001307 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001308 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001309
1310 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1311 FD->InitStorage.setInt(ISK);
1312 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1313 ? Record.readType().getAsOpaquePtr()
1314 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001315 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001316
1317 if (auto *BW = Record.readExpr())
1318 FD->setBitWidth(BW);
1319
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001320 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001321 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001322 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001323 }
Richard Smith0b87e072013-10-07 08:02:11 +00001324 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001325}
1326
John McCall5e77d762013-04-16 07:28:30 +00001327void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1328 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001329 PD->GetterId = Record.getIdentifierInfo();
1330 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001331}
1332
Francois Pichet783dd6e2010-11-21 06:08:52 +00001333void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1334 VisitValueDecl(FD);
1335
David L. Jonesbe1557a2016-12-21 00:17:49 +00001336 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001337 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001338 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001339
1340 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001341 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001342
1343 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001344}
1345
Larisse Voufo39a1e502013-08-06 01:03:05 +00001346ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001347 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001348 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001349
David L. Jonesbe1557a2016-12-21 00:17:49 +00001350 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1351 VD->VarDeclBits.TSCSpec = Record.readInt();
1352 VD->VarDeclBits.InitStyle = Record.readInt();
Erik Pilkington1e368822019-01-04 18:33:06 +00001353 VD->VarDeclBits.ARCPseudoStrong = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001354 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001355 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1356 Record.readInt();
1357 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001358 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001359 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001360 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001361 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1362 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1363 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1364 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1365 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001366 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
Akira Hatanaka8e57b072018-10-01 21:51:28 +00001367 VD->NonParmVarDeclBits.EscapingByref = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001368 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001369 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001370 VD->setCachedLinkage(VarLinkage);
1371
1372 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001373 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001374 VD->getLexicalDeclContext()->isFunctionOrMethod())
1375 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001376
David L. Jonesbe1557a2016-12-21 00:17:49 +00001377 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001378 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001379 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001380 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1381 Eval->CheckedICE = true;
1382 Eval->IsICE = Val == 3;
1383 }
1384 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001385
Akira Hatanaka9978da32018-08-10 15:09:24 +00001386 if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1387 Expr *CopyExpr = Record.readExpr();
1388 if (CopyExpr)
1389 Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1390 }
1391
Richard Smitha4653622017-09-06 20:01:14 +00001392 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1393 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1394
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001395 enum VarKind {
1396 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1397 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001398 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001399 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001400 // Only true variables (not parameters or implicit parameters) can be
1401 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001402 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1403 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001404 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001405 break;
1406 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001407 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001408 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001409 break;
1410 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001411 auto *Tmpl = ReadDeclAs<VarDecl>();
1412 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001413 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001414 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001415 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001416 break;
1417 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001418 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001419
1420 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001421}
1422
Sebastian Redlb3298c32010-08-18 23:56:48 +00001423void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001424 VisitVarDecl(PD);
1425}
1426
Sebastian Redlb3298c32010-08-18 23:56:48 +00001427void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001428 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001429 unsigned isObjCMethodParam = Record.readInt();
1430 unsigned scopeDepth = Record.readInt();
1431 unsigned scopeIndex = Record.readInt();
1432 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001433 if (isObjCMethodParam) {
1434 assert(scopeDepth == 0);
1435 PD->setObjCMethodScopeInfo(scopeIndex);
1436 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1437 } else {
1438 PD->setScopeInfo(scopeDepth, scopeIndex);
1439 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001440 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1441 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1442 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001443 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001444
1445 // FIXME: If this is a redeclaration of a function from another module, handle
1446 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001447}
1448
Richard Smith7b76d812016-08-12 02:21:25 +00001449void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1450 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001451 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith7b76d812016-08-12 02:21:25 +00001452 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001453 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001454}
1455
1456void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1457 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001458 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001459}
1460
Sebastian Redlb3298c32010-08-18 23:56:48 +00001461void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001462 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001463 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001464 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001465}
1466
Sebastian Redlb3298c32010-08-18 23:56:48 +00001467void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001468 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001469 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001470 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001471 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001472 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001473 Params.reserve(NumParams);
1474 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001475 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001476 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001477
David L. Jonesbe1557a2016-12-21 00:17:49 +00001478 BD->setIsVariadic(Record.readInt());
1479 BD->setBlockMissingReturnType(Record.readInt());
1480 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001481 BD->setDoesNotEscape(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001482
David L. Jonesbe1557a2016-12-21 00:17:49 +00001483 bool capturesCXXThis = Record.readInt();
1484 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001485 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001486 captures.reserve(numCaptures);
1487 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001488 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001489 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001490 bool byRef = (flags & 1);
1491 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001492 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001493
1494 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1495 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001496 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001497}
1498
Ben Langmuirce914fc2013-05-03 19:20:19 +00001499void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1500 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001501 unsigned ContextParamPos = Record.readInt();
1502 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001503 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001504 for (unsigned I = 0; I < CD->NumParams; ++I) {
1505 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001506 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001507 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001508 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001509 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001510}
1511
Sebastian Redlb3298c32010-08-18 23:56:48 +00001512void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001513 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001514 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001515 D->setExternLoc(ReadSourceLocation());
1516 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001517}
1518
Richard Smith8df390f2016-09-08 23:14:54 +00001519void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1520 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001521 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001522}
1523
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001524void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1525 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001526 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001527}
1528
Sebastian Redlb3298c32010-08-18 23:56:48 +00001529void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001530 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001531 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001532 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001533 D->LocStart = ReadSourceLocation();
1534 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001535
Richard Smith15e32fd2015-03-17 02:23:11 +00001536 // Defer loading the anonymous namespace until we've finished merging
1537 // this namespace; loading it might load a later declaration of the
1538 // same namespace, and we have an invariant that older declarations
1539 // get merged before newer ones try to merge.
1540 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001541 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001542 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001543 } else {
1544 // Link this namespace back to the first declaration, which has already
1545 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001546 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001547 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001548
1549 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001550
1551 if (AnonNamespace) {
1552 // Each module has its own anonymous namespace, which is disjoint from
1553 // any other module's anonymous namespaces, so don't attach the anonymous
1554 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001555 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001556 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001557 D->setAnonymousNamespace(Anon);
1558 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001559}
1560
Sebastian Redlb3298c32010-08-18 23:56:48 +00001561void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001562 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001563 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001564 D->NamespaceLoc = ReadSourceLocation();
1565 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001566 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001567 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001568 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001569}
1570
Sebastian Redlb3298c32010-08-18 23:56:48 +00001571void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001572 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001573 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001574 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001575 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1576 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001577 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001578 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001579 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001580 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001581}
1582
Richard Smith151c4562016-12-20 21:35:28 +00001583void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1584 VisitNamedDecl(D);
1585 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001586 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001587 for (unsigned I = 0; I != D->NumExpansions; ++I)
1588 Expansions[I] = ReadDeclAs<NamedDecl>();
1589 mergeMergeable(D);
1590}
1591
Sebastian Redlb3298c32010-08-18 23:56:48 +00001592void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001593 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001594 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001595 D->Underlying = ReadDeclAs<NamedDecl>();
1596 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001597 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001598 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001599 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001600 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001601 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001602}
1603
Richard Smith5179eb72016-06-28 19:03:57 +00001604void ASTDeclReader::VisitConstructorUsingShadowDecl(
1605 ConstructorUsingShadowDecl *D) {
1606 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001607 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1608 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001609 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001610}
1611
Sebastian Redlb3298c32010-08-18 23:56:48 +00001612void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001613 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001614 D->UsingLoc = ReadSourceLocation();
1615 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001616 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001617 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1618 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001619}
1620
Sebastian Redlb3298c32010-08-18 23:56:48 +00001621void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001622 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001623 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001624 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001625 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001626 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001627 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001628}
1629
Sebastian Redlb3298c32010-08-18 23:56:48 +00001630void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001631 UnresolvedUsingTypenameDecl *D) {
1632 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001633 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001634 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001635 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001636 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001637}
1638
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001639void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001640 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001641 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001642 Data.UserDeclaredConstructor = Record.readInt();
1643 Data.UserDeclaredSpecialMembers = Record.readInt();
1644 Data.Aggregate = Record.readInt();
1645 Data.PlainOldData = Record.readInt();
1646 Data.Empty = Record.readInt();
1647 Data.Polymorphic = Record.readInt();
1648 Data.Abstract = Record.readInt();
1649 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001650 Data.IsCXX11StandardLayout = Record.readInt();
1651 Data.HasBasesWithFields = Record.readInt();
1652 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001653 Data.HasPrivateFields = Record.readInt();
1654 Data.HasProtectedFields = Record.readInt();
1655 Data.HasPublicFields = Record.readInt();
1656 Data.HasMutableFields = Record.readInt();
1657 Data.HasVariantMembers = Record.readInt();
1658 Data.HasOnlyCMembers = Record.readInt();
1659 Data.HasInClassInitializer = Record.readInt();
1660 Data.HasUninitializedReferenceMember = Record.readInt();
1661 Data.HasUninitializedFields = Record.readInt();
1662 Data.HasInheritedConstructor = Record.readInt();
1663 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001664 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001665 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1666 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1667 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001668 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001669 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1670 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1671 Data.DefaultedDestructorIsDeleted = Record.readInt();
1672 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001673 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001674 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001675 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001676 Data.HasIrrelevantDestructor = Record.readInt();
1677 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1678 Data.HasDefaultedDefaultConstructor = Record.readInt();
1679 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1680 Data.HasConstexprDefaultConstructor = Record.readInt();
1681 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1682 Data.ComputedVisibleConversions = Record.readInt();
1683 Data.UserProvidedDefaultConstructor = Record.readInt();
1684 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001685 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1686 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001687 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1688 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1689 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001690 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001691 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001692
Richard Smithcd4a7a42017-09-07 00:55:55 +00001693 if (Record.readInt())
1694 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001695
David L. Jonesbe1557a2016-12-21 00:17:49 +00001696 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001697 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001698 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001699 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001700 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001701 Data.VBases = ReadGlobalOffset();
1702
David L. Jonesb6a8f022016-12-21 04:34:52 +00001703 Record.readUnresolvedSet(Data.Conversions);
1704 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001705 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001706 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001707
Douglas Gregor99ae8062012-02-14 17:54:36 +00001708 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001709 using Capture = LambdaCapture;
1710
1711 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001712 Lambda.Dependent = Record.readInt();
1713 Lambda.IsGenericLambda = Record.readInt();
1714 Lambda.CaptureDefault = Record.readInt();
1715 Lambda.NumCaptures = Record.readInt();
1716 Lambda.NumExplicitCaptures = Record.readInt();
1717 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001718 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001719 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1720 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001721 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001722 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001723 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001724 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001725 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001726 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001727 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001728 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001729 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001730 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001731 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001732 break;
1733 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001734 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001735 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001736 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001737 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1738 break;
1739 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001740 }
1741 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001742}
1743
Richard Smithcd45dbc2014-04-19 03:48:30 +00001744void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001745 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001746 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001747 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001748 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001749
Richard Smith02793752015-03-27 21:16:39 +00001750 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001751 // Track that we merged the definitions.
1752 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1753 DD.Definition));
1754 Reader.PendingDefinitions.erase(MergeDD.Definition);
Erich Keanef92f31c2018-08-01 20:48:16 +00001755 MergeDD.Definition->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +00001756 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001757 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1758 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001759 }
1760
Richard Smith2a9e5c52015-02-03 03:32:14 +00001761 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1762 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1763 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001764 // We faked up this definition data because we found a class for which we'd
1765 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001766 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001767 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001768
1769 // Don't change which declaration is the definition; that is required
1770 // to be invariant once we select it.
1771 auto *Def = DD.Definition;
1772 DD = std::move(MergeDD);
1773 DD.Definition = Def;
1774 return;
1775 }
1776
Richard Smithcd45dbc2014-04-19 03:48:30 +00001777 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001778 bool DetectedOdrViolation = false;
1779#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1780#define MATCH_FIELD(Field) \
1781 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1782 OR_FIELD(Field)
1783 MATCH_FIELD(UserDeclaredConstructor)
1784 MATCH_FIELD(UserDeclaredSpecialMembers)
1785 MATCH_FIELD(Aggregate)
1786 MATCH_FIELD(PlainOldData)
1787 MATCH_FIELD(Empty)
1788 MATCH_FIELD(Polymorphic)
1789 MATCH_FIELD(Abstract)
1790 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001791 MATCH_FIELD(IsCXX11StandardLayout)
1792 MATCH_FIELD(HasBasesWithFields)
1793 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001794 MATCH_FIELD(HasPrivateFields)
1795 MATCH_FIELD(HasProtectedFields)
1796 MATCH_FIELD(HasPublicFields)
1797 MATCH_FIELD(HasMutableFields)
1798 MATCH_FIELD(HasVariantMembers)
1799 MATCH_FIELD(HasOnlyCMembers)
1800 MATCH_FIELD(HasInClassInitializer)
1801 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001802 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001803 MATCH_FIELD(HasInheritedConstructor)
1804 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001805 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001806 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1807 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1808 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001809 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001810 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1811 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1812 MATCH_FIELD(DefaultedDestructorIsDeleted)
1813 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001814 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001815 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001816 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001817 MATCH_FIELD(HasIrrelevantDestructor)
1818 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001819 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001820 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1821 OR_FIELD(HasConstexprDefaultConstructor)
1822 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1823 // ComputedVisibleConversions is handled below.
1824 MATCH_FIELD(UserProvidedDefaultConstructor)
1825 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001826 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1827 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001828 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1829 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1830 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1831 MATCH_FIELD(IsLambda)
1832#undef OR_FIELD
1833#undef MATCH_FIELD
1834
1835 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1836 DetectedOdrViolation = true;
1837 // FIXME: Issue a diagnostic if the base classes don't match when we come
1838 // to lazily load them.
1839
1840 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1841 // match when we come to lazily load them.
1842 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1843 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1844 DD.ComputedVisibleConversions = true;
1845 }
1846
1847 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1848 // lazily load it.
1849
1850 if (DD.IsLambda) {
1851 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1852 // when they occur within the body of a function template specialization).
1853 }
1854
Richard Trieufd1acbb2017-04-11 21:31:00 +00001855 if (D->getODRHash() != MergeDD.ODRHash) {
1856 DetectedOdrViolation = true;
1857 }
1858
Richard Smithcd45dbc2014-04-19 03:48:30 +00001859 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001860 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1861 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001862}
1863
Richard Smith2a9e5c52015-02-03 03:32:14 +00001864void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001865 struct CXXRecordDecl::DefinitionData *DD;
1866 ASTContext &C = Reader.getContext();
1867
1868 // Determine whether this is a lambda closure type, so that we can
1869 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001870 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001871 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001872 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001873 LCD_None);
1874 else
1875 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1876
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001877 CXXRecordDecl *Canon = D->getCanonicalDecl();
1878 // Set decl definition data before reading it, so that during deserialization
1879 // when we read CXXRecordDecl, it already has definition data and we don't
1880 // set fake one.
1881 if (!Canon->DefinitionData)
1882 Canon->DefinitionData = DD;
1883 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001884 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001885
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001886 // We might already have a different definition for this record. This can
1887 // happen either because we're reading an update record, or because we've
1888 // already done some merging. Either way, just merge into it.
1889 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001890 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001891 return;
1892 }
1893
Richard Smith97100e32015-06-20 00:22:34 +00001894 // Mark this declaration as being a definition.
Erich Keanef92f31c2018-08-01 20:48:16 +00001895 D->setCompleteDefinition(true);
Richard Smith2a9e5c52015-02-03 03:32:14 +00001896
Richard Smith97100e32015-06-20 00:22:34 +00001897 // If this is not the first declaration or is an update record, we can have
1898 // other redeclarations already. Make a note that we need to propagate the
1899 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001900 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001901 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001902}
1903
Richard Smith1d209d02013-05-23 01:49:11 +00001904ASTDeclReader::RedeclarableResult
1905ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1906 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001907
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001908 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001909
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001910 enum CXXRecKind {
1911 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1912 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001913 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001914 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001915 // Merged when we merge the folding set entry in the primary template.
1916 if (!isa<ClassTemplateSpecializationDecl>(D))
1917 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001918 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001919 case CXXRecTemplate: {
1920 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001921 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001922 D->TemplateOrInstantiation = Template;
1923 if (!Template->getTemplatedDecl()) {
1924 // We've not actually loaded the ClassTemplateDecl yet, because we're
1925 // currently being loaded as its pattern. Rely on it to set up our
1926 // TypeForDecl (see VisitClassTemplateDecl).
1927 //
1928 // Beware: we do not yet know our canonical declaration, and may still
1929 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001930 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001931 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001932 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001933 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001934 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001935 auto *RD = ReadDeclAs<CXXRecordDecl>();
1936 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001937 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001938 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1939 MSI->setPointOfInstantiation(POI);
1940 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001941 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001942 break;
1943 }
1944 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001945
David L. Jonesbe1557a2016-12-21 00:17:49 +00001946 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001947 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001948 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001949 else
1950 // Propagate DefinitionData pointer from the canonical declaration.
1951 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1952
Richard Smith676c4042013-08-29 23:59:27 +00001953 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001954 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001955 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001956 DeclID KeyFn = ReadDeclID();
Erich Keanef92f31c2018-08-01 20:48:16 +00001957 if (KeyFn && D->isCompleteDefinition())
Richard Smitha9a1c682014-07-07 06:38:20 +00001958 // FIXME: This is wrong for the ARM ABI, where some other module may have
1959 // made this function no longer be a key function. We need an update
1960 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001961 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001962 }
Richard Smith1d209d02013-05-23 01:49:11 +00001963
1964 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001965}
1966
Richard Smithbc491202017-02-17 20:05:37 +00001967void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1968 VisitFunctionDecl(D);
Erich Keane9c665062018-08-01 21:02:40 +00001969 D->setIsCopyDeductionCandidate(Record.readInt());
Richard Smithbc491202017-02-17 20:05:37 +00001970}
1971
Sebastian Redlb3298c32010-08-18 23:56:48 +00001972void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001973 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001974
David L. Jonesbe1557a2016-12-21 00:17:49 +00001975 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001976 if (D->isCanonicalDecl()) {
1977 while (NumOverridenMethods--) {
1978 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1979 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001980 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001981 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1982 }
1983 } else {
1984 // We don't care about which declarations this used to override; we get
1985 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001986 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001987 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001988}
1989
Sebastian Redlb3298c32010-08-18 23:56:48 +00001990void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001991 // We need the inherited constructor information to merge the declaration,
1992 // so we have to read it before we call VisitCXXMethodDecl.
1993 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001994 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1995 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001996 *D->getTrailingObjects<InheritedConstructor>() =
1997 InheritedConstructor(Shadow, Ctor);
1998 }
1999
Chris Lattnerca025db2010-05-07 21:43:38 +00002000 VisitCXXMethodDecl(D);
2001}
2002
Sebastian Redlb3298c32010-08-18 23:56:48 +00002003void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002004 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00002005
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002006 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002007 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00002008 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00002009 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00002010 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00002011 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00002012 Canon->OperatorDeleteThisArg = ThisArg;
2013 }
Richard Smithf8134002015-03-10 01:41:22 +00002014 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002015}
2016
Sebastian Redlb3298c32010-08-18 23:56:48 +00002017void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002018 VisitCXXMethodDecl(D);
2019}
2020
Douglas Gregorba345522011-12-02 23:23:56 +00002021void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2022 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002023 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002024 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002025 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002026 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002027 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002028 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002029}
2030
Sebastian Redlb3298c32010-08-18 23:56:48 +00002031void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002032 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002033 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002034}
2035
Sebastian Redlb3298c32010-08-18 23:56:48 +00002036void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002037 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002038 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002039 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002040 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002041 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002042 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002043 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002044 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002045 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002046 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002047 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002048}
2049
Sebastian Redlb3298c32010-08-18 23:56:48 +00002050void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002051 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002052 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002053 D->NumParams = NumParams;
2054 D->Params = new TemplateParameterList*[NumParams];
2055 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002056 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002057 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002058 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002059 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002060 D->Friend = GetTypeSourceInfo();
2061 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002062}
2063
Richard Smithf17fdbd2014-04-24 02:25:27 +00002064DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002065 VisitNamedDecl(D);
2066
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002067 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002068 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002069 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002070 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002071 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002072
Richard Smithf17fdbd2014-04-24 02:25:27 +00002073 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002074}
2075
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002076ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002077ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002078 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002079
Douglas Gregor68444de2012-01-14 15:13:49 +00002080 // Make sure we've allocated the Common pointer first. We do this before
2081 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2082 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2083 if (!CanonD->Common) {
2084 CanonD->Common = CanonD->newCommon(Reader.getContext());
2085 Reader.PendingDefinitions.insert(CanonD);
2086 }
2087 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002088
Douglas Gregor68444de2012-01-14 15:13:49 +00002089 // If this is the first declaration of the template, fill in the information
2090 // for the 'common' pointer.
2091 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002092 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002093 assert(RTD->getKind() == D->getKind() &&
2094 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002095 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002096 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002097 D->setMemberSpecialization();
2098 }
2099 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002100
Richard Smithf17fdbd2014-04-24 02:25:27 +00002101 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002102 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002103
Richard Smithf17fdbd2014-04-24 02:25:27 +00002104 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002105
Richard Smithd46d6de2013-10-13 23:50:45 +00002106 // If we merged the template with a prior declaration chain, merge the common
2107 // pointer.
2108 // FIXME: Actually merge here, don't just overwrite.
2109 D->Common = D->getCanonicalDecl()->Common;
2110
Douglas Gregor68444de2012-01-14 15:13:49 +00002111 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002112}
2113
Sebastian Redlb3298c32010-08-18 23:56:48 +00002114void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002115 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002116
Douglas Gregor68444de2012-01-14 15:13:49 +00002117 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002118 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2119 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002120 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002121 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002122 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002123 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002124
2125 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2126 // We were loaded before our templated declaration was. We've not set up
2127 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2128 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002129 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002130 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002131 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002132}
2133
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002134void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2135 llvm_unreachable("BuiltinTemplates are not serialized");
2136}
2137
Larisse Voufo30616382013-08-23 22:21:36 +00002138/// TODO: Unify with ClassTemplateDecl version?
2139/// May require unifying ClassTemplateDecl and
2140/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002141void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2142 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2143
2144 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002145 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002146 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002147 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002148 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002149 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002150 }
2151}
2152
Richard Smith1d209d02013-05-23 01:49:11 +00002153ASTDeclReader::RedeclarableResult
2154ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2155 ClassTemplateSpecializationDecl *D) {
2156 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002157
Douglas Gregor4163aca2011-09-09 21:34:22 +00002158 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002159 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002160 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002161 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002162 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002163 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002164 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002165 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002166 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002167 auto *PS =
2168 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002169 SpecializedPartialSpecialization();
2170 PS->PartialSpecialization
2171 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2172 PS->TemplateArgs = ArgList;
2173 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002174 }
2175 }
2176
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002177 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002178 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002179 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002180 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002181 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002182
David L. Jonesbe1557a2016-12-21 00:17:49 +00002183 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002184 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002185 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002186 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002187 // Set this as, or find, the canonical declaration for this specialization
2188 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002189 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002190 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002191 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002192 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002193 CanonSpec =
2194 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2195 }
2196 // If there was already a canonical specialization, merge into it.
2197 if (CanonSpec != D) {
2198 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2199
2200 // This declaration might be a definition. Merge with any existing
2201 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002202 if (auto *DDD = D->DefinitionData) {
2203 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002204 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002205 else
Richard Smith053f6c62014-05-16 23:01:30 +00002206 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002207 }
Richard Smith547864d2014-07-11 18:22:58 +00002208 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002209 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002210 }
2211 }
Richard Smith1d209d02013-05-23 01:49:11 +00002212
Richard Smithd55889a2013-09-09 16:55:27 +00002213 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002214 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002215 auto *ExplicitInfo =
2216 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002217 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002218 ExplicitInfo->ExternLoc = ReadSourceLocation();
2219 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002220 D->ExplicitInfo = ExplicitInfo;
2221 }
2222
Richard Smith1d209d02013-05-23 01:49:11 +00002223 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002224}
2225
Sebastian Redlb3298c32010-08-18 23:56:48 +00002226void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002227 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002228 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002229
David L. Jonesb6a8f022016-12-21 04:34:52 +00002230 D->TemplateParams = Record.readTemplateParameterList();
2231 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002232
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002233 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002234 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002235 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002236 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002237 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002238 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002239}
2240
Sebastian Redlb7448632011-08-31 13:59:56 +00002241void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2242 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002243 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002244 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002245}
2246
Sebastian Redlb3298c32010-08-18 23:56:48 +00002247void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002248 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002249
Douglas Gregor68444de2012-01-14 15:13:49 +00002250 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002251 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002252 SmallVector<serialization::DeclID, 32> SpecIDs;
2253 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002254 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002255 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002256}
2257
Larisse Voufo30616382013-08-23 22:21:36 +00002258/// TODO: Unify with ClassTemplateSpecializationDecl version?
2259/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2260/// VarTemplate(Partial)SpecializationDecl with a new data
2261/// structure Template(Partial)SpecializationDecl, and
2262/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002263ASTDeclReader::RedeclarableResult
2264ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2265 VarTemplateSpecializationDecl *D) {
2266 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2267
2268 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002269 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002270 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002271 D->SpecializedTemplate = VTD;
2272 } else {
2273 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002274 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002275 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002276 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002277 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002278 new (C)
2279 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2280 PS->PartialSpecialization =
2281 cast<VarTemplatePartialSpecializationDecl>(InstD);
2282 PS->TemplateArgs = ArgList;
2283 D->SpecializedTemplate = PS;
2284 }
2285 }
2286
2287 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002288 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002289 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002290 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2291 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002292 ExplicitInfo->ExternLoc = ReadSourceLocation();
2293 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002294 D->ExplicitInfo = ExplicitInfo;
2295 }
2296
2297 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002298 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002299 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002300 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002301 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002302 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002303
David L. Jonesbe1557a2016-12-21 00:17:49 +00002304 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002305 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002306 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002307 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002308 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002309 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002310 CanonPattern->getCommonPtr()->PartialSpecializations
2311 .GetOrInsertNode(Partial);
2312 } else {
2313 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2314 }
2315 }
2316 }
2317
2318 return Redecl;
2319}
2320
Larisse Voufo30616382013-08-23 22:21:36 +00002321/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2322/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2323/// VarTemplate(Partial)SpecializationDecl with a new data
2324/// structure Template(Partial)SpecializationDecl, and
2325/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002326void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2327 VarTemplatePartialSpecializationDecl *D) {
2328 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2329
David L. Jonesb6a8f022016-12-21 04:34:52 +00002330 D->TemplateParams = Record.readTemplateParameterList();
2331 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002332
2333 // These are read/set from/to the first declaration.
2334 if (ThisDeclID == Redecl.getFirstID()) {
2335 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002336 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002337 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002338 }
2339}
2340
Sebastian Redlb3298c32010-08-18 23:56:48 +00002341void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002342 VisitTypeDecl(D);
2343
David L. Jonesbe1557a2016-12-21 00:17:49 +00002344 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002345
David L. Jonesbe1557a2016-12-21 00:17:49 +00002346 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002347 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002348}
2349
Sebastian Redlb3298c32010-08-18 23:56:48 +00002350void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002351 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002352 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002353 D->setDepth(Record.readInt());
2354 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002355 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002356 auto TypesAndInfos =
2357 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002358 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002359 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002360 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002361 }
2362 } else {
2363 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002364 D->ParameterPack = Record.readInt();
2365 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002366 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002367 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002368}
2369
Sebastian Redlb3298c32010-08-18 23:56:48 +00002370void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002371 VisitTemplateDecl(D);
2372 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002373 D->setDepth(Record.readInt());
2374 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002375 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002376 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002377 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2378 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002379 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002380 } else {
2381 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002382 D->ParameterPack = Record.readInt();
2383 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002384 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002385 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002386 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002387}
2388
Richard Smith3f1b5d02011-05-05 21:57:07 +00002389void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2390 VisitRedeclarableTemplateDecl(D);
2391}
2392
Sebastian Redlb3298c32010-08-18 23:56:48 +00002393void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002394 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002395 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002396 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002397 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002398 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002399}
2400
Michael Han84324352013-02-22 17:15:32 +00002401void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2402 VisitDecl(D);
2403}
2404
Mike Stump11289f42009-09-09 15:08:12 +00002405std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002406ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002407 uint64_t LexicalOffset = ReadLocalOffset();
2408 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002409 return std::make_pair(LexicalOffset, VisibleOffset);
2410}
2411
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002412template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002413ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002414ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002415 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002416 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002417
Richard Smith5fc18a92015-07-12 23:43:21 +00002418 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002419 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002420
Richard Smithd61d4ac2015-08-22 20:13:39 +00002421 uint64_t RedeclOffset = 0;
2422
Douglas Gregor358cd442012-01-15 16:58:34 +00002423 // 0 indicates that this declaration was the only declaration of its entity,
2424 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002425 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002426 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002427 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002428 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002429 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002430 // This declaration was the first local declaration, but may have imported
2431 // other declarations.
2432 IsKeyDecl = N == 1;
2433 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002434
Richard Smithfe620d22015-03-05 23:24:12 +00002435 // We have some declarations that must be before us in our redeclaration
2436 // chain. Read them now, and remember that we ought to merge with one of
2437 // them.
2438 // FIXME: Provide a known merge target to the second and subsequent such
2439 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002440 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002441 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002442
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002443 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002444 } else {
2445 // This declaration was not the first local declaration. Read the first
2446 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002447 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002448 }
2449
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002450 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002451 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002452 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2453 // We temporarily set the first (canonical) declaration as the previous one
2454 // which is the one that matters and mark the real previous DeclID to be
2455 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002456 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002457 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002458 }
Richard Smithd8a83712015-08-22 01:47:18 +00002459
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002460 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002461
2462 // Note that we need to load local redeclarations of this decl and build a
2463 // decl chain for them. This must happen *after* we perform the preloading
2464 // above; this ensures that the redeclaration chain is built in the correct
2465 // order.
2466 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002467 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002468
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002469 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002470}
2471
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002472/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002473/// of the same entity.
2474template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002475void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002476 RedeclarableResult &Redecl,
2477 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002478 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002479 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002480 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002481
Richard Smith202850a2015-03-10 02:57:50 +00002482 // If we're not the canonical declaration, we don't need to merge.
2483 if (!DBase->isFirstDecl())
2484 return;
2485
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002486 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002487
Richard Smith5a4737c2015-02-06 02:42:59 +00002488 if (auto *Existing = Redecl.getKnownMergeTarget())
2489 // We already know of an existing declaration we should merge with.
2490 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2491 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002492 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002493 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2494}
2495
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002496/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002497/// We use this to put code in a template that will only be valid for certain
2498/// instantiations.
2499template<typename T> static T assert_cast(T t) { return t; }
2500template<typename T> static T assert_cast(...) {
2501 llvm_unreachable("bad assert_cast");
2502}
2503
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002504/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002505/// declarations.
2506void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2507 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002508 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002509 auto *DPattern = D->getTemplatedDecl();
2510 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002511 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2512 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002513 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002514
Richard Smith547864d2014-07-11 18:22:58 +00002515 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2516 // Merge with any existing definition.
2517 // FIXME: This is duplicated in several places. Refactor.
2518 auto *ExistingClass =
2519 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002520 if (auto *DDD = DClass->DefinitionData) {
2521 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002522 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002523 } else {
2524 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002525 // We may have skipped this before because we thought that DClass
2526 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002527 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002528 }
2529 }
2530 DClass->DefinitionData = ExistingClass->DefinitionData;
2531
Richard Smithf17fdbd2014-04-24 02:25:27 +00002532 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2533 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002534 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002535 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2536 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2537 Result);
2538 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2539 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002540 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2541 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2542 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002543 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002544}
2545
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002546/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002547/// of the same entity.
2548template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002549void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2550 RedeclarableResult &Redecl,
2551 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002552 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002553 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002554 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002555 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002556 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2557 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002558
Richard Smithd55889a2013-09-09 16:55:27 +00002559 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002560 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002561 // appropriate canonical declaration.
2562 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002563 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002564 ExistingCanon->Used |= D->Used;
2565 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002566
2567 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002568 // We cannot have loaded any redeclarations of this declaration yet, so
2569 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002570 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002571 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002572 assert_cast<NamespaceDecl*>(ExistingCanon));
2573
2574 // When we merge a template, merge its pattern.
2575 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2576 mergeTemplatePattern(
2577 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002578 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002579
Richard Smith5fc18a92015-07-12 23:43:21 +00002580 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002581 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002582 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002583 }
2584}
2585
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002586/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2587/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2588/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2589/// that some types are mergeable during deserialization, otherwise name
2590/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002591static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002592 if (!ND)
2593 return false;
2594 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002595 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002596 return true;
2597 return false;
2598}
2599
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002600/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002601/// of the same entity, for the case where the entity is not actually
2602/// redeclarable. This happens, for instance, when merging the fields of
2603/// identical class definitions from two different modules.
2604template<typename T>
2605void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2606 // If modules are not available, there is no reason to perform this merge.
2607 if (!Reader.getContext().getLangOpts().Modules)
2608 return;
2609
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002610 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2611 // Note that C identically-named things in different translation units are
2612 // not redeclarations, but may still have compatible types, where ODR-like
2613 // semantics may apply.
2614 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2615 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002616 return;
2617
Richard Smith0b87e072013-10-07 08:02:11 +00002618 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2619 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002620 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2621 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002622}
2623
Alexey Bataeva769e072013-03-22 06:34:35 +00002624void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2625 VisitDecl(D);
2626 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002627 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002628 Vars.reserve(NumVars);
2629 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002630 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002631 }
2632 D->setVars(Vars);
2633}
2634
Kelvin Li1408f912018-09-26 04:28:39 +00002635void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
2636 VisitDecl(D);
2637 unsigned NumClauses = D->clauselist_size();
2638 SmallVector<OMPClause *, 8> Clauses;
2639 Clauses.reserve(NumClauses);
2640 OMPClauseReader ClauseReader(Record);
2641 for (unsigned I = 0; I != NumClauses; ++I)
2642 Clauses.push_back(ClauseReader.readClause());
2643 D->setClauses(Clauses);
2644}
2645
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002646void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002647 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002648 D->setLocation(ReadSourceLocation());
Alexey Bataeve6aa4692018-09-13 16:54:05 +00002649 Expr *In = Record.readExpr();
2650 Expr *Out = Record.readExpr();
2651 D->setCombinerData(In, Out);
2652 Expr *Combiner = Record.readExpr();
2653 D->setCombiner(Combiner);
2654 Expr *Orig = Record.readExpr();
2655 Expr *Priv = Record.readExpr();
2656 D->setInitializerData(Orig, Priv);
2657 Expr *Init = Record.readExpr();
2658 auto IK = static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt());
2659 D->setInitializer(Init, IK);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002660 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002661}
2662
Alexey Bataev4244be22016-02-11 05:35:55 +00002663void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002664 VisitVarDecl(D);
2665}
2666
Chris Lattner487412d2009-04-27 05:27:42 +00002667//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002668// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002669//===----------------------------------------------------------------------===//
2670
Richard Smithe43e2b32018-08-20 21:47:29 +00002671namespace {
2672class AttrReader {
2673 ModuleFile *F;
2674 ASTReader *Reader;
2675 const ASTReader::RecordData &Record;
2676 unsigned &Idx;
2677
2678public:
2679 AttrReader(ModuleFile &F, ASTReader &Reader,
2680 const ASTReader::RecordData &Record, unsigned &Idx)
2681 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
2682
2683 const uint64_t &readInt() { return Record[Idx++]; }
2684
2685 SourceRange readSourceRange() {
2686 return Reader->ReadSourceRange(*F, Record, Idx);
2687 }
2688
2689 Expr *readExpr() { return Reader->ReadExpr(*F); }
2690
2691 std::string readString() {
2692 return Reader->ReadString(Record, Idx);
2693 }
2694
2695 TypeSourceInfo *getTypeSourceInfo() {
2696 return Reader->GetTypeSourceInfo(*F, Record, Idx);
2697 }
2698
2699 IdentifierInfo *getIdentifierInfo() {
2700 return Reader->GetIdentifierInfo(*F, Record, Idx);
2701 }
2702
2703 VersionTuple readVersionTuple() {
2704 return ASTReader::ReadVersionTuple(Record, Idx);
2705 }
2706
2707 template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
2708 return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
2709 }
2710};
2711}
2712
2713Attr *ASTReader::ReadAttr(ModuleFile &M, const RecordData &Rec,
2714 unsigned &Idx) {
2715 AttrReader Record(M, *this, Rec, Idx);
2716 auto V = Record.readInt();
2717 if (!V)
2718 return nullptr;
2719
2720 Attr *New = nullptr;
2721 // Kind is stored as a 1-based integer because 0 is used to indicate a null
2722 // Attr pointer.
2723 auto Kind = static_cast<attr::Kind>(V - 1);
2724 SourceRange Range = Record.readSourceRange();
2725 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002726
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002727#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002728
Richard Smithe43e2b32018-08-20 21:47:29 +00002729 assert(New && "Unable to decode attribute?");
2730 return New;
2731}
2732
2733/// Reads attributes from the current stream position.
2734void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2735 for (unsigned I = 0, E = Record.readInt(); I != E; ++I)
2736 Attrs.push_back(Record.readAttr());
Chris Lattner8f63ab52009-04-27 06:01:06 +00002737}
2738
2739//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002740// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002741//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002742
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002743/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002744/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002745///
Chris Lattner487412d2009-04-27 05:27:42 +00002746/// This routine notes that this declaration has already been loaded,
2747/// so that future GetDecl calls will return this declaration rather
2748/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002749inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002750 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2751 DeclsLoaded[Index] = D;
2752}
2753
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002754/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002755/// this declaration (via HandleTopLevelDecl).
2756///
2757/// This routine should return true for anything that might affect
2758/// code generation, e.g., inline function definitions, Objective-C
2759/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002760static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002761 // An ObjCMethodDecl is never considered as "interesting" because its
2762 // implementation container always is.
2763
Richard Smithcd4a7a42017-09-07 00:55:55 +00002764 // An ImportDecl or VarDecl imported from a module map module will get
2765 // emitted when we import the relevant module.
2766 if (isa<ImportDecl>(D) || isa<VarDecl>(D)) {
2767 auto *M = D->getImportedOwningModule();
2768 if (M && M->Kind == Module::ModuleMapModule &&
2769 Ctx.DeclMustBeEmitted(D))
2770 return false;
2771 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002772
Fangrui Song6907ce22018-07-30 19:24:48 +00002773 if (isa<FileScopeAsmDecl>(D) ||
2774 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002775 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002776 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002777 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002778 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002779 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002780 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2781 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002782 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002783 return Var->isFileVarDecl() &&
Alexey Bataevd01b7492018-08-15 19:45:12 +00002784 (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
2785 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002786 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002787 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002788
2789 if (auto *ES = D->getASTContext().getExternalSource())
2790 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2791 return true;
2792
Douglas Gregor87d81242011-09-10 00:22:34 +00002793 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002794}
2795
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002796/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002797ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002798ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002799 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2800 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002801 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002802 const DeclOffset &DOffs =
2803 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002804 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002805 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002806}
2807
Douglas Gregord32f0352011-07-22 06:10:01 +00002808ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002809 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002810
2811 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2812 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2813}
2814
Douglas Gregorde3ef502011-11-30 23:21:26 +00002815uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002816 return LocalOffset + M.GlobalBitOffset;
2817}
2818
Richard Smithbf78e642013-06-24 22:51:00 +00002819static bool isSameTemplateParameterList(const TemplateParameterList *X,
2820 const TemplateParameterList *Y);
2821
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002822/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002823/// that they may be used in declarations of the same template.
2824static bool isSameTemplateParameter(const NamedDecl *X,
2825 const NamedDecl *Y) {
2826 if (X->getKind() != Y->getKind())
2827 return false;
2828
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002829 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2830 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002831 return TX->isParameterPack() == TY->isParameterPack();
2832 }
2833
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002834 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2835 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002836 return TX->isParameterPack() == TY->isParameterPack() &&
2837 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2838 }
2839
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002840 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2841 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002842 return TX->isParameterPack() == TY->isParameterPack() &&
2843 isSameTemplateParameterList(TX->getTemplateParameters(),
2844 TY->getTemplateParameters());
2845}
2846
Richard Smith32952e12014-10-14 02:00:47 +00002847static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2848 if (auto *NS = X->getAsNamespace())
2849 return NS;
2850 if (auto *NAS = X->getAsNamespaceAlias())
2851 return NAS->getNamespace();
2852 return nullptr;
2853}
2854
2855static bool isSameQualifier(const NestedNameSpecifier *X,
2856 const NestedNameSpecifier *Y) {
2857 if (auto *NSX = getNamespace(X)) {
2858 auto *NSY = getNamespace(Y);
2859 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2860 return false;
2861 } else if (X->getKind() != Y->getKind())
2862 return false;
2863
2864 // FIXME: For namespaces and types, we're permitted to check that the entity
2865 // is named via the same tokens. We should probably do so.
2866 switch (X->getKind()) {
2867 case NestedNameSpecifier::Identifier:
2868 if (X->getAsIdentifier() != Y->getAsIdentifier())
2869 return false;
2870 break;
2871 case NestedNameSpecifier::Namespace:
2872 case NestedNameSpecifier::NamespaceAlias:
2873 // We've already checked that we named the same namespace.
2874 break;
2875 case NestedNameSpecifier::TypeSpec:
2876 case NestedNameSpecifier::TypeSpecWithTemplate:
2877 if (X->getAsType()->getCanonicalTypeInternal() !=
2878 Y->getAsType()->getCanonicalTypeInternal())
2879 return false;
2880 break;
2881 case NestedNameSpecifier::Global:
2882 case NestedNameSpecifier::Super:
2883 return true;
2884 }
2885
2886 // Recurse into earlier portion of NNS, if any.
2887 auto *PX = X->getPrefix();
2888 auto *PY = Y->getPrefix();
2889 if (PX && PY)
2890 return isSameQualifier(PX, PY);
2891 return !PX && !PY;
2892}
2893
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002894/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002895/// that they may be used in declarations of the same template.
2896static bool isSameTemplateParameterList(const TemplateParameterList *X,
2897 const TemplateParameterList *Y) {
2898 if (X->size() != Y->size())
2899 return false;
2900
2901 for (unsigned I = 0, N = X->size(); I != N; ++I)
2902 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2903 return false;
2904
2905 return true;
2906}
2907
George Burgess IV95845082017-02-15 22:43:27 +00002908/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002909/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002910static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2911 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002912 // Note that pass_object_size attributes are represented in the function's
2913 // ExtParameterInfo, so we don't need to check them here.
2914
George Burgess IV95845082017-02-15 22:43:27 +00002915 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00002916 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
2917 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
Michael Kruse157a3552018-12-10 15:16:37 +00002918
2919 for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) {
2920 Optional<EnableIfAttr *> Cand1A = std::get<0>(Pair);
2921 Optional<EnableIfAttr *> Cand2A = std::get<1>(Pair);
2922
2923 // Return false if the number of enable_if attributes is different.
2924 if (!Cand1A || !Cand2A)
2925 return false;
2926
George Burgess IV95845082017-02-15 22:43:27 +00002927 Cand1ID.clear();
2928 Cand2ID.clear();
2929
Michael Kruse157a3552018-12-10 15:16:37 +00002930 (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2931 (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2932
2933 // Return false if any of the enable_if expressions of A and B are
2934 // different.
George Burgess IV95845082017-02-15 22:43:27 +00002935 if (Cand1ID != Cand2ID)
2936 return false;
2937 }
Michael Kruse157a3552018-12-10 15:16:37 +00002938 return true;
George Burgess IV95845082017-02-15 22:43:27 +00002939}
2940
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002941/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002942static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2943 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002944
Douglas Gregor022857e2011-12-22 01:48:48 +00002945 if (X == Y)
2946 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002947
Douglas Gregor022857e2011-12-22 01:48:48 +00002948 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00002949 //
2950 // Note that we can't use DeclContext::Equals here, because the DeclContexts
2951 // could be two different declarations of the same function. (We will fix the
2952 // semantic DC to refer to the primary definition after merging.)
2953 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
2954 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00002955 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002956
2957 // Two typedefs refer to the same entity if they have the same underlying
2958 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002959 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
2960 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002961 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2962 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002963
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002964 // Must have the same kind.
2965 if (X->getKind() != Y->getKind())
2966 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002967
Douglas Gregorda389302012-01-01 21:47:52 +00002968 // Objective-C classes and protocols with the same name always match.
2969 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002970 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002971
2972 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002973 // No need to handle these here: we merge them when adding them to the
2974 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002975 return false;
2976 }
Richard Smithd55889a2013-09-09 16:55:27 +00002977
Douglas Gregor2009cee2012-01-03 22:46:00 +00002978 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002979 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
2980 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00002981 return (TagX->getTagKind() == TagY->getTagKind()) ||
2982 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2983 TagX->getTagKind() == TTK_Interface) &&
2984 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2985 TagY->getTagKind() == TTK_Interface));
2986 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002987
Joao Matose9a3ed42012-08-31 22:18:20 +00002988 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002989 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2990 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002991 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
2992 const auto *FuncY = cast<FunctionDecl>(Y);
2993 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2994 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002995 if (CtorX->getInheritedConstructor() &&
2996 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2997 CtorY->getInheritedConstructor().getConstructor()))
2998 return false;
2999 }
Erich Keane281d20b2018-01-08 21:34:17 +00003000
3001 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
3002 return false;
3003
3004 // Multiversioned functions with different feature strings are represented
3005 // as separate declarations.
3006 if (FuncX->isMultiVersion()) {
3007 const auto *TAX = FuncX->getAttr<TargetAttr>();
3008 const auto *TAY = FuncY->getAttr<TargetAttr>();
3009 assert(TAX && TAY && "Multiversion Function without target attribute");
3010
3011 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
3012 return false;
3013 }
3014
Richard Smitha54d3242017-03-08 23:00:26 +00003015 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00003016 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
3017 // Map to the first declaration that we've already merged into this one.
3018 // The TSI of redeclarations might not match (due to calling conventions
3019 // being inherited onto the type but not the TSI), but the TSI type of
3020 // the first declaration of the function should match across modules.
3021 FD = FD->getCanonicalDecl();
3022 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
3023 : FD->getType();
3024 };
3025 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
3026 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00003027 // We can get functions with different types on the redecl chain in C++17
3028 // if they have differing exception specifications and at least one of
3029 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00003030 auto *XFPT = XT->getAs<FunctionProtoType>();
3031 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003032 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00003033 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
3034 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00003035 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00003036 return true;
3037 return false;
3038 }
George Burgess IV95845082017-02-15 22:43:27 +00003039 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00003040 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00003041 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003042
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003043 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003044 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
3045 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00003046 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
3047 ASTContext &C = VarX->getASTContext();
3048 if (C.hasSameType(VarX->getType(), VarY->getType()))
3049 return true;
3050
3051 // We can get decls with different types on the redecl chain. Eg.
3052 // template <typename T> struct S { static T Var[]; }; // #1
3053 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
3054 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00003055 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00003056 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
3057 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
3058 if (!VarXTy || !VarYTy)
3059 return false;
3060 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
3061 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
3062 }
3063 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003064 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003065
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003066 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003067 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
3068 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003069 return NamespaceX->isInline() == NamespaceY->isInline();
3070 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003071
Richard Smith8f8f05c2013-06-24 04:45:28 +00003072 // Identical template names and kinds match if their template parameter lists
3073 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003074 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
3075 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00003076 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00003077 TemplateY->getTemplatedDecl()) &&
3078 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3079 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003080 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003081
Richard Smith0b87e072013-10-07 08:02:11 +00003082 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003083 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3084 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003085 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003086 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3087 }
3088
Richard Smith8cbd8952015-08-04 02:05:09 +00003089 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003090 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3091 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003092 return IFDX->getAnonField()->getCanonicalDecl() ==
3093 IFDY->getAnonField()->getCanonicalDecl();
3094 }
3095
Richard Smith01a73372013-10-15 22:02:41 +00003096 // Enumerators with the same name match.
3097 if (isa<EnumConstantDecl>(X))
3098 // FIXME: Also check the value is odr-equivalent.
3099 return true;
3100
Richard Smithfd8634a2013-10-23 02:17:46 +00003101 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003102 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3103 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003104 return USX->getTargetDecl() == USY->getTargetDecl();
3105 }
3106
Richard Smith32952e12014-10-14 02:00:47 +00003107 // Using declarations with the same qualifier match. (We already know that
3108 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003109 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3110 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003111 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3112 UX->hasTypename() == UY->hasTypename() &&
3113 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3114 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003115 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3116 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003117 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3118 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3119 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003120 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003121 return isSameQualifier(
3122 UX->getQualifier(),
3123 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3124
Richard Smithf4634362014-09-03 23:11:22 +00003125 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003126 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3127 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003128 return NAX->getNamespace()->Equals(NAY->getNamespace());
3129 }
3130
Douglas Gregor022857e2011-12-22 01:48:48 +00003131 return false;
3132}
3133
Richard Smithd55889a2013-09-09 16:55:27 +00003134/// Find the context in which we should search for previous declarations when
3135/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003136DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3137 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003138 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003139 return ND->getOriginalNamespace();
3140
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003141 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003142 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003143 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003144 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003145 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003146
3147 // If there's no definition yet, then DC's definition is added by an update
3148 // record, but we've not yet loaded that update record. In this case, we
3149 // commit to DC being the canonical definition now, and will fix this when
3150 // we load the update record.
3151 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003152 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003153 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003154 RD->DefinitionData = DD;
3155 RD->getCanonicalDecl()->DefinitionData = DD;
3156
3157 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003158 Reader.PendingFakeDefinitionData.insert(
3159 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003160 }
3161
3162 return DD->Definition;
3163 }
Richard Smithd55889a2013-09-09 16:55:27 +00003164
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003165 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003166 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3167 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003168
Richard Smith4eca9b92015-02-04 23:37:59 +00003169 // We can see the TU here only if we have no Sema object. In that case,
3170 // there's no TU scope to look in, so using the DC alone is sufficient.
3171 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3172 return TU;
3173
Craig Toppera13603a2014-05-22 05:54:18 +00003174 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003175}
3176
Douglas Gregor022857e2011-12-22 01:48:48 +00003177ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003178 // Record that we had a typedef name for linkage whether or not we merge
3179 // with that declaration.
3180 if (TypedefNameForLinkage) {
3181 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3182 Reader.ImportedTypedefNamesForLinkage.insert(
3183 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3184 return;
3185 }
3186
Douglas Gregor9b47f942012-01-09 17:38:47 +00003187 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003188 return;
Richard Smith95d99302013-07-13 02:00:19 +00003189
Richard Smith70d58502014-08-30 00:04:23 +00003190 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003191 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003192 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003193 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3194 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003195 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003196 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003197 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003198 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3199 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003200 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003201 // Add the declaration to its redeclaration context so later merging
3202 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003203 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003204 }
3205}
3206
Richard Smith88ebade2014-08-23 01:45:27 +00003207/// Find the declaration that should be merged into, given the declaration found
3208/// by name lookup. If we're merging an anonymous declaration within a typedef,
3209/// we need a matching typedef, and we merge with the type inside it.
3210static NamedDecl *getDeclForMerging(NamedDecl *Found,
3211 bool IsTypedefNameForLinkage) {
3212 if (!IsTypedefNameForLinkage)
3213 return Found;
3214
3215 // If we found a typedef declaration that gives a name to some other
3216 // declaration, then we want that inner declaration. Declarations from
3217 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003218 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003219 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003220
3221 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003222 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003223
Hans Wennborgdcfba332015-10-06 23:40:43 +00003224 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003225}
3226
Richard Smith600adef2018-07-04 02:25:38 +00003227/// Find the declaration to use to populate the anonymous declaration table
3228/// for the given lexical DeclContext. We only care about finding local
3229/// definitions of the context; we'll merge imported ones as we go.
3230DeclContext *
3231ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3232 // For classes, we track the definition as we merge.
3233 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3234 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3235 return DD ? DD->Definition : nullptr;
3236 }
3237
3238 // For anything else, walk its merged redeclarations looking for a definition.
3239 // Note that we can't just call getDefinition here because the redeclaration
3240 // chain isn't wired up.
3241 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3242 if (auto *FD = dyn_cast<FunctionDecl>(D))
3243 if (FD->isThisDeclarationADefinition())
3244 return FD;
3245 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3246 if (MD->isThisDeclarationADefinition())
3247 return MD;
3248 }
3249
3250 // No merged definition yet.
3251 return nullptr;
3252}
3253
Richard Smithd08aeb62014-08-28 01:33:39 +00003254NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3255 DeclContext *DC,
3256 unsigned Index) {
3257 // If the lexical context has been merged, look into the now-canonical
3258 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003259 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003260
3261 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003262 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003263 if (Index < Previous.size() && Previous[Index])
3264 return Previous[Index];
3265
3266 // If this is the first time, but we have parsed a declaration of the context,
3267 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003268 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3269 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3270 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003271 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003272 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3273 else
Richard Smith2b560572015-02-07 03:11:11 +00003274 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3275 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003276 }
3277
3278 return Index < Previous.size() ? Previous[Index] : nullptr;
3279}
3280
3281void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3282 DeclContext *DC, unsigned Index,
3283 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003284 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003285
Richard Smith600adef2018-07-04 02:25:38 +00003286 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003287 if (Index >= Previous.size())
3288 Previous.resize(Index + 1);
3289 if (!Previous[Index])
3290 Previous[Index] = D;
3291}
3292
Douglas Gregor022857e2011-12-22 01:48:48 +00003293ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003294 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3295 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003296
Richard Smithd08aeb62014-08-28 01:33:39 +00003297 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3298 // Don't bother trying to find unnamed declarations that are in
3299 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003300 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3301 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003302 Result.suppress();
3303 return Result;
3304 }
Richard Smith95d99302013-07-13 02:00:19 +00003305
Douglas Gregor022857e2011-12-22 01:48:48 +00003306 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003307 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003308 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003309 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003310 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3311 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003312 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3313 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003314 // Go on to check in other places in case an existing typedef name
3315 // was not imported.
3316 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003317
Richard Smith2b560572015-02-07 03:11:11 +00003318 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003319 // This is an anonymous declaration that we may need to merge. Look it up
3320 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003321 if (auto *Existing = getAnonymousDeclForMerging(
3322 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3323 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003324 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3325 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003326 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003327 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003328 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003329
3330 // Temporarily consider the identifier to be up-to-date. We don't want to
3331 // cause additional lookups here.
3332 class UpToDateIdentifierRAII {
3333 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003334 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003335
3336 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003337 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003338 if (II) {
3339 WasOutToDate = II->isOutOfDate();
3340 if (WasOutToDate)
3341 II->setOutOfDate(false);
3342 }
3343 }
3344
3345 ~UpToDateIdentifierRAII() {
3346 if (WasOutToDate)
3347 II->setOutOfDate(true);
3348 }
3349 } UpToDate(Name.getAsIdentifierInfo());
3350
Fangrui Song6907ce22018-07-30 19:24:48 +00003351 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003352 IEnd = IdResolver.end();
3353 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003354 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003355 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003356 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3357 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003358 }
Richard Smith8a639892015-01-24 01:07:20 +00003359 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003360 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003361 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003362 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003363 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003364 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3365 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003366 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003367 } else {
3368 // Not in a mergeable context.
3369 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003370 }
Richard Smithd55889a2013-09-09 16:55:27 +00003371
Richard Smith2b9e3e32013-10-18 06:05:18 +00003372 // If this declaration is from a merged context, make a note that we need to
3373 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003374 //
3375 // FIXME: We should do something similar if we merge two definitions of the
3376 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003377 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3378 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3379 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003380 Reader.PendingOdrMergeChecks.push_back(D);
3381
Richard Smithd08aeb62014-08-28 01:33:39 +00003382 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003383 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003384}
3385
Richard Smithb321ecb2014-05-13 01:15:00 +00003386template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003387Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3388 return D->RedeclLink.getLatestNotUpdated();
3389}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003390
Richard Smithc3a53252015-02-28 05:57:02 +00003391Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3392 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3393}
3394
3395Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3396 assert(D);
3397
3398 switch (D->getKind()) {
3399#define ABSTRACT_DECL(TYPE)
3400#define DECL(TYPE, BASE) \
3401 case Decl::TYPE: \
3402 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3403#include "clang/AST/DeclNodes.inc"
3404 }
3405 llvm_unreachable("unknown decl kind");
3406}
3407
Richard Smith8ce51082015-03-11 01:44:51 +00003408Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3409 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3410}
3411
Richard Smithc3a53252015-02-28 05:57:02 +00003412template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003413void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3414 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003415 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003416 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003417 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003418}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003419
Richard Smith24d166c2014-07-31 23:52:38 +00003420namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003421
Richard Smith6de7a242014-07-31 23:46:44 +00003422template<>
3423void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003424 Redeclarable<VarDecl> *D,
3425 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003426 auto *VD = static_cast<VarDecl *>(D);
3427 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003428 D->RedeclLink.setPrevious(PrevVD);
3429 D->First = PrevVD->First;
3430
3431 // We should keep at most one definition on the chain.
3432 // FIXME: Cache the definition once we've found it. Building a chain with
3433 // N definitions currently takes O(N^2) time here.
3434 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3435 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3436 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3437 Reader.mergeDefinitionVisibility(CurD, VD);
3438 VD->demoteThisDefinitionToDeclaration();
3439 break;
3440 }
3441 }
3442 }
3443}
3444
Richard Smitha62d1982018-08-03 01:00:01 +00003445static bool isUndeducedReturnType(QualType T) {
3446 auto *DT = T->getContainedDeducedType();
3447 return DT && !DT->isDeduced();
3448}
3449
Richard Smithedbc6e92016-10-14 21:41:24 +00003450template<>
3451void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003452 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003453 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003454 auto *FD = static_cast<FunctionDecl *>(D);
3455 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003456
3457 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003458 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003459
3460 // If the previous declaration is an inline function declaration, then this
3461 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003462 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003463 // FIXME: [dcl.fct.spec]p4:
3464 // If a function with external linkage is declared inline in one
3465 // translation unit, it shall be declared inline in all translation
3466 // units in which it appears.
3467 //
3468 // Be careful of this case:
3469 //
3470 // module A:
3471 // template<typename T> struct X { void f(); };
3472 // template<typename T> inline void X<T>::f() {}
3473 //
3474 // module B instantiates the declaration of X<int>::f
3475 // module C instantiates the definition of X<int>::f
3476 //
3477 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003478 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003479 }
3480
Richard Smith6de7a242014-07-31 23:46:44 +00003481 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3482 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003483 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003484 // If we need to propagate an exception specification along the redecl
3485 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003486 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3487 bool WasUnresolved =
3488 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3489 if (IsUnresolved != WasUnresolved)
3490 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003491 {Canon, IsUnresolved ? PrevFD : FD});
3492
3493 // If we need to propagate a deduced return type along the redecl chain,
3494 // make a note of that so that we can do it later.
3495 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3496 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3497 if (IsUndeduced != WasUndeduced)
3498 Reader.PendingDeducedTypeUpdates.insert(
3499 {cast<FunctionDecl>(Canon),
3500 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003501 }
3502}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003503
3504} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003505
Richard Smith6de7a242014-07-31 23:46:44 +00003506void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003507 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3508}
3509
Richard Smith8346e522015-06-10 01:47:58 +00003510/// Inherit the default template argument from \p From to \p To. Returns
3511/// \c false if there is no default template for \p From.
3512template <typename ParmDecl>
3513static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3514 Decl *ToD) {
3515 auto *To = cast<ParmDecl>(ToD);
3516 if (!From->hasDefaultArgument())
3517 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003518 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003519 return true;
3520}
3521
3522static void inheritDefaultTemplateArguments(ASTContext &Context,
3523 TemplateDecl *From,
3524 TemplateDecl *To) {
3525 auto *FromTP = From->getTemplateParameters();
3526 auto *ToTP = To->getTemplateParameters();
3527 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3528
3529 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003530 NamedDecl *FromParam = FromTP->getParam(I);
3531 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003532
Richard Smith559cc692018-07-31 21:01:53 +00003533 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3534 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3535 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3536 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3537 else
3538 inheritDefaultTemplateArgument(
3539 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003540 }
3541}
3542
Richard Smith6de7a242014-07-31 23:46:44 +00003543void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003544 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003545 assert(D && Previous);
3546
3547 switch (D->getKind()) {
3548#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003549#define DECL(TYPE, BASE) \
3550 case Decl::TYPE: \
3551 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003552 break;
3553#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003554 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003555
3556 // If the declaration was visible in one module, a redeclaration of it in
3557 // another module remains visible even if it wouldn't be visible by itself.
3558 //
3559 // FIXME: In this case, the declaration should only be visible if a module
3560 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003561 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003562 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003563 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003564
Richard Smith8346e522015-06-10 01:47:58 +00003565 // If the declaration declares a template, it may inherit default arguments
3566 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003567 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003568 inheritDefaultTemplateArguments(Reader.getContext(),
3569 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003570}
3571
Richard Smithb321ecb2014-05-13 01:15:00 +00003572template<typename DeclT>
3573void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003574 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003575}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003576
Richard Smithb321ecb2014-05-13 01:15:00 +00003577void ASTDeclReader::attachLatestDeclImpl(...) {
3578 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3579}
3580
Douglas Gregor05f10352011-12-17 23:38:30 +00003581void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3582 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003583
3584 switch (D->getKind()) {
3585#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003586#define DECL(TYPE, BASE) \
3587 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003588 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3589 break;
3590#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003591 }
3592}
3593
Richard Smith851072e2014-05-19 20:59:20 +00003594template<typename DeclT>
3595void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3596 D->RedeclLink.markIncomplete();
3597}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003598
Richard Smith851072e2014-05-19 20:59:20 +00003599void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3600 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3601}
3602
3603void ASTReader::markIncompleteDeclChain(Decl *D) {
3604 switch (D->getKind()) {
3605#define ABSTRACT_DECL(TYPE)
3606#define DECL(TYPE, BASE) \
3607 case Decl::TYPE: \
3608 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3609 break;
3610#include "clang/AST/DeclNodes.inc"
3611 }
3612}
3613
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003614/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003615Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003616 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003617 SourceLocation DeclLoc;
3618 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003619 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003620 // Keep track of where we are in the stream, then jump back there
3621 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003622 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003623
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003624 ReadingKindTracker ReadingKind(Read_Decl, *this);
3625
Douglas Gregor1342e842009-07-06 18:54:52 +00003626 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003627 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003628
Sebastian Redl2c373b92010-10-05 15:59:54 +00003629 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003630 ASTRecordReader Record(*this, *Loc.F);
3631 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003632 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003633
Richard Smithdbafb6c2017-06-29 23:23:46 +00003634 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003635 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003636 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003637 case DECL_CONTEXT_LEXICAL:
3638 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003639 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003640 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003641 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003642 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003643 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003644 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003645 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003646 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003647 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003648 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003649 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003650 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003651 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003652 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003653 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003654 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003655 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003656 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003657 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003658 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003659 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003660 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003661 case DECL_EXPORT:
3662 D = ExportDecl::CreateDeserialized(Context, ID);
3663 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003664 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003665 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003666 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003667 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003668 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003669 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003670 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003671 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003672 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003673 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003674 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003675 break;
Richard Smith151c4562016-12-20 21:35:28 +00003676 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003677 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003678 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003679 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003680 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003681 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003682 case DECL_CONSTRUCTOR_USING_SHADOW:
3683 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3684 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003685 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003686 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003687 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003688 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003689 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003690 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003691 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003692 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003693 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003694 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003695 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003696 break;
Richard Smithbc491202017-02-17 20:05:37 +00003697 case DECL_CXX_DEDUCTION_GUIDE:
3698 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3699 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003700 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003701 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003702 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003703 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003704 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3705 break;
3706 case DECL_CXX_INHERITED_CONSTRUCTOR:
3707 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003708 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003709 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003710 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003711 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003712 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003713 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003714 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003715 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003716 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003717 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003718 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003719 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003720 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003721 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003722 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003723 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003724 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003725 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003726 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003727 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003728 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003729 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003730 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003731 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003732 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003733 case DECL_VAR_TEMPLATE:
3734 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3735 break;
3736 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3737 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3738 break;
3739 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3740 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3741 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003742 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003743 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003744 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003745 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003746 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003747 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003748 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003749 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003750 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003751 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003752 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003753 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003754 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003755 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3756 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003757 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003758 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003759 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003760 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003761 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3762 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003763 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003764 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003765 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003766 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003767 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003768 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003769 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003770 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003771 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003772 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003773 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003774 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003775 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003776 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003777 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003778 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003779 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003780 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003781 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003782 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003783 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003784 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003785 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003786 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003787 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003788 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003789 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003790 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003791 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003792 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003793 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003794 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003795 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003796 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003797 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003798 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003799 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003800 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003801 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003802 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003803 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003804 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003805 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003806 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003807 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003808 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003809 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003810 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003811 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003812 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003813 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003814 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003815 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003816 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003817 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003818 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003819 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003820 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003821 break;
3822 case DECL_BINDING:
3823 D = BindingDecl::CreateDeserialized(Context, ID);
3824 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003825 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003826 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003827 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003828 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003829 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003830 break;
John McCall5e77d762013-04-16 07:28:30 +00003831 case DECL_MS_PROPERTY:
3832 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3833 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003834 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003835 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003836 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003837 case DECL_CXX_BASE_SPECIFIERS:
3838 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003839 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003840 case DECL_CXX_CTOR_INITIALIZERS:
3841 Error("attempt to read a C++ ctor initializer record as a declaration");
3842 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003843 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003844 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003845 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003846 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003847 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003848 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003849 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003850 break;
Kelvin Li1408f912018-09-26 04:28:39 +00003851 case DECL_OMP_REQUIRES:
3852 D = OMPRequiresDecl::CreateDeserialized(Context, ID, Record.readInt());
3853 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003854 case DECL_OMP_DECLARE_REDUCTION:
3855 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3856 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003857 case DECL_OMP_CAPTUREDEXPR:
3858 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003859 break;
Nico Weber66220292016-03-02 17:28:48 +00003860 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003861 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003862 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003863 case DECL_PRAGMA_DETECT_MISMATCH:
3864 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003865 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003866 break;
Michael Han84324352013-02-22 17:15:32 +00003867 case DECL_EMPTY:
3868 D = EmptyDecl::CreateDeserialized(Context, ID);
3869 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003870 case DECL_OBJC_TYPE_PARAM:
3871 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3872 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003873 }
Chris Lattner487412d2009-04-27 05:27:42 +00003874
Sebastian Redlb3298c32010-08-18 23:56:48 +00003875 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003876 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003877 // Set the DeclContext before doing any deserialization, to make sure internal
3878 // calls to Decl::getASTContext() by Decl's methods will find the
3879 // TranslationUnitDecl without crashing.
3880 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003881 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003882
3883 // If this declaration is also a declaration context, get the
3884 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003885 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003886 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003887 if (Offsets.first &&
3888 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3889 return nullptr;
3890 if (Offsets.second &&
3891 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3892 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003893 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003894 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003895
Douglas Gregordab42432011-08-12 00:15:20 +00003896 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003897 PendingUpdateRecords.push_back(
3898 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003899
Douglas Gregor404cdde2012-01-27 01:47:08 +00003900 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003901 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003902 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3903 // we put the Decl in PendingDefinitions so we can pull the categories here.
3904 if (Class->isThisDeclarationADefinition() ||
3905 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003906 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00003907
Richard Smith13fb8602016-07-15 21:33:46 +00003908 // If we have deserialized a declaration that has a definition the
3909 // AST consumer might need to know about, queue it.
3910 // We don't pass it to the consumer immediately because we may be in recursive
3911 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003912 PotentiallyInterestingDecls.push_back(
3913 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003914
Douglas Gregordab42432011-08-12 00:15:20 +00003915 return D;
3916}
3917
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003918void ASTReader::PassInterestingDeclsToConsumer() {
3919 assert(Consumer);
3920
3921 if (PassingDeclsToConsumer)
3922 return;
3923
3924 // Guard variable to avoid recursively redoing the process of passing
3925 // decls to consumer.
3926 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3927 true);
3928
3929 // Ensure that we've loaded all potentially-interesting declarations
3930 // that need to be eagerly loaded.
3931 for (auto ID : EagerlyDeserializedDecls)
3932 GetDecl(ID);
3933 EagerlyDeserializedDecls.clear();
3934
3935 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003936 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003937 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003938 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3939 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003940 }
3941}
3942
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003943void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003944 // The declaration may have been modified by files later in the chain.
3945 // If this is the case, read the record containing the updates from each file
3946 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003947 serialization::GlobalDeclID ID = Record.ID;
3948 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003949 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003950 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003951
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003952 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00003953
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003954 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003955 auto UpdateOffsets = std::move(UpdI->second);
3956 DeclUpdateOffsets.erase(UpdI);
3957
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003958 // Check if this decl was interesting to the consumer. If we just loaded
3959 // the declaration, then we know it was interesting and we skip the call
3960 // to isConsumerInterestedIn because it is unsafe to call in the
3961 // current ASTReader state.
3962 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003963 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003964 for (auto &FileAndOffset : UpdateOffsets) {
3965 ModuleFile *F = FileAndOffset.first;
3966 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003967 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3968 SavedStreamPosition SavedPosition(Cursor);
3969 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003970 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003971 ASTRecordReader Record(*this, *F);
3972 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003973 (void)RecCode;
3974 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003975
David L. Jonesbe1557a2016-12-21 00:17:49 +00003976 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3977 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003978 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003979
3980 // We might have made this declaration interesting. If so, remember that
3981 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003982 if (!WasInteresting &&
3983 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
3984 PotentiallyInterestingDecls.push_back(
3985 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003986 WasInteresting = true;
3987 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003988 }
3989 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00003990 // Add the lazy specializations to the template.
3991 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3992 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3993 "Must not have pending specializations");
3994 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3995 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3996 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3997 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3998 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3999 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
4000 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00004001
4002 // Load the pending visible updates for this decl context, if it has any.
4003 auto I = PendingVisibleUpdates.find(ID);
4004 if (I != PendingVisibleUpdates.end()) {
4005 auto VisibleUpdates = std::move(I->second);
4006 PendingVisibleUpdates.erase(I);
4007
4008 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004009 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00004010 Lookups[DC].Table.add(
4011 Update.Mod, Update.Data,
4012 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
4013 DC->setHasExternalVisibleStorage(true);
4014 }
Chris Lattner487412d2009-04-27 05:27:42 +00004015}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004016
Richard Smithd61d4ac2015-08-22 20:13:39 +00004017void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
4018 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00004019 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00004020 if (FirstLocal != CanonDecl) {
4021 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
4022 ASTDeclReader::attachPreviousDecl(
4023 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
4024 CanonDecl);
4025 }
4026
4027 if (!LocalOffset) {
4028 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
4029 return;
4030 }
4031
4032 // Load the list of other redeclarations from this module file.
4033 ModuleFile *M = getOwningModuleFile(FirstLocal);
4034 assert(M && "imported decl from no module file");
4035
4036 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
4037 SavedStreamPosition SavedPosition(Cursor);
4038 Cursor.JumpToBit(LocalOffset);
4039
4040 RecordData Record;
4041 unsigned Code = Cursor.ReadCode();
4042 unsigned RecCode = Cursor.readRecord(Code, Record);
4043 (void)RecCode;
4044 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
4045
4046 // FIXME: We have several different dispatches on decl kind here; maybe
4047 // we should instead generate one loop per kind and dispatch up-front?
4048 Decl *MostRecent = FirstLocal;
4049 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4050 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00004051 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
4052 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00004053 }
Richard Smithc3a53252015-02-28 05:57:02 +00004054 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00004055}
4056
4057namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004058
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004059 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004060 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004061 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004062 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004063 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00004064 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004065 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004066 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004067 serialization::GlobalDeclID InterfaceID;
4068 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00004069
Douglas Gregor404cdde2012-01-27 01:47:08 +00004070 void add(ObjCCategoryDecl *Cat) {
4071 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00004072 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004073 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00004074
Douglas Gregor404cdde2012-01-27 01:47:08 +00004075 // Check for duplicate categories.
4076 if (Cat->getDeclName()) {
4077 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00004078 if (Existing &&
4079 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00004080 != Reader.getOwningModuleFile(Cat)) {
4081 // FIXME: We should not warn for duplicates in diamond:
4082 //
4083 // MT //
4084 // / \ //
4085 // ML MR //
4086 // \ / //
4087 // MB //
4088 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004089 // If there are duplicates in ML/MR, there will be warning when
4090 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004091 // importing.
4092 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4093 << Interface->getDeclName() << Cat->getDeclName();
4094 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4095 } else if (!Existing) {
4096 // Record this category.
4097 Existing = Cat;
4098 }
4099 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004100
Douglas Gregor404cdde2012-01-27 01:47:08 +00004101 // Add this category to the end of the chain.
4102 if (Tail)
4103 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4104 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004105 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004106 Tail = Cat;
4107 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004108
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004109 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004110 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004111 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004112 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4113 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004114 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004115 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4116 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004117 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004118 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004119 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004120 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004121
Douglas Gregor404cdde2012-01-27 01:47:08 +00004122 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004123 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004124 }
4125 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004126
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004127 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004128 // If we've loaded all of the category information we care about from
4129 // this module file, we're done.
4130 if (M.Generation <= PreviousGeneration)
4131 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004132
4133 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004134 // module file. If there is no such mapping, we'll find nothing here
4135 // (or in any module it imports).
4136 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4137 if (!LocalID)
4138 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004139
Douglas Gregor404cdde2012-01-27 01:47:08 +00004140 // Perform a binary search to find the local redeclarations for this
4141 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004142 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004143 const ObjCCategoriesInfo *Result
4144 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004145 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004146 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004147 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4148 Result->DefinitionID != LocalID) {
4149 // We didn't find anything. If the class definition is in this module
4150 // file, then the module files it depends on cannot have any categories,
4151 // so suppress further lookup.
4152 return Reader.isDeclIDFromModule(InterfaceID, M);
4153 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004154
Douglas Gregor404cdde2012-01-27 01:47:08 +00004155 // We found something. Dig out all of the categories.
4156 unsigned Offset = Result->Offset;
4157 unsigned N = M.ObjCCategories[Offset];
4158 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4159 for (unsigned I = 0; I != N; ++I)
4160 add(cast_or_null<ObjCCategoryDecl>(
4161 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4162 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004163 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004164 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004165
4166} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004167
Douglas Gregor404cdde2012-01-27 01:47:08 +00004168void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4169 ObjCInterfaceDecl *D,
4170 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004171 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004172 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004173 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004174}
Douglas Gregordab42432011-08-12 00:15:20 +00004175
Richard Smithd6db68c2014-08-07 20:58:41 +00004176template<typename DeclT, typename Fn>
4177static void forAllLaterRedecls(DeclT *D, Fn F) {
4178 F(D);
4179
4180 // Check whether we've already merged D into its redeclaration chain.
4181 // MostRecent may or may not be nullptr if D has not been merged. If
4182 // not, walk the merged redecl chain and see if it's there.
4183 auto *MostRecent = D->getMostRecentDecl();
4184 bool Found = false;
4185 for (auto *Redecl = MostRecent; Redecl && !Found;
4186 Redecl = Redecl->getPreviousDecl())
4187 Found = (Redecl == D);
4188
4189 // If this declaration is merged, apply the functor to all later decls.
4190 if (Found) {
4191 for (auto *Redecl = MostRecent; Redecl != D;
4192 Redecl = Redecl->getPreviousDecl())
4193 F(Redecl);
4194 }
4195}
4196
Vassil Vassilev7d264622017-06-30 22:40:17 +00004197void ASTDeclReader::UpdateDecl(Decl *D,
4198 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004199 while (Record.getIdx() < Record.size()) {
4200 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004201 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004202 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004203 // FIXME: If we also have an update record for instantiating the
4204 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004205 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004206 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004207 // FIXME: We should call addHiddenDecl instead, to add the member
4208 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004209 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004210 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004211 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004212
4213 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004214 // It will be added to the template's lazy specialization set.
4215 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004216 break;
4217
4218 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004219 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004220
Douglas Gregor540fd812012-01-09 18:07:24 +00004221 // Each module has its own anonymous namespace, which is disjoint from
4222 // any other module's anonymous namespaces, so don't attach the anonymous
4223 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004224 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004225 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004226 TU->setAnonymousNamespace(Anon);
4227 else
4228 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4229 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004230 break;
4231 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004232
Richard Smith891fc7f2017-12-05 01:31:47 +00004233 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004234 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004235 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4236 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004237 uint64_t Val = Record.readInt();
4238 if (Val && !VD->getInit()) {
4239 VD->setInit(Record.readExpr());
4240 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4241 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4242 Eval->CheckedICE = true;
4243 Eval->IsICE = Val == 3;
4244 }
4245 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004246 break;
Richard Smith05a21352017-06-22 22:18:46 +00004247 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004248
Richard Smith891fc7f2017-12-05 01:31:47 +00004249 case UPD_CXX_POINT_OF_INSTANTIATION: {
4250 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004251 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004252 VTSD->setPointOfInstantiation(POI);
4253 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4254 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4255 } else {
4256 auto *FD = cast<FunctionDecl>(D);
4257 if (auto *FTSInfo = FD->TemplateOrSpecialization
4258 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4259 FTSInfo->setPointOfInstantiation(POI);
4260 else
4261 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4262 ->setPointOfInstantiation(POI);
4263 }
4264 break;
4265 }
4266
John McCall32791cc2016-01-06 22:34:54 +00004267 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004268 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004269
4270 // We have to read the default argument regardless of whether we use it
4271 // so that hypothetical further update records aren't messed up.
4272 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004273 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004274
4275 // Only apply the update if the parameter still has an uninstantiated
4276 // default argument.
4277 if (Param->hasUninstantiatedDefaultArg())
4278 Param->setDefaultArg(DefaultArg);
4279 break;
4280 }
4281
Richard Smith4b054b22016-08-24 21:25:37 +00004282 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004283 auto *FD = cast<FieldDecl>(D);
4284 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004285
4286 // Only apply the update if the field still has an uninstantiated
4287 // default member initializer.
4288 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4289 if (DefaultInit)
4290 FD->setInClassInitializer(DefaultInit);
4291 else
4292 // Instantiation failed. We can get here if we serialized an AST for
4293 // an invalid program.
4294 FD->removeInClassInitializer();
4295 }
4296 break;
4297 }
4298
Richard Smith4d235792014-08-07 18:53:08 +00004299 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004300 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004301 if (Reader.PendingBodies[FD]) {
4302 // FIXME: Maybe check for ODR violations.
4303 // It's safe to stop now because this update record is always last.
4304 return;
4305 }
4306
David L. Jonesbe1557a2016-12-21 00:17:49 +00004307 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004308 // Maintain AST consistency: any later redeclarations of this function
4309 // are inline if this one is. (We might have merged another declaration
4310 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004311 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4312 FD->setImplicitlyInline();
4313 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004314 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004315 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004316 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004317 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004318 break;
4319 }
4320
Richard Smithcd45dbc2014-04-19 03:48:30 +00004321 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4322 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004323 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004324 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004325 OldDD && (OldDD->Definition != RD ||
4326 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004327 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004328 RD->setArgPassingRestrictions(
4329 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004330 ReadCXXRecordDefinition(RD, /*Update*/true);
4331
Richard Smithcd45dbc2014-04-19 03:48:30 +00004332 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004333 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004334 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004335 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004336 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004337 }
4338
David L. Jonesbe1557a2016-12-21 00:17:49 +00004339 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004340 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004341 if (MemberSpecializationInfo *MSInfo =
4342 RD->getMemberSpecializationInfo()) {
4343 MSInfo->setTemplateSpecializationKind(TSK);
4344 MSInfo->setPointOfInstantiation(POI);
4345 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004346 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004347 Spec->setTemplateSpecializationKind(TSK);
4348 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004349
David L. Jonesbe1557a2016-12-21 00:17:49 +00004350 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004351 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004352 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004353 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004354 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004355 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004356 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004357
4358 // FIXME: If we already have a partial specialization set,
4359 // check that it matches.
4360 if (!Spec->getSpecializedTemplateOrPartial()
4361 .is<ClassTemplatePartialSpecializationDecl *>())
4362 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004363 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004364 }
4365
David L. Jonesbe1557a2016-12-21 00:17:49 +00004366 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004367 RD->setLocation(ReadSourceLocation());
4368 RD->setLocStart(ReadSourceLocation());
4369 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004370
David L. Jonesbe1557a2016-12-21 00:17:49 +00004371 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004372 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004373 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004374 // If the declaration already has attributes, we assume that some other
4375 // AST file already loaded them.
4376 if (!D->hasAttrs())
4377 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004378 }
4379 break;
4380 }
4381
Richard Smithf8134002015-03-10 01:41:22 +00004382 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4383 // Set the 'operator delete' directly to avoid emitting another update
4384 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004385 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004386 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004387 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004388 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004389 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004390 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004391 First->OperatorDeleteThisArg = ThisArg;
4392 }
Richard Smithf8134002015-03-10 01:41:22 +00004393 break;
4394 }
4395
Richard Smith564417a2014-03-20 21:47:22 +00004396 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004397 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004398 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004399 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004400
4401 // Update this declaration's exception specification, if needed.
4402 auto *FD = cast<FunctionDecl>(D);
4403 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4404 // FIXME: If the exception specification is already present, check that it
4405 // matches.
4406 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004407 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004408 FPT->getReturnType(), FPT->getParamTypes(),
4409 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004410
4411 // When we get to the end of deserializing, see if there are other decls
4412 // that we need to propagate this exception specification onto.
4413 Reader.PendingExceptionSpecUpdates.insert(
4414 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004415 }
Richard Smith564417a2014-03-20 21:47:22 +00004416 break;
4417 }
4418
Richard Smith1fa5d642013-05-11 05:45:24 +00004419 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004420 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004421 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004422 Reader.PendingDeducedTypeUpdates.insert(
4423 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004424 break;
4425 }
Eli Friedman276dd182013-09-05 00:02:25 +00004426
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004427 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004428 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004429 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004430 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004431
4432 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004433 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4434 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004435 break;
4436
4437 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004438 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4439 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004440 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004441
Alexey Bataev97720002014-11-11 04:05:39 +00004442 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004443 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4444 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004445 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004446
Alex Denisovfde64952015-06-26 05:28:36 +00004447 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004448 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004449 auto *Exported = cast<NamedDecl>(D);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004450 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith13897eb2018-09-12 23:37:00 +00004451 Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
4452 Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004453 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004454 }
Alex Denisovfde64952015-06-26 05:28:36 +00004455
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004456 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00004457 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
4458 Reader.getContext(),
4459 static_cast<OMPDeclareTargetDeclAttr::MapTypeTy>(Record.readInt()),
4460 ReadSourceRange()));
4461 break;
4462
Alex Denisovfde64952015-06-26 05:28:36 +00004463 case UPD_ADDED_ATTR_TO_RECORD:
4464 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004465 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004466 assert(Attrs.size() == 1);
4467 D->addAttr(Attrs[0]);
4468 break;
4469 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004470 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004471}