blob: b33b88c2715e34ca6eea2fe6ad962992636d19e7 [file] [log] [blame]
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001//===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file implements the ASTReader::ReadDeclRecord method, which is the
Chris Lattner487412d2009-04-27 05:27:42 +000011// entrypoint for loading a decl.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000015#include "ASTCommon.h"
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +000016#include "ASTReaderInternals.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000018#include "clang/AST/Attr.h"
19#include "clang/AST/AttrIterator.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/AST/DeclCXX.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000023#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclOpenMP.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/AST/DeclTemplate.h"
27#include "clang/AST/DeclVisitor.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000028#include "clang/AST/DeclarationName.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "clang/AST/Expr.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000030#include "clang/AST/ExternalASTSource.h"
31#include "clang/AST/LambdaCapture.h"
32#include "clang/AST/NestedNameSpecifier.h"
33#include "clang/AST/Redeclarable.h"
34#include "clang/AST/Stmt.h"
35#include "clang/AST/TemplateBase.h"
36#include "clang/AST/Type.h"
37#include "clang/AST/UnresolvedSet.h"
38#include "clang/Basic/AttrKinds.h"
39#include "clang/Basic/ExceptionSpecificationType.h"
40#include "clang/Basic/IdentifierTable.h"
41#include "clang/Basic/LLVM.h"
42#include "clang/Basic/Lambda.h"
43#include "clang/Basic/LangOptions.h"
44#include "clang/Basic/Linkage.h"
45#include "clang/Basic/Module.h"
46#include "clang/Basic/PragmaKinds.h"
47#include "clang/Basic/SourceLocation.h"
48#include "clang/Basic/Specifiers.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000049#include "clang/Sema/IdentifierResolver.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000050#include "clang/Sema/SemaDiagnostic.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000051#include "clang/Serialization/ASTBitCodes.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000052#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000053#include "clang/Serialization/ContinuousRangeMap.h"
54#include "clang/Serialization/Module.h"
55#include "llvm/ADT/DenseMap.h"
56#include "llvm/ADT/FoldingSet.h"
57#include "llvm/ADT/STLExtras.h"
58#include "llvm/ADT/SmallPtrSet.h"
59#include "llvm/ADT/SmallVector.h"
60#include "llvm/ADT/iterator_range.h"
61#include "llvm/Bitcode/BitstreamReader.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000064#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000065#include <algorithm>
66#include <cassert>
67#include <cstdint>
68#include <cstring>
69#include <string>
70#include <utility>
Hans Wennborgdcfba332015-10-06 23:40:43 +000071
Chris Lattner487412d2009-04-27 05:27:42 +000072using namespace clang;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000073using namespace serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000074
75//===----------------------------------------------------------------------===//
76// Declaration deserialization
77//===----------------------------------------------------------------------===//
78
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000079namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000080
Sebastian Redlb3298c32010-08-18 23:56:48 +000081 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000082 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000083 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000084 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000085 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000086 const SourceLocation ThisDeclLoc;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000087
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000088 using RecordData = ASTReader::RecordData;
89
Richard Smith600adef2018-07-04 02:25:38 +000090 TypeID DeferredTypeID = 0;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000091 unsigned AnonymousDeclNumber;
92 GlobalDeclID NamedDeclForTagDecl = 0;
93 IdentifierInfo *TypedefNameForLinkage = nullptr;
94
95 bool HasPendingBody = false;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +000096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000097 ///A flag to carry the information for a decl from the entity is
Vassil Vassilev928c8252016-04-28 14:13:28 +000098 /// used. We use it to delay the marking of the canonical decl as used until
99 /// the entire declaration is deserialized and merged.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000100 bool IsDeclMarkedUsed = false;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000101
Sebastian Redlc67764e2010-07-22 22:43:28 +0000102 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000103
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000104 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000105 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000106 assert(LocalOffset < Loc.Offset && "offset point after current record");
107 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +0000108 }
109
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000110 uint64_t ReadGlobalOffset() {
111 uint64_t Local = ReadLocalOffset();
112 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +0000113 }
114
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000115 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000116 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000117 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000118
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000119 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000120 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000121 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000122
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000123 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000124 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000125 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000126
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000127 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000128 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000129 }
Richard Smith0b884372015-02-27 00:25:58 +0000130
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000131 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000132 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +0000133 }
134
Richard Smith0b884372015-02-27 00:25:58 +0000135 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000136 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000137 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +0000138 }
139
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000140 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000141 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000142 }
143
144 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000145 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000146 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000147 }
148
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000149 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000150 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000151 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000152
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000153 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000154 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000155 }
156
157 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000158 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000159 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000160
David L. Jonesbe1557a2016-12-21 00:17:49 +0000161 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000162 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000163
164 Module *readModule() {
165 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000166 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000167
Richard Smith2a9e5c52015-02-03 03:32:14 +0000168 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000169 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
170 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000171 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000172 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000173 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000174 void MergeDefinitionData(ObjCInterfaceDecl *D,
175 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000176 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
177 void MergeDefinitionData(ObjCProtocolDecl *D,
178 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000179
Richard Smith600adef2018-07-04 02:25:38 +0000180 static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
181
Richard Smithd08aeb62014-08-28 01:33:39 +0000182 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
183 DeclContext *DC,
184 unsigned Index);
185 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
186 unsigned Index, NamedDecl *D);
187
Richard Smith0144f512015-08-22 02:09:38 +0000188 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000189 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000190 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000191 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000192 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000193
Douglas Gregor022857e2011-12-22 01:48:48 +0000194 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000195 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000196 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000198 /// Retrieve the first ID.
Douglas Gregor022857e2011-12-22 01:48:48 +0000199 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000200
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000201 /// Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000202 bool isKeyDecl() const { return IsKeyDecl; }
203
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000204 /// Get a known declaration that this should be merged with, if
Richard Smith5a4737c2015-02-06 02:42:59 +0000205 /// any.
206 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000207 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000209 /// Class used to capture the result of searching for an existing
Douglas Gregor022857e2011-12-22 01:48:48 +0000210 /// declaration of a specific kind and name, along with the ability
211 /// to update the place where this result was found (the declaration
212 /// chain hanging off an identifier or the DeclContext we searched in)
213 /// if requested.
214 class FindExistingResult {
215 ASTReader &Reader;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000216 NamedDecl *New = nullptr;
217 NamedDecl *Existing = nullptr;
218 bool AddResult = false;
219 unsigned AnonymousDeclNumber = 0;
220 IdentifierInfo *TypedefNameForLinkage = nullptr;
Richard Smithd08aeb62014-08-28 01:33:39 +0000221
Douglas Gregor022857e2011-12-22 01:48:48 +0000222 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000223 FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000224
Richard Smithd08aeb62014-08-28 01:33:39 +0000225 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000226 unsigned AnonymousDeclNumber,
227 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000228 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000229 AnonymousDeclNumber(AnonymousDeclNumber),
230 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000231
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000232 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000233 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
234 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000235 AnonymousDeclNumber(Other.AnonymousDeclNumber),
236 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000237 Other.AddResult = false;
238 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000239
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000240 FindExistingResult &operator=(FindExistingResult &&) = delete;
Douglas Gregor022857e2011-12-22 01:48:48 +0000241 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000242
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000243 /// Suppress the addition of this result into the known set of
Douglas Gregor2009cee2012-01-03 22:46:00 +0000244 /// names.
245 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000246
Douglas Gregor022857e2011-12-22 01:48:48 +0000247 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000248
Douglas Gregor022857e2011-12-22 01:48:48 +0000249 template<typename T>
250 operator T*() const { return dyn_cast_or_null<T>(Existing); }
251 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000252
Richard Smith8a639892015-01-24 01:07:20 +0000253 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
254 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000255 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000256
Chris Lattner487412d2009-04-27 05:27:42 +0000257 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000258 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
259 ASTReader::RecordLocation Loc,
260 DeclID thisDeclID, SourceLocation ThisDeclLoc)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000261 : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
262 ThisDeclLoc(ThisDeclLoc) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000263
Vassil Vassilev7d264622017-06-30 22:40:17 +0000264 template <typename T> static
265 void AddLazySpecializations(T *D,
266 SmallVectorImpl<serialization::DeclID>& IDs) {
267 if (IDs.empty())
268 return;
269
270 // FIXME: We should avoid this pattern of getting the ASTContext.
271 ASTContext &C = D->getASTContext();
272
273 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
274
275 if (auto &Old = LazySpecializations) {
276 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +0000277 llvm::sort(IDs.begin(), IDs.end());
Vassil Vassilev7d264622017-06-30 22:40:17 +0000278 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
279 }
280
281 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
282 *Result = IDs.size();
283 std::copy(IDs.begin(), IDs.end(), Result + 1);
284
285 LazySpecializations = Result;
286 }
287
Richard Smithb321ecb2014-05-13 01:15:00 +0000288 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000289 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
290 static Decl *getMostRecentDeclImpl(...);
291 static Decl *getMostRecentDecl(Decl *D);
292
293 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000294 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000295 Redeclarable<DeclT> *D, Decl *Previous,
296 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000297 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000298 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
299 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000300
301 template <typename DeclT>
302 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
303 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000304 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000305
Richard Smith851072e2014-05-19 20:59:20 +0000306 template <typename DeclT>
307 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
308 static void markIncompleteDeclChainImpl(...);
309
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000310 /// Determine whether this declaration has a pending body.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000311 bool hasPendingBody() const { return HasPendingBody; }
312
David Blaikieac4345c2017-02-12 18:45:31 +0000313 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000314 void Visit(Decl *D);
315
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000316 void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000317
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000318 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
319 ObjCCategoryDecl *Next) {
320 Cat->NextClassCategory = Next;
321 }
322
Chris Lattner487412d2009-04-27 05:27:42 +0000323 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000324 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000325 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000326 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
327 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000328 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000329 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000330 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
331 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000332 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000333 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000334 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000335 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000336 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000337 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000338 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000339 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
340 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
341 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
342 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
343 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000344 ClassTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000345
Richard Smith1d209d02013-05-23 01:49:11 +0000346 void VisitClassTemplateSpecializationDecl(
347 ClassTemplateSpecializationDecl *D) {
348 VisitClassTemplateSpecializationDeclImpl(D);
349 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000350
Chris Lattnerca025db2010-05-07 21:43:38 +0000351 void VisitClassTemplatePartialSpecializationDecl(
352 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000353 void VisitClassScopeFunctionSpecializationDecl(
354 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000355 RedeclarableResult
356 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000357
Larisse Voufo39a1e502013-08-06 01:03:05 +0000358 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
359 VisitVarTemplateSpecializationDeclImpl(D);
360 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000361
Larisse Voufo39a1e502013-08-06 01:03:05 +0000362 void VisitVarTemplatePartialSpecializationDecl(
363 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000364 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000365 void VisitValueDecl(ValueDecl *VD);
366 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000367 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000368 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000369 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000370 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000371 void VisitCXXMethodDecl(CXXMethodDecl *D);
372 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
373 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
374 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000375 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000376 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000377 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000378 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
379 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000380 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
381 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000382 void VisitDecompositionDecl(DecompositionDecl *DD);
383 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000384 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000385 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000386 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000387 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000388 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000389 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000390 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000391 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000392 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000393 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000394 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000395 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000396 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000397 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000398 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000399 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000400 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000401 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000402 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000403 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
404 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000405 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000406 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000407 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000408
Chris Lattner487412d2009-04-27 05:27:42 +0000409 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000410
411 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000412 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000413
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000414 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000415 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
416 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000417
418 template<typename T>
419 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000420 RedeclarableResult &Redecl,
421 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000422
Richard Smith0b87e072013-10-07 08:02:11 +0000423 template<typename T>
424 void mergeMergeable(Mergeable<T> *D);
425
Richard Smithf17fdbd2014-04-24 02:25:27 +0000426 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
427 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000428 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000429
Douglas Gregor85f3f952015-07-07 03:57:15 +0000430 ObjCTypeParamList *ReadObjCTypeParamList();
431
Alexis Hunted053252010-05-30 07:21:58 +0000432 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000433 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000434 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000435 void VisitObjCContainerDecl(ObjCContainerDecl *D);
436 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
437 void VisitObjCIvarDecl(ObjCIvarDecl *D);
438 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
439 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000440 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
441 void VisitObjCImplDecl(ObjCImplDecl *D);
442 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
443 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
444 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
445 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
446 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000447 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000448 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000449 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000450 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000451
452} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000453
Richard Smith86dfc1e2015-06-18 22:07:00 +0000454namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000455
Richard Smith86dfc1e2015-06-18 22:07:00 +0000456/// Iterator over the redeclarations of a declaration that have already
457/// been merged into the same redeclaration chain.
458template<typename DeclT>
459class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000460 DeclT *Start;
461 DeclT *Canonical = nullptr;
462 DeclT *Current = nullptr;
463
Richard Smith86dfc1e2015-06-18 22:07:00 +0000464public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000465 MergedRedeclIterator() = default;
466 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000467
468 DeclT *operator*() { return Current; }
469
470 MergedRedeclIterator &operator++() {
471 if (Current->isFirstDecl()) {
472 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000473 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000474 } else
475 Current = Current->getPreviousDecl();
476
477 // If we started in the merged portion, we'll reach our start position
478 // eventually. Otherwise, we'll never reach it, but the second declaration
479 // we reached was the canonical declaration, so stop when we see that one
480 // again.
481 if (Current == Start || Current == Canonical)
482 Current = nullptr;
483 return *this;
484 }
485
486 friend bool operator!=(const MergedRedeclIterator &A,
487 const MergedRedeclIterator &B) {
488 return A.Current != B.Current;
489 }
490};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000491
492} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000493
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000494template <typename DeclT>
495static llvm::iterator_range<MergedRedeclIterator<DeclT>>
496merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000497 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
498 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000499}
500
Sebastian Redlb3298c32010-08-18 23:56:48 +0000501uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000502 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000503}
504
David Blaikieac4345c2017-02-12 18:45:31 +0000505void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000506 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000507 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000508 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
509 CD->NumCtorInitializers = Record.readInt();
510 if (CD->NumCtorInitializers)
511 CD->CtorInitializers = ReadGlobalOffset();
512 }
513 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000514 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
515 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000516}
517
Sebastian Redlb3298c32010-08-18 23:56:48 +0000518void ASTDeclReader::Visit(Decl *D) {
519 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000520
Vassil Vassilev928c8252016-04-28 14:13:28 +0000521 // At this point we have deserialized and merged the decl and it is safe to
522 // update its canonical decl to signal that the entire entity is used.
523 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
524 IsDeclMarkedUsed = false;
525
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000526 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000527 if (auto *TInfo = DD->getTypeSourceInfo())
528 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000529 }
530
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000531 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000532 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000533 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000534
535 // If this is a tag declaration with a typedef name for linkage, it's safe
536 // to load that typedef now.
537 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000538 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
539 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000540 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000541 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000542 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000543 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Richard Smith600adef2018-07-04 02:25:38 +0000544 if (DeferredTypeID)
545 FD->setType(Reader.GetType(DeferredTypeID));
546
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000547 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000548 // We only read it if FD doesn't already have a body (e.g., from another
549 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000550 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000551 if (Record.readInt())
552 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000553 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000554}
555
Sebastian Redlb3298c32010-08-18 23:56:48 +0000556void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000557 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
558 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000559 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000560 // parameter or of a parameter of a function template immediately. These
561 // entities might be used in the formulation of its DeclContext (for
562 // example, a function parameter can be used in decltype() in trailing
563 // return type of the function). Use the translation unit DeclContext as a
564 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000565 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
566 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000567 if (!LexicalDCIDForTemplateParmDecl)
568 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000569 Reader.addPendingDeclContextInfo(D,
570 SemaDCIDForTemplateParmDecl,
571 LexicalDCIDForTemplateParmDecl);
Douglas Gregor4163aca2011-09-09 21:34:22 +0000572 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000573 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000574 auto *SemaDC = ReadDeclAs<DeclContext>();
575 auto *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000576 if (!LexicalDC)
577 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000578 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000579 // Avoid calling setLexicalDeclContext() directly because it uses
580 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000581 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
582 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000583 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000584 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000585 D->setInvalidDecl(Record.readInt());
586 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000587 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000588 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000589 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
590 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000591 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000592 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000593 D->setImplicit(Record.readInt());
594 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000595 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000596 D->setReferenced(Record.readInt());
597 D->setTopLevelDeclInObjCContainer(Record.readInt());
598 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000599 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000600 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000601
Douglas Gregorcf68c582011-12-01 22:20:10 +0000602 // Determine whether this declaration is part of a (sub)module. If so, it
603 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000604 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000605 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000606 D->setModuleOwnershipKind(
607 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
608 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000609 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000610
Richard Smith90dc5252017-06-23 01:04:34 +0000611 if (ModulePrivate) {
612 // Module-private declarations are never visible, so there is no work to
613 // do.
Richard Smith42413142015-05-15 20:05:43 +0000614 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
615 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000616 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000617 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000618 // Mark the declaration as visible when its owning module becomes visible.
619 if (Owner->NameVisibility == Module::AllVisible)
620 D->setVisibleDespiteOwningModule();
621 else
Richard Smith42413142015-05-15 20:05:43 +0000622 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000623 }
Richard Smithd19389a2017-07-05 07:47:11 +0000624 } else if (ModulePrivate) {
625 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000626 }
Chris Lattner487412d2009-04-27 05:27:42 +0000627}
628
Nico Weber66220292016-03-02 17:28:48 +0000629void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
630 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000631 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000632 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000633 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000634 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
635 D->getTrailingObjects<char>()[Arg.size()] = '\0';
636}
637
Nico Webercbbaeb12016-03-02 19:28:54 +0000638void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
639 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000640 D->setLocation(ReadSourceLocation());
641 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000642 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
643 D->getTrailingObjects<char>()[Name.size()] = '\0';
644
645 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000646 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000647 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
648 Value.size());
649 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
650}
651
Sebastian Redlb3298c32010-08-18 23:56:48 +0000652void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000653 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000654}
655
Sebastian Redlb3298c32010-08-18 23:56:48 +0000656void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000657 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000658 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000659 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000660}
661
Sebastian Redlb3298c32010-08-18 23:56:48 +0000662void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000663 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000664 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000665 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000666 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000667}
668
Richard Smith43ccec8e2014-08-26 03:52:16 +0000669ASTDeclReader::RedeclarableResult
670ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000671 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000672 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000673 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000674 if (Record.readInt()) { // isModed
675 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000676 TD->setModedTypeSourceInfo(TInfo, modedT);
677 } else
678 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000679 // Read and discard the declaration for which this is a typedef name for
680 // linkage, if it exists. We cannot rely on our type to pull in this decl,
681 // because it might have been merged with a type from another module and
682 // thus might not refer to our version of the declaration.
683 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000684 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000685}
686
687void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000688 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
689 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000690}
691
Richard Smithdda56e42011-04-15 14:24:37 +0000692void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000693 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000694 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000695 // Merged when we merge the template.
696 TD->setDescribedAliasTemplate(Template);
697 else
698 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000699}
700
Richard Smith1d209d02013-05-23 01:49:11 +0000701ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000702 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000703 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000704
David L. Jonesbe1557a2016-12-21 00:17:49 +0000705 TD->IdentifierNamespace = Record.readInt();
706 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000707 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000708 TD->setCompleteDefinition(Record.readInt());
709 TD->setEmbeddedInDeclarator(Record.readInt());
710 TD->setFreeStanding(Record.readInt());
711 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000712 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000713
David L. Jonesbe1557a2016-12-21 00:17:49 +0000714 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000715 case 0:
716 break;
717 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000718 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000719 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000720 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000721 break;
722 }
723 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000724 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000725 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000726 break;
Richard Smith70d58502014-08-30 00:04:23 +0000727 default:
728 llvm_unreachable("unexpected tag info kind");
729 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000730
Richard Smithcd45dbc2014-04-19 03:48:30 +0000731 if (!isa<CXXRecordDecl>(TD))
732 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000733 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000734}
735
Sebastian Redlb3298c32010-08-18 23:56:48 +0000736void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000737 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000738 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000739 ED->setIntegerTypeSourceInfo(TI);
740 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000741 ED->setIntegerType(Record.readType());
742 ED->setPromotionType(Record.readType());
743 ED->setNumPositiveBits(Record.readInt());
744 ED->setNumNegativeBits(Record.readInt());
745 ED->IsScoped = Record.readInt();
746 ED->IsScopedUsingClassTag = Record.readInt();
747 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000748
Richard Smith01a73372013-10-15 22:02:41 +0000749 // If this is a definition subject to the ODR, and we already have a
750 // definition, merge this one into it.
751 if (ED->IsCompleteDefinition &&
752 Reader.getContext().getLangOpts().Modules &&
753 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000754 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
755 if (!OldDef) {
756 // This is the first time we've seen an imported definition. Look for a
757 // local definition before deciding that we are the first definition.
758 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
759 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
760 OldDef = D;
761 break;
762 }
763 }
764 }
765 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000766 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
767 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000768 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000769 } else {
770 OldDef = ED;
771 }
772 }
773
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000774 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
775 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000776 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000777 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
778 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
779 }
Chris Lattner487412d2009-04-27 05:27:42 +0000780}
781
Richard Smith1d209d02013-05-23 01:49:11 +0000782ASTDeclReader::RedeclarableResult
783ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
784 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000785 RD->setHasFlexibleArrayMember(Record.readInt());
786 RD->setAnonymousStructOrUnion(Record.readInt());
787 RD->setHasObjectMember(Record.readInt());
788 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000789 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
790 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
791 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000792 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000793 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000794 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000795}
796
Sebastian Redlb3298c32010-08-18 23:56:48 +0000797void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000798 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000799 // For function declarations, defer reading the type in case the function has
800 // a deduced return type that references an entity declared within the
801 // function.
802 if (isa<FunctionDecl>(VD))
803 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
804 else
805 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000806}
807
Sebastian Redlb3298c32010-08-18 23:56:48 +0000808void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000809 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000810 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000811 ECD->setInitExpr(Record.readExpr());
812 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000813 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000814}
815
Sebastian Redlb3298c32010-08-18 23:56:48 +0000816void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000817 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000818 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000819 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000820 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000821 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000822 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000823 }
Richard Smithc23d7342018-06-29 20:46:25 +0000824 QualType TSIType = Record.readType();
825 DD->setTypeSourceInfo(
826 TSIType.isNull() ? nullptr
827 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000828}
829
Sebastian Redlb3298c32010-08-18 23:56:48 +0000830void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000831 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000832 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000833
Richard Smith600adef2018-07-04 02:25:38 +0000834 // Attach a type to this function. Use the real type if possible, but fall
835 // back to the type as written if it involves a deduced return type.
836 if (FD->getTypeSourceInfo() &&
837 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
838 ->getReturnType()->getContainedAutoType()) {
839 // We'll set up the real type in Visit, once we've finished loading the
840 // function.
841 FD->setType(FD->getTypeSourceInfo()->getType());
842 } else {
843 FD->setType(Reader.GetType(DeferredTypeID));
844 DeferredTypeID = 0;
845 }
846
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000847 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000848 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000849
Douglas Gregorb2585692012-01-04 17:13:46 +0000850 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
851 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000852
David L. Jonesbe1557a2016-12-21 00:17:49 +0000853 FD->SClass = (StorageClass)Record.readInt();
854 FD->IsInline = Record.readInt();
855 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000856 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000857 FD->IsVirtualAsWritten = Record.readInt();
858 FD->IsPure = Record.readInt();
859 FD->HasInheritedPrototype = Record.readInt();
860 FD->HasWrittenPrototype = Record.readInt();
861 FD->IsDeleted = Record.readInt();
862 FD->IsTrivial = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000863 FD->IsTrivialForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000864 FD->IsDefaulted = Record.readInt();
865 FD->IsExplicitlyDefaulted = Record.readInt();
866 FD->HasImplicitReturnZero = Record.readInt();
867 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000868 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000869 FD->HasSkippedBody = Record.readInt();
Erich Keane281d20b2018-01-08 21:34:17 +0000870 FD->IsMultiVersion = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000871 FD->IsLateTemplateParsed = Record.readInt();
872 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000873 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000874
Richard Trieue6caa262017-12-23 00:41:01 +0000875 FD->ODRHash = Record.readInt();
876 FD->HasODRHash = true;
877
David L. Jonesbe1557a2016-12-21 00:17:49 +0000878 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000879 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000880 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000881 break;
882 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000883 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000884 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000885 break;
886 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000887 auto *InstFD = ReadDeclAs<FunctionDecl>();
888 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000889 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000890 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000891 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000892 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000893 break;
894 }
895 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000896 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
897 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000898
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000899 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000900 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000901 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000902
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000903 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000904 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000905 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000906 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000907 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000908 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000909 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000910 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000911 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000912
913 LAngleLoc = ReadSourceLocation();
914 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000915 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000916
917 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000918
Douglas Gregor4163aca2011-09-09 21:34:22 +0000919 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000920 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000921 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000922 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000923 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000924 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000925 FunctionTemplateSpecializationInfo *FTInfo
926 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
927 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000928 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
929 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000930 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000931 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000932
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000933 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000934 // The template that contains the specializations set. It's not safe to
935 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000936 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000937 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
938 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
939 // FunctionTemplateSpecializationInfo's Profile().
940 // We avoid getASTContext because a decl in the parent hierarchy may
941 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000942 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000943 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000944 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000945 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000946 FunctionTemplateSpecializationInfo *ExistingInfo =
947 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000948 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000949 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
950 else {
951 assert(Reader.getContext().getLangOpts().Modules &&
952 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000953 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000954 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000955 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000956 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000957 }
958 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
959 // Templates.
960 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000961 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000962 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000963 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
964
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000965 // Templates args.
966 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000967 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000968 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000969 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000970 TemplArgs.setLAngleLoc(ReadSourceLocation());
971 TemplArgs.setRAngleLoc(ReadSourceLocation());
972
Douglas Gregor4163aca2011-09-09 21:34:22 +0000973 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000974 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000975 // These are not merged; we don't need to merge redeclarations of dependent
976 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000977 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000978 }
979 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000980
Chris Lattnerca025db2010-05-07 21:43:38 +0000981 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000982 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000983 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000984 Params.reserve(NumParams);
985 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000986 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000987 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000988}
989
Sebastian Redlb3298c32010-08-18 23:56:48 +0000990void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000991 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000992 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000993 // Load the body on-demand. Most clients won't care, because method
994 // definitions rarely show up in headers.
995 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
996 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000997 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
998 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000999 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001000 MD->setInstanceMethod(Record.readInt());
1001 MD->setVariadic(Record.readInt());
1002 MD->setPropertyAccessor(Record.readInt());
1003 MD->setDefined(Record.readInt());
1004 MD->IsOverriding = Record.readInt();
1005 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001006
David L. Jonesbe1557a2016-12-21 00:17:49 +00001007 MD->IsRedeclaration = Record.readInt();
1008 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001009 if (MD->HasRedeclaration)
1010 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001011 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001012
David L. Jonesbe1557a2016-12-21 00:17:49 +00001013 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1014 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
1015 MD->SetRelatedResultType(Record.readInt());
1016 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001017 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1018 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001019 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001020 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001021 Params.reserve(NumParams);
1022 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001023 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001024
David L. Jonesbe1557a2016-12-21 00:17:49 +00001025 MD->SelLocsKind = Record.readInt();
1026 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001027 SmallVector<SourceLocation, 16> SelLocs;
1028 SelLocs.reserve(NumStoredSelLocs);
1029 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001030 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001031
1032 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001033}
1034
Douglas Gregor85f3f952015-07-07 03:57:15 +00001035void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001036 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001037
David L. Jonesbe1557a2016-12-21 00:17:49 +00001038 D->Variance = Record.readInt();
1039 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001040 D->VarianceLoc = ReadSourceLocation();
1041 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001042}
1043
Sebastian Redlb3298c32010-08-18 23:56:48 +00001044void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001045 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001046 CD->setAtStartLoc(ReadSourceLocation());
1047 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001048}
1049
Douglas Gregor85f3f952015-07-07 03:57:15 +00001050ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001051 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001052 if (numParams == 0)
1053 return nullptr;
1054
1055 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1056 typeParams.reserve(numParams);
1057 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001058 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001059 if (!typeParam)
1060 return nullptr;
1061
1062 typeParams.push_back(typeParam);
1063 }
1064
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001065 SourceLocation lAngleLoc = ReadSourceLocation();
1066 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001067
1068 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1069 typeParams, rAngleLoc);
1070}
1071
Manman Renec315f12016-09-09 23:48:27 +00001072void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001073 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001074 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001075 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001076
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001077 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001078 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001079
Manman Renec315f12016-09-09 23:48:27 +00001080 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001081 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001082 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1083 Protocols.reserve(NumProtocols);
1084 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001085 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001086 SmallVector<SourceLocation, 16> ProtoLocs;
1087 ProtoLocs.reserve(NumProtocols);
1088 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001089 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001090 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1091 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001092
Manman Renec315f12016-09-09 23:48:27 +00001093 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001094 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001095 Protocols.clear();
1096 Protocols.reserve(NumProtocols);
1097 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001098 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001099 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1100 Reader.getContext());
1101}
1102
1103void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1104 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1105 // FIXME: odr checking?
1106}
1107
Sebastian Redlb3298c32010-08-18 23:56:48 +00001108void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001109 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001110 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001111 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001112 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001113
1114 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001115 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001116 // Read the definition.
1117 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001118
David L. Jonesbe1557a2016-12-21 00:17:49 +00001119 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001120 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1121 if (Canon->Data.getPointer()) {
1122 // If we already have a definition, keep the definition invariant and
1123 // merge the data.
1124 MergeDefinitionData(Canon, std::move(ID->data()));
1125 ID->Data = Canon->Data;
1126 } else {
1127 // Set the definition data of the canonical declaration, so other
1128 // redeclarations will see it.
1129 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001130
Manman Renec315f12016-09-09 23:48:27 +00001131 // We will rebuild this list lazily.
1132 ID->setIvarList(nullptr);
1133 }
Craig Toppera13603a2014-05-22 05:54:18 +00001134
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001135 // Note that we have deserialized a definition.
1136 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001137
Douglas Gregor404cdde2012-01-27 01:47:08 +00001138 // Note that we've loaded this Objective-C class.
1139 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001140 } else {
1141 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001142 }
Chris Lattner487412d2009-04-27 05:27:42 +00001143}
1144
Sebastian Redlb3298c32010-08-18 23:56:48 +00001145void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001146 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001147 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001148 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001149 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001150 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001151 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001152}
1153
Graydon Hoaree0a68352017-06-28 18:36:27 +00001154void ASTDeclReader::ReadObjCDefinitionData(
1155 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001156 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001157 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1158 ProtoRefs.reserve(NumProtoRefs);
1159 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001160 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001161 SmallVector<SourceLocation, 16> ProtoLocs;
1162 ProtoLocs.reserve(NumProtoRefs);
1163 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001164 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001165 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1166 ProtoLocs.data(), Reader.getContext());
1167}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001168
Graydon Hoaree0a68352017-06-28 18:36:27 +00001169void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1170 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1171 // FIXME: odr checking?
1172}
1173
1174void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1175 RedeclarableResult Redecl = VisitRedeclarable(PD);
1176 VisitObjCContainerDecl(PD);
1177 mergeRedeclarable(PD, Redecl);
1178
1179 if (Record.readInt()) {
1180 // Read the definition.
1181 PD->allocateDefinitionData();
1182
1183 ReadObjCDefinitionData(PD->data());
1184
1185 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1186 if (Canon->Data.getPointer()) {
1187 // If we already have a definition, keep the definition invariant and
1188 // merge the data.
1189 MergeDefinitionData(Canon, std::move(PD->data()));
1190 PD->Data = Canon->Data;
1191 } else {
1192 // Set the definition data of the canonical declaration, so other
1193 // redeclarations will see it.
1194 PD->getCanonicalDecl()->Data = PD->Data;
1195 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001196 // Note that we have deserialized a definition.
1197 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001198 } else {
1199 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001200 }
Chris Lattner487412d2009-04-27 05:27:42 +00001201}
1202
Sebastian Redlb3298c32010-08-18 23:56:48 +00001203void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001204 VisitFieldDecl(FD);
1205}
1206
Sebastian Redlb3298c32010-08-18 23:56:48 +00001207void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001208 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001209 CD->setCategoryNameLoc(ReadSourceLocation());
1210 CD->setIvarLBraceLoc(ReadSourceLocation());
1211 CD->setIvarRBraceLoc(ReadSourceLocation());
1212
Douglas Gregor404cdde2012-01-27 01:47:08 +00001213 // Note that this category has been deserialized. We do this before
1214 // deserializing the interface declaration, so that it will consider this
1215 /// category.
1216 Reader.CategoriesDeserialized.insert(CD);
1217
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001218 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001219 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001220 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001221 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001222 ProtoRefs.reserve(NumProtoRefs);
1223 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001224 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001225 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001226 ProtoLocs.reserve(NumProtoRefs);
1227 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001228 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001229 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001230 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001231
1232 // Protocols in the class extension belong to the class.
1233 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1234 CD->ClassInterface->mergeClassExtensionProtocolList(
1235 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1236 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001237}
1238
Sebastian Redlb3298c32010-08-18 23:56:48 +00001239void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001240 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001241 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001242}
1243
Sebastian Redlb3298c32010-08-18 23:56:48 +00001244void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001245 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001246 D->setAtLoc(ReadSourceLocation());
1247 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001248 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001249 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001250 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001251 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001252 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001253 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001254 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001255 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001256 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001257 DeclarationName GetterName = Record.readDeclarationName();
1258 SourceLocation GetterLoc = ReadSourceLocation();
1259 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1260 DeclarationName SetterName = Record.readDeclarationName();
1261 SourceLocation SetterLoc = ReadSourceLocation();
1262 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001263 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1264 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1265 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001266}
1267
Sebastian Redlb3298c32010-08-18 23:56:48 +00001268void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001269 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001270 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001271}
1272
Sebastian Redlb3298c32010-08-18 23:56:48 +00001273void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001274 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001275 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001276}
1277
Sebastian Redlb3298c32010-08-18 23:56:48 +00001278void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001279 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001280 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1281 D->SuperLoc = ReadSourceLocation();
1282 D->setIvarLBraceLoc(ReadSourceLocation());
1283 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001284 D->setHasNonZeroConstructors(Record.readInt());
1285 D->setHasDestructors(Record.readInt());
1286 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001287 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001288 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001289}
1290
Sebastian Redlb3298c32010-08-18 23:56:48 +00001291void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001292 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001293 D->setAtLoc(ReadSourceLocation());
1294 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1295 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1296 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001297 D->setGetterCXXConstructor(Record.readExpr());
1298 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001299}
1300
Sebastian Redlb3298c32010-08-18 23:56:48 +00001301void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001302 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001303 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001304
1305 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1306 FD->InitStorage.setInt(ISK);
1307 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1308 ? Record.readType().getAsOpaquePtr()
1309 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001310 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001311
1312 if (auto *BW = Record.readExpr())
1313 FD->setBitWidth(BW);
1314
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001315 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001316 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001317 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001318 }
Richard Smith0b87e072013-10-07 08:02:11 +00001319 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001320}
1321
John McCall5e77d762013-04-16 07:28:30 +00001322void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1323 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001324 PD->GetterId = Record.getIdentifierInfo();
1325 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001326}
1327
Francois Pichet783dd6e2010-11-21 06:08:52 +00001328void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1329 VisitValueDecl(FD);
1330
David L. Jonesbe1557a2016-12-21 00:17:49 +00001331 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001332 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001333 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001334
1335 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001336 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001337
1338 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001339}
1340
Larisse Voufo39a1e502013-08-06 01:03:05 +00001341ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001342 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001343 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001344
David L. Jonesbe1557a2016-12-21 00:17:49 +00001345 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1346 VD->VarDeclBits.TSCSpec = Record.readInt();
1347 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001348 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001349 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1350 Record.readInt();
1351 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001352 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001353 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001354 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001355 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1356 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1357 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1358 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1359 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1360 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001361 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001362 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001363 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001364 VD->setCachedLinkage(VarLinkage);
1365
1366 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001367 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001368 VD->getLexicalDeclContext()->isFunctionOrMethod())
1369 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001370
David L. Jonesbe1557a2016-12-21 00:17:49 +00001371 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001372 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001373 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001374 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1375 Eval->CheckedICE = true;
1376 Eval->IsICE = Val == 3;
1377 }
1378 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001379
Richard Smitha4653622017-09-06 20:01:14 +00001380 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1381 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1382
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001383 enum VarKind {
1384 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1385 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001386 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001387 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001388 // Only true variables (not parameters or implicit parameters) can be
1389 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001390 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1391 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001392 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001393 break;
1394 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001395 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001396 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001397 break;
1398 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001399 auto *Tmpl = ReadDeclAs<VarDecl>();
1400 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001401 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001402 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001403 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001404 break;
1405 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001406 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001407
1408 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001409}
1410
Sebastian Redlb3298c32010-08-18 23:56:48 +00001411void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001412 VisitVarDecl(PD);
1413}
1414
Sebastian Redlb3298c32010-08-18 23:56:48 +00001415void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001416 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001417 unsigned isObjCMethodParam = Record.readInt();
1418 unsigned scopeDepth = Record.readInt();
1419 unsigned scopeIndex = Record.readInt();
1420 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001421 if (isObjCMethodParam) {
1422 assert(scopeDepth == 0);
1423 PD->setObjCMethodScopeInfo(scopeIndex);
1424 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1425 } else {
1426 PD->setScopeInfo(scopeDepth, scopeIndex);
1427 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001428 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1429 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1430 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001431 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001432
1433 // FIXME: If this is a redeclaration of a function from another module, handle
1434 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001435}
1436
Richard Smith7b76d812016-08-12 02:21:25 +00001437void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1438 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001439 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith7b76d812016-08-12 02:21:25 +00001440 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001441 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001442}
1443
1444void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1445 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001446 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001447}
1448
Sebastian Redlb3298c32010-08-18 23:56:48 +00001449void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001450 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001451 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001452 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001453}
1454
Sebastian Redlb3298c32010-08-18 23:56:48 +00001455void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001456 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001457 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001458 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001459 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001460 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001461 Params.reserve(NumParams);
1462 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001463 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001464 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001465
David L. Jonesbe1557a2016-12-21 00:17:49 +00001466 BD->setIsVariadic(Record.readInt());
1467 BD->setBlockMissingReturnType(Record.readInt());
1468 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001469
David L. Jonesbe1557a2016-12-21 00:17:49 +00001470 bool capturesCXXThis = Record.readInt();
1471 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001472 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001473 captures.reserve(numCaptures);
1474 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001475 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001476 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001477 bool byRef = (flags & 1);
1478 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001479 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001480
1481 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1482 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001483 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001484}
1485
Ben Langmuirce914fc2013-05-03 19:20:19 +00001486void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1487 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001488 unsigned ContextParamPos = Record.readInt();
1489 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001490 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001491 for (unsigned I = 0; I < CD->NumParams; ++I) {
1492 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001493 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001494 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001495 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001496 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001497}
1498
Sebastian Redlb3298c32010-08-18 23:56:48 +00001499void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001500 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001501 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001502 D->setExternLoc(ReadSourceLocation());
1503 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001504}
1505
Richard Smith8df390f2016-09-08 23:14:54 +00001506void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1507 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001508 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001509}
1510
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001511void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1512 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001513 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001514}
1515
Sebastian Redlb3298c32010-08-18 23:56:48 +00001516void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001517 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001518 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001519 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001520 D->LocStart = ReadSourceLocation();
1521 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001522
Richard Smith15e32fd2015-03-17 02:23:11 +00001523 // Defer loading the anonymous namespace until we've finished merging
1524 // this namespace; loading it might load a later declaration of the
1525 // same namespace, and we have an invariant that older declarations
1526 // get merged before newer ones try to merge.
1527 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001528 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001529 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001530 } else {
1531 // Link this namespace back to the first declaration, which has already
1532 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001533 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001534 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001535
1536 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001537
1538 if (AnonNamespace) {
1539 // Each module has its own anonymous namespace, which is disjoint from
1540 // any other module's anonymous namespaces, so don't attach the anonymous
1541 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001542 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001543 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001544 D->setAnonymousNamespace(Anon);
1545 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001546}
1547
Sebastian Redlb3298c32010-08-18 23:56:48 +00001548void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001549 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001550 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001551 D->NamespaceLoc = ReadSourceLocation();
1552 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001553 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001554 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001555 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001556}
1557
Sebastian Redlb3298c32010-08-18 23:56:48 +00001558void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001559 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001560 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001561 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001562 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1563 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001564 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001565 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001566 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001567 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001568}
1569
Richard Smith151c4562016-12-20 21:35:28 +00001570void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1571 VisitNamedDecl(D);
1572 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001573 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001574 for (unsigned I = 0; I != D->NumExpansions; ++I)
1575 Expansions[I] = ReadDeclAs<NamedDecl>();
1576 mergeMergeable(D);
1577}
1578
Sebastian Redlb3298c32010-08-18 23:56:48 +00001579void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001580 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001581 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001582 D->Underlying = ReadDeclAs<NamedDecl>();
1583 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001584 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001585 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001586 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001587 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001588 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001589}
1590
Richard Smith5179eb72016-06-28 19:03:57 +00001591void ASTDeclReader::VisitConstructorUsingShadowDecl(
1592 ConstructorUsingShadowDecl *D) {
1593 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001594 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1595 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001596 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001597}
1598
Sebastian Redlb3298c32010-08-18 23:56:48 +00001599void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001600 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001601 D->UsingLoc = ReadSourceLocation();
1602 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001603 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001604 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1605 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001606}
1607
Sebastian Redlb3298c32010-08-18 23:56:48 +00001608void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001609 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001610 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001611 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001612 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001613 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001614 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001615}
1616
Sebastian Redlb3298c32010-08-18 23:56:48 +00001617void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001618 UnresolvedUsingTypenameDecl *D) {
1619 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001620 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001621 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001622 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001623 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001624}
1625
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001626void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001627 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001628 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001629 Data.UserDeclaredConstructor = Record.readInt();
1630 Data.UserDeclaredSpecialMembers = Record.readInt();
1631 Data.Aggregate = Record.readInt();
1632 Data.PlainOldData = Record.readInt();
1633 Data.Empty = Record.readInt();
1634 Data.Polymorphic = Record.readInt();
1635 Data.Abstract = Record.readInt();
1636 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001637 Data.IsCXX11StandardLayout = Record.readInt();
1638 Data.HasBasesWithFields = Record.readInt();
1639 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001640 Data.HasPrivateFields = Record.readInt();
1641 Data.HasProtectedFields = Record.readInt();
1642 Data.HasPublicFields = Record.readInt();
1643 Data.HasMutableFields = Record.readInt();
1644 Data.HasVariantMembers = Record.readInt();
1645 Data.HasOnlyCMembers = Record.readInt();
1646 Data.HasInClassInitializer = Record.readInt();
1647 Data.HasUninitializedReferenceMember = Record.readInt();
1648 Data.HasUninitializedFields = Record.readInt();
1649 Data.HasInheritedConstructor = Record.readInt();
1650 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001651 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001652 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1653 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1654 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001655 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001656 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1657 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1658 Data.DefaultedDestructorIsDeleted = Record.readInt();
1659 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001660 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001661 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001662 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001663 Data.HasIrrelevantDestructor = Record.readInt();
1664 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1665 Data.HasDefaultedDefaultConstructor = Record.readInt();
1666 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1667 Data.HasConstexprDefaultConstructor = Record.readInt();
1668 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1669 Data.ComputedVisibleConversions = Record.readInt();
1670 Data.UserProvidedDefaultConstructor = Record.readInt();
1671 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001672 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1673 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001674 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1675 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1676 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001677 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001678 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001679
Richard Smithcd4a7a42017-09-07 00:55:55 +00001680 if (Record.readInt())
1681 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001682
David L. Jonesbe1557a2016-12-21 00:17:49 +00001683 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001684 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001685 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001686 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001687 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001688 Data.VBases = ReadGlobalOffset();
1689
David L. Jonesb6a8f022016-12-21 04:34:52 +00001690 Record.readUnresolvedSet(Data.Conversions);
1691 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001692 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001693 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001694
Douglas Gregor99ae8062012-02-14 17:54:36 +00001695 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001696 using Capture = LambdaCapture;
1697
1698 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001699 Lambda.Dependent = Record.readInt();
1700 Lambda.IsGenericLambda = Record.readInt();
1701 Lambda.CaptureDefault = Record.readInt();
1702 Lambda.NumCaptures = Record.readInt();
1703 Lambda.NumExplicitCaptures = Record.readInt();
1704 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001705 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001706 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1707 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001708 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001709 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001710 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001711 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001712 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001713 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001714 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001715 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001716 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001717 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001718 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001719 break;
1720 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001721 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001722 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001723 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001724 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1725 break;
1726 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001727 }
1728 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001729}
1730
Richard Smithcd45dbc2014-04-19 03:48:30 +00001731void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001732 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001733 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001734 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001735 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001736
Richard Smith02793752015-03-27 21:16:39 +00001737 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001738 // Track that we merged the definitions.
1739 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1740 DD.Definition));
1741 Reader.PendingDefinitions.erase(MergeDD.Definition);
1742 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001743 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001744 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1745 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001746 }
1747
Richard Smith2a9e5c52015-02-03 03:32:14 +00001748 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1749 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1750 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001751 // We faked up this definition data because we found a class for which we'd
1752 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001753 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001754 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001755
1756 // Don't change which declaration is the definition; that is required
1757 // to be invariant once we select it.
1758 auto *Def = DD.Definition;
1759 DD = std::move(MergeDD);
1760 DD.Definition = Def;
1761 return;
1762 }
1763
Richard Smithcd45dbc2014-04-19 03:48:30 +00001764 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001765 bool DetectedOdrViolation = false;
1766#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1767#define MATCH_FIELD(Field) \
1768 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1769 OR_FIELD(Field)
1770 MATCH_FIELD(UserDeclaredConstructor)
1771 MATCH_FIELD(UserDeclaredSpecialMembers)
1772 MATCH_FIELD(Aggregate)
1773 MATCH_FIELD(PlainOldData)
1774 MATCH_FIELD(Empty)
1775 MATCH_FIELD(Polymorphic)
1776 MATCH_FIELD(Abstract)
1777 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001778 MATCH_FIELD(IsCXX11StandardLayout)
1779 MATCH_FIELD(HasBasesWithFields)
1780 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001781 MATCH_FIELD(HasPrivateFields)
1782 MATCH_FIELD(HasProtectedFields)
1783 MATCH_FIELD(HasPublicFields)
1784 MATCH_FIELD(HasMutableFields)
1785 MATCH_FIELD(HasVariantMembers)
1786 MATCH_FIELD(HasOnlyCMembers)
1787 MATCH_FIELD(HasInClassInitializer)
1788 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001789 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001790 MATCH_FIELD(HasInheritedConstructor)
1791 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001792 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001793 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1794 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1795 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001796 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001797 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1798 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1799 MATCH_FIELD(DefaultedDestructorIsDeleted)
1800 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001801 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001802 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001803 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001804 MATCH_FIELD(HasIrrelevantDestructor)
1805 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001806 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001807 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1808 OR_FIELD(HasConstexprDefaultConstructor)
1809 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1810 // ComputedVisibleConversions is handled below.
1811 MATCH_FIELD(UserProvidedDefaultConstructor)
1812 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001813 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1814 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001815 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1816 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1817 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1818 MATCH_FIELD(IsLambda)
1819#undef OR_FIELD
1820#undef MATCH_FIELD
1821
1822 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1823 DetectedOdrViolation = true;
1824 // FIXME: Issue a diagnostic if the base classes don't match when we come
1825 // to lazily load them.
1826
1827 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1828 // match when we come to lazily load them.
1829 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1830 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1831 DD.ComputedVisibleConversions = true;
1832 }
1833
1834 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1835 // lazily load it.
1836
1837 if (DD.IsLambda) {
1838 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1839 // when they occur within the body of a function template specialization).
1840 }
1841
Richard Trieufd1acbb2017-04-11 21:31:00 +00001842 if (D->getODRHash() != MergeDD.ODRHash) {
1843 DetectedOdrViolation = true;
1844 }
1845
Richard Smithcd45dbc2014-04-19 03:48:30 +00001846 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001847 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1848 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001849}
1850
Richard Smith2a9e5c52015-02-03 03:32:14 +00001851void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001852 struct CXXRecordDecl::DefinitionData *DD;
1853 ASTContext &C = Reader.getContext();
1854
1855 // Determine whether this is a lambda closure type, so that we can
1856 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001857 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001858 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001859 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001860 LCD_None);
1861 else
1862 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1863
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001864 CXXRecordDecl *Canon = D->getCanonicalDecl();
1865 // Set decl definition data before reading it, so that during deserialization
1866 // when we read CXXRecordDecl, it already has definition data and we don't
1867 // set fake one.
1868 if (!Canon->DefinitionData)
1869 Canon->DefinitionData = DD;
1870 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001871 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001872
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001873 // We might already have a different definition for this record. This can
1874 // happen either because we're reading an update record, or because we've
1875 // already done some merging. Either way, just merge into it.
1876 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001877 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001878 return;
1879 }
1880
Richard Smith97100e32015-06-20 00:22:34 +00001881 // Mark this declaration as being a definition.
1882 D->IsCompleteDefinition = true;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001883
Richard Smith97100e32015-06-20 00:22:34 +00001884 // If this is not the first declaration or is an update record, we can have
1885 // other redeclarations already. Make a note that we need to propagate the
1886 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001887 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001888 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001889}
1890
Richard Smith1d209d02013-05-23 01:49:11 +00001891ASTDeclReader::RedeclarableResult
1892ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1893 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001894
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001895 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001896
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001897 enum CXXRecKind {
1898 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1899 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001900 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001901 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001902 // Merged when we merge the folding set entry in the primary template.
1903 if (!isa<ClassTemplateSpecializationDecl>(D))
1904 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001905 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001906 case CXXRecTemplate: {
1907 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001908 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001909 D->TemplateOrInstantiation = Template;
1910 if (!Template->getTemplatedDecl()) {
1911 // We've not actually loaded the ClassTemplateDecl yet, because we're
1912 // currently being loaded as its pattern. Rely on it to set up our
1913 // TypeForDecl (see VisitClassTemplateDecl).
1914 //
1915 // Beware: we do not yet know our canonical declaration, and may still
1916 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001917 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001918 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001919 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001920 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001921 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001922 auto *RD = ReadDeclAs<CXXRecordDecl>();
1923 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001924 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001925 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1926 MSI->setPointOfInstantiation(POI);
1927 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001928 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001929 break;
1930 }
1931 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001932
David L. Jonesbe1557a2016-12-21 00:17:49 +00001933 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001934 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001935 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001936 else
1937 // Propagate DefinitionData pointer from the canonical declaration.
1938 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1939
Richard Smith676c4042013-08-29 23:59:27 +00001940 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001941 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001942 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001943 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001944 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001945 // FIXME: This is wrong for the ARM ABI, where some other module may have
1946 // made this function no longer be a key function. We need an update
1947 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001948 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001949 }
Richard Smith1d209d02013-05-23 01:49:11 +00001950
1951 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001952}
1953
Richard Smithbc491202017-02-17 20:05:37 +00001954void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1955 VisitFunctionDecl(D);
Faisal Vali1f3a2af2017-11-11 18:02:29 +00001956 D->IsCopyDeductionCandidate = Record.readInt();
Richard Smithbc491202017-02-17 20:05:37 +00001957}
1958
Sebastian Redlb3298c32010-08-18 23:56:48 +00001959void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001960 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001961
David L. Jonesbe1557a2016-12-21 00:17:49 +00001962 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001963 if (D->isCanonicalDecl()) {
1964 while (NumOverridenMethods--) {
1965 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1966 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001967 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001968 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1969 }
1970 } else {
1971 // We don't care about which declarations this used to override; we get
1972 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001973 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001974 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001975}
1976
Sebastian Redlb3298c32010-08-18 23:56:48 +00001977void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001978 // We need the inherited constructor information to merge the declaration,
1979 // so we have to read it before we call VisitCXXMethodDecl.
1980 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001981 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1982 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001983 *D->getTrailingObjects<InheritedConstructor>() =
1984 InheritedConstructor(Shadow, Ctor);
1985 }
1986
Chris Lattnerca025db2010-05-07 21:43:38 +00001987 VisitCXXMethodDecl(D);
1988}
1989
Sebastian Redlb3298c32010-08-18 23:56:48 +00001990void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001991 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001992
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001993 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00001994 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00001995 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00001996 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00001997 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00001998 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00001999 Canon->OperatorDeleteThisArg = ThisArg;
2000 }
Richard Smithf8134002015-03-10 01:41:22 +00002001 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002002}
2003
Sebastian Redlb3298c32010-08-18 23:56:48 +00002004void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002005 VisitCXXMethodDecl(D);
2006}
2007
Douglas Gregorba345522011-12-02 23:23:56 +00002008void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2009 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002010 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002011 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002012 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002013 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002014 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002015 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002016}
2017
Sebastian Redlb3298c32010-08-18 23:56:48 +00002018void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002019 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002020 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002021}
2022
Sebastian Redlb3298c32010-08-18 23:56:48 +00002023void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002024 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002025 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002026 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002027 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002028 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002029 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002030 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002031 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002032 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002033 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002034 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002035}
2036
Sebastian Redlb3298c32010-08-18 23:56:48 +00002037void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002038 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002039 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002040 D->NumParams = NumParams;
2041 D->Params = new TemplateParameterList*[NumParams];
2042 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002043 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002044 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002045 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002046 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002047 D->Friend = GetTypeSourceInfo();
2048 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002049}
2050
Richard Smithf17fdbd2014-04-24 02:25:27 +00002051DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002052 VisitNamedDecl(D);
2053
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002054 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002055 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002056 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002057 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002058 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002059
Richard Smithf17fdbd2014-04-24 02:25:27 +00002060 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002061}
2062
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002063ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002064ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002065 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002066
Douglas Gregor68444de2012-01-14 15:13:49 +00002067 // Make sure we've allocated the Common pointer first. We do this before
2068 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2069 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2070 if (!CanonD->Common) {
2071 CanonD->Common = CanonD->newCommon(Reader.getContext());
2072 Reader.PendingDefinitions.insert(CanonD);
2073 }
2074 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002075
Douglas Gregor68444de2012-01-14 15:13:49 +00002076 // If this is the first declaration of the template, fill in the information
2077 // for the 'common' pointer.
2078 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002079 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002080 assert(RTD->getKind() == D->getKind() &&
2081 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002082 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002083 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002084 D->setMemberSpecialization();
2085 }
2086 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002087
Richard Smithf17fdbd2014-04-24 02:25:27 +00002088 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002089 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002090
Richard Smithf17fdbd2014-04-24 02:25:27 +00002091 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002092
Richard Smithd46d6de2013-10-13 23:50:45 +00002093 // If we merged the template with a prior declaration chain, merge the common
2094 // pointer.
2095 // FIXME: Actually merge here, don't just overwrite.
2096 D->Common = D->getCanonicalDecl()->Common;
2097
Douglas Gregor68444de2012-01-14 15:13:49 +00002098 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002099}
2100
Sebastian Redlb3298c32010-08-18 23:56:48 +00002101void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002102 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002103
Douglas Gregor68444de2012-01-14 15:13:49 +00002104 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002105 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2106 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002107 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002108 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002109 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002110 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002111
2112 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2113 // We were loaded before our templated declaration was. We've not set up
2114 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2115 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002116 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002117 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002118 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002119}
2120
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002121void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2122 llvm_unreachable("BuiltinTemplates are not serialized");
2123}
2124
Larisse Voufo30616382013-08-23 22:21:36 +00002125/// TODO: Unify with ClassTemplateDecl version?
2126/// May require unifying ClassTemplateDecl and
2127/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002128void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2129 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2130
2131 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002132 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002133 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002134 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002135 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002136 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002137 }
2138}
2139
Richard Smith1d209d02013-05-23 01:49:11 +00002140ASTDeclReader::RedeclarableResult
2141ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2142 ClassTemplateSpecializationDecl *D) {
2143 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002144
Douglas Gregor4163aca2011-09-09 21:34:22 +00002145 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002146 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002147 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002148 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002149 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002150 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002151 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002152 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002153 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002154 auto *PS =
2155 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002156 SpecializedPartialSpecialization();
2157 PS->PartialSpecialization
2158 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2159 PS->TemplateArgs = ArgList;
2160 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002161 }
2162 }
2163
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002164 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002165 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002166 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002167 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002168 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002169
David L. Jonesbe1557a2016-12-21 00:17:49 +00002170 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002171 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002172 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002173 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002174 // Set this as, or find, the canonical declaration for this specialization
2175 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002176 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002177 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002178 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002179 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002180 CanonSpec =
2181 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2182 }
2183 // If there was already a canonical specialization, merge into it.
2184 if (CanonSpec != D) {
2185 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2186
2187 // This declaration might be a definition. Merge with any existing
2188 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002189 if (auto *DDD = D->DefinitionData) {
2190 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002191 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002192 else
Richard Smith053f6c62014-05-16 23:01:30 +00002193 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002194 }
Richard Smith547864d2014-07-11 18:22:58 +00002195 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002196 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002197 }
2198 }
Richard Smith1d209d02013-05-23 01:49:11 +00002199
Richard Smithd55889a2013-09-09 16:55:27 +00002200 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002201 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002202 auto *ExplicitInfo =
2203 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002204 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002205 ExplicitInfo->ExternLoc = ReadSourceLocation();
2206 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002207 D->ExplicitInfo = ExplicitInfo;
2208 }
2209
Richard Smith1d209d02013-05-23 01:49:11 +00002210 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002211}
2212
Sebastian Redlb3298c32010-08-18 23:56:48 +00002213void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002214 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002215 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002216
David L. Jonesb6a8f022016-12-21 04:34:52 +00002217 D->TemplateParams = Record.readTemplateParameterList();
2218 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002219
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002220 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002221 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002222 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002223 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002224 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002225 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002226}
2227
Sebastian Redlb7448632011-08-31 13:59:56 +00002228void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2229 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002230 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002231 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002232}
2233
Sebastian Redlb3298c32010-08-18 23:56:48 +00002234void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002235 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002236
Douglas Gregor68444de2012-01-14 15:13:49 +00002237 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002238 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002239 SmallVector<serialization::DeclID, 32> SpecIDs;
2240 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002241 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002242 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002243}
2244
Larisse Voufo30616382013-08-23 22:21:36 +00002245/// TODO: Unify with ClassTemplateSpecializationDecl version?
2246/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2247/// VarTemplate(Partial)SpecializationDecl with a new data
2248/// structure Template(Partial)SpecializationDecl, and
2249/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002250ASTDeclReader::RedeclarableResult
2251ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2252 VarTemplateSpecializationDecl *D) {
2253 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2254
2255 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002256 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002257 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002258 D->SpecializedTemplate = VTD;
2259 } else {
2260 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002261 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002262 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002263 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002264 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002265 new (C)
2266 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2267 PS->PartialSpecialization =
2268 cast<VarTemplatePartialSpecializationDecl>(InstD);
2269 PS->TemplateArgs = ArgList;
2270 D->SpecializedTemplate = PS;
2271 }
2272 }
2273
2274 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002275 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002276 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002277 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2278 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002279 ExplicitInfo->ExternLoc = ReadSourceLocation();
2280 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002281 D->ExplicitInfo = ExplicitInfo;
2282 }
2283
2284 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002285 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002286 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002287 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002288 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002289 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002290
David L. Jonesbe1557a2016-12-21 00:17:49 +00002291 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002292 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002293 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002294 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002295 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002296 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002297 CanonPattern->getCommonPtr()->PartialSpecializations
2298 .GetOrInsertNode(Partial);
2299 } else {
2300 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2301 }
2302 }
2303 }
2304
2305 return Redecl;
2306}
2307
Larisse Voufo30616382013-08-23 22:21:36 +00002308/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2309/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2310/// VarTemplate(Partial)SpecializationDecl with a new data
2311/// structure Template(Partial)SpecializationDecl, and
2312/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002313void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2314 VarTemplatePartialSpecializationDecl *D) {
2315 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2316
David L. Jonesb6a8f022016-12-21 04:34:52 +00002317 D->TemplateParams = Record.readTemplateParameterList();
2318 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002319
2320 // These are read/set from/to the first declaration.
2321 if (ThisDeclID == Redecl.getFirstID()) {
2322 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002323 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002324 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002325 }
2326}
2327
Sebastian Redlb3298c32010-08-18 23:56:48 +00002328void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002329 VisitTypeDecl(D);
2330
David L. Jonesbe1557a2016-12-21 00:17:49 +00002331 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002332
David L. Jonesbe1557a2016-12-21 00:17:49 +00002333 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002334 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002335}
2336
Sebastian Redlb3298c32010-08-18 23:56:48 +00002337void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002338 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002339 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002340 D->setDepth(Record.readInt());
2341 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002342 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002343 auto TypesAndInfos =
2344 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002345 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002346 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002347 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002348 }
2349 } else {
2350 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002351 D->ParameterPack = Record.readInt();
2352 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002353 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002354 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002355}
2356
Sebastian Redlb3298c32010-08-18 23:56:48 +00002357void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002358 VisitTemplateDecl(D);
2359 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002360 D->setDepth(Record.readInt());
2361 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002362 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002363 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002364 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2365 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002366 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002367 } else {
2368 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002369 D->ParameterPack = Record.readInt();
2370 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002371 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002372 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002373 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002374}
2375
Richard Smith3f1b5d02011-05-05 21:57:07 +00002376void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2377 VisitRedeclarableTemplateDecl(D);
2378}
2379
Sebastian Redlb3298c32010-08-18 23:56:48 +00002380void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002381 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002382 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002383 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002384 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002385 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002386}
2387
Michael Han84324352013-02-22 17:15:32 +00002388void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2389 VisitDecl(D);
2390}
2391
Mike Stump11289f42009-09-09 15:08:12 +00002392std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002393ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002394 uint64_t LexicalOffset = ReadLocalOffset();
2395 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002396 return std::make_pair(LexicalOffset, VisibleOffset);
2397}
2398
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002399template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002400ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002401ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002402 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002403 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002404
Richard Smith5fc18a92015-07-12 23:43:21 +00002405 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002406 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002407
Richard Smithd61d4ac2015-08-22 20:13:39 +00002408 uint64_t RedeclOffset = 0;
2409
Douglas Gregor358cd442012-01-15 16:58:34 +00002410 // 0 indicates that this declaration was the only declaration of its entity,
2411 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002412 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002413 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002414 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002415 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002416 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002417 // This declaration was the first local declaration, but may have imported
2418 // other declarations.
2419 IsKeyDecl = N == 1;
2420 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002421
Richard Smithfe620d22015-03-05 23:24:12 +00002422 // We have some declarations that must be before us in our redeclaration
2423 // chain. Read them now, and remember that we ought to merge with one of
2424 // them.
2425 // FIXME: Provide a known merge target to the second and subsequent such
2426 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002427 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002428 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002429
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002430 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002431 } else {
2432 // This declaration was not the first local declaration. Read the first
2433 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002434 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002435 }
2436
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002437 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002438 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002439 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2440 // We temporarily set the first (canonical) declaration as the previous one
2441 // which is the one that matters and mark the real previous DeclID to be
2442 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002443 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002444 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002445 }
Richard Smithd8a83712015-08-22 01:47:18 +00002446
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002447 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002448
2449 // Note that we need to load local redeclarations of this decl and build a
2450 // decl chain for them. This must happen *after* we perform the preloading
2451 // above; this ensures that the redeclaration chain is built in the correct
2452 // order.
2453 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002454 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002455
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002456 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002457}
2458
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002459/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002460/// of the same entity.
2461template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002462void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002463 RedeclarableResult &Redecl,
2464 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002465 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002466 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002467 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002468
Richard Smith202850a2015-03-10 02:57:50 +00002469 // If we're not the canonical declaration, we don't need to merge.
2470 if (!DBase->isFirstDecl())
2471 return;
2472
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002473 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002474
Richard Smith5a4737c2015-02-06 02:42:59 +00002475 if (auto *Existing = Redecl.getKnownMergeTarget())
2476 // We already know of an existing declaration we should merge with.
2477 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2478 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002479 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002480 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2481}
2482
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002483/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002484/// We use this to put code in a template that will only be valid for certain
2485/// instantiations.
2486template<typename T> static T assert_cast(T t) { return t; }
2487template<typename T> static T assert_cast(...) {
2488 llvm_unreachable("bad assert_cast");
2489}
2490
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002491/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002492/// declarations.
2493void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2494 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002495 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002496 auto *DPattern = D->getTemplatedDecl();
2497 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002498 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2499 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002500 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002501
Richard Smith547864d2014-07-11 18:22:58 +00002502 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2503 // Merge with any existing definition.
2504 // FIXME: This is duplicated in several places. Refactor.
2505 auto *ExistingClass =
2506 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002507 if (auto *DDD = DClass->DefinitionData) {
2508 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002509 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002510 } else {
2511 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002512 // We may have skipped this before because we thought that DClass
2513 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002514 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002515 }
2516 }
2517 DClass->DefinitionData = ExistingClass->DefinitionData;
2518
Richard Smithf17fdbd2014-04-24 02:25:27 +00002519 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2520 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002521 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002522 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2523 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2524 Result);
2525 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2526 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002527 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2528 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2529 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002530 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002531}
2532
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002533/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002534/// of the same entity.
2535template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002536void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2537 RedeclarableResult &Redecl,
2538 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002539 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002540 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002541 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002542 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002543 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2544 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002545
Richard Smithd55889a2013-09-09 16:55:27 +00002546 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002547 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002548 // appropriate canonical declaration.
2549 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002550 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002551 ExistingCanon->Used |= D->Used;
2552 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002553
2554 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002555 // We cannot have loaded any redeclarations of this declaration yet, so
2556 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002557 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002558 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002559 assert_cast<NamespaceDecl*>(ExistingCanon));
2560
2561 // When we merge a template, merge its pattern.
2562 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2563 mergeTemplatePattern(
2564 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002565 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002566
Richard Smith5fc18a92015-07-12 23:43:21 +00002567 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002568 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002569 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002570 }
2571}
2572
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002573/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2574/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2575/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2576/// that some types are mergeable during deserialization, otherwise name
2577/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002578static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002579 if (!ND)
2580 return false;
2581 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002582 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002583 return true;
2584 return false;
2585}
2586
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002587/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002588/// of the same entity, for the case where the entity is not actually
2589/// redeclarable. This happens, for instance, when merging the fields of
2590/// identical class definitions from two different modules.
2591template<typename T>
2592void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2593 // If modules are not available, there is no reason to perform this merge.
2594 if (!Reader.getContext().getLangOpts().Modules)
2595 return;
2596
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002597 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2598 // Note that C identically-named things in different translation units are
2599 // not redeclarations, but may still have compatible types, where ODR-like
2600 // semantics may apply.
2601 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2602 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002603 return;
2604
Richard Smith0b87e072013-10-07 08:02:11 +00002605 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2606 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002607 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2608 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002609}
2610
Alexey Bataeva769e072013-03-22 06:34:35 +00002611void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2612 VisitDecl(D);
2613 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002614 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002615 Vars.reserve(NumVars);
2616 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002617 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002618 }
2619 D->setVars(Vars);
2620}
2621
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002622void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002623 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002624 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002625 D->setCombiner(Record.readExpr());
Alexey Bataev070f43a2017-09-06 14:49:58 +00002626 D->setInitializer(
2627 Record.readExpr(),
2628 static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002629 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002630}
2631
Alexey Bataev4244be22016-02-11 05:35:55 +00002632void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002633 VisitVarDecl(D);
2634}
2635
Chris Lattner487412d2009-04-27 05:27:42 +00002636//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002637// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002638//===----------------------------------------------------------------------===//
2639
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002640/// Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002641void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2642 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002643 Attr *New = nullptr;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002644 auto Kind = (attr::Kind)Record.readInt();
David L. Jones267b8842017-01-24 01:04:30 +00002645 SourceRange Range = Record.readSourceRange();
Richard Smithdbafb6c2017-06-29 23:23:46 +00002646 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002647
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002648#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002649
2650 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002651 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002652 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002653}
2654
2655//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002656// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002657//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002658
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002659/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002660/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002661///
Chris Lattner487412d2009-04-27 05:27:42 +00002662/// This routine notes that this declaration has already been loaded,
2663/// so that future GetDecl calls will return this declaration rather
2664/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002665inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002666 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2667 DeclsLoaded[Index] = D;
2668}
2669
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002670/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002671/// this declaration (via HandleTopLevelDecl).
2672///
2673/// This routine should return true for anything that might affect
2674/// code generation, e.g., inline function definitions, Objective-C
2675/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002676static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002677 // An ObjCMethodDecl is never considered as "interesting" because its
2678 // implementation container always is.
2679
Richard Smithcd4a7a42017-09-07 00:55:55 +00002680 // An ImportDecl or VarDecl imported from a module map module will get
2681 // emitted when we import the relevant module.
2682 if (isa<ImportDecl>(D) || isa<VarDecl>(D)) {
2683 auto *M = D->getImportedOwningModule();
2684 if (M && M->Kind == Module::ModuleMapModule &&
2685 Ctx.DeclMustBeEmitted(D))
2686 return false;
2687 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002688
Douglas Gregor87d81242011-09-10 00:22:34 +00002689 if (isa<FileScopeAsmDecl>(D) ||
2690 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002691 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002692 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002693 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002694 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002695 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002696 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2697 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002698 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002699 return Var->isFileVarDecl() &&
2700 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002701 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002702 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002703
2704 if (auto *ES = D->getASTContext().getExternalSource())
2705 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2706 return true;
2707
Douglas Gregor87d81242011-09-10 00:22:34 +00002708 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002709}
2710
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002711/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002712ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002713ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002714 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2715 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002716 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002717 const DeclOffset &DOffs =
2718 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002719 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002720 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002721}
2722
Douglas Gregord32f0352011-07-22 06:10:01 +00002723ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002724 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002725
2726 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2727 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2728}
2729
Douglas Gregorde3ef502011-11-30 23:21:26 +00002730uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002731 return LocalOffset + M.GlobalBitOffset;
2732}
2733
Richard Smithbf78e642013-06-24 22:51:00 +00002734static bool isSameTemplateParameterList(const TemplateParameterList *X,
2735 const TemplateParameterList *Y);
2736
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002737/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002738/// that they may be used in declarations of the same template.
2739static bool isSameTemplateParameter(const NamedDecl *X,
2740 const NamedDecl *Y) {
2741 if (X->getKind() != Y->getKind())
2742 return false;
2743
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002744 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2745 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002746 return TX->isParameterPack() == TY->isParameterPack();
2747 }
2748
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002749 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2750 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002751 return TX->isParameterPack() == TY->isParameterPack() &&
2752 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2753 }
2754
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002755 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2756 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002757 return TX->isParameterPack() == TY->isParameterPack() &&
2758 isSameTemplateParameterList(TX->getTemplateParameters(),
2759 TY->getTemplateParameters());
2760}
2761
Richard Smith32952e12014-10-14 02:00:47 +00002762static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2763 if (auto *NS = X->getAsNamespace())
2764 return NS;
2765 if (auto *NAS = X->getAsNamespaceAlias())
2766 return NAS->getNamespace();
2767 return nullptr;
2768}
2769
2770static bool isSameQualifier(const NestedNameSpecifier *X,
2771 const NestedNameSpecifier *Y) {
2772 if (auto *NSX = getNamespace(X)) {
2773 auto *NSY = getNamespace(Y);
2774 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2775 return false;
2776 } else if (X->getKind() != Y->getKind())
2777 return false;
2778
2779 // FIXME: For namespaces and types, we're permitted to check that the entity
2780 // is named via the same tokens. We should probably do so.
2781 switch (X->getKind()) {
2782 case NestedNameSpecifier::Identifier:
2783 if (X->getAsIdentifier() != Y->getAsIdentifier())
2784 return false;
2785 break;
2786 case NestedNameSpecifier::Namespace:
2787 case NestedNameSpecifier::NamespaceAlias:
2788 // We've already checked that we named the same namespace.
2789 break;
2790 case NestedNameSpecifier::TypeSpec:
2791 case NestedNameSpecifier::TypeSpecWithTemplate:
2792 if (X->getAsType()->getCanonicalTypeInternal() !=
2793 Y->getAsType()->getCanonicalTypeInternal())
2794 return false;
2795 break;
2796 case NestedNameSpecifier::Global:
2797 case NestedNameSpecifier::Super:
2798 return true;
2799 }
2800
2801 // Recurse into earlier portion of NNS, if any.
2802 auto *PX = X->getPrefix();
2803 auto *PY = Y->getPrefix();
2804 if (PX && PY)
2805 return isSameQualifier(PX, PY);
2806 return !PX && !PY;
2807}
2808
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002809/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002810/// that they may be used in declarations of the same template.
2811static bool isSameTemplateParameterList(const TemplateParameterList *X,
2812 const TemplateParameterList *Y) {
2813 if (X->size() != Y->size())
2814 return false;
2815
2816 for (unsigned I = 0, N = X->size(); I != N; ++I)
2817 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2818 return false;
2819
2820 return true;
2821}
2822
George Burgess IV95845082017-02-15 22:43:27 +00002823/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002824/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002825static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2826 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002827 // Note that pass_object_size attributes are represented in the function's
2828 // ExtParameterInfo, so we don't need to check them here.
2829
Michael Kruse41dd6ce2018-06-25 20:06:13 +00002830 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2831 // Since this is an equality check, we can ignore that enable_if attrs show up
2832 // in reverse order.
2833 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2834 AEnableIfs.push_back(EIA);
2835
2836 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2837 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2838 BEnableIfs.push_back(EIA);
2839
2840 // Two very common cases: either we have 0 enable_if attrs, or we have an
2841 // unequal number of enable_if attrs.
2842 if (AEnableIfs.empty() && BEnableIfs.empty())
2843 return true;
2844
2845 if (AEnableIfs.size() != BEnableIfs.size())
2846 return false;
2847
George Burgess IV95845082017-02-15 22:43:27 +00002848 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Kruse41dd6ce2018-06-25 20:06:13 +00002849 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
George Burgess IV95845082017-02-15 22:43:27 +00002850 Cand1ID.clear();
2851 Cand2ID.clear();
2852
Michael Kruse41dd6ce2018-06-25 20:06:13 +00002853 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2854 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
George Burgess IV95845082017-02-15 22:43:27 +00002855 if (Cand1ID != Cand2ID)
2856 return false;
2857 }
2858
Michael Kruse41dd6ce2018-06-25 20:06:13 +00002859 return true;
George Burgess IV95845082017-02-15 22:43:27 +00002860}
2861
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002862/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002863static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2864 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002865
Douglas Gregor022857e2011-12-22 01:48:48 +00002866 if (X == Y)
2867 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002868
Douglas Gregor022857e2011-12-22 01:48:48 +00002869 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00002870 //
2871 // Note that we can't use DeclContext::Equals here, because the DeclContexts
2872 // could be two different declarations of the same function. (We will fix the
2873 // semantic DC to refer to the primary definition after merging.)
2874 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
2875 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00002876 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002877
2878 // Two typedefs refer to the same entity if they have the same underlying
2879 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002880 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
2881 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002882 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2883 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002884
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002885 // Must have the same kind.
2886 if (X->getKind() != Y->getKind())
2887 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002888
Douglas Gregorda389302012-01-01 21:47:52 +00002889 // Objective-C classes and protocols with the same name always match.
2890 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002891 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002892
2893 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002894 // No need to handle these here: we merge them when adding them to the
2895 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002896 return false;
2897 }
Richard Smithd55889a2013-09-09 16:55:27 +00002898
Douglas Gregor2009cee2012-01-03 22:46:00 +00002899 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002900 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
2901 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00002902 return (TagX->getTagKind() == TagY->getTagKind()) ||
2903 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2904 TagX->getTagKind() == TTK_Interface) &&
2905 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2906 TagY->getTagKind() == TTK_Interface));
2907 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002908
Joao Matose9a3ed42012-08-31 22:18:20 +00002909 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002910 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2911 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002912 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
2913 const auto *FuncY = cast<FunctionDecl>(Y);
2914 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2915 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002916 if (CtorX->getInheritedConstructor() &&
2917 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2918 CtorY->getInheritedConstructor().getConstructor()))
2919 return false;
2920 }
Erich Keane281d20b2018-01-08 21:34:17 +00002921
2922 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
2923 return false;
2924
2925 // Multiversioned functions with different feature strings are represented
2926 // as separate declarations.
2927 if (FuncX->isMultiVersion()) {
2928 const auto *TAX = FuncX->getAttr<TargetAttr>();
2929 const auto *TAY = FuncY->getAttr<TargetAttr>();
2930 assert(TAX && TAY && "Multiversion Function without target attribute");
2931
2932 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
2933 return false;
2934 }
2935
Richard Smitha54d3242017-03-08 23:00:26 +00002936 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00002937 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
2938 // Map to the first declaration that we've already merged into this one.
2939 // The TSI of redeclarations might not match (due to calling conventions
2940 // being inherited onto the type but not the TSI), but the TSI type of
2941 // the first declaration of the function should match across modules.
2942 FD = FD->getCanonicalDecl();
2943 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
2944 : FD->getType();
2945 };
2946 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
2947 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00002948 // We can get functions with different types on the redecl chain in C++17
2949 // if they have differing exception specifications and at least one of
2950 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00002951 auto *XFPT = XT->getAs<FunctionProtoType>();
2952 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002953 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00002954 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2955 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00002956 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00002957 return true;
2958 return false;
2959 }
George Burgess IV95845082017-02-15 22:43:27 +00002960 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002961 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002962 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002963
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002964 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002965 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
2966 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002967 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2968 ASTContext &C = VarX->getASTContext();
2969 if (C.hasSameType(VarX->getType(), VarY->getType()))
2970 return true;
2971
2972 // We can get decls with different types on the redecl chain. Eg.
2973 // template <typename T> struct S { static T Var[]; }; // #1
2974 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2975 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002976 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002977 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2978 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2979 if (!VarXTy || !VarYTy)
2980 return false;
2981 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2982 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2983 }
2984 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002985 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002986
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002987 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002988 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2989 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002990 return NamespaceX->isInline() == NamespaceY->isInline();
2991 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002992
Richard Smith8f8f05c2013-06-24 04:45:28 +00002993 // Identical template names and kinds match if their template parameter lists
2994 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002995 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
2996 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002997 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002998 TemplateY->getTemplatedDecl()) &&
2999 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3000 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003001 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003002
Richard Smith0b87e072013-10-07 08:02:11 +00003003 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003004 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3005 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003006 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003007 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3008 }
3009
Richard Smith8cbd8952015-08-04 02:05:09 +00003010 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003011 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3012 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003013 return IFDX->getAnonField()->getCanonicalDecl() ==
3014 IFDY->getAnonField()->getCanonicalDecl();
3015 }
3016
Richard Smith01a73372013-10-15 22:02:41 +00003017 // Enumerators with the same name match.
3018 if (isa<EnumConstantDecl>(X))
3019 // FIXME: Also check the value is odr-equivalent.
3020 return true;
3021
Richard Smithfd8634a2013-10-23 02:17:46 +00003022 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003023 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3024 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003025 return USX->getTargetDecl() == USY->getTargetDecl();
3026 }
3027
Richard Smith32952e12014-10-14 02:00:47 +00003028 // Using declarations with the same qualifier match. (We already know that
3029 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003030 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3031 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003032 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3033 UX->hasTypename() == UY->hasTypename() &&
3034 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3035 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003036 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3037 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003038 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3039 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3040 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003041 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003042 return isSameQualifier(
3043 UX->getQualifier(),
3044 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3045
Richard Smithf4634362014-09-03 23:11:22 +00003046 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003047 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3048 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003049 return NAX->getNamespace()->Equals(NAY->getNamespace());
3050 }
3051
Douglas Gregor022857e2011-12-22 01:48:48 +00003052 return false;
3053}
3054
Richard Smithd55889a2013-09-09 16:55:27 +00003055/// Find the context in which we should search for previous declarations when
3056/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003057DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3058 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003059 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003060 return ND->getOriginalNamespace();
3061
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003062 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003063 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003064 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003065 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003066 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003067
3068 // If there's no definition yet, then DC's definition is added by an update
3069 // record, but we've not yet loaded that update record. In this case, we
3070 // commit to DC being the canonical definition now, and will fix this when
3071 // we load the update record.
3072 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003073 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Richard Smith8a639892015-01-24 01:07:20 +00003074 RD->IsCompleteDefinition = true;
3075 RD->DefinitionData = DD;
3076 RD->getCanonicalDecl()->DefinitionData = DD;
3077
3078 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003079 Reader.PendingFakeDefinitionData.insert(
3080 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003081 }
3082
3083 return DD->Definition;
3084 }
Richard Smithd55889a2013-09-09 16:55:27 +00003085
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003086 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003087 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3088 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003089
Richard Smith4eca9b92015-02-04 23:37:59 +00003090 // We can see the TU here only if we have no Sema object. In that case,
3091 // there's no TU scope to look in, so using the DC alone is sufficient.
3092 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3093 return TU;
3094
Craig Toppera13603a2014-05-22 05:54:18 +00003095 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003096}
3097
Douglas Gregor022857e2011-12-22 01:48:48 +00003098ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003099 // Record that we had a typedef name for linkage whether or not we merge
3100 // with that declaration.
3101 if (TypedefNameForLinkage) {
3102 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3103 Reader.ImportedTypedefNamesForLinkage.insert(
3104 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3105 return;
3106 }
3107
Douglas Gregor9b47f942012-01-09 17:38:47 +00003108 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003109 return;
Richard Smith95d99302013-07-13 02:00:19 +00003110
Richard Smith70d58502014-08-30 00:04:23 +00003111 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003112 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003113 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003114 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3115 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003116 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003117 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003118 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003119 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3120 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003121 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003122 // Add the declaration to its redeclaration context so later merging
3123 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003124 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003125 }
3126}
3127
Richard Smith88ebade2014-08-23 01:45:27 +00003128/// Find the declaration that should be merged into, given the declaration found
3129/// by name lookup. If we're merging an anonymous declaration within a typedef,
3130/// we need a matching typedef, and we merge with the type inside it.
3131static NamedDecl *getDeclForMerging(NamedDecl *Found,
3132 bool IsTypedefNameForLinkage) {
3133 if (!IsTypedefNameForLinkage)
3134 return Found;
3135
3136 // If we found a typedef declaration that gives a name to some other
3137 // declaration, then we want that inner declaration. Declarations from
3138 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003139 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003140 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003141
3142 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003143 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003144
Hans Wennborgdcfba332015-10-06 23:40:43 +00003145 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003146}
3147
Richard Smith600adef2018-07-04 02:25:38 +00003148/// Find the declaration to use to populate the anonymous declaration table
3149/// for the given lexical DeclContext. We only care about finding local
3150/// definitions of the context; we'll merge imported ones as we go.
3151DeclContext *
3152ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3153 // For classes, we track the definition as we merge.
3154 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3155 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3156 return DD ? DD->Definition : nullptr;
3157 }
3158
3159 // For anything else, walk its merged redeclarations looking for a definition.
3160 // Note that we can't just call getDefinition here because the redeclaration
3161 // chain isn't wired up.
3162 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3163 if (auto *FD = dyn_cast<FunctionDecl>(D))
3164 if (FD->isThisDeclarationADefinition())
3165 return FD;
3166 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3167 if (MD->isThisDeclarationADefinition())
3168 return MD;
3169 }
3170
3171 // No merged definition yet.
3172 return nullptr;
3173}
3174
Richard Smithd08aeb62014-08-28 01:33:39 +00003175NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3176 DeclContext *DC,
3177 unsigned Index) {
3178 // If the lexical context has been merged, look into the now-canonical
3179 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003180 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003181
3182 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003183 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003184 if (Index < Previous.size() && Previous[Index])
3185 return Previous[Index];
3186
3187 // If this is the first time, but we have parsed a declaration of the context,
3188 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003189 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3190 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3191 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003192 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003193 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3194 else
Richard Smith2b560572015-02-07 03:11:11 +00003195 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3196 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003197 }
3198
3199 return Index < Previous.size() ? Previous[Index] : nullptr;
3200}
3201
3202void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3203 DeclContext *DC, unsigned Index,
3204 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003205 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003206
Richard Smith600adef2018-07-04 02:25:38 +00003207 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003208 if (Index >= Previous.size())
3209 Previous.resize(Index + 1);
3210 if (!Previous[Index])
3211 Previous[Index] = D;
3212}
3213
Douglas Gregor022857e2011-12-22 01:48:48 +00003214ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003215 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3216 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003217
Richard Smithd08aeb62014-08-28 01:33:39 +00003218 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3219 // Don't bother trying to find unnamed declarations that are in
3220 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003221 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3222 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003223 Result.suppress();
3224 return Result;
3225 }
Richard Smith95d99302013-07-13 02:00:19 +00003226
Douglas Gregor022857e2011-12-22 01:48:48 +00003227 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003228 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003229 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003230 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003231 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3232 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003233 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3234 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003235 // Go on to check in other places in case an existing typedef name
3236 // was not imported.
3237 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003238
Richard Smith2b560572015-02-07 03:11:11 +00003239 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003240 // This is an anonymous declaration that we may need to merge. Look it up
3241 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003242 if (auto *Existing = getAnonymousDeclForMerging(
3243 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3244 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003245 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3246 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003247 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003248 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003249 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003250
3251 // Temporarily consider the identifier to be up-to-date. We don't want to
3252 // cause additional lookups here.
3253 class UpToDateIdentifierRAII {
3254 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003255 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003256
3257 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003258 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003259 if (II) {
3260 WasOutToDate = II->isOutOfDate();
3261 if (WasOutToDate)
3262 II->setOutOfDate(false);
3263 }
3264 }
3265
3266 ~UpToDateIdentifierRAII() {
3267 if (WasOutToDate)
3268 II->setOutOfDate(true);
3269 }
3270 } UpToDate(Name.getAsIdentifierInfo());
3271
Douglas Gregor2009cee2012-01-03 22:46:00 +00003272 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003273 IEnd = IdResolver.end();
3274 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003275 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003276 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003277 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3278 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003279 }
Richard Smith8a639892015-01-24 01:07:20 +00003280 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003281 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003282 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003283 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003284 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003285 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3286 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003287 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003288 } else {
3289 // Not in a mergeable context.
3290 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003291 }
Richard Smithd55889a2013-09-09 16:55:27 +00003292
Richard Smith2b9e3e32013-10-18 06:05:18 +00003293 // If this declaration is from a merged context, make a note that we need to
3294 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003295 //
3296 // FIXME: We should do something similar if we merge two definitions of the
3297 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003298 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3299 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3300 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003301 Reader.PendingOdrMergeChecks.push_back(D);
3302
Richard Smithd08aeb62014-08-28 01:33:39 +00003303 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003304 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003305}
3306
Richard Smithb321ecb2014-05-13 01:15:00 +00003307template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003308Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3309 return D->RedeclLink.getLatestNotUpdated();
3310}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003311
Richard Smithc3a53252015-02-28 05:57:02 +00003312Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3313 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3314}
3315
3316Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3317 assert(D);
3318
3319 switch (D->getKind()) {
3320#define ABSTRACT_DECL(TYPE)
3321#define DECL(TYPE, BASE) \
3322 case Decl::TYPE: \
3323 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3324#include "clang/AST/DeclNodes.inc"
3325 }
3326 llvm_unreachable("unknown decl kind");
3327}
3328
Richard Smith8ce51082015-03-11 01:44:51 +00003329Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3330 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3331}
3332
Richard Smithc3a53252015-02-28 05:57:02 +00003333template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003334void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3335 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003336 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003337 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003338 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003339}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003340
Richard Smith24d166c2014-07-31 23:52:38 +00003341namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003342
Richard Smith6de7a242014-07-31 23:46:44 +00003343template<>
3344void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003345 Redeclarable<VarDecl> *D,
3346 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003347 auto *VD = static_cast<VarDecl *>(D);
3348 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003349 D->RedeclLink.setPrevious(PrevVD);
3350 D->First = PrevVD->First;
3351
3352 // We should keep at most one definition on the chain.
3353 // FIXME: Cache the definition once we've found it. Building a chain with
3354 // N definitions currently takes O(N^2) time here.
3355 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3356 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3357 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3358 Reader.mergeDefinitionVisibility(CurD, VD);
3359 VD->demoteThisDefinitionToDeclaration();
3360 break;
3361 }
3362 }
3363 }
3364}
3365
3366template<>
3367void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003368 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003369 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003370 auto *FD = static_cast<FunctionDecl *>(D);
3371 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003372
3373 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003374 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003375
3376 // If the previous declaration is an inline function declaration, then this
3377 // declaration is too.
3378 if (PrevFD->IsInline != FD->IsInline) {
3379 // FIXME: [dcl.fct.spec]p4:
3380 // If a function with external linkage is declared inline in one
3381 // translation unit, it shall be declared inline in all translation
3382 // units in which it appears.
3383 //
3384 // Be careful of this case:
3385 //
3386 // module A:
3387 // template<typename T> struct X { void f(); };
3388 // template<typename T> inline void X<T>::f() {}
3389 //
3390 // module B instantiates the declaration of X<int>::f
3391 // module C instantiates the definition of X<int>::f
3392 //
3393 // If module B and C are merged, we do not have a violation of this rule.
3394 FD->IsInline = true;
3395 }
3396
Richard Smith9e2341d2015-03-23 03:25:59 +00003397 // If we need to propagate an exception specification along the redecl
3398 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003399 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3400 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003401 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003402 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3403 bool WasUnresolved =
3404 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3405 if (IsUnresolved != WasUnresolved)
3406 Reader.PendingExceptionSpecUpdates.insert(
3407 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003408 }
3409}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003410
3411} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003412
Richard Smith6de7a242014-07-31 23:46:44 +00003413void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003414 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3415}
3416
Richard Smith8346e522015-06-10 01:47:58 +00003417/// Inherit the default template argument from \p From to \p To. Returns
3418/// \c false if there is no default template for \p From.
3419template <typename ParmDecl>
3420static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3421 Decl *ToD) {
3422 auto *To = cast<ParmDecl>(ToD);
3423 if (!From->hasDefaultArgument())
3424 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003425 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003426 return true;
3427}
3428
3429static void inheritDefaultTemplateArguments(ASTContext &Context,
3430 TemplateDecl *From,
3431 TemplateDecl *To) {
3432 auto *FromTP = From->getTemplateParameters();
3433 auto *ToTP = To->getTemplateParameters();
3434 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3435
3436 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3437 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003438 if (FromParam->isParameterPack())
3439 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003440 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3441
3442 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003443 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003444 break;
3445 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003446 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003447 break;
3448 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003449 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003450 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3451 break;
3452 }
3453 }
3454}
3455
Richard Smith6de7a242014-07-31 23:46:44 +00003456void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003457 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003458 assert(D && Previous);
3459
3460 switch (D->getKind()) {
3461#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003462#define DECL(TYPE, BASE) \
3463 case Decl::TYPE: \
3464 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003465 break;
3466#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003467 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003468
3469 // If the declaration was visible in one module, a redeclaration of it in
3470 // another module remains visible even if it wouldn't be visible by itself.
3471 //
3472 // FIXME: In this case, the declaration should only be visible if a module
3473 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003474 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003475 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003476 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003477
Richard Smith8346e522015-06-10 01:47:58 +00003478 // If the declaration declares a template, it may inherit default arguments
3479 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003480 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003481 inheritDefaultTemplateArguments(Reader.getContext(),
3482 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003483}
3484
Richard Smithb321ecb2014-05-13 01:15:00 +00003485template<typename DeclT>
3486void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003487 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003488}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003489
Richard Smithb321ecb2014-05-13 01:15:00 +00003490void ASTDeclReader::attachLatestDeclImpl(...) {
3491 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3492}
3493
Douglas Gregor05f10352011-12-17 23:38:30 +00003494void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3495 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003496
3497 switch (D->getKind()) {
3498#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003499#define DECL(TYPE, BASE) \
3500 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003501 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3502 break;
3503#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003504 }
3505}
3506
Richard Smith851072e2014-05-19 20:59:20 +00003507template<typename DeclT>
3508void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3509 D->RedeclLink.markIncomplete();
3510}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003511
Richard Smith851072e2014-05-19 20:59:20 +00003512void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3513 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3514}
3515
3516void ASTReader::markIncompleteDeclChain(Decl *D) {
3517 switch (D->getKind()) {
3518#define ABSTRACT_DECL(TYPE)
3519#define DECL(TYPE, BASE) \
3520 case Decl::TYPE: \
3521 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3522 break;
3523#include "clang/AST/DeclNodes.inc"
3524 }
3525}
3526
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003527/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003528Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003529 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003530 SourceLocation DeclLoc;
3531 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003532 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003533 // Keep track of where we are in the stream, then jump back there
3534 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003535 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003536
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003537 ReadingKindTracker ReadingKind(Read_Decl, *this);
3538
Douglas Gregor1342e842009-07-06 18:54:52 +00003539 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003540 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003541
Sebastian Redl2c373b92010-10-05 15:59:54 +00003542 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003543 ASTRecordReader Record(*this, *Loc.F);
3544 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003545 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003546
Richard Smithdbafb6c2017-06-29 23:23:46 +00003547 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003548 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003549 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003550 case DECL_CONTEXT_LEXICAL:
3551 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003552 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003553 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003554 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003555 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003556 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003557 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003558 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003559 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003560 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003561 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003562 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003563 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003564 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003565 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003566 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003567 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003568 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003569 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003570 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003571 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003572 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003573 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003574 case DECL_EXPORT:
3575 D = ExportDecl::CreateDeserialized(Context, ID);
3576 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003577 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003578 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003579 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003580 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003581 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003582 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003583 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003584 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003585 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003586 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003587 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003588 break;
Richard Smith151c4562016-12-20 21:35:28 +00003589 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003590 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003591 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003592 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003593 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003594 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003595 case DECL_CONSTRUCTOR_USING_SHADOW:
3596 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3597 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003598 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003599 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003600 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003601 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003602 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003603 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003604 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003605 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003606 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003607 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003608 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003609 break;
Richard Smithbc491202017-02-17 20:05:37 +00003610 case DECL_CXX_DEDUCTION_GUIDE:
3611 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3612 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003613 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003614 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003615 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003616 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003617 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3618 break;
3619 case DECL_CXX_INHERITED_CONSTRUCTOR:
3620 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003621 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003622 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003623 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003624 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003625 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003626 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003627 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003628 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003629 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003630 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003631 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003632 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003633 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003634 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003635 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003636 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003637 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003638 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003639 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003640 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003641 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003642 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003643 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003644 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003645 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003646 case DECL_VAR_TEMPLATE:
3647 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3648 break;
3649 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3650 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3651 break;
3652 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3653 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3654 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003655 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003656 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003657 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003658 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003659 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003660 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003661 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003662 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003663 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003664 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003665 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003666 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003667 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003668 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3669 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003670 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003671 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003672 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003673 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003674 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3675 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003676 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003677 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003678 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003679 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003680 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003681 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003682 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003683 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003684 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003685 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003686 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003687 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003688 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003689 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003690 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003691 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003692 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003693 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003694 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003695 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003696 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003697 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003698 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003699 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003700 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003701 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003702 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003703 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003704 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003705 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003706 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003707 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003708 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003709 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003710 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003711 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003712 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003713 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003714 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003715 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003716 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003717 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003718 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003719 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003720 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003721 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003722 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003723 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003724 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003725 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003726 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003727 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003728 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003729 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003730 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003731 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003732 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003733 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003734 break;
3735 case DECL_BINDING:
3736 D = BindingDecl::CreateDeserialized(Context, ID);
3737 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003738 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003739 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003740 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003741 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003742 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003743 break;
John McCall5e77d762013-04-16 07:28:30 +00003744 case DECL_MS_PROPERTY:
3745 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3746 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003747 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003748 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003749 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003750 case DECL_CXX_BASE_SPECIFIERS:
3751 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003752 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003753 case DECL_CXX_CTOR_INITIALIZERS:
3754 Error("attempt to read a C++ ctor initializer record as a declaration");
3755 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003756 case DECL_IMPORT:
3757 // Note: last entry of the ImportDecl record is the number of stored source
3758 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003759 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003760 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003761 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003762 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003763 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003764 case DECL_OMP_DECLARE_REDUCTION:
3765 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3766 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003767 case DECL_OMP_CAPTUREDEXPR:
3768 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003769 break;
Nico Weber66220292016-03-02 17:28:48 +00003770 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003771 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003772 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003773 case DECL_PRAGMA_DETECT_MISMATCH:
3774 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003775 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003776 break;
Michael Han84324352013-02-22 17:15:32 +00003777 case DECL_EMPTY:
3778 D = EmptyDecl::CreateDeserialized(Context, ID);
3779 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003780 case DECL_OBJC_TYPE_PARAM:
3781 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3782 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003783 }
Chris Lattner487412d2009-04-27 05:27:42 +00003784
Sebastian Redlb3298c32010-08-18 23:56:48 +00003785 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003786 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003787 // Set the DeclContext before doing any deserialization, to make sure internal
3788 // calls to Decl::getASTContext() by Decl's methods will find the
3789 // TranslationUnitDecl without crashing.
3790 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003791 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003792
3793 // If this declaration is also a declaration context, get the
3794 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003795 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003796 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003797 if (Offsets.first &&
3798 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3799 return nullptr;
3800 if (Offsets.second &&
3801 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3802 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003803 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003804 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003805
Douglas Gregordab42432011-08-12 00:15:20 +00003806 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003807 PendingUpdateRecords.push_back(
3808 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003809
Douglas Gregor404cdde2012-01-27 01:47:08 +00003810 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003811 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003812 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3813 // we put the Decl in PendingDefinitions so we can pull the categories here.
3814 if (Class->isThisDeclarationADefinition() ||
3815 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003816 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003817
Richard Smith13fb8602016-07-15 21:33:46 +00003818 // If we have deserialized a declaration that has a definition the
3819 // AST consumer might need to know about, queue it.
3820 // We don't pass it to the consumer immediately because we may be in recursive
3821 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003822 PotentiallyInterestingDecls.push_back(
3823 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003824
Douglas Gregordab42432011-08-12 00:15:20 +00003825 return D;
3826}
3827
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003828void ASTReader::PassInterestingDeclsToConsumer() {
3829 assert(Consumer);
3830
3831 if (PassingDeclsToConsumer)
3832 return;
3833
3834 // Guard variable to avoid recursively redoing the process of passing
3835 // decls to consumer.
3836 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3837 true);
3838
3839 // Ensure that we've loaded all potentially-interesting declarations
3840 // that need to be eagerly loaded.
3841 for (auto ID : EagerlyDeserializedDecls)
3842 GetDecl(ID);
3843 EagerlyDeserializedDecls.clear();
3844
3845 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003846 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003847 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003848 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3849 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003850 }
3851}
3852
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003853void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003854 // The declaration may have been modified by files later in the chain.
3855 // If this is the case, read the record containing the updates from each file
3856 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003857 serialization::GlobalDeclID ID = Record.ID;
3858 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003859 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003860 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003861
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003862 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00003863
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003864 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003865 auto UpdateOffsets = std::move(UpdI->second);
3866 DeclUpdateOffsets.erase(UpdI);
3867
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003868 // Check if this decl was interesting to the consumer. If we just loaded
3869 // the declaration, then we know it was interesting and we skip the call
3870 // to isConsumerInterestedIn because it is unsafe to call in the
3871 // current ASTReader state.
3872 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003873 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003874 for (auto &FileAndOffset : UpdateOffsets) {
3875 ModuleFile *F = FileAndOffset.first;
3876 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003877 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3878 SavedStreamPosition SavedPosition(Cursor);
3879 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003880 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003881 ASTRecordReader Record(*this, *F);
3882 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003883 (void)RecCode;
3884 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003885
David L. Jonesbe1557a2016-12-21 00:17:49 +00003886 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3887 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003888 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003889
3890 // We might have made this declaration interesting. If so, remember that
3891 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003892 if (!WasInteresting &&
3893 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
3894 PotentiallyInterestingDecls.push_back(
3895 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003896 WasInteresting = true;
3897 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003898 }
3899 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00003900 // Add the lazy specializations to the template.
3901 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3902 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3903 "Must not have pending specializations");
3904 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3905 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3906 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3907 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3908 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3909 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
3910 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00003911
3912 // Load the pending visible updates for this decl context, if it has any.
3913 auto I = PendingVisibleUpdates.find(ID);
3914 if (I != PendingVisibleUpdates.end()) {
3915 auto VisibleUpdates = std::move(I->second);
3916 PendingVisibleUpdates.erase(I);
3917
3918 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003919 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00003920 Lookups[DC].Table.add(
3921 Update.Mod, Update.Data,
3922 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3923 DC->setHasExternalVisibleStorage(true);
3924 }
Chris Lattner487412d2009-04-27 05:27:42 +00003925}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003926
Richard Smithd61d4ac2015-08-22 20:13:39 +00003927void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3928 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003929 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003930 if (FirstLocal != CanonDecl) {
3931 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3932 ASTDeclReader::attachPreviousDecl(
3933 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3934 CanonDecl);
3935 }
3936
3937 if (!LocalOffset) {
3938 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3939 return;
3940 }
3941
3942 // Load the list of other redeclarations from this module file.
3943 ModuleFile *M = getOwningModuleFile(FirstLocal);
3944 assert(M && "imported decl from no module file");
3945
3946 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3947 SavedStreamPosition SavedPosition(Cursor);
3948 Cursor.JumpToBit(LocalOffset);
3949
3950 RecordData Record;
3951 unsigned Code = Cursor.ReadCode();
3952 unsigned RecCode = Cursor.readRecord(Code, Record);
3953 (void)RecCode;
3954 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3955
3956 // FIXME: We have several different dispatches on decl kind here; maybe
3957 // we should instead generate one loop per kind and dispatch up-front?
3958 Decl *MostRecent = FirstLocal;
3959 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3960 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003961 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3962 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003963 }
Richard Smithc3a53252015-02-28 05:57:02 +00003964 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003965}
3966
3967namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003968
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003969 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003970 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003971 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003972 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003973 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003974 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003975 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003976 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003977 serialization::GlobalDeclID InterfaceID;
3978 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003979
3980 void add(ObjCCategoryDecl *Cat) {
3981 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003982 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003983 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003984
3985 // Check for duplicate categories.
3986 if (Cat->getDeclName()) {
3987 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3988 if (Existing &&
3989 Reader.getOwningModuleFile(Existing)
3990 != Reader.getOwningModuleFile(Cat)) {
3991 // FIXME: We should not warn for duplicates in diamond:
3992 //
3993 // MT //
3994 // / \ //
3995 // ML MR //
3996 // \ / //
3997 // MB //
3998 //
3999 // If there are duplicates in ML/MR, there will be warning when
4000 // creating MB *and* when importing MB. We should not warn when
4001 // importing.
4002 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4003 << Interface->getDeclName() << Cat->getDeclName();
4004 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4005 } else if (!Existing) {
4006 // Record this category.
4007 Existing = Cat;
4008 }
4009 }
4010
4011 // Add this category to the end of the chain.
4012 if (Tail)
4013 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4014 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004015 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004016 Tail = Cat;
4017 }
4018
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004019 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004020 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004021 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004022 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4023 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004024 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004025 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4026 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004027 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004028 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004029 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004030 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004031
4032 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004033 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004034 }
4035 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004036
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004037 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004038 // If we've loaded all of the category information we care about from
4039 // this module file, we're done.
4040 if (M.Generation <= PreviousGeneration)
4041 return true;
4042
4043 // Map global ID of the definition down to the local ID used in this
4044 // module file. If there is no such mapping, we'll find nothing here
4045 // (or in any module it imports).
4046 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4047 if (!LocalID)
4048 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004049
Douglas Gregor404cdde2012-01-27 01:47:08 +00004050 // Perform a binary search to find the local redeclarations for this
4051 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004052 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004053 const ObjCCategoriesInfo *Result
4054 = std::lower_bound(M.ObjCCategoriesMap,
4055 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004056 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004057 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4058 Result->DefinitionID != LocalID) {
4059 // We didn't find anything. If the class definition is in this module
4060 // file, then the module files it depends on cannot have any categories,
4061 // so suppress further lookup.
4062 return Reader.isDeclIDFromModule(InterfaceID, M);
4063 }
4064
4065 // We found something. Dig out all of the categories.
4066 unsigned Offset = Result->Offset;
4067 unsigned N = M.ObjCCategories[Offset];
4068 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4069 for (unsigned I = 0; I != N; ++I)
4070 add(cast_or_null<ObjCCategoryDecl>(
4071 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4072 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004073 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004074 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004075
4076} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004077
Douglas Gregor404cdde2012-01-27 01:47:08 +00004078void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4079 ObjCInterfaceDecl *D,
4080 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004081 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004082 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004083 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004084}
Douglas Gregordab42432011-08-12 00:15:20 +00004085
Richard Smithd6db68c2014-08-07 20:58:41 +00004086template<typename DeclT, typename Fn>
4087static void forAllLaterRedecls(DeclT *D, Fn F) {
4088 F(D);
4089
4090 // Check whether we've already merged D into its redeclaration chain.
4091 // MostRecent may or may not be nullptr if D has not been merged. If
4092 // not, walk the merged redecl chain and see if it's there.
4093 auto *MostRecent = D->getMostRecentDecl();
4094 bool Found = false;
4095 for (auto *Redecl = MostRecent; Redecl && !Found;
4096 Redecl = Redecl->getPreviousDecl())
4097 Found = (Redecl == D);
4098
4099 // If this declaration is merged, apply the functor to all later decls.
4100 if (Found) {
4101 for (auto *Redecl = MostRecent; Redecl != D;
4102 Redecl = Redecl->getPreviousDecl())
4103 F(Redecl);
4104 }
4105}
4106
Vassil Vassilev7d264622017-06-30 22:40:17 +00004107void ASTDeclReader::UpdateDecl(Decl *D,
4108 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004109 while (Record.getIdx() < Record.size()) {
4110 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004111 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004112 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004113 // FIXME: If we also have an update record for instantiating the
4114 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004115 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004116 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004117 // FIXME: We should call addHiddenDecl instead, to add the member
4118 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004119 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004120 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004121 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004122
4123 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004124 // It will be added to the template's lazy specialization set.
4125 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004126 break;
4127
4128 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004129 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004130
Douglas Gregor540fd812012-01-09 18:07:24 +00004131 // Each module has its own anonymous namespace, which is disjoint from
4132 // any other module's anonymous namespaces, so don't attach the anonymous
4133 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004134 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004135 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004136 TU->setAnonymousNamespace(Anon);
4137 else
4138 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4139 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004140 break;
4141 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004142
Richard Smith891fc7f2017-12-05 01:31:47 +00004143 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004144 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004145 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4146 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004147 uint64_t Val = Record.readInt();
4148 if (Val && !VD->getInit()) {
4149 VD->setInit(Record.readExpr());
4150 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4151 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4152 Eval->CheckedICE = true;
4153 Eval->IsICE = Val == 3;
4154 }
4155 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004156 break;
Richard Smith05a21352017-06-22 22:18:46 +00004157 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004158
Richard Smith891fc7f2017-12-05 01:31:47 +00004159 case UPD_CXX_POINT_OF_INSTANTIATION: {
4160 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004161 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004162 VTSD->setPointOfInstantiation(POI);
4163 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4164 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4165 } else {
4166 auto *FD = cast<FunctionDecl>(D);
4167 if (auto *FTSInfo = FD->TemplateOrSpecialization
4168 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4169 FTSInfo->setPointOfInstantiation(POI);
4170 else
4171 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4172 ->setPointOfInstantiation(POI);
4173 }
4174 break;
4175 }
4176
John McCall32791cc2016-01-06 22:34:54 +00004177 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004178 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004179
4180 // We have to read the default argument regardless of whether we use it
4181 // so that hypothetical further update records aren't messed up.
4182 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004183 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004184
4185 // Only apply the update if the parameter still has an uninstantiated
4186 // default argument.
4187 if (Param->hasUninstantiatedDefaultArg())
4188 Param->setDefaultArg(DefaultArg);
4189 break;
4190 }
4191
Richard Smith4b054b22016-08-24 21:25:37 +00004192 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004193 auto *FD = cast<FieldDecl>(D);
4194 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004195
4196 // Only apply the update if the field still has an uninstantiated
4197 // default member initializer.
4198 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4199 if (DefaultInit)
4200 FD->setInClassInitializer(DefaultInit);
4201 else
4202 // Instantiation failed. We can get here if we serialized an AST for
4203 // an invalid program.
4204 FD->removeInClassInitializer();
4205 }
4206 break;
4207 }
4208
Richard Smith4d235792014-08-07 18:53:08 +00004209 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004210 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004211 if (Reader.PendingBodies[FD]) {
4212 // FIXME: Maybe check for ODR violations.
4213 // It's safe to stop now because this update record is always last.
4214 return;
4215 }
4216
David L. Jonesbe1557a2016-12-21 00:17:49 +00004217 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004218 // Maintain AST consistency: any later redeclarations of this function
4219 // are inline if this one is. (We might have merged another declaration
4220 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004221 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4222 FD->setImplicitlyInline();
4223 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004224 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004225 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004226 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004227 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004228 break;
4229 }
4230
Richard Smithcd45dbc2014-04-19 03:48:30 +00004231 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4232 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004233 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004234 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004235 OldDD && (OldDD->Definition != RD ||
4236 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004237 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004238 RD->setArgPassingRestrictions(
4239 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004240 ReadCXXRecordDefinition(RD, /*Update*/true);
4241
Richard Smithcd45dbc2014-04-19 03:48:30 +00004242 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004243 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004244 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004245 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004246 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004247 }
4248
David L. Jonesbe1557a2016-12-21 00:17:49 +00004249 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004250 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004251 if (MemberSpecializationInfo *MSInfo =
4252 RD->getMemberSpecializationInfo()) {
4253 MSInfo->setTemplateSpecializationKind(TSK);
4254 MSInfo->setPointOfInstantiation(POI);
4255 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004256 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004257 Spec->setTemplateSpecializationKind(TSK);
4258 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004259
David L. Jonesbe1557a2016-12-21 00:17:49 +00004260 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004261 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004262 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004263 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004264 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004265 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004266 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004267
4268 // FIXME: If we already have a partial specialization set,
4269 // check that it matches.
4270 if (!Spec->getSpecializedTemplateOrPartial()
4271 .is<ClassTemplatePartialSpecializationDecl *>())
4272 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004273 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004274 }
4275
David L. Jonesbe1557a2016-12-21 00:17:49 +00004276 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004277 RD->setLocation(ReadSourceLocation());
4278 RD->setLocStart(ReadSourceLocation());
4279 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004280
David L. Jonesbe1557a2016-12-21 00:17:49 +00004281 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004282 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004283 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004284 // If the declaration already has attributes, we assume that some other
4285 // AST file already loaded them.
4286 if (!D->hasAttrs())
4287 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004288 }
4289 break;
4290 }
4291
Richard Smithf8134002015-03-10 01:41:22 +00004292 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4293 // Set the 'operator delete' directly to avoid emitting another update
4294 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004295 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004296 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004297 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004298 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004299 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004300 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004301 First->OperatorDeleteThisArg = ThisArg;
4302 }
Richard Smithf8134002015-03-10 01:41:22 +00004303 break;
4304 }
4305
Richard Smith564417a2014-03-20 21:47:22 +00004306 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004307 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004308 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004309 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004310
4311 // Update this declaration's exception specification, if needed.
4312 auto *FD = cast<FunctionDecl>(D);
4313 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4314 // FIXME: If the exception specification is already present, check that it
4315 // matches.
4316 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004317 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004318 FPT->getReturnType(), FPT->getParamTypes(),
4319 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004320
4321 // When we get to the end of deserializing, see if there are other decls
4322 // that we need to propagate this exception specification onto.
4323 Reader.PendingExceptionSpecUpdates.insert(
4324 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004325 }
Richard Smith564417a2014-03-20 21:47:22 +00004326 break;
4327 }
4328
Richard Smith1fa5d642013-05-11 05:45:24 +00004329 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004330 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004331 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004332 for (auto *Redecl : merged_redecls(D)) {
4333 // FIXME: If the return type is already deduced, check that it matches.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004334 auto *FD = cast<FunctionDecl>(Redecl);
Richard Smithdbafb6c2017-06-29 23:23:46 +00004335 Reader.getContext().adjustDeducedFunctionResultType(FD,
4336 DeducedResultType);
Richard Smithd6db68c2014-08-07 20:58:41 +00004337 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004338 break;
4339 }
Eli Friedman276dd182013-09-05 00:02:25 +00004340
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004341 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004342 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004343 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004344 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004345
4346 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004347 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4348 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004349 break;
4350
4351 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004352 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4353 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004354 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004355
Alexey Bataev97720002014-11-11 04:05:39 +00004356 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004357 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4358 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004359 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004360
Alex Denisovfde64952015-06-26 05:28:36 +00004361 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004362 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004363 auto *Exported = cast<NamedDecl>(D);
4364 if (auto *TD = dyn_cast<TagDecl>(Exported))
4365 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004366 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004367 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004368 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4369 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004370 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4371 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004372 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004373 // If Owner is made visible at some later point, make this declaration
4374 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004375 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004376 } else {
4377 // The declaration is now visible.
Richard Smith90dc5252017-06-23 01:04:34 +00004378 Exported->setVisibleDespiteOwningModule();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004379 }
4380 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004381 }
Alex Denisovfde64952015-06-26 05:28:36 +00004382
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004383 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004384 case UPD_ADDED_ATTR_TO_RECORD:
4385 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004386 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004387 assert(Attrs.size() == 1);
4388 D->addAttr(Attrs[0]);
4389 break;
4390 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004391 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004392}