blob: da30612b4f1c920d7ca251276822d654246f5ba0 [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);
Fangrui Song6907ce22018-07-30 19:24:48 +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);
Fangrui Song6907ce22018-07-30 19:24:48 +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());
Fangrui Song6907ce22018-07-30 19:24:48 +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 Trieuab4d7302018-07-25 22:52:05 +0000749 ED->HasODRHash = true;
750 ED->ODRHash = Record.readInt();
751
Richard Smith01a73372013-10-15 22:02:41 +0000752 // If this is a definition subject to the ODR, and we already have a
753 // definition, merge this one into it.
754 if (ED->IsCompleteDefinition &&
755 Reader.getContext().getLangOpts().Modules &&
756 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000757 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
758 if (!OldDef) {
759 // This is the first time we've seen an imported definition. Look for a
760 // local definition before deciding that we are the first definition.
761 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
762 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
763 OldDef = D;
764 break;
765 }
766 }
767 }
768 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000769 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
770 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000771 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000772 if (OldDef->getODRHash() != ED->getODRHash())
773 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000774 } else {
775 OldDef = ED;
776 }
777 }
778
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000779 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
780 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000781 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000782 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
783 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
784 }
Chris Lattner487412d2009-04-27 05:27:42 +0000785}
786
Richard Smith1d209d02013-05-23 01:49:11 +0000787ASTDeclReader::RedeclarableResult
788ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
789 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000790 RD->setHasFlexibleArrayMember(Record.readInt());
791 RD->setAnonymousStructOrUnion(Record.readInt());
792 RD->setHasObjectMember(Record.readInt());
793 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000794 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
795 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
796 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000797 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000798 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000799 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000800}
801
Sebastian Redlb3298c32010-08-18 23:56:48 +0000802void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000803 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000804 // For function declarations, defer reading the type in case the function has
805 // a deduced return type that references an entity declared within the
806 // function.
807 if (isa<FunctionDecl>(VD))
808 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
809 else
810 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000811}
812
Sebastian Redlb3298c32010-08-18 23:56:48 +0000813void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000814 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000815 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000816 ECD->setInitExpr(Record.readExpr());
817 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000818 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000819}
820
Sebastian Redlb3298c32010-08-18 23:56:48 +0000821void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000822 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000823 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000824 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000825 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000826 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000827 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000828 }
Richard Smithc23d7342018-06-29 20:46:25 +0000829 QualType TSIType = Record.readType();
830 DD->setTypeSourceInfo(
831 TSIType.isNull() ? nullptr
832 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000833}
834
Sebastian Redlb3298c32010-08-18 23:56:48 +0000835void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000836 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000837 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000838
Richard Smith600adef2018-07-04 02:25:38 +0000839 // Attach a type to this function. Use the real type if possible, but fall
840 // back to the type as written if it involves a deduced return type.
841 if (FD->getTypeSourceInfo() &&
842 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
843 ->getReturnType()->getContainedAutoType()) {
844 // We'll set up the real type in Visit, once we've finished loading the
845 // function.
846 FD->setType(FD->getTypeSourceInfo()->getType());
847 } else {
848 FD->setType(Reader.GetType(DeferredTypeID));
849 DeferredTypeID = 0;
850 }
851
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000852 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000853 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000854
Douglas Gregorb2585692012-01-04 17:13:46 +0000855 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
856 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000857
David L. Jonesbe1557a2016-12-21 00:17:49 +0000858 FD->SClass = (StorageClass)Record.readInt();
859 FD->IsInline = Record.readInt();
860 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000861 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000862 FD->IsVirtualAsWritten = Record.readInt();
863 FD->IsPure = Record.readInt();
864 FD->HasInheritedPrototype = Record.readInt();
865 FD->HasWrittenPrototype = Record.readInt();
866 FD->IsDeleted = Record.readInt();
867 FD->IsTrivial = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000868 FD->IsTrivialForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000869 FD->IsDefaulted = Record.readInt();
870 FD->IsExplicitlyDefaulted = Record.readInt();
871 FD->HasImplicitReturnZero = Record.readInt();
872 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000873 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000874 FD->HasSkippedBody = Record.readInt();
Erich Keane281d20b2018-01-08 21:34:17 +0000875 FD->IsMultiVersion = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000876 FD->IsLateTemplateParsed = Record.readInt();
877 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000878 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000879
Richard Trieue6caa262017-12-23 00:41:01 +0000880 FD->ODRHash = Record.readInt();
881 FD->HasODRHash = true;
882
David L. Jonesbe1557a2016-12-21 00:17:49 +0000883 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000884 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000885 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000886 break;
887 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000888 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000889 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000890 break;
891 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000892 auto *InstFD = ReadDeclAs<FunctionDecl>();
893 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000894 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000895 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000896 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000897 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000898 break;
899 }
900 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000901 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
902 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000903
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000904 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000905 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000906 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000907
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000908 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000909 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000910 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000911 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000912 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000913 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000914 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000915 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000916 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000917
918 LAngleLoc = ReadSourceLocation();
919 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000920 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000921
922 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000923
Douglas Gregor4163aca2011-09-09 21:34:22 +0000924 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000925 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000926 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000927 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000928 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000929 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000930 FunctionTemplateSpecializationInfo *FTInfo
931 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
932 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000933 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
934 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000935 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000936 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000937
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000938 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000939 // The template that contains the specializations set. It's not safe to
940 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000941 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000942 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
943 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
944 // FunctionTemplateSpecializationInfo's Profile().
945 // We avoid getASTContext because a decl in the parent hierarchy may
946 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000947 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000948 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000949 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000950 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000951 FunctionTemplateSpecializationInfo *ExistingInfo =
952 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000953 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000954 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
955 else {
956 assert(Reader.getContext().getLangOpts().Modules &&
957 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000958 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000959 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000960 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000961 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000962 }
963 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
964 // Templates.
965 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000966 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000967 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000968 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
969
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000970 // Templates args.
971 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000972 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000973 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000974 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000975 TemplArgs.setLAngleLoc(ReadSourceLocation());
976 TemplArgs.setRAngleLoc(ReadSourceLocation());
977
Douglas Gregor4163aca2011-09-09 21:34:22 +0000978 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000979 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000980 // These are not merged; we don't need to merge redeclarations of dependent
981 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000982 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000983 }
984 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000985
Chris Lattnerca025db2010-05-07 21:43:38 +0000986 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000987 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000988 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000989 Params.reserve(NumParams);
990 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000991 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000992 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000993}
994
Sebastian Redlb3298c32010-08-18 23:56:48 +0000995void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000996 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000997 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000998 // Load the body on-demand. Most clients won't care, because method
999 // definitions rarely show up in headers.
1000 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1001 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001002 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1003 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001004 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001005 MD->setInstanceMethod(Record.readInt());
1006 MD->setVariadic(Record.readInt());
1007 MD->setPropertyAccessor(Record.readInt());
1008 MD->setDefined(Record.readInt());
1009 MD->IsOverriding = Record.readInt();
1010 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001011
David L. Jonesbe1557a2016-12-21 00:17:49 +00001012 MD->IsRedeclaration = Record.readInt();
1013 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001014 if (MD->HasRedeclaration)
1015 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001016 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001017
David L. Jonesbe1557a2016-12-21 00:17:49 +00001018 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1019 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
1020 MD->SetRelatedResultType(Record.readInt());
1021 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001022 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1023 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001024 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001025 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001026 Params.reserve(NumParams);
1027 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001028 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001029
David L. Jonesbe1557a2016-12-21 00:17:49 +00001030 MD->SelLocsKind = Record.readInt();
1031 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001032 SmallVector<SourceLocation, 16> SelLocs;
1033 SelLocs.reserve(NumStoredSelLocs);
1034 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001035 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001036
1037 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001038}
1039
Douglas Gregor85f3f952015-07-07 03:57:15 +00001040void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001041 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001042
David L. Jonesbe1557a2016-12-21 00:17:49 +00001043 D->Variance = Record.readInt();
1044 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001045 D->VarianceLoc = ReadSourceLocation();
1046 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001047}
1048
Sebastian Redlb3298c32010-08-18 23:56:48 +00001049void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001050 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001051 CD->setAtStartLoc(ReadSourceLocation());
1052 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001053}
1054
Douglas Gregor85f3f952015-07-07 03:57:15 +00001055ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001056 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001057 if (numParams == 0)
1058 return nullptr;
1059
1060 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1061 typeParams.reserve(numParams);
1062 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001063 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001064 if (!typeParam)
1065 return nullptr;
1066
1067 typeParams.push_back(typeParam);
1068 }
1069
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001070 SourceLocation lAngleLoc = ReadSourceLocation();
1071 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001072
1073 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1074 typeParams, rAngleLoc);
1075}
1076
Manman Renec315f12016-09-09 23:48:27 +00001077void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001078 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001079 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001080 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001081
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001082 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001083 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001084
Manman Renec315f12016-09-09 23:48:27 +00001085 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001086 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001087 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1088 Protocols.reserve(NumProtocols);
1089 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001090 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001091 SmallVector<SourceLocation, 16> ProtoLocs;
1092 ProtoLocs.reserve(NumProtocols);
1093 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001094 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001095 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1096 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001097
Manman Renec315f12016-09-09 23:48:27 +00001098 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001099 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001100 Protocols.clear();
1101 Protocols.reserve(NumProtocols);
1102 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001103 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001104 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1105 Reader.getContext());
1106}
1107
1108void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1109 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1110 // FIXME: odr checking?
1111}
1112
Sebastian Redlb3298c32010-08-18 23:56:48 +00001113void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001114 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001115 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001116 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001117 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001118
1119 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001120 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001121 // Read the definition.
1122 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001123
David L. Jonesbe1557a2016-12-21 00:17:49 +00001124 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001125 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1126 if (Canon->Data.getPointer()) {
1127 // If we already have a definition, keep the definition invariant and
1128 // merge the data.
1129 MergeDefinitionData(Canon, std::move(ID->data()));
1130 ID->Data = Canon->Data;
1131 } else {
1132 // Set the definition data of the canonical declaration, so other
1133 // redeclarations will see it.
1134 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001135
Manman Renec315f12016-09-09 23:48:27 +00001136 // We will rebuild this list lazily.
1137 ID->setIvarList(nullptr);
1138 }
Craig Toppera13603a2014-05-22 05:54:18 +00001139
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001140 // Note that we have deserialized a definition.
1141 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001142
Douglas Gregor404cdde2012-01-27 01:47:08 +00001143 // Note that we've loaded this Objective-C class.
1144 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001145 } else {
1146 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001147 }
Chris Lattner487412d2009-04-27 05:27:42 +00001148}
1149
Sebastian Redlb3298c32010-08-18 23:56:48 +00001150void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001151 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001152 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001153 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001154 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001155 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001156 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001157}
1158
Graydon Hoaree0a68352017-06-28 18:36:27 +00001159void ASTDeclReader::ReadObjCDefinitionData(
1160 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001161 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001162 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1163 ProtoRefs.reserve(NumProtoRefs);
1164 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001165 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001166 SmallVector<SourceLocation, 16> ProtoLocs;
1167 ProtoLocs.reserve(NumProtoRefs);
1168 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001169 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001170 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1171 ProtoLocs.data(), Reader.getContext());
1172}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001173
Graydon Hoaree0a68352017-06-28 18:36:27 +00001174void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1175 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1176 // FIXME: odr checking?
1177}
1178
1179void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1180 RedeclarableResult Redecl = VisitRedeclarable(PD);
1181 VisitObjCContainerDecl(PD);
1182 mergeRedeclarable(PD, Redecl);
1183
1184 if (Record.readInt()) {
1185 // Read the definition.
1186 PD->allocateDefinitionData();
1187
1188 ReadObjCDefinitionData(PD->data());
1189
1190 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1191 if (Canon->Data.getPointer()) {
1192 // If we already have a definition, keep the definition invariant and
1193 // merge the data.
1194 MergeDefinitionData(Canon, std::move(PD->data()));
1195 PD->Data = Canon->Data;
1196 } else {
1197 // Set the definition data of the canonical declaration, so other
1198 // redeclarations will see it.
1199 PD->getCanonicalDecl()->Data = PD->Data;
1200 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001201 // Note that we have deserialized a definition.
1202 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001203 } else {
1204 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001205 }
Chris Lattner487412d2009-04-27 05:27:42 +00001206}
1207
Sebastian Redlb3298c32010-08-18 23:56:48 +00001208void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001209 VisitFieldDecl(FD);
1210}
1211
Sebastian Redlb3298c32010-08-18 23:56:48 +00001212void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001213 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001214 CD->setCategoryNameLoc(ReadSourceLocation());
1215 CD->setIvarLBraceLoc(ReadSourceLocation());
1216 CD->setIvarRBraceLoc(ReadSourceLocation());
1217
Douglas Gregor404cdde2012-01-27 01:47:08 +00001218 // Note that this category has been deserialized. We do this before
1219 // deserializing the interface declaration, so that it will consider this
1220 /// category.
1221 Reader.CategoriesDeserialized.insert(CD);
1222
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001223 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001224 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001225 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001226 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001227 ProtoRefs.reserve(NumProtoRefs);
1228 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001229 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001230 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001231 ProtoLocs.reserve(NumProtoRefs);
1232 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001233 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001234 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001235 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001236
1237 // Protocols in the class extension belong to the class.
1238 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1239 CD->ClassInterface->mergeClassExtensionProtocolList(
1240 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1241 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001242}
1243
Sebastian Redlb3298c32010-08-18 23:56:48 +00001244void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001245 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001246 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001247}
1248
Sebastian Redlb3298c32010-08-18 23:56:48 +00001249void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001250 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001251 D->setAtLoc(ReadSourceLocation());
1252 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001253 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001254 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001255 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001256 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001257 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001258 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001259 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001260 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001261 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001262 DeclarationName GetterName = Record.readDeclarationName();
1263 SourceLocation GetterLoc = ReadSourceLocation();
1264 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1265 DeclarationName SetterName = Record.readDeclarationName();
1266 SourceLocation SetterLoc = ReadSourceLocation();
1267 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001268 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1269 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1270 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001271}
1272
Sebastian Redlb3298c32010-08-18 23:56:48 +00001273void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001274 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001275 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001276}
1277
Sebastian Redlb3298c32010-08-18 23:56:48 +00001278void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001279 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001280 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001281}
1282
Sebastian Redlb3298c32010-08-18 23:56:48 +00001283void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001284 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001285 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1286 D->SuperLoc = ReadSourceLocation();
1287 D->setIvarLBraceLoc(ReadSourceLocation());
1288 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001289 D->setHasNonZeroConstructors(Record.readInt());
1290 D->setHasDestructors(Record.readInt());
1291 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001292 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001293 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001294}
1295
Sebastian Redlb3298c32010-08-18 23:56:48 +00001296void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001297 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001298 D->setAtLoc(ReadSourceLocation());
1299 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1300 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1301 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001302 D->setGetterCXXConstructor(Record.readExpr());
1303 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001304}
1305
Sebastian Redlb3298c32010-08-18 23:56:48 +00001306void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001307 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001308 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001309
1310 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1311 FD->InitStorage.setInt(ISK);
1312 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1313 ? Record.readType().getAsOpaquePtr()
1314 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001315 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001316
1317 if (auto *BW = Record.readExpr())
1318 FD->setBitWidth(BW);
1319
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001320 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001321 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001322 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001323 }
Richard Smith0b87e072013-10-07 08:02:11 +00001324 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001325}
1326
John McCall5e77d762013-04-16 07:28:30 +00001327void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1328 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001329 PD->GetterId = Record.getIdentifierInfo();
1330 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001331}
1332
Francois Pichet783dd6e2010-11-21 06:08:52 +00001333void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1334 VisitValueDecl(FD);
1335
David L. Jonesbe1557a2016-12-21 00:17:49 +00001336 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001337 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001338 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001339
1340 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001341 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001342
1343 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001344}
1345
Larisse Voufo39a1e502013-08-06 01:03:05 +00001346ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001347 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001348 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001349
David L. Jonesbe1557a2016-12-21 00:17:49 +00001350 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1351 VD->VarDeclBits.TSCSpec = Record.readInt();
1352 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001353 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001354 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1355 Record.readInt();
1356 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001357 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001358 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001359 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001360 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1361 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1362 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1363 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1364 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1365 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001366 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001367 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001368 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001369 VD->setCachedLinkage(VarLinkage);
1370
1371 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001372 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001373 VD->getLexicalDeclContext()->isFunctionOrMethod())
1374 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001375
David L. Jonesbe1557a2016-12-21 00:17:49 +00001376 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001377 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001378 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001379 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1380 Eval->CheckedICE = true;
1381 Eval->IsICE = Val == 3;
1382 }
1383 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001384
Richard Smitha4653622017-09-06 20:01:14 +00001385 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1386 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1387
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001388 enum VarKind {
1389 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1390 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001391 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001392 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001393 // Only true variables (not parameters or implicit parameters) can be
1394 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001395 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1396 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001397 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001398 break;
1399 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001400 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001401 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001402 break;
1403 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001404 auto *Tmpl = ReadDeclAs<VarDecl>();
1405 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001406 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001407 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001408 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001409 break;
1410 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001411 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001412
1413 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001414}
1415
Sebastian Redlb3298c32010-08-18 23:56:48 +00001416void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001417 VisitVarDecl(PD);
1418}
1419
Sebastian Redlb3298c32010-08-18 23:56:48 +00001420void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001421 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001422 unsigned isObjCMethodParam = Record.readInt();
1423 unsigned scopeDepth = Record.readInt();
1424 unsigned scopeIndex = Record.readInt();
1425 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001426 if (isObjCMethodParam) {
1427 assert(scopeDepth == 0);
1428 PD->setObjCMethodScopeInfo(scopeIndex);
1429 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1430 } else {
1431 PD->setScopeInfo(scopeDepth, scopeIndex);
1432 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001433 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1434 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1435 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001436 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001437
1438 // FIXME: If this is a redeclaration of a function from another module, handle
1439 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001440}
1441
Richard Smith7b76d812016-08-12 02:21:25 +00001442void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1443 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001444 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith7b76d812016-08-12 02:21:25 +00001445 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001446 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001447}
1448
1449void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1450 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001451 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001452}
1453
Sebastian Redlb3298c32010-08-18 23:56:48 +00001454void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001455 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001456 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001457 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001458}
1459
Sebastian Redlb3298c32010-08-18 23:56:48 +00001460void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001461 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001462 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001463 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001464 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001465 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001466 Params.reserve(NumParams);
1467 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001468 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001469 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001470
David L. Jonesbe1557a2016-12-21 00:17:49 +00001471 BD->setIsVariadic(Record.readInt());
1472 BD->setBlockMissingReturnType(Record.readInt());
1473 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001474
David L. Jonesbe1557a2016-12-21 00:17:49 +00001475 bool capturesCXXThis = Record.readInt();
1476 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001477 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001478 captures.reserve(numCaptures);
1479 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001480 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001481 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001482 bool byRef = (flags & 1);
1483 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001484 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001485
1486 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1487 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001488 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001489}
1490
Ben Langmuirce914fc2013-05-03 19:20:19 +00001491void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1492 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001493 unsigned ContextParamPos = Record.readInt();
1494 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001495 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001496 for (unsigned I = 0; I < CD->NumParams; ++I) {
1497 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001498 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001499 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001500 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001501 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001502}
1503
Sebastian Redlb3298c32010-08-18 23:56:48 +00001504void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001505 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001506 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001507 D->setExternLoc(ReadSourceLocation());
1508 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001509}
1510
Richard Smith8df390f2016-09-08 23:14:54 +00001511void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1512 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001513 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001514}
1515
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001516void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1517 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001518 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001519}
1520
Sebastian Redlb3298c32010-08-18 23:56:48 +00001521void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001522 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001523 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001524 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001525 D->LocStart = ReadSourceLocation();
1526 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001527
Richard Smith15e32fd2015-03-17 02:23:11 +00001528 // Defer loading the anonymous namespace until we've finished merging
1529 // this namespace; loading it might load a later declaration of the
1530 // same namespace, and we have an invariant that older declarations
1531 // get merged before newer ones try to merge.
1532 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001533 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001534 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001535 } else {
1536 // Link this namespace back to the first declaration, which has already
1537 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001538 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001539 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001540
1541 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001542
1543 if (AnonNamespace) {
1544 // Each module has its own anonymous namespace, which is disjoint from
1545 // any other module's anonymous namespaces, so don't attach the anonymous
1546 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001547 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001548 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001549 D->setAnonymousNamespace(Anon);
1550 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001551}
1552
Sebastian Redlb3298c32010-08-18 23:56:48 +00001553void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001554 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001555 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001556 D->NamespaceLoc = ReadSourceLocation();
1557 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001558 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001559 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001560 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001561}
1562
Sebastian Redlb3298c32010-08-18 23:56:48 +00001563void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001564 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001565 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001566 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001567 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1568 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001569 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001570 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001571 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001572 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001573}
1574
Richard Smith151c4562016-12-20 21:35:28 +00001575void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1576 VisitNamedDecl(D);
1577 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001578 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001579 for (unsigned I = 0; I != D->NumExpansions; ++I)
1580 Expansions[I] = ReadDeclAs<NamedDecl>();
1581 mergeMergeable(D);
1582}
1583
Sebastian Redlb3298c32010-08-18 23:56:48 +00001584void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001585 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001586 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001587 D->Underlying = ReadDeclAs<NamedDecl>();
1588 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001589 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001590 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001591 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001592 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001593 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001594}
1595
Richard Smith5179eb72016-06-28 19:03:57 +00001596void ASTDeclReader::VisitConstructorUsingShadowDecl(
1597 ConstructorUsingShadowDecl *D) {
1598 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001599 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1600 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001601 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001602}
1603
Sebastian Redlb3298c32010-08-18 23:56:48 +00001604void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001605 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001606 D->UsingLoc = ReadSourceLocation();
1607 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001608 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001609 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1610 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001611}
1612
Sebastian Redlb3298c32010-08-18 23:56:48 +00001613void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001614 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001615 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001616 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001617 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001618 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001619 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001620}
1621
Sebastian Redlb3298c32010-08-18 23:56:48 +00001622void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001623 UnresolvedUsingTypenameDecl *D) {
1624 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001625 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001626 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001627 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001628 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001629}
1630
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001631void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001632 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001633 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001634 Data.UserDeclaredConstructor = Record.readInt();
1635 Data.UserDeclaredSpecialMembers = Record.readInt();
1636 Data.Aggregate = Record.readInt();
1637 Data.PlainOldData = Record.readInt();
1638 Data.Empty = Record.readInt();
1639 Data.Polymorphic = Record.readInt();
1640 Data.Abstract = Record.readInt();
1641 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001642 Data.IsCXX11StandardLayout = Record.readInt();
1643 Data.HasBasesWithFields = Record.readInt();
1644 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001645 Data.HasPrivateFields = Record.readInt();
1646 Data.HasProtectedFields = Record.readInt();
1647 Data.HasPublicFields = Record.readInt();
1648 Data.HasMutableFields = Record.readInt();
1649 Data.HasVariantMembers = Record.readInt();
1650 Data.HasOnlyCMembers = Record.readInt();
1651 Data.HasInClassInitializer = Record.readInt();
1652 Data.HasUninitializedReferenceMember = Record.readInt();
1653 Data.HasUninitializedFields = Record.readInt();
1654 Data.HasInheritedConstructor = Record.readInt();
1655 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001656 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001657 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1658 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1659 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001660 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001661 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1662 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1663 Data.DefaultedDestructorIsDeleted = Record.readInt();
1664 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001665 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001666 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001667 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001668 Data.HasIrrelevantDestructor = Record.readInt();
1669 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1670 Data.HasDefaultedDefaultConstructor = Record.readInt();
1671 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1672 Data.HasConstexprDefaultConstructor = Record.readInt();
1673 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1674 Data.ComputedVisibleConversions = Record.readInt();
1675 Data.UserProvidedDefaultConstructor = Record.readInt();
1676 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001677 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1678 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001679 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1680 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1681 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001682 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001683 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001684
Richard Smithcd4a7a42017-09-07 00:55:55 +00001685 if (Record.readInt())
1686 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001687
David L. Jonesbe1557a2016-12-21 00:17:49 +00001688 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001689 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001690 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001691 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001692 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001693 Data.VBases = ReadGlobalOffset();
1694
David L. Jonesb6a8f022016-12-21 04:34:52 +00001695 Record.readUnresolvedSet(Data.Conversions);
1696 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001697 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001698 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001699
Douglas Gregor99ae8062012-02-14 17:54:36 +00001700 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001701 using Capture = LambdaCapture;
1702
1703 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001704 Lambda.Dependent = Record.readInt();
1705 Lambda.IsGenericLambda = Record.readInt();
1706 Lambda.CaptureDefault = Record.readInt();
1707 Lambda.NumCaptures = Record.readInt();
1708 Lambda.NumExplicitCaptures = Record.readInt();
1709 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001710 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001711 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1712 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001713 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001714 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001715 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001716 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001717 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001718 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001719 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001720 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001721 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001722 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001723 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001724 break;
1725 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001726 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001727 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001728 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001729 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1730 break;
1731 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001732 }
1733 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001734}
1735
Richard Smithcd45dbc2014-04-19 03:48:30 +00001736void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001737 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001738 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001739 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001740 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001741
Richard Smith02793752015-03-27 21:16:39 +00001742 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001743 // Track that we merged the definitions.
1744 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1745 DD.Definition));
1746 Reader.PendingDefinitions.erase(MergeDD.Definition);
1747 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001748 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001749 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1750 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001751 }
1752
Richard Smith2a9e5c52015-02-03 03:32:14 +00001753 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1754 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1755 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001756 // We faked up this definition data because we found a class for which we'd
1757 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001758 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001759 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001760
1761 // Don't change which declaration is the definition; that is required
1762 // to be invariant once we select it.
1763 auto *Def = DD.Definition;
1764 DD = std::move(MergeDD);
1765 DD.Definition = Def;
1766 return;
1767 }
1768
Richard Smithcd45dbc2014-04-19 03:48:30 +00001769 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001770 bool DetectedOdrViolation = false;
1771#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1772#define MATCH_FIELD(Field) \
1773 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1774 OR_FIELD(Field)
1775 MATCH_FIELD(UserDeclaredConstructor)
1776 MATCH_FIELD(UserDeclaredSpecialMembers)
1777 MATCH_FIELD(Aggregate)
1778 MATCH_FIELD(PlainOldData)
1779 MATCH_FIELD(Empty)
1780 MATCH_FIELD(Polymorphic)
1781 MATCH_FIELD(Abstract)
1782 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001783 MATCH_FIELD(IsCXX11StandardLayout)
1784 MATCH_FIELD(HasBasesWithFields)
1785 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001786 MATCH_FIELD(HasPrivateFields)
1787 MATCH_FIELD(HasProtectedFields)
1788 MATCH_FIELD(HasPublicFields)
1789 MATCH_FIELD(HasMutableFields)
1790 MATCH_FIELD(HasVariantMembers)
1791 MATCH_FIELD(HasOnlyCMembers)
1792 MATCH_FIELD(HasInClassInitializer)
1793 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001794 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001795 MATCH_FIELD(HasInheritedConstructor)
1796 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001797 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001798 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1799 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1800 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001801 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001802 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1803 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1804 MATCH_FIELD(DefaultedDestructorIsDeleted)
1805 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001806 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001807 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001808 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001809 MATCH_FIELD(HasIrrelevantDestructor)
1810 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001811 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001812 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1813 OR_FIELD(HasConstexprDefaultConstructor)
1814 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1815 // ComputedVisibleConversions is handled below.
1816 MATCH_FIELD(UserProvidedDefaultConstructor)
1817 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001818 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1819 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001820 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1821 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1822 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1823 MATCH_FIELD(IsLambda)
1824#undef OR_FIELD
1825#undef MATCH_FIELD
1826
1827 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1828 DetectedOdrViolation = true;
1829 // FIXME: Issue a diagnostic if the base classes don't match when we come
1830 // to lazily load them.
1831
1832 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1833 // match when we come to lazily load them.
1834 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1835 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1836 DD.ComputedVisibleConversions = true;
1837 }
1838
1839 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1840 // lazily load it.
1841
1842 if (DD.IsLambda) {
1843 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1844 // when they occur within the body of a function template specialization).
1845 }
1846
Richard Trieufd1acbb2017-04-11 21:31:00 +00001847 if (D->getODRHash() != MergeDD.ODRHash) {
1848 DetectedOdrViolation = true;
1849 }
1850
Richard Smithcd45dbc2014-04-19 03:48:30 +00001851 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001852 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1853 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001854}
1855
Richard Smith2a9e5c52015-02-03 03:32:14 +00001856void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001857 struct CXXRecordDecl::DefinitionData *DD;
1858 ASTContext &C = Reader.getContext();
1859
1860 // Determine whether this is a lambda closure type, so that we can
1861 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001862 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001863 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001864 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001865 LCD_None);
1866 else
1867 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1868
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001869 CXXRecordDecl *Canon = D->getCanonicalDecl();
1870 // Set decl definition data before reading it, so that during deserialization
1871 // when we read CXXRecordDecl, it already has definition data and we don't
1872 // set fake one.
1873 if (!Canon->DefinitionData)
1874 Canon->DefinitionData = DD;
1875 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001876 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001877
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001878 // We might already have a different definition for this record. This can
1879 // happen either because we're reading an update record, or because we've
1880 // already done some merging. Either way, just merge into it.
1881 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001882 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001883 return;
1884 }
1885
Richard Smith97100e32015-06-20 00:22:34 +00001886 // Mark this declaration as being a definition.
1887 D->IsCompleteDefinition = true;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001888
Richard Smith97100e32015-06-20 00:22:34 +00001889 // If this is not the first declaration or is an update record, we can have
1890 // other redeclarations already. Make a note that we need to propagate the
1891 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001892 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001893 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001894}
1895
Richard Smith1d209d02013-05-23 01:49:11 +00001896ASTDeclReader::RedeclarableResult
1897ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1898 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001899
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001900 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001901
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001902 enum CXXRecKind {
1903 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1904 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001905 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001906 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001907 // Merged when we merge the folding set entry in the primary template.
1908 if (!isa<ClassTemplateSpecializationDecl>(D))
1909 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001910 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001911 case CXXRecTemplate: {
1912 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001913 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001914 D->TemplateOrInstantiation = Template;
1915 if (!Template->getTemplatedDecl()) {
1916 // We've not actually loaded the ClassTemplateDecl yet, because we're
1917 // currently being loaded as its pattern. Rely on it to set up our
1918 // TypeForDecl (see VisitClassTemplateDecl).
1919 //
1920 // Beware: we do not yet know our canonical declaration, and may still
1921 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001922 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001923 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001924 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001925 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001926 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001927 auto *RD = ReadDeclAs<CXXRecordDecl>();
1928 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001929 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001930 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1931 MSI->setPointOfInstantiation(POI);
1932 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001933 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001934 break;
1935 }
1936 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001937
David L. Jonesbe1557a2016-12-21 00:17:49 +00001938 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001939 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001940 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001941 else
1942 // Propagate DefinitionData pointer from the canonical declaration.
1943 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1944
Richard Smith676c4042013-08-29 23:59:27 +00001945 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001946 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001947 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001948 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001949 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001950 // FIXME: This is wrong for the ARM ABI, where some other module may have
1951 // made this function no longer be a key function. We need an update
1952 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001953 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001954 }
Richard Smith1d209d02013-05-23 01:49:11 +00001955
1956 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001957}
1958
Richard Smithbc491202017-02-17 20:05:37 +00001959void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1960 VisitFunctionDecl(D);
Faisal Vali1f3a2af2017-11-11 18:02:29 +00001961 D->IsCopyDeductionCandidate = Record.readInt();
Richard Smithbc491202017-02-17 20:05:37 +00001962}
1963
Sebastian Redlb3298c32010-08-18 23:56:48 +00001964void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001965 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001966
David L. Jonesbe1557a2016-12-21 00:17:49 +00001967 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001968 if (D->isCanonicalDecl()) {
1969 while (NumOverridenMethods--) {
1970 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1971 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001972 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001973 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1974 }
1975 } else {
1976 // We don't care about which declarations this used to override; we get
1977 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001978 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001979 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001980}
1981
Sebastian Redlb3298c32010-08-18 23:56:48 +00001982void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001983 // We need the inherited constructor information to merge the declaration,
1984 // so we have to read it before we call VisitCXXMethodDecl.
1985 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001986 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1987 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001988 *D->getTrailingObjects<InheritedConstructor>() =
1989 InheritedConstructor(Shadow, Ctor);
1990 }
1991
Chris Lattnerca025db2010-05-07 21:43:38 +00001992 VisitCXXMethodDecl(D);
1993}
1994
Sebastian Redlb3298c32010-08-18 23:56:48 +00001995void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001996 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001997
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001998 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00001999 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00002000 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00002001 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00002002 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00002003 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00002004 Canon->OperatorDeleteThisArg = ThisArg;
2005 }
Richard Smithf8134002015-03-10 01:41:22 +00002006 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002007}
2008
Sebastian Redlb3298c32010-08-18 23:56:48 +00002009void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002010 VisitCXXMethodDecl(D);
2011}
2012
Douglas Gregorba345522011-12-02 23:23:56 +00002013void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2014 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002015 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002016 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002017 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002018 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002019 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002020 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002021}
2022
Sebastian Redlb3298c32010-08-18 23:56:48 +00002023void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002024 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002025 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002026}
2027
Sebastian Redlb3298c32010-08-18 23:56:48 +00002028void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002029 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002030 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002031 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002032 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002033 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002034 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002035 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002036 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002037 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002038 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002039 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002040}
2041
Sebastian Redlb3298c32010-08-18 23:56:48 +00002042void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002043 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002044 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002045 D->NumParams = NumParams;
2046 D->Params = new TemplateParameterList*[NumParams];
2047 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002048 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002049 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002050 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002051 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002052 D->Friend = GetTypeSourceInfo();
2053 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002054}
2055
Richard Smithf17fdbd2014-04-24 02:25:27 +00002056DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002057 VisitNamedDecl(D);
2058
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002059 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002060 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002061 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002062 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002063 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002064
Richard Smithf17fdbd2014-04-24 02:25:27 +00002065 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002066}
2067
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002068ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002069ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002070 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002071
Douglas Gregor68444de2012-01-14 15:13:49 +00002072 // Make sure we've allocated the Common pointer first. We do this before
2073 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2074 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2075 if (!CanonD->Common) {
2076 CanonD->Common = CanonD->newCommon(Reader.getContext());
2077 Reader.PendingDefinitions.insert(CanonD);
2078 }
2079 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002080
Douglas Gregor68444de2012-01-14 15:13:49 +00002081 // If this is the first declaration of the template, fill in the information
2082 // for the 'common' pointer.
2083 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002084 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002085 assert(RTD->getKind() == D->getKind() &&
2086 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002087 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002088 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002089 D->setMemberSpecialization();
2090 }
2091 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002092
Richard Smithf17fdbd2014-04-24 02:25:27 +00002093 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002094 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002095
Richard Smithf17fdbd2014-04-24 02:25:27 +00002096 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002097
Richard Smithd46d6de2013-10-13 23:50:45 +00002098 // If we merged the template with a prior declaration chain, merge the common
2099 // pointer.
2100 // FIXME: Actually merge here, don't just overwrite.
2101 D->Common = D->getCanonicalDecl()->Common;
2102
Douglas Gregor68444de2012-01-14 15:13:49 +00002103 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002104}
2105
Sebastian Redlb3298c32010-08-18 23:56:48 +00002106void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002107 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002108
Douglas Gregor68444de2012-01-14 15:13:49 +00002109 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002110 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2111 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002112 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002113 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002114 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002115 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002116
2117 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2118 // We were loaded before our templated declaration was. We've not set up
2119 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2120 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002121 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002122 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002123 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002124}
2125
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002126void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2127 llvm_unreachable("BuiltinTemplates are not serialized");
2128}
2129
Larisse Voufo30616382013-08-23 22:21:36 +00002130/// TODO: Unify with ClassTemplateDecl version?
2131/// May require unifying ClassTemplateDecl and
2132/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002133void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2134 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2135
2136 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002137 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002138 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002139 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002140 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002141 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002142 }
2143}
2144
Richard Smith1d209d02013-05-23 01:49:11 +00002145ASTDeclReader::RedeclarableResult
2146ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2147 ClassTemplateSpecializationDecl *D) {
2148 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002149
Douglas Gregor4163aca2011-09-09 21:34:22 +00002150 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002151 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002152 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002153 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002154 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002155 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002156 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002157 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002158 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002159 auto *PS =
2160 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002161 SpecializedPartialSpecialization();
2162 PS->PartialSpecialization
2163 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2164 PS->TemplateArgs = ArgList;
2165 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002166 }
2167 }
2168
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002169 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002170 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002171 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002172 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002173 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002174
David L. Jonesbe1557a2016-12-21 00:17:49 +00002175 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002176 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002177 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002178 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002179 // Set this as, or find, the canonical declaration for this specialization
2180 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002181 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002182 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002183 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002184 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002185 CanonSpec =
2186 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2187 }
2188 // If there was already a canonical specialization, merge into it.
2189 if (CanonSpec != D) {
2190 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2191
2192 // This declaration might be a definition. Merge with any existing
2193 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002194 if (auto *DDD = D->DefinitionData) {
2195 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002196 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002197 else
Richard Smith053f6c62014-05-16 23:01:30 +00002198 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002199 }
Richard Smith547864d2014-07-11 18:22:58 +00002200 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002201 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002202 }
2203 }
Richard Smith1d209d02013-05-23 01:49:11 +00002204
Richard Smithd55889a2013-09-09 16:55:27 +00002205 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002206 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002207 auto *ExplicitInfo =
2208 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002209 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002210 ExplicitInfo->ExternLoc = ReadSourceLocation();
2211 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002212 D->ExplicitInfo = ExplicitInfo;
2213 }
2214
Richard Smith1d209d02013-05-23 01:49:11 +00002215 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002216}
2217
Sebastian Redlb3298c32010-08-18 23:56:48 +00002218void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002219 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002220 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002221
David L. Jonesb6a8f022016-12-21 04:34:52 +00002222 D->TemplateParams = Record.readTemplateParameterList();
2223 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002224
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002225 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002226 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002227 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002228 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002229 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002230 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002231}
2232
Sebastian Redlb7448632011-08-31 13:59:56 +00002233void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2234 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002235 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002236 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002237}
2238
Sebastian Redlb3298c32010-08-18 23:56:48 +00002239void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002240 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002241
Douglas Gregor68444de2012-01-14 15:13:49 +00002242 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002243 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002244 SmallVector<serialization::DeclID, 32> SpecIDs;
2245 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002246 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002247 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002248}
2249
Larisse Voufo30616382013-08-23 22:21:36 +00002250/// TODO: Unify with ClassTemplateSpecializationDecl version?
2251/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2252/// VarTemplate(Partial)SpecializationDecl with a new data
2253/// structure Template(Partial)SpecializationDecl, and
2254/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002255ASTDeclReader::RedeclarableResult
2256ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2257 VarTemplateSpecializationDecl *D) {
2258 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2259
2260 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002261 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002262 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002263 D->SpecializedTemplate = VTD;
2264 } else {
2265 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002266 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002267 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002268 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002269 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002270 new (C)
2271 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2272 PS->PartialSpecialization =
2273 cast<VarTemplatePartialSpecializationDecl>(InstD);
2274 PS->TemplateArgs = ArgList;
2275 D->SpecializedTemplate = PS;
2276 }
2277 }
2278
2279 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002280 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002281 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002282 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2283 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002284 ExplicitInfo->ExternLoc = ReadSourceLocation();
2285 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002286 D->ExplicitInfo = ExplicitInfo;
2287 }
2288
2289 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002290 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002291 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002292 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002293 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002294 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002295
David L. Jonesbe1557a2016-12-21 00:17:49 +00002296 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002297 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002298 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002299 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002300 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002301 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002302 CanonPattern->getCommonPtr()->PartialSpecializations
2303 .GetOrInsertNode(Partial);
2304 } else {
2305 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2306 }
2307 }
2308 }
2309
2310 return Redecl;
2311}
2312
Larisse Voufo30616382013-08-23 22:21:36 +00002313/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2314/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2315/// VarTemplate(Partial)SpecializationDecl with a new data
2316/// structure Template(Partial)SpecializationDecl, and
2317/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002318void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2319 VarTemplatePartialSpecializationDecl *D) {
2320 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2321
David L. Jonesb6a8f022016-12-21 04:34:52 +00002322 D->TemplateParams = Record.readTemplateParameterList();
2323 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002324
2325 // These are read/set from/to the first declaration.
2326 if (ThisDeclID == Redecl.getFirstID()) {
2327 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002328 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002329 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002330 }
2331}
2332
Sebastian Redlb3298c32010-08-18 23:56:48 +00002333void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002334 VisitTypeDecl(D);
2335
David L. Jonesbe1557a2016-12-21 00:17:49 +00002336 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002337
David L. Jonesbe1557a2016-12-21 00:17:49 +00002338 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002339 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002340}
2341
Sebastian Redlb3298c32010-08-18 23:56:48 +00002342void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002343 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002344 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002345 D->setDepth(Record.readInt());
2346 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002347 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002348 auto TypesAndInfos =
2349 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002350 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002351 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002352 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002353 }
2354 } else {
2355 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002356 D->ParameterPack = Record.readInt();
2357 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002358 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002359 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002360}
2361
Sebastian Redlb3298c32010-08-18 23:56:48 +00002362void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002363 VisitTemplateDecl(D);
2364 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002365 D->setDepth(Record.readInt());
2366 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002367 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002368 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002369 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2370 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002371 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002372 } else {
2373 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002374 D->ParameterPack = Record.readInt();
2375 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002376 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002377 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002378 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002379}
2380
Richard Smith3f1b5d02011-05-05 21:57:07 +00002381void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2382 VisitRedeclarableTemplateDecl(D);
2383}
2384
Sebastian Redlb3298c32010-08-18 23:56:48 +00002385void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002386 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002387 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002388 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002389 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002390 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002391}
2392
Michael Han84324352013-02-22 17:15:32 +00002393void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2394 VisitDecl(D);
2395}
2396
Mike Stump11289f42009-09-09 15:08:12 +00002397std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002398ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002399 uint64_t LexicalOffset = ReadLocalOffset();
2400 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002401 return std::make_pair(LexicalOffset, VisibleOffset);
2402}
2403
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002404template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002405ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002406ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002407 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002408 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002409
Richard Smith5fc18a92015-07-12 23:43:21 +00002410 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002411 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002412
Richard Smithd61d4ac2015-08-22 20:13:39 +00002413 uint64_t RedeclOffset = 0;
2414
Douglas Gregor358cd442012-01-15 16:58:34 +00002415 // 0 indicates that this declaration was the only declaration of its entity,
2416 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002417 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002418 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002419 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002420 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002421 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002422 // This declaration was the first local declaration, but may have imported
2423 // other declarations.
2424 IsKeyDecl = N == 1;
2425 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002426
Richard Smithfe620d22015-03-05 23:24:12 +00002427 // We have some declarations that must be before us in our redeclaration
2428 // chain. Read them now, and remember that we ought to merge with one of
2429 // them.
2430 // FIXME: Provide a known merge target to the second and subsequent such
2431 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002432 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002433 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002434
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002435 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002436 } else {
2437 // This declaration was not the first local declaration. Read the first
2438 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002439 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002440 }
2441
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002442 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002443 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002444 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2445 // We temporarily set the first (canonical) declaration as the previous one
2446 // which is the one that matters and mark the real previous DeclID to be
2447 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002448 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002449 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002450 }
Richard Smithd8a83712015-08-22 01:47:18 +00002451
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002452 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002453
2454 // Note that we need to load local redeclarations of this decl and build a
2455 // decl chain for them. This must happen *after* we perform the preloading
2456 // above; this ensures that the redeclaration chain is built in the correct
2457 // order.
2458 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002459 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002460
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002461 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002462}
2463
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002464/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002465/// of the same entity.
2466template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002467void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002468 RedeclarableResult &Redecl,
2469 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002470 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002471 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002472 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002473
Richard Smith202850a2015-03-10 02:57:50 +00002474 // If we're not the canonical declaration, we don't need to merge.
2475 if (!DBase->isFirstDecl())
2476 return;
2477
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002478 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002479
Richard Smith5a4737c2015-02-06 02:42:59 +00002480 if (auto *Existing = Redecl.getKnownMergeTarget())
2481 // We already know of an existing declaration we should merge with.
2482 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2483 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002484 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002485 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2486}
2487
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002488/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002489/// We use this to put code in a template that will only be valid for certain
2490/// instantiations.
2491template<typename T> static T assert_cast(T t) { return t; }
2492template<typename T> static T assert_cast(...) {
2493 llvm_unreachable("bad assert_cast");
2494}
2495
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002496/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002497/// declarations.
2498void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2499 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002500 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002501 auto *DPattern = D->getTemplatedDecl();
2502 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002503 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2504 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002505 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002506
Richard Smith547864d2014-07-11 18:22:58 +00002507 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2508 // Merge with any existing definition.
2509 // FIXME: This is duplicated in several places. Refactor.
2510 auto *ExistingClass =
2511 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002512 if (auto *DDD = DClass->DefinitionData) {
2513 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002514 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002515 } else {
2516 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002517 // We may have skipped this before because we thought that DClass
2518 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002519 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002520 }
2521 }
2522 DClass->DefinitionData = ExistingClass->DefinitionData;
2523
Richard Smithf17fdbd2014-04-24 02:25:27 +00002524 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2525 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002526 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002527 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2528 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2529 Result);
2530 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2531 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002532 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2533 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2534 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002535 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002536}
2537
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002538/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002539/// of the same entity.
2540template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002541void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2542 RedeclarableResult &Redecl,
2543 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002544 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002545 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002546 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002547 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002548 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2549 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002550
Richard Smithd55889a2013-09-09 16:55:27 +00002551 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002552 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002553 // appropriate canonical declaration.
2554 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002555 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002556 ExistingCanon->Used |= D->Used;
2557 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002558
2559 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002560 // We cannot have loaded any redeclarations of this declaration yet, so
2561 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002562 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002563 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002564 assert_cast<NamespaceDecl*>(ExistingCanon));
2565
2566 // When we merge a template, merge its pattern.
2567 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2568 mergeTemplatePattern(
2569 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002570 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002571
Richard Smith5fc18a92015-07-12 23:43:21 +00002572 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002573 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002574 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002575 }
2576}
2577
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002578/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2579/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2580/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2581/// that some types are mergeable during deserialization, otherwise name
2582/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002583static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002584 if (!ND)
2585 return false;
2586 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002587 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002588 return true;
2589 return false;
2590}
2591
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002592/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002593/// of the same entity, for the case where the entity is not actually
2594/// redeclarable. This happens, for instance, when merging the fields of
2595/// identical class definitions from two different modules.
2596template<typename T>
2597void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2598 // If modules are not available, there is no reason to perform this merge.
2599 if (!Reader.getContext().getLangOpts().Modules)
2600 return;
2601
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002602 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2603 // Note that C identically-named things in different translation units are
2604 // not redeclarations, but may still have compatible types, where ODR-like
2605 // semantics may apply.
2606 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2607 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002608 return;
2609
Richard Smith0b87e072013-10-07 08:02:11 +00002610 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2611 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002612 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2613 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002614}
2615
Alexey Bataeva769e072013-03-22 06:34:35 +00002616void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2617 VisitDecl(D);
2618 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002619 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002620 Vars.reserve(NumVars);
2621 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002622 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002623 }
2624 D->setVars(Vars);
2625}
2626
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002627void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002628 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002629 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002630 D->setCombiner(Record.readExpr());
Alexey Bataev070f43a2017-09-06 14:49:58 +00002631 D->setInitializer(
2632 Record.readExpr(),
2633 static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002634 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002635}
2636
Alexey Bataev4244be22016-02-11 05:35:55 +00002637void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002638 VisitVarDecl(D);
2639}
2640
Chris Lattner487412d2009-04-27 05:27:42 +00002641//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002642// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002643//===----------------------------------------------------------------------===//
2644
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002645/// Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002646void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2647 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002648 Attr *New = nullptr;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002649 auto Kind = (attr::Kind)Record.readInt();
David L. Jones267b8842017-01-24 01:04:30 +00002650 SourceRange Range = Record.readSourceRange();
Richard Smithdbafb6c2017-06-29 23:23:46 +00002651 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002652
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002653#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002654
2655 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002656 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002657 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002658}
2659
2660//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002661// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002662//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002663
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002664/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002665/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002666///
Chris Lattner487412d2009-04-27 05:27:42 +00002667/// This routine notes that this declaration has already been loaded,
2668/// so that future GetDecl calls will return this declaration rather
2669/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002670inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002671 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2672 DeclsLoaded[Index] = D;
2673}
2674
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002675/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002676/// this declaration (via HandleTopLevelDecl).
2677///
2678/// This routine should return true for anything that might affect
2679/// code generation, e.g., inline function definitions, Objective-C
2680/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002681static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002682 // An ObjCMethodDecl is never considered as "interesting" because its
2683 // implementation container always is.
2684
Richard Smithcd4a7a42017-09-07 00:55:55 +00002685 // An ImportDecl or VarDecl imported from a module map module will get
2686 // emitted when we import the relevant module.
2687 if (isa<ImportDecl>(D) || isa<VarDecl>(D)) {
2688 auto *M = D->getImportedOwningModule();
2689 if (M && M->Kind == Module::ModuleMapModule &&
2690 Ctx.DeclMustBeEmitted(D))
2691 return false;
2692 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002693
Fangrui Song6907ce22018-07-30 19:24:48 +00002694 if (isa<FileScopeAsmDecl>(D) ||
2695 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002696 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002697 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002698 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002699 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002700 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002701 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2702 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002703 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002704 return Var->isFileVarDecl() &&
2705 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002706 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002707 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002708
2709 if (auto *ES = D->getASTContext().getExternalSource())
2710 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2711 return true;
2712
Douglas Gregor87d81242011-09-10 00:22:34 +00002713 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002714}
2715
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002716/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002717ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002718ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002719 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2720 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002721 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002722 const DeclOffset &DOffs =
2723 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002724 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002725 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002726}
2727
Douglas Gregord32f0352011-07-22 06:10:01 +00002728ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002729 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002730
2731 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2732 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2733}
2734
Douglas Gregorde3ef502011-11-30 23:21:26 +00002735uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002736 return LocalOffset + M.GlobalBitOffset;
2737}
2738
Richard Smithbf78e642013-06-24 22:51:00 +00002739static bool isSameTemplateParameterList(const TemplateParameterList *X,
2740 const TemplateParameterList *Y);
2741
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002742/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002743/// that they may be used in declarations of the same template.
2744static bool isSameTemplateParameter(const NamedDecl *X,
2745 const NamedDecl *Y) {
2746 if (X->getKind() != Y->getKind())
2747 return false;
2748
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002749 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2750 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002751 return TX->isParameterPack() == TY->isParameterPack();
2752 }
2753
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002754 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2755 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002756 return TX->isParameterPack() == TY->isParameterPack() &&
2757 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2758 }
2759
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002760 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2761 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002762 return TX->isParameterPack() == TY->isParameterPack() &&
2763 isSameTemplateParameterList(TX->getTemplateParameters(),
2764 TY->getTemplateParameters());
2765}
2766
Richard Smith32952e12014-10-14 02:00:47 +00002767static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2768 if (auto *NS = X->getAsNamespace())
2769 return NS;
2770 if (auto *NAS = X->getAsNamespaceAlias())
2771 return NAS->getNamespace();
2772 return nullptr;
2773}
2774
2775static bool isSameQualifier(const NestedNameSpecifier *X,
2776 const NestedNameSpecifier *Y) {
2777 if (auto *NSX = getNamespace(X)) {
2778 auto *NSY = getNamespace(Y);
2779 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2780 return false;
2781 } else if (X->getKind() != Y->getKind())
2782 return false;
2783
2784 // FIXME: For namespaces and types, we're permitted to check that the entity
2785 // is named via the same tokens. We should probably do so.
2786 switch (X->getKind()) {
2787 case NestedNameSpecifier::Identifier:
2788 if (X->getAsIdentifier() != Y->getAsIdentifier())
2789 return false;
2790 break;
2791 case NestedNameSpecifier::Namespace:
2792 case NestedNameSpecifier::NamespaceAlias:
2793 // We've already checked that we named the same namespace.
2794 break;
2795 case NestedNameSpecifier::TypeSpec:
2796 case NestedNameSpecifier::TypeSpecWithTemplate:
2797 if (X->getAsType()->getCanonicalTypeInternal() !=
2798 Y->getAsType()->getCanonicalTypeInternal())
2799 return false;
2800 break;
2801 case NestedNameSpecifier::Global:
2802 case NestedNameSpecifier::Super:
2803 return true;
2804 }
2805
2806 // Recurse into earlier portion of NNS, if any.
2807 auto *PX = X->getPrefix();
2808 auto *PY = Y->getPrefix();
2809 if (PX && PY)
2810 return isSameQualifier(PX, PY);
2811 return !PX && !PY;
2812}
2813
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002814/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002815/// that they may be used in declarations of the same template.
2816static bool isSameTemplateParameterList(const TemplateParameterList *X,
2817 const TemplateParameterList *Y) {
2818 if (X->size() != Y->size())
2819 return false;
2820
2821 for (unsigned I = 0, N = X->size(); I != N; ++I)
2822 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2823 return false;
2824
2825 return true;
2826}
2827
George Burgess IV95845082017-02-15 22:43:27 +00002828/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002829/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002830static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2831 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002832 // Note that pass_object_size attributes are represented in the function's
2833 // ExtParameterInfo, so we don't need to check them here.
2834
Michael Kruse41dd6ce2018-06-25 20:06:13 +00002835 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2836 // Since this is an equality check, we can ignore that enable_if attrs show up
2837 // in reverse order.
2838 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2839 AEnableIfs.push_back(EIA);
2840
2841 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2842 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2843 BEnableIfs.push_back(EIA);
2844
2845 // Two very common cases: either we have 0 enable_if attrs, or we have an
2846 // unequal number of enable_if attrs.
2847 if (AEnableIfs.empty() && BEnableIfs.empty())
2848 return true;
2849
2850 if (AEnableIfs.size() != BEnableIfs.size())
2851 return false;
2852
George Burgess IV95845082017-02-15 22:43:27 +00002853 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Kruse41dd6ce2018-06-25 20:06:13 +00002854 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
George Burgess IV95845082017-02-15 22:43:27 +00002855 Cand1ID.clear();
2856 Cand2ID.clear();
2857
Michael Kruse41dd6ce2018-06-25 20:06:13 +00002858 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2859 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
George Burgess IV95845082017-02-15 22:43:27 +00002860 if (Cand1ID != Cand2ID)
2861 return false;
2862 }
2863
Michael Kruse41dd6ce2018-06-25 20:06:13 +00002864 return true;
George Burgess IV95845082017-02-15 22:43:27 +00002865}
2866
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002867/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002868static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2869 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002870
Douglas Gregor022857e2011-12-22 01:48:48 +00002871 if (X == Y)
2872 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002873
Douglas Gregor022857e2011-12-22 01:48:48 +00002874 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00002875 //
2876 // Note that we can't use DeclContext::Equals here, because the DeclContexts
2877 // could be two different declarations of the same function. (We will fix the
2878 // semantic DC to refer to the primary definition after merging.)
2879 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
2880 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00002881 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002882
2883 // Two typedefs refer to the same entity if they have the same underlying
2884 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002885 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
2886 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002887 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2888 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002889
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002890 // Must have the same kind.
2891 if (X->getKind() != Y->getKind())
2892 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002893
Douglas Gregorda389302012-01-01 21:47:52 +00002894 // Objective-C classes and protocols with the same name always match.
2895 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002896 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002897
2898 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002899 // No need to handle these here: we merge them when adding them to the
2900 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002901 return false;
2902 }
Richard Smithd55889a2013-09-09 16:55:27 +00002903
Douglas Gregor2009cee2012-01-03 22:46:00 +00002904 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002905 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
2906 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00002907 return (TagX->getTagKind() == TagY->getTagKind()) ||
2908 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2909 TagX->getTagKind() == TTK_Interface) &&
2910 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2911 TagY->getTagKind() == TTK_Interface));
2912 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002913
Joao Matose9a3ed42012-08-31 22:18:20 +00002914 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002915 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2916 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002917 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
2918 const auto *FuncY = cast<FunctionDecl>(Y);
2919 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2920 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002921 if (CtorX->getInheritedConstructor() &&
2922 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2923 CtorY->getInheritedConstructor().getConstructor()))
2924 return false;
2925 }
Erich Keane281d20b2018-01-08 21:34:17 +00002926
2927 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
2928 return false;
2929
2930 // Multiversioned functions with different feature strings are represented
2931 // as separate declarations.
2932 if (FuncX->isMultiVersion()) {
2933 const auto *TAX = FuncX->getAttr<TargetAttr>();
2934 const auto *TAY = FuncY->getAttr<TargetAttr>();
2935 assert(TAX && TAY && "Multiversion Function without target attribute");
2936
2937 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
2938 return false;
2939 }
2940
Richard Smitha54d3242017-03-08 23:00:26 +00002941 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00002942 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
2943 // Map to the first declaration that we've already merged into this one.
2944 // The TSI of redeclarations might not match (due to calling conventions
2945 // being inherited onto the type but not the TSI), but the TSI type of
2946 // the first declaration of the function should match across modules.
2947 FD = FD->getCanonicalDecl();
2948 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
2949 : FD->getType();
2950 };
2951 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
2952 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00002953 // We can get functions with different types on the redecl chain in C++17
2954 // if they have differing exception specifications and at least one of
2955 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00002956 auto *XFPT = XT->getAs<FunctionProtoType>();
2957 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002958 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00002959 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2960 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00002961 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00002962 return true;
2963 return false;
2964 }
George Burgess IV95845082017-02-15 22:43:27 +00002965 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002966 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002967 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002968
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002969 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002970 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
2971 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002972 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2973 ASTContext &C = VarX->getASTContext();
2974 if (C.hasSameType(VarX->getType(), VarY->getType()))
2975 return true;
2976
2977 // We can get decls with different types on the redecl chain. Eg.
2978 // template <typename T> struct S { static T Var[]; }; // #1
2979 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2980 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002981 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002982 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2983 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2984 if (!VarXTy || !VarYTy)
2985 return false;
2986 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2987 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2988 }
2989 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002990 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002991
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002992 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002993 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2994 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002995 return NamespaceX->isInline() == NamespaceY->isInline();
2996 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002997
Richard Smith8f8f05c2013-06-24 04:45:28 +00002998 // Identical template names and kinds match if their template parameter lists
2999 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003000 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
3001 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00003002 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00003003 TemplateY->getTemplatedDecl()) &&
3004 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3005 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003006 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003007
Richard Smith0b87e072013-10-07 08:02:11 +00003008 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003009 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3010 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003011 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003012 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3013 }
3014
Richard Smith8cbd8952015-08-04 02:05:09 +00003015 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003016 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3017 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003018 return IFDX->getAnonField()->getCanonicalDecl() ==
3019 IFDY->getAnonField()->getCanonicalDecl();
3020 }
3021
Richard Smith01a73372013-10-15 22:02:41 +00003022 // Enumerators with the same name match.
3023 if (isa<EnumConstantDecl>(X))
3024 // FIXME: Also check the value is odr-equivalent.
3025 return true;
3026
Richard Smithfd8634a2013-10-23 02:17:46 +00003027 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003028 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3029 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003030 return USX->getTargetDecl() == USY->getTargetDecl();
3031 }
3032
Richard Smith32952e12014-10-14 02:00:47 +00003033 // Using declarations with the same qualifier match. (We already know that
3034 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003035 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3036 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003037 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3038 UX->hasTypename() == UY->hasTypename() &&
3039 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3040 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003041 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3042 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003043 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3044 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3045 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003046 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003047 return isSameQualifier(
3048 UX->getQualifier(),
3049 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3050
Richard Smithf4634362014-09-03 23:11:22 +00003051 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003052 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3053 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003054 return NAX->getNamespace()->Equals(NAY->getNamespace());
3055 }
3056
Douglas Gregor022857e2011-12-22 01:48:48 +00003057 return false;
3058}
3059
Richard Smithd55889a2013-09-09 16:55:27 +00003060/// Find the context in which we should search for previous declarations when
3061/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003062DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3063 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003064 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003065 return ND->getOriginalNamespace();
3066
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003067 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003068 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003069 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003070 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003071 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003072
3073 // If there's no definition yet, then DC's definition is added by an update
3074 // record, but we've not yet loaded that update record. In this case, we
3075 // commit to DC being the canonical definition now, and will fix this when
3076 // we load the update record.
3077 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003078 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Richard Smith8a639892015-01-24 01:07:20 +00003079 RD->IsCompleteDefinition = true;
3080 RD->DefinitionData = DD;
3081 RD->getCanonicalDecl()->DefinitionData = DD;
3082
3083 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003084 Reader.PendingFakeDefinitionData.insert(
3085 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003086 }
3087
3088 return DD->Definition;
3089 }
Richard Smithd55889a2013-09-09 16:55:27 +00003090
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003091 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003092 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3093 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003094
Richard Smith4eca9b92015-02-04 23:37:59 +00003095 // We can see the TU here only if we have no Sema object. In that case,
3096 // there's no TU scope to look in, so using the DC alone is sufficient.
3097 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3098 return TU;
3099
Craig Toppera13603a2014-05-22 05:54:18 +00003100 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003101}
3102
Douglas Gregor022857e2011-12-22 01:48:48 +00003103ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003104 // Record that we had a typedef name for linkage whether or not we merge
3105 // with that declaration.
3106 if (TypedefNameForLinkage) {
3107 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3108 Reader.ImportedTypedefNamesForLinkage.insert(
3109 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3110 return;
3111 }
3112
Douglas Gregor9b47f942012-01-09 17:38:47 +00003113 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003114 return;
Richard Smith95d99302013-07-13 02:00:19 +00003115
Richard Smith70d58502014-08-30 00:04:23 +00003116 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003117 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003118 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003119 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3120 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003121 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003122 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003123 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003124 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3125 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003126 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003127 // Add the declaration to its redeclaration context so later merging
3128 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003129 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003130 }
3131}
3132
Richard Smith88ebade2014-08-23 01:45:27 +00003133/// Find the declaration that should be merged into, given the declaration found
3134/// by name lookup. If we're merging an anonymous declaration within a typedef,
3135/// we need a matching typedef, and we merge with the type inside it.
3136static NamedDecl *getDeclForMerging(NamedDecl *Found,
3137 bool IsTypedefNameForLinkage) {
3138 if (!IsTypedefNameForLinkage)
3139 return Found;
3140
3141 // If we found a typedef declaration that gives a name to some other
3142 // declaration, then we want that inner declaration. Declarations from
3143 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003144 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003145 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003146
3147 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003148 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003149
Hans Wennborgdcfba332015-10-06 23:40:43 +00003150 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003151}
3152
Richard Smith600adef2018-07-04 02:25:38 +00003153/// Find the declaration to use to populate the anonymous declaration table
3154/// for the given lexical DeclContext. We only care about finding local
3155/// definitions of the context; we'll merge imported ones as we go.
3156DeclContext *
3157ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3158 // For classes, we track the definition as we merge.
3159 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3160 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3161 return DD ? DD->Definition : nullptr;
3162 }
3163
3164 // For anything else, walk its merged redeclarations looking for a definition.
3165 // Note that we can't just call getDefinition here because the redeclaration
3166 // chain isn't wired up.
3167 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3168 if (auto *FD = dyn_cast<FunctionDecl>(D))
3169 if (FD->isThisDeclarationADefinition())
3170 return FD;
3171 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3172 if (MD->isThisDeclarationADefinition())
3173 return MD;
3174 }
3175
3176 // No merged definition yet.
3177 return nullptr;
3178}
3179
Richard Smithd08aeb62014-08-28 01:33:39 +00003180NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3181 DeclContext *DC,
3182 unsigned Index) {
3183 // If the lexical context has been merged, look into the now-canonical
3184 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003185 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003186
3187 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003188 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003189 if (Index < Previous.size() && Previous[Index])
3190 return Previous[Index];
3191
3192 // If this is the first time, but we have parsed a declaration of the context,
3193 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003194 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3195 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3196 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003197 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003198 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3199 else
Richard Smith2b560572015-02-07 03:11:11 +00003200 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3201 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003202 }
3203
3204 return Index < Previous.size() ? Previous[Index] : nullptr;
3205}
3206
3207void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3208 DeclContext *DC, unsigned Index,
3209 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003210 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003211
Richard Smith600adef2018-07-04 02:25:38 +00003212 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003213 if (Index >= Previous.size())
3214 Previous.resize(Index + 1);
3215 if (!Previous[Index])
3216 Previous[Index] = D;
3217}
3218
Douglas Gregor022857e2011-12-22 01:48:48 +00003219ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003220 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3221 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003222
Richard Smithd08aeb62014-08-28 01:33:39 +00003223 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3224 // Don't bother trying to find unnamed declarations that are in
3225 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003226 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3227 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003228 Result.suppress();
3229 return Result;
3230 }
Richard Smith95d99302013-07-13 02:00:19 +00003231
Douglas Gregor022857e2011-12-22 01:48:48 +00003232 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003233 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003234 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003235 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003236 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3237 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003238 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3239 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003240 // Go on to check in other places in case an existing typedef name
3241 // was not imported.
3242 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003243
Richard Smith2b560572015-02-07 03:11:11 +00003244 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003245 // This is an anonymous declaration that we may need to merge. Look it up
3246 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003247 if (auto *Existing = getAnonymousDeclForMerging(
3248 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3249 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003250 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3251 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003252 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003253 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003254 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003255
3256 // Temporarily consider the identifier to be up-to-date. We don't want to
3257 // cause additional lookups here.
3258 class UpToDateIdentifierRAII {
3259 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003260 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003261
3262 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003263 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003264 if (II) {
3265 WasOutToDate = II->isOutOfDate();
3266 if (WasOutToDate)
3267 II->setOutOfDate(false);
3268 }
3269 }
3270
3271 ~UpToDateIdentifierRAII() {
3272 if (WasOutToDate)
3273 II->setOutOfDate(true);
3274 }
3275 } UpToDate(Name.getAsIdentifierInfo());
3276
Fangrui Song6907ce22018-07-30 19:24:48 +00003277 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003278 IEnd = IdResolver.end();
3279 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003280 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003281 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003282 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3283 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003284 }
Richard Smith8a639892015-01-24 01:07:20 +00003285 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003286 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003287 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003288 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003289 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003290 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3291 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003292 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003293 } else {
3294 // Not in a mergeable context.
3295 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003296 }
Richard Smithd55889a2013-09-09 16:55:27 +00003297
Richard Smith2b9e3e32013-10-18 06:05:18 +00003298 // If this declaration is from a merged context, make a note that we need to
3299 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003300 //
3301 // FIXME: We should do something similar if we merge two definitions of the
3302 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003303 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3304 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3305 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003306 Reader.PendingOdrMergeChecks.push_back(D);
3307
Richard Smithd08aeb62014-08-28 01:33:39 +00003308 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003309 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003310}
3311
Richard Smithb321ecb2014-05-13 01:15:00 +00003312template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003313Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3314 return D->RedeclLink.getLatestNotUpdated();
3315}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003316
Richard Smithc3a53252015-02-28 05:57:02 +00003317Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3318 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3319}
3320
3321Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3322 assert(D);
3323
3324 switch (D->getKind()) {
3325#define ABSTRACT_DECL(TYPE)
3326#define DECL(TYPE, BASE) \
3327 case Decl::TYPE: \
3328 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3329#include "clang/AST/DeclNodes.inc"
3330 }
3331 llvm_unreachable("unknown decl kind");
3332}
3333
Richard Smith8ce51082015-03-11 01:44:51 +00003334Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3335 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3336}
3337
Richard Smithc3a53252015-02-28 05:57:02 +00003338template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003339void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3340 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003341 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003342 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003343 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003344}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003345
Richard Smith24d166c2014-07-31 23:52:38 +00003346namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003347
Richard Smith6de7a242014-07-31 23:46:44 +00003348template<>
3349void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003350 Redeclarable<VarDecl> *D,
3351 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003352 auto *VD = static_cast<VarDecl *>(D);
3353 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003354 D->RedeclLink.setPrevious(PrevVD);
3355 D->First = PrevVD->First;
3356
3357 // We should keep at most one definition on the chain.
3358 // FIXME: Cache the definition once we've found it. Building a chain with
3359 // N definitions currently takes O(N^2) time here.
3360 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3361 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3362 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3363 Reader.mergeDefinitionVisibility(CurD, VD);
3364 VD->demoteThisDefinitionToDeclaration();
3365 break;
3366 }
3367 }
3368 }
3369}
3370
3371template<>
3372void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003373 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003374 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003375 auto *FD = static_cast<FunctionDecl *>(D);
3376 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003377
3378 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003379 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003380
3381 // If the previous declaration is an inline function declaration, then this
3382 // declaration is too.
3383 if (PrevFD->IsInline != FD->IsInline) {
3384 // FIXME: [dcl.fct.spec]p4:
3385 // If a function with external linkage is declared inline in one
3386 // translation unit, it shall be declared inline in all translation
3387 // units in which it appears.
3388 //
3389 // Be careful of this case:
3390 //
3391 // module A:
3392 // template<typename T> struct X { void f(); };
3393 // template<typename T> inline void X<T>::f() {}
3394 //
3395 // module B instantiates the declaration of X<int>::f
3396 // module C instantiates the definition of X<int>::f
3397 //
3398 // If module B and C are merged, we do not have a violation of this rule.
3399 FD->IsInline = true;
3400 }
3401
Richard Smith9e2341d2015-03-23 03:25:59 +00003402 // If we need to propagate an exception specification along the redecl
3403 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003404 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3405 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003406 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003407 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3408 bool WasUnresolved =
3409 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3410 if (IsUnresolved != WasUnresolved)
3411 Reader.PendingExceptionSpecUpdates.insert(
3412 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003413 }
3414}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003415
3416} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003417
Richard Smith6de7a242014-07-31 23:46:44 +00003418void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003419 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3420}
3421
Richard Smith8346e522015-06-10 01:47:58 +00003422/// Inherit the default template argument from \p From to \p To. Returns
3423/// \c false if there is no default template for \p From.
3424template <typename ParmDecl>
3425static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3426 Decl *ToD) {
3427 auto *To = cast<ParmDecl>(ToD);
3428 if (!From->hasDefaultArgument())
3429 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003430 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003431 return true;
3432}
3433
3434static void inheritDefaultTemplateArguments(ASTContext &Context,
3435 TemplateDecl *From,
3436 TemplateDecl *To) {
3437 auto *FromTP = From->getTemplateParameters();
3438 auto *ToTP = To->getTemplateParameters();
3439 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3440
3441 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3442 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003443 if (FromParam->isParameterPack())
3444 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003445 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3446
3447 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003448 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003449 break;
3450 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003451 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003452 break;
3453 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003454 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003455 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3456 break;
3457 }
3458 }
3459}
3460
Richard Smith6de7a242014-07-31 23:46:44 +00003461void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003462 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003463 assert(D && Previous);
3464
3465 switch (D->getKind()) {
3466#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003467#define DECL(TYPE, BASE) \
3468 case Decl::TYPE: \
3469 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003470 break;
3471#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003472 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003473
3474 // If the declaration was visible in one module, a redeclaration of it in
3475 // another module remains visible even if it wouldn't be visible by itself.
3476 //
3477 // FIXME: In this case, the declaration should only be visible if a module
3478 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003479 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003480 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003481 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003482
Richard Smith8346e522015-06-10 01:47:58 +00003483 // If the declaration declares a template, it may inherit default arguments
3484 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003485 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003486 inheritDefaultTemplateArguments(Reader.getContext(),
3487 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003488}
3489
Richard Smithb321ecb2014-05-13 01:15:00 +00003490template<typename DeclT>
3491void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003492 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003493}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003494
Richard Smithb321ecb2014-05-13 01:15:00 +00003495void ASTDeclReader::attachLatestDeclImpl(...) {
3496 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3497}
3498
Douglas Gregor05f10352011-12-17 23:38:30 +00003499void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3500 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003501
3502 switch (D->getKind()) {
3503#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003504#define DECL(TYPE, BASE) \
3505 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003506 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3507 break;
3508#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003509 }
3510}
3511
Richard Smith851072e2014-05-19 20:59:20 +00003512template<typename DeclT>
3513void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3514 D->RedeclLink.markIncomplete();
3515}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003516
Richard Smith851072e2014-05-19 20:59:20 +00003517void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3518 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3519}
3520
3521void ASTReader::markIncompleteDeclChain(Decl *D) {
3522 switch (D->getKind()) {
3523#define ABSTRACT_DECL(TYPE)
3524#define DECL(TYPE, BASE) \
3525 case Decl::TYPE: \
3526 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3527 break;
3528#include "clang/AST/DeclNodes.inc"
3529 }
3530}
3531
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003532/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003533Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003534 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003535 SourceLocation DeclLoc;
3536 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003537 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003538 // Keep track of where we are in the stream, then jump back there
3539 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003540 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003541
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003542 ReadingKindTracker ReadingKind(Read_Decl, *this);
3543
Douglas Gregor1342e842009-07-06 18:54:52 +00003544 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003545 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003546
Sebastian Redl2c373b92010-10-05 15:59:54 +00003547 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003548 ASTRecordReader Record(*this, *Loc.F);
3549 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003550 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003551
Richard Smithdbafb6c2017-06-29 23:23:46 +00003552 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003553 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003554 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003555 case DECL_CONTEXT_LEXICAL:
3556 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003557 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003558 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003559 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003560 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003561 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003562 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003563 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003564 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003565 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003566 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003567 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003568 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003569 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003570 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003571 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003572 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003573 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003574 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003575 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003576 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003577 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003578 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003579 case DECL_EXPORT:
3580 D = ExportDecl::CreateDeserialized(Context, ID);
3581 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003582 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003583 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003584 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003585 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003586 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003587 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003588 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003589 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003590 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003591 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003592 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003593 break;
Richard Smith151c4562016-12-20 21:35:28 +00003594 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003595 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003596 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003597 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003598 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003599 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003600 case DECL_CONSTRUCTOR_USING_SHADOW:
3601 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3602 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003603 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003604 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003605 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003606 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003607 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003608 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003609 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003610 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003611 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003612 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003613 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003614 break;
Richard Smithbc491202017-02-17 20:05:37 +00003615 case DECL_CXX_DEDUCTION_GUIDE:
3616 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3617 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003618 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003619 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003620 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003621 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003622 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3623 break;
3624 case DECL_CXX_INHERITED_CONSTRUCTOR:
3625 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003626 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003627 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003628 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003629 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003630 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003631 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003632 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003633 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003634 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003635 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003636 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003637 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003638 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003639 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003640 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003641 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003642 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003643 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003644 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003645 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003646 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003647 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003648 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003649 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003650 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003651 case DECL_VAR_TEMPLATE:
3652 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3653 break;
3654 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3655 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3656 break;
3657 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3658 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3659 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003660 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003661 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003662 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003663 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003664 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003665 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003666 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003667 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003668 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003669 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003670 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003671 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003672 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003673 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3674 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003675 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003676 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003677 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003678 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003679 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3680 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003681 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003682 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003683 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003684 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003685 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003686 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003687 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003688 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003689 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003690 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003691 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003692 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003693 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003694 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003695 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003696 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003697 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003698 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003699 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003700 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003701 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003702 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003703 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003704 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003705 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003706 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003707 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003708 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003709 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003710 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003711 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003712 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003713 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003714 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003715 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003716 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003717 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003718 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003719 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003720 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003721 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003722 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003723 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003724 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003725 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003726 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003727 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003728 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003729 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003730 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003731 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003732 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003733 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003734 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003735 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003736 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003737 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003738 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003739 break;
3740 case DECL_BINDING:
3741 D = BindingDecl::CreateDeserialized(Context, ID);
3742 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003743 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003744 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003745 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003746 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003747 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003748 break;
John McCall5e77d762013-04-16 07:28:30 +00003749 case DECL_MS_PROPERTY:
3750 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3751 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003752 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003753 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003754 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003755 case DECL_CXX_BASE_SPECIFIERS:
3756 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003757 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003758 case DECL_CXX_CTOR_INITIALIZERS:
3759 Error("attempt to read a C++ ctor initializer record as a declaration");
3760 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003761 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003762 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003763 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003764 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003765 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003766 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003767 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003768 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003769 case DECL_OMP_DECLARE_REDUCTION:
3770 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3771 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003772 case DECL_OMP_CAPTUREDEXPR:
3773 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003774 break;
Nico Weber66220292016-03-02 17:28:48 +00003775 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003776 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003777 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003778 case DECL_PRAGMA_DETECT_MISMATCH:
3779 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003780 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003781 break;
Michael Han84324352013-02-22 17:15:32 +00003782 case DECL_EMPTY:
3783 D = EmptyDecl::CreateDeserialized(Context, ID);
3784 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003785 case DECL_OBJC_TYPE_PARAM:
3786 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3787 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003788 }
Chris Lattner487412d2009-04-27 05:27:42 +00003789
Sebastian Redlb3298c32010-08-18 23:56:48 +00003790 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003791 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003792 // Set the DeclContext before doing any deserialization, to make sure internal
3793 // calls to Decl::getASTContext() by Decl's methods will find the
3794 // TranslationUnitDecl without crashing.
3795 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003796 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003797
3798 // If this declaration is also a declaration context, get the
3799 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003800 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003801 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003802 if (Offsets.first &&
3803 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3804 return nullptr;
3805 if (Offsets.second &&
3806 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3807 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003808 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003809 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003810
Douglas Gregordab42432011-08-12 00:15:20 +00003811 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003812 PendingUpdateRecords.push_back(
3813 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003814
Douglas Gregor404cdde2012-01-27 01:47:08 +00003815 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003816 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003817 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3818 // we put the Decl in PendingDefinitions so we can pull the categories here.
3819 if (Class->isThisDeclarationADefinition() ||
3820 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003821 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00003822
Richard Smith13fb8602016-07-15 21:33:46 +00003823 // If we have deserialized a declaration that has a definition the
3824 // AST consumer might need to know about, queue it.
3825 // We don't pass it to the consumer immediately because we may be in recursive
3826 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003827 PotentiallyInterestingDecls.push_back(
3828 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003829
Douglas Gregordab42432011-08-12 00:15:20 +00003830 return D;
3831}
3832
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003833void ASTReader::PassInterestingDeclsToConsumer() {
3834 assert(Consumer);
3835
3836 if (PassingDeclsToConsumer)
3837 return;
3838
3839 // Guard variable to avoid recursively redoing the process of passing
3840 // decls to consumer.
3841 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3842 true);
3843
3844 // Ensure that we've loaded all potentially-interesting declarations
3845 // that need to be eagerly loaded.
3846 for (auto ID : EagerlyDeserializedDecls)
3847 GetDecl(ID);
3848 EagerlyDeserializedDecls.clear();
3849
3850 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003851 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003852 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003853 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3854 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003855 }
3856}
3857
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003858void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003859 // The declaration may have been modified by files later in the chain.
3860 // If this is the case, read the record containing the updates from each file
3861 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003862 serialization::GlobalDeclID ID = Record.ID;
3863 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003864 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003865 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003866
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003867 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00003868
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003869 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003870 auto UpdateOffsets = std::move(UpdI->second);
3871 DeclUpdateOffsets.erase(UpdI);
3872
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003873 // Check if this decl was interesting to the consumer. If we just loaded
3874 // the declaration, then we know it was interesting and we skip the call
3875 // to isConsumerInterestedIn because it is unsafe to call in the
3876 // current ASTReader state.
3877 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003878 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003879 for (auto &FileAndOffset : UpdateOffsets) {
3880 ModuleFile *F = FileAndOffset.first;
3881 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003882 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3883 SavedStreamPosition SavedPosition(Cursor);
3884 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003885 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003886 ASTRecordReader Record(*this, *F);
3887 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003888 (void)RecCode;
3889 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003890
David L. Jonesbe1557a2016-12-21 00:17:49 +00003891 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3892 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003893 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003894
3895 // We might have made this declaration interesting. If so, remember that
3896 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003897 if (!WasInteresting &&
3898 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
3899 PotentiallyInterestingDecls.push_back(
3900 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003901 WasInteresting = true;
3902 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003903 }
3904 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00003905 // Add the lazy specializations to the template.
3906 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3907 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3908 "Must not have pending specializations");
3909 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3910 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3911 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3912 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3913 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3914 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
3915 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00003916
3917 // Load the pending visible updates for this decl context, if it has any.
3918 auto I = PendingVisibleUpdates.find(ID);
3919 if (I != PendingVisibleUpdates.end()) {
3920 auto VisibleUpdates = std::move(I->second);
3921 PendingVisibleUpdates.erase(I);
3922
3923 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003924 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00003925 Lookups[DC].Table.add(
3926 Update.Mod, Update.Data,
3927 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3928 DC->setHasExternalVisibleStorage(true);
3929 }
Chris Lattner487412d2009-04-27 05:27:42 +00003930}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003931
Richard Smithd61d4ac2015-08-22 20:13:39 +00003932void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3933 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003934 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003935 if (FirstLocal != CanonDecl) {
3936 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3937 ASTDeclReader::attachPreviousDecl(
3938 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3939 CanonDecl);
3940 }
3941
3942 if (!LocalOffset) {
3943 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3944 return;
3945 }
3946
3947 // Load the list of other redeclarations from this module file.
3948 ModuleFile *M = getOwningModuleFile(FirstLocal);
3949 assert(M && "imported decl from no module file");
3950
3951 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3952 SavedStreamPosition SavedPosition(Cursor);
3953 Cursor.JumpToBit(LocalOffset);
3954
3955 RecordData Record;
3956 unsigned Code = Cursor.ReadCode();
3957 unsigned RecCode = Cursor.readRecord(Code, Record);
3958 (void)RecCode;
3959 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3960
3961 // FIXME: We have several different dispatches on decl kind here; maybe
3962 // we should instead generate one loop per kind and dispatch up-front?
3963 Decl *MostRecent = FirstLocal;
3964 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3965 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003966 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3967 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003968 }
Richard Smithc3a53252015-02-28 05:57:02 +00003969 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003970}
3971
3972namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003973
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003974 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003975 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003976 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003977 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003978 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003979 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003980 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003981 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003982 serialization::GlobalDeclID InterfaceID;
3983 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00003984
Douglas Gregor404cdde2012-01-27 01:47:08 +00003985 void add(ObjCCategoryDecl *Cat) {
3986 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003987 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003988 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003989
Douglas Gregor404cdde2012-01-27 01:47:08 +00003990 // Check for duplicate categories.
3991 if (Cat->getDeclName()) {
3992 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00003993 if (Existing &&
3994 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003995 != Reader.getOwningModuleFile(Cat)) {
3996 // FIXME: We should not warn for duplicates in diamond:
3997 //
3998 // MT //
3999 // / \ //
4000 // ML MR //
4001 // \ / //
4002 // MB //
4003 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004004 // If there are duplicates in ML/MR, there will be warning when
4005 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004006 // importing.
4007 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4008 << Interface->getDeclName() << Cat->getDeclName();
4009 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4010 } else if (!Existing) {
4011 // Record this category.
4012 Existing = Cat;
4013 }
4014 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004015
Douglas Gregor404cdde2012-01-27 01:47:08 +00004016 // Add this category to the end of the chain.
4017 if (Tail)
4018 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4019 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004020 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004021 Tail = Cat;
4022 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004023
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004024 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004025 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004026 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004027 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4028 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004029 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004030 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4031 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004032 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004033 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004034 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004035 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004036
Douglas Gregor404cdde2012-01-27 01:47:08 +00004037 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004038 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004039 }
4040 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004041
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004042 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004043 // If we've loaded all of the category information we care about from
4044 // this module file, we're done.
4045 if (M.Generation <= PreviousGeneration)
4046 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004047
4048 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004049 // module file. If there is no such mapping, we'll find nothing here
4050 // (or in any module it imports).
4051 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4052 if (!LocalID)
4053 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004054
Douglas Gregor404cdde2012-01-27 01:47:08 +00004055 // Perform a binary search to find the local redeclarations for this
4056 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004057 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004058 const ObjCCategoriesInfo *Result
4059 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004060 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004061 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004062 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4063 Result->DefinitionID != LocalID) {
4064 // We didn't find anything. If the class definition is in this module
4065 // file, then the module files it depends on cannot have any categories,
4066 // so suppress further lookup.
4067 return Reader.isDeclIDFromModule(InterfaceID, M);
4068 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004069
Douglas Gregor404cdde2012-01-27 01:47:08 +00004070 // We found something. Dig out all of the categories.
4071 unsigned Offset = Result->Offset;
4072 unsigned N = M.ObjCCategories[Offset];
4073 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4074 for (unsigned I = 0; I != N; ++I)
4075 add(cast_or_null<ObjCCategoryDecl>(
4076 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4077 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004078 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004079 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004080
4081} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004082
Douglas Gregor404cdde2012-01-27 01:47:08 +00004083void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4084 ObjCInterfaceDecl *D,
4085 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004086 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004087 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004088 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004089}
Douglas Gregordab42432011-08-12 00:15:20 +00004090
Richard Smithd6db68c2014-08-07 20:58:41 +00004091template<typename DeclT, typename Fn>
4092static void forAllLaterRedecls(DeclT *D, Fn F) {
4093 F(D);
4094
4095 // Check whether we've already merged D into its redeclaration chain.
4096 // MostRecent may or may not be nullptr if D has not been merged. If
4097 // not, walk the merged redecl chain and see if it's there.
4098 auto *MostRecent = D->getMostRecentDecl();
4099 bool Found = false;
4100 for (auto *Redecl = MostRecent; Redecl && !Found;
4101 Redecl = Redecl->getPreviousDecl())
4102 Found = (Redecl == D);
4103
4104 // If this declaration is merged, apply the functor to all later decls.
4105 if (Found) {
4106 for (auto *Redecl = MostRecent; Redecl != D;
4107 Redecl = Redecl->getPreviousDecl())
4108 F(Redecl);
4109 }
4110}
4111
Vassil Vassilev7d264622017-06-30 22:40:17 +00004112void ASTDeclReader::UpdateDecl(Decl *D,
4113 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004114 while (Record.getIdx() < Record.size()) {
4115 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004116 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004117 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004118 // FIXME: If we also have an update record for instantiating the
4119 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004120 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004121 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004122 // FIXME: We should call addHiddenDecl instead, to add the member
4123 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004124 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004125 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004126 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004127
4128 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004129 // It will be added to the template's lazy specialization set.
4130 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004131 break;
4132
4133 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004134 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004135
Douglas Gregor540fd812012-01-09 18:07:24 +00004136 // Each module has its own anonymous namespace, which is disjoint from
4137 // any other module's anonymous namespaces, so don't attach the anonymous
4138 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004139 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004140 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004141 TU->setAnonymousNamespace(Anon);
4142 else
4143 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4144 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004145 break;
4146 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004147
Richard Smith891fc7f2017-12-05 01:31:47 +00004148 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004149 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004150 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4151 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004152 uint64_t Val = Record.readInt();
4153 if (Val && !VD->getInit()) {
4154 VD->setInit(Record.readExpr());
4155 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4156 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4157 Eval->CheckedICE = true;
4158 Eval->IsICE = Val == 3;
4159 }
4160 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004161 break;
Richard Smith05a21352017-06-22 22:18:46 +00004162 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004163
Richard Smith891fc7f2017-12-05 01:31:47 +00004164 case UPD_CXX_POINT_OF_INSTANTIATION: {
4165 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004166 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004167 VTSD->setPointOfInstantiation(POI);
4168 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4169 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4170 } else {
4171 auto *FD = cast<FunctionDecl>(D);
4172 if (auto *FTSInfo = FD->TemplateOrSpecialization
4173 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4174 FTSInfo->setPointOfInstantiation(POI);
4175 else
4176 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4177 ->setPointOfInstantiation(POI);
4178 }
4179 break;
4180 }
4181
John McCall32791cc2016-01-06 22:34:54 +00004182 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004183 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004184
4185 // We have to read the default argument regardless of whether we use it
4186 // so that hypothetical further update records aren't messed up.
4187 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004188 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004189
4190 // Only apply the update if the parameter still has an uninstantiated
4191 // default argument.
4192 if (Param->hasUninstantiatedDefaultArg())
4193 Param->setDefaultArg(DefaultArg);
4194 break;
4195 }
4196
Richard Smith4b054b22016-08-24 21:25:37 +00004197 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004198 auto *FD = cast<FieldDecl>(D);
4199 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004200
4201 // Only apply the update if the field still has an uninstantiated
4202 // default member initializer.
4203 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4204 if (DefaultInit)
4205 FD->setInClassInitializer(DefaultInit);
4206 else
4207 // Instantiation failed. We can get here if we serialized an AST for
4208 // an invalid program.
4209 FD->removeInClassInitializer();
4210 }
4211 break;
4212 }
4213
Richard Smith4d235792014-08-07 18:53:08 +00004214 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004215 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004216 if (Reader.PendingBodies[FD]) {
4217 // FIXME: Maybe check for ODR violations.
4218 // It's safe to stop now because this update record is always last.
4219 return;
4220 }
4221
David L. Jonesbe1557a2016-12-21 00:17:49 +00004222 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004223 // Maintain AST consistency: any later redeclarations of this function
4224 // are inline if this one is. (We might have merged another declaration
4225 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004226 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4227 FD->setImplicitlyInline();
4228 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004229 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004230 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004231 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004232 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004233 break;
4234 }
4235
Richard Smithcd45dbc2014-04-19 03:48:30 +00004236 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4237 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004238 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004239 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004240 OldDD && (OldDD->Definition != RD ||
4241 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004242 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004243 RD->setArgPassingRestrictions(
4244 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004245 ReadCXXRecordDefinition(RD, /*Update*/true);
4246
Richard Smithcd45dbc2014-04-19 03:48:30 +00004247 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004248 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004249 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004250 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004251 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004252 }
4253
David L. Jonesbe1557a2016-12-21 00:17:49 +00004254 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004255 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004256 if (MemberSpecializationInfo *MSInfo =
4257 RD->getMemberSpecializationInfo()) {
4258 MSInfo->setTemplateSpecializationKind(TSK);
4259 MSInfo->setPointOfInstantiation(POI);
4260 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004261 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004262 Spec->setTemplateSpecializationKind(TSK);
4263 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004264
David L. Jonesbe1557a2016-12-21 00:17:49 +00004265 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004266 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004267 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004268 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004269 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004270 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004271 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004272
4273 // FIXME: If we already have a partial specialization set,
4274 // check that it matches.
4275 if (!Spec->getSpecializedTemplateOrPartial()
4276 .is<ClassTemplatePartialSpecializationDecl *>())
4277 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004278 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004279 }
4280
David L. Jonesbe1557a2016-12-21 00:17:49 +00004281 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004282 RD->setLocation(ReadSourceLocation());
4283 RD->setLocStart(ReadSourceLocation());
4284 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004285
David L. Jonesbe1557a2016-12-21 00:17:49 +00004286 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004287 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004288 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004289 // If the declaration already has attributes, we assume that some other
4290 // AST file already loaded them.
4291 if (!D->hasAttrs())
4292 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004293 }
4294 break;
4295 }
4296
Richard Smithf8134002015-03-10 01:41:22 +00004297 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4298 // Set the 'operator delete' directly to avoid emitting another update
4299 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004300 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004301 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004302 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004303 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004304 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004305 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004306 First->OperatorDeleteThisArg = ThisArg;
4307 }
Richard Smithf8134002015-03-10 01:41:22 +00004308 break;
4309 }
4310
Richard Smith564417a2014-03-20 21:47:22 +00004311 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004312 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004313 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004314 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004315
4316 // Update this declaration's exception specification, if needed.
4317 auto *FD = cast<FunctionDecl>(D);
4318 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4319 // FIXME: If the exception specification is already present, check that it
4320 // matches.
4321 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004322 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004323 FPT->getReturnType(), FPT->getParamTypes(),
4324 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004325
4326 // When we get to the end of deserializing, see if there are other decls
4327 // that we need to propagate this exception specification onto.
4328 Reader.PendingExceptionSpecUpdates.insert(
4329 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004330 }
Richard Smith564417a2014-03-20 21:47:22 +00004331 break;
4332 }
4333
Richard Smith1fa5d642013-05-11 05:45:24 +00004334 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004335 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004336 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004337 for (auto *Redecl : merged_redecls(D)) {
4338 // FIXME: If the return type is already deduced, check that it matches.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004339 auto *FD = cast<FunctionDecl>(Redecl);
Richard Smithdbafb6c2017-06-29 23:23:46 +00004340 Reader.getContext().adjustDeducedFunctionResultType(FD,
4341 DeducedResultType);
Richard Smithd6db68c2014-08-07 20:58:41 +00004342 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004343 break;
4344 }
Eli Friedman276dd182013-09-05 00:02:25 +00004345
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004346 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004347 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004348 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004349 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004350
4351 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004352 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4353 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004354 break;
4355
4356 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004357 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4358 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004359 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004360
Alexey Bataev97720002014-11-11 04:05:39 +00004361 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004362 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4363 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004364 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004365
Alex Denisovfde64952015-06-26 05:28:36 +00004366 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004367 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004368 auto *Exported = cast<NamedDecl>(D);
4369 if (auto *TD = dyn_cast<TagDecl>(Exported))
4370 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004371 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004372 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004373 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4374 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004375 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4376 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004377 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004378 // If Owner is made visible at some later point, make this declaration
4379 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004380 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004381 } else {
4382 // The declaration is now visible.
Richard Smith90dc5252017-06-23 01:04:34 +00004383 Exported->setVisibleDespiteOwningModule();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004384 }
4385 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004386 }
Alex Denisovfde64952015-06-26 05:28:36 +00004387
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004388 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004389 case UPD_ADDED_ATTR_TO_RECORD:
4390 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004391 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004392 assert(Attrs.size() == 1);
4393 D->addAttr(Attrs[0]);
4394 break;
4395 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004396 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004397}