blob: c8ce1a1fcdd29a4c14fdbd475ac2750bffb087a8 [file] [log] [blame]
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001//===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file implements the ASTReader::ReadDeclRecord method, which is the
Chris Lattner487412d2009-04-27 05:27:42 +000011// entrypoint for loading a decl.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000015#include "ASTCommon.h"
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +000016#include "ASTReaderInternals.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000018#include "clang/AST/Attr.h"
19#include "clang/AST/AttrIterator.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/AST/DeclCXX.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000023#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclOpenMP.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/AST/DeclTemplate.h"
27#include "clang/AST/DeclVisitor.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000028#include "clang/AST/DeclarationName.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "clang/AST/Expr.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000030#include "clang/AST/ExternalASTSource.h"
31#include "clang/AST/LambdaCapture.h"
32#include "clang/AST/NestedNameSpecifier.h"
33#include "clang/AST/Redeclarable.h"
34#include "clang/AST/Stmt.h"
35#include "clang/AST/TemplateBase.h"
36#include "clang/AST/Type.h"
37#include "clang/AST/UnresolvedSet.h"
38#include "clang/Basic/AttrKinds.h"
39#include "clang/Basic/ExceptionSpecificationType.h"
40#include "clang/Basic/IdentifierTable.h"
41#include "clang/Basic/LLVM.h"
42#include "clang/Basic/Lambda.h"
43#include "clang/Basic/LangOptions.h"
44#include "clang/Basic/Linkage.h"
45#include "clang/Basic/Module.h"
46#include "clang/Basic/PragmaKinds.h"
47#include "clang/Basic/SourceLocation.h"
48#include "clang/Basic/Specifiers.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000049#include "clang/Sema/IdentifierResolver.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000050#include "clang/Sema/SemaDiagnostic.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000051#include "clang/Serialization/ASTBitCodes.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000052#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000053#include "clang/Serialization/ContinuousRangeMap.h"
54#include "clang/Serialization/Module.h"
55#include "llvm/ADT/DenseMap.h"
56#include "llvm/ADT/FoldingSet.h"
57#include "llvm/ADT/STLExtras.h"
58#include "llvm/ADT/SmallPtrSet.h"
59#include "llvm/ADT/SmallVector.h"
60#include "llvm/ADT/iterator_range.h"
61#include "llvm/Bitcode/BitstreamReader.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000064#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000065#include <algorithm>
66#include <cassert>
67#include <cstdint>
68#include <cstring>
69#include <string>
70#include <utility>
Hans Wennborgdcfba332015-10-06 23:40:43 +000071
Chris Lattner487412d2009-04-27 05:27:42 +000072using namespace clang;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000073using namespace serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000074
75//===----------------------------------------------------------------------===//
76// Declaration deserialization
77//===----------------------------------------------------------------------===//
78
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000079namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000080
Sebastian Redlb3298c32010-08-18 23:56:48 +000081 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000082 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000083 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000084 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000085 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000086 const SourceLocation ThisDeclLoc;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000087
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000088 using RecordData = ASTReader::RecordData;
89
Richard Smith600adef2018-07-04 02:25:38 +000090 TypeID DeferredTypeID = 0;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000091 unsigned AnonymousDeclNumber;
92 GlobalDeclID NamedDeclForTagDecl = 0;
93 IdentifierInfo *TypedefNameForLinkage = nullptr;
94
95 bool HasPendingBody = false;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +000096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000097 ///A flag to carry the information for a decl from the entity is
Vassil Vassilev928c8252016-04-28 14:13:28 +000098 /// used. We use it to delay the marking of the canonical decl as used until
99 /// the entire declaration is deserialized and merged.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000100 bool IsDeclMarkedUsed = false;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000101
Sebastian Redlc67764e2010-07-22 22:43:28 +0000102 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000103
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000104 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000105 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000106 assert(LocalOffset < Loc.Offset && "offset point after current record");
107 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +0000108 }
109
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000110 uint64_t ReadGlobalOffset() {
111 uint64_t Local = ReadLocalOffset();
112 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +0000113 }
114
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000115 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000116 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000117 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000118
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000119 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000120 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000121 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000122
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000123 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000124 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000125 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000126
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000127 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000128 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000129 }
Richard Smith0b884372015-02-27 00:25:58 +0000130
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000131 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000132 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +0000133 }
134
Richard Smith0b884372015-02-27 00:25:58 +0000135 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000136 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000137 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +0000138 }
139
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000140 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000141 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000142 }
143
144 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000145 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000146 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000147 }
148
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000149 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000150 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000151 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000152
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000153 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000154 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000155 }
156
157 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000158 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000159 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000160
David L. Jonesbe1557a2016-12-21 00:17:49 +0000161 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000162 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000163
164 Module *readModule() {
165 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000166 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000167
Richard Smith2a9e5c52015-02-03 03:32:14 +0000168 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000169 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
170 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000171 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000172 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000173 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000174 void MergeDefinitionData(ObjCInterfaceDecl *D,
175 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000176 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
177 void MergeDefinitionData(ObjCProtocolDecl *D,
178 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000179
Richard Smith600adef2018-07-04 02:25:38 +0000180 static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
181
Richard Smithd08aeb62014-08-28 01:33:39 +0000182 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
183 DeclContext *DC,
184 unsigned Index);
185 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
186 unsigned Index, NamedDecl *D);
187
Richard Smith0144f512015-08-22 02:09:38 +0000188 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000189 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000190 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000191 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000192 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000193
Douglas Gregor022857e2011-12-22 01:48:48 +0000194 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000195 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000196 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000198 /// Retrieve the first ID.
Douglas Gregor022857e2011-12-22 01:48:48 +0000199 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000200
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000201 /// Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000202 bool isKeyDecl() const { return IsKeyDecl; }
203
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000204 /// Get a known declaration that this should be merged with, if
Richard Smith5a4737c2015-02-06 02:42:59 +0000205 /// any.
206 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000207 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000209 /// Class used to capture the result of searching for an existing
Douglas Gregor022857e2011-12-22 01:48:48 +0000210 /// declaration of a specific kind and name, along with the ability
211 /// to update the place where this result was found (the declaration
212 /// chain hanging off an identifier or the DeclContext we searched in)
213 /// if requested.
214 class FindExistingResult {
215 ASTReader &Reader;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000216 NamedDecl *New = nullptr;
217 NamedDecl *Existing = nullptr;
218 bool AddResult = false;
219 unsigned AnonymousDeclNumber = 0;
220 IdentifierInfo *TypedefNameForLinkage = nullptr;
Richard Smithd08aeb62014-08-28 01:33:39 +0000221
Douglas Gregor022857e2011-12-22 01:48:48 +0000222 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000223 FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000224
Richard Smithd08aeb62014-08-28 01:33:39 +0000225 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000226 unsigned AnonymousDeclNumber,
227 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000228 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000229 AnonymousDeclNumber(AnonymousDeclNumber),
230 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000231
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000232 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000233 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
234 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000235 AnonymousDeclNumber(Other.AnonymousDeclNumber),
236 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000237 Other.AddResult = false;
238 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000239
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000240 FindExistingResult &operator=(FindExistingResult &&) = delete;
Douglas Gregor022857e2011-12-22 01:48:48 +0000241 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000242
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000243 /// Suppress the addition of this result into the known set of
Douglas Gregor2009cee2012-01-03 22:46:00 +0000244 /// names.
245 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000246
Douglas Gregor022857e2011-12-22 01:48:48 +0000247 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000248
Douglas Gregor022857e2011-12-22 01:48:48 +0000249 template<typename T>
250 operator T*() const { return dyn_cast_or_null<T>(Existing); }
251 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000252
Richard Smith8a639892015-01-24 01:07:20 +0000253 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
254 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000255 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000256
Chris Lattner487412d2009-04-27 05:27:42 +0000257 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000258 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
259 ASTReader::RecordLocation Loc,
260 DeclID thisDeclID, SourceLocation ThisDeclLoc)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000261 : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
262 ThisDeclLoc(ThisDeclLoc) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000263
Vassil Vassilev7d264622017-06-30 22:40:17 +0000264 template <typename T> static
265 void AddLazySpecializations(T *D,
266 SmallVectorImpl<serialization::DeclID>& IDs) {
267 if (IDs.empty())
268 return;
269
270 // FIXME: We should avoid this pattern of getting the ASTContext.
271 ASTContext &C = D->getASTContext();
272
273 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
274
275 if (auto &Old = LazySpecializations) {
276 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +0000277 llvm::sort(IDs.begin(), IDs.end());
Vassil Vassilev7d264622017-06-30 22:40:17 +0000278 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
279 }
280
281 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
282 *Result = IDs.size();
283 std::copy(IDs.begin(), IDs.end(), Result + 1);
284
285 LazySpecializations = Result;
286 }
287
Richard Smithb321ecb2014-05-13 01:15:00 +0000288 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000289 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
290 static Decl *getMostRecentDeclImpl(...);
291 static Decl *getMostRecentDecl(Decl *D);
292
293 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000294 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000295 Redeclarable<DeclT> *D, Decl *Previous,
296 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000297 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000298 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
299 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000300
301 template <typename DeclT>
302 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
303 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000304 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000305
Richard Smith851072e2014-05-19 20:59:20 +0000306 template <typename DeclT>
307 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
308 static void markIncompleteDeclChainImpl(...);
309
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000310 /// Determine whether this declaration has a pending body.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000311 bool hasPendingBody() const { return HasPendingBody; }
312
David Blaikieac4345c2017-02-12 18:45:31 +0000313 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000314 void Visit(Decl *D);
315
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000316 void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000317
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000318 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
319 ObjCCategoryDecl *Next) {
320 Cat->NextClassCategory = Next;
321 }
322
Chris Lattner487412d2009-04-27 05:27:42 +0000323 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000324 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000325 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000326 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
327 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000328 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000329 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000330 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
331 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000332 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000333 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000334 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000335 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000336 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000337 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000338 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000339 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
340 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
341 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
342 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
343 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000344 ClassTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000345
Richard Smith1d209d02013-05-23 01:49:11 +0000346 void VisitClassTemplateSpecializationDecl(
347 ClassTemplateSpecializationDecl *D) {
348 VisitClassTemplateSpecializationDeclImpl(D);
349 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000350
Chris Lattnerca025db2010-05-07 21:43:38 +0000351 void VisitClassTemplatePartialSpecializationDecl(
352 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000353 void VisitClassScopeFunctionSpecializationDecl(
354 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000355 RedeclarableResult
356 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000357
Larisse Voufo39a1e502013-08-06 01:03:05 +0000358 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
359 VisitVarTemplateSpecializationDeclImpl(D);
360 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000361
Larisse Voufo39a1e502013-08-06 01:03:05 +0000362 void VisitVarTemplatePartialSpecializationDecl(
363 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000364 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000365 void VisitValueDecl(ValueDecl *VD);
366 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000367 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000368 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000369 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000370 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000371 void VisitCXXMethodDecl(CXXMethodDecl *D);
372 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
373 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
374 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000375 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000376 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000377 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000378 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
379 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000380 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
381 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000382 void VisitDecompositionDecl(DecompositionDecl *DD);
383 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000384 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000385 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000386 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000387 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000388 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000389 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000390 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000391 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000392 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000393 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000394 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000395 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000396 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000397 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000398 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000399 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000400 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000401 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000402 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000403 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
404 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000405 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000406 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000407 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000408
Chris Lattner487412d2009-04-27 05:27:42 +0000409 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000410
411 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000412 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000413
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000414 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000415 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
416 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000417
418 template<typename T>
419 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000420 RedeclarableResult &Redecl,
421 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000422
Richard Smith0b87e072013-10-07 08:02:11 +0000423 template<typename T>
424 void mergeMergeable(Mergeable<T> *D);
425
Richard Smithf17fdbd2014-04-24 02:25:27 +0000426 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
427 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000428 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000429
Douglas Gregor85f3f952015-07-07 03:57:15 +0000430 ObjCTypeParamList *ReadObjCTypeParamList();
431
Alexis Hunted053252010-05-30 07:21:58 +0000432 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000433 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000434 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000435 void VisitObjCContainerDecl(ObjCContainerDecl *D);
436 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
437 void VisitObjCIvarDecl(ObjCIvarDecl *D);
438 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
439 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000440 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
441 void VisitObjCImplDecl(ObjCImplDecl *D);
442 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
443 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
444 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
445 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
446 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000447 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000448 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000449 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000450 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000451
452} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000453
Richard Smith86dfc1e2015-06-18 22:07:00 +0000454namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000455
Richard Smith86dfc1e2015-06-18 22:07:00 +0000456/// Iterator over the redeclarations of a declaration that have already
457/// been merged into the same redeclaration chain.
458template<typename DeclT>
459class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000460 DeclT *Start;
461 DeclT *Canonical = nullptr;
462 DeclT *Current = nullptr;
463
Richard Smith86dfc1e2015-06-18 22:07:00 +0000464public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000465 MergedRedeclIterator() = default;
466 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000467
468 DeclT *operator*() { return Current; }
469
470 MergedRedeclIterator &operator++() {
471 if (Current->isFirstDecl()) {
472 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000473 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000474 } else
475 Current = Current->getPreviousDecl();
476
477 // If we started in the merged portion, we'll reach our start position
478 // eventually. Otherwise, we'll never reach it, but the second declaration
479 // we reached was the canonical declaration, so stop when we see that one
480 // again.
481 if (Current == Start || Current == Canonical)
482 Current = nullptr;
483 return *this;
484 }
485
486 friend bool operator!=(const MergedRedeclIterator &A,
487 const MergedRedeclIterator &B) {
488 return A.Current != B.Current;
489 }
490};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000491
492} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000493
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000494template <typename DeclT>
495static llvm::iterator_range<MergedRedeclIterator<DeclT>>
496merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000497 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
498 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000499}
500
Sebastian Redlb3298c32010-08-18 23:56:48 +0000501uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000502 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000503}
504
David Blaikieac4345c2017-02-12 18:45:31 +0000505void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000506 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000507 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000508 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Erich Keane9c665062018-08-01 21:02:40 +0000509 CD->setNumCtorInitializers(Record.readInt());
510 if (CD->getNumCtorInitializers())
David Blaikieac4345c2017-02-12 18:45:31 +0000511 CD->CtorInitializers = ReadGlobalOffset();
512 }
513 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000514 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
515 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000516}
517
Sebastian Redlb3298c32010-08-18 23:56:48 +0000518void ASTDeclReader::Visit(Decl *D) {
519 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000520
Vassil Vassilev928c8252016-04-28 14:13:28 +0000521 // At this point we have deserialized and merged the decl and it is safe to
522 // update its canonical decl to signal that the entire entity is used.
523 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
524 IsDeclMarkedUsed = false;
525
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000526 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000527 if (auto *TInfo = DD->getTypeSourceInfo())
528 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000529 }
530
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000531 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000532 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000533 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000534
535 // If this is a tag declaration with a typedef name for linkage, it's safe
536 // to load that typedef now.
537 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000538 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
539 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000540 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000541 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000542 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000543 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000544 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000545 // We only read it if FD doesn't already have a body (e.g., from another
546 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000547 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000548 if (Record.readInt())
549 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000550 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000551}
552
Sebastian Redlb3298c32010-08-18 23:56:48 +0000553void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000554 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
555 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000556 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000557 // parameter or of a parameter of a function template immediately. These
558 // entities might be used in the formulation of its DeclContext (for
559 // example, a function parameter can be used in decltype() in trailing
560 // return type of the function). Use the translation unit DeclContext as a
561 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000562 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
563 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000564 if (!LexicalDCIDForTemplateParmDecl)
565 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000566 Reader.addPendingDeclContextInfo(D,
567 SemaDCIDForTemplateParmDecl,
568 LexicalDCIDForTemplateParmDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000569 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000570 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000571 auto *SemaDC = ReadDeclAs<DeclContext>();
572 auto *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000573 if (!LexicalDC)
574 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000575 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000576 // Avoid calling setLexicalDeclContext() directly because it uses
577 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000578 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
579 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000580 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000581 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000582 D->setInvalidDecl(Record.readInt());
583 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000584 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000585 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000586 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
587 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000588 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000589 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000590 D->setImplicit(Record.readInt());
591 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000592 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000593 D->setReferenced(Record.readInt());
594 D->setTopLevelDeclInObjCContainer(Record.readInt());
595 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000596 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000597 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000598
Douglas Gregorcf68c582011-12-01 22:20:10 +0000599 // Determine whether this declaration is part of a (sub)module. If so, it
600 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000601 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000602 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000603 D->setModuleOwnershipKind(
604 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
605 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000606 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000607
Richard Smith90dc5252017-06-23 01:04:34 +0000608 if (ModulePrivate) {
609 // Module-private declarations are never visible, so there is no work to
610 // do.
Richard Smith42413142015-05-15 20:05:43 +0000611 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
612 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000613 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000614 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000615 // Mark the declaration as visible when its owning module becomes visible.
616 if (Owner->NameVisibility == Module::AllVisible)
617 D->setVisibleDespiteOwningModule();
618 else
Richard Smith42413142015-05-15 20:05:43 +0000619 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000620 }
Richard Smithd19389a2017-07-05 07:47:11 +0000621 } else if (ModulePrivate) {
622 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000623 }
Chris Lattner487412d2009-04-27 05:27:42 +0000624}
625
Nico Weber66220292016-03-02 17:28:48 +0000626void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
627 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000628 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000629 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000630 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000631 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
632 D->getTrailingObjects<char>()[Arg.size()] = '\0';
633}
634
Nico Webercbbaeb12016-03-02 19:28:54 +0000635void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
636 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000637 D->setLocation(ReadSourceLocation());
638 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000639 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
640 D->getTrailingObjects<char>()[Name.size()] = '\0';
641
642 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000643 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000644 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
645 Value.size());
646 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
647}
648
Sebastian Redlb3298c32010-08-18 23:56:48 +0000649void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000650 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000651}
652
Sebastian Redlb3298c32010-08-18 23:56:48 +0000653void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000654 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000655 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000656 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000657}
658
Sebastian Redlb3298c32010-08-18 23:56:48 +0000659void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000660 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000661 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000662 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000663 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000664}
665
Richard Smith43ccec8e2014-08-26 03:52:16 +0000666ASTDeclReader::RedeclarableResult
667ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000668 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000669 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000670 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000671 if (Record.readInt()) { // isModed
672 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000673 TD->setModedTypeSourceInfo(TInfo, modedT);
674 } else
675 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000676 // Read and discard the declaration for which this is a typedef name for
677 // linkage, if it exists. We cannot rely on our type to pull in this decl,
678 // because it might have been merged with a type from another module and
679 // thus might not refer to our version of the declaration.
680 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000681 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000682}
683
684void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000685 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
686 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000687}
688
Richard Smithdda56e42011-04-15 14:24:37 +0000689void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000690 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000691 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000692 // Merged when we merge the template.
693 TD->setDescribedAliasTemplate(Template);
694 else
695 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000696}
697
Richard Smith1d209d02013-05-23 01:49:11 +0000698ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000699 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000700 VisitTypeDecl(TD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000701
David L. Jonesbe1557a2016-12-21 00:17:49 +0000702 TD->IdentifierNamespace = Record.readInt();
703 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000704 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000705 TD->setCompleteDefinition(Record.readInt());
706 TD->setEmbeddedInDeclarator(Record.readInt());
707 TD->setFreeStanding(Record.readInt());
708 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000709 TD->setBraceRange(ReadSourceRange());
Fangrui Song6907ce22018-07-30 19:24:48 +0000710
David L. Jonesbe1557a2016-12-21 00:17:49 +0000711 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000712 case 0:
713 break;
714 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000715 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000716 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000717 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000718 break;
719 }
720 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000721 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000722 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000723 break;
Richard Smith70d58502014-08-30 00:04:23 +0000724 default:
725 llvm_unreachable("unexpected tag info kind");
726 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000727
Richard Smithcd45dbc2014-04-19 03:48:30 +0000728 if (!isa<CXXRecordDecl>(TD))
729 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000730 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000731}
732
Sebastian Redlb3298c32010-08-18 23:56:48 +0000733void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000734 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000735 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000736 ED->setIntegerTypeSourceInfo(TI);
737 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000738 ED->setIntegerType(Record.readType());
739 ED->setPromotionType(Record.readType());
740 ED->setNumPositiveBits(Record.readInt());
741 ED->setNumNegativeBits(Record.readInt());
Erich Keanef92f31c2018-08-01 20:48:16 +0000742 ED->setScoped(Record.readInt());
743 ED->setScopedUsingClassTag(Record.readInt());
744 ED->setFixed(Record.readInt());
Richard Smith4b38ded2012-03-14 23:13:10 +0000745
Erich Keanef92f31c2018-08-01 20:48:16 +0000746 ED->setHasODRHash(true);
Richard Trieuab4d7302018-07-25 22:52:05 +0000747 ED->ODRHash = Record.readInt();
748
Richard Smith01a73372013-10-15 22:02:41 +0000749 // If this is a definition subject to the ODR, and we already have a
750 // definition, merge this one into it.
Erich Keanef92f31c2018-08-01 20:48:16 +0000751 if (ED->isCompleteDefinition() &&
Richard Smith01a73372013-10-15 22:02:41 +0000752 Reader.getContext().getLangOpts().Modules &&
753 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000754 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
755 if (!OldDef) {
756 // This is the first time we've seen an imported definition. Look for a
757 // local definition before deciding that we are the first definition.
758 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
759 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
760 OldDef = D;
761 break;
762 }
763 }
764 }
765 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000766 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
Erich Keanef92f31c2018-08-01 20:48:16 +0000767 ED->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +0000768 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000769 if (OldDef->getODRHash() != ED->getODRHash())
770 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000771 } else {
772 OldDef = ED;
773 }
774 }
775
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000776 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
777 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000778 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000779 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
780 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
781 }
Chris Lattner487412d2009-04-27 05:27:42 +0000782}
783
Richard Smith1d209d02013-05-23 01:49:11 +0000784ASTDeclReader::RedeclarableResult
785ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
786 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000787 RD->setHasFlexibleArrayMember(Record.readInt());
788 RD->setAnonymousStructOrUnion(Record.readInt());
789 RD->setHasObjectMember(Record.readInt());
790 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000791 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
792 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
793 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000794 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000795 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000796 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000797}
798
Sebastian Redlb3298c32010-08-18 23:56:48 +0000799void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000800 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000801 // For function declarations, defer reading the type in case the function has
802 // a deduced return type that references an entity declared within the
803 // function.
804 if (isa<FunctionDecl>(VD))
805 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
806 else
807 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000808}
809
Sebastian Redlb3298c32010-08-18 23:56:48 +0000810void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000811 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000812 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000813 ECD->setInitExpr(Record.readExpr());
814 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000815 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000816}
817
Sebastian Redlb3298c32010-08-18 23:56:48 +0000818void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000819 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000820 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000821 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000822 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000823 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000824 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000825 }
Richard Smithc23d7342018-06-29 20:46:25 +0000826 QualType TSIType = Record.readType();
827 DD->setTypeSourceInfo(
828 TSIType.isNull() ? nullptr
829 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000830}
831
Sebastian Redlb3298c32010-08-18 23:56:48 +0000832void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000833 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000834 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000835
Richard Smith600adef2018-07-04 02:25:38 +0000836 // Attach a type to this function. Use the real type if possible, but fall
837 // back to the type as written if it involves a deduced return type.
838 if (FD->getTypeSourceInfo() &&
839 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
840 ->getReturnType()->getContainedAutoType()) {
841 // We'll set up the real type in Visit, once we've finished loading the
842 // function.
843 FD->setType(FD->getTypeSourceInfo()->getType());
Richard Smitha62d1982018-08-03 01:00:01 +0000844 Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
Richard Smith600adef2018-07-04 02:25:38 +0000845 } else {
846 FD->setType(Reader.GetType(DeferredTypeID));
Richard Smith600adef2018-07-04 02:25:38 +0000847 }
Richard Smitha62d1982018-08-03 01:00:01 +0000848 DeferredTypeID = 0;
Richard Smith600adef2018-07-04 02:25:38 +0000849
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000850 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000851 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000852
Douglas Gregorb2585692012-01-04 17:13:46 +0000853 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
854 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000855
Erich Keane9c665062018-08-01 21:02:40 +0000856 FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
857 FD->setInlineSpecified(Record.readInt());
858 FD->setImplicitlyInline(Record.readInt());
859 FD->setExplicitSpecified(Record.readInt());
860 FD->setVirtualAsWritten(Record.readInt());
861 FD->setPure(Record.readInt());
862 FD->setHasInheritedPrototype(Record.readInt());
863 FD->setHasWrittenPrototype(Record.readInt());
864 FD->setDeletedAsWritten(Record.readInt());
865 FD->setTrivial(Record.readInt());
866 FD->setTrivialForCall(Record.readInt());
867 FD->setDefaulted(Record.readInt());
868 FD->setExplicitlyDefaulted(Record.readInt());
869 FD->setHasImplicitReturnZero(Record.readInt());
870 FD->setConstexpr(Record.readInt());
871 FD->setUsesSEHTry(Record.readInt());
872 FD->setHasSkippedBody(Record.readInt());
873 FD->setIsMultiVersion(Record.readInt());
874 FD->setLateTemplateParsed(Record.readInt());
875
876 FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000877 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000878
Richard Trieue6caa262017-12-23 00:41:01 +0000879 FD->ODRHash = Record.readInt();
Erich Keane9c665062018-08-01 21:02:40 +0000880 FD->setHasODRHash(true);
Richard Trieue6caa262017-12-23 00:41:01 +0000881
David L. Jonesbe1557a2016-12-21 00:17:49 +0000882 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000883 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000884 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000885 break;
886 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000887 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000888 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000889 break;
890 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000891 auto *InstFD = ReadDeclAs<FunctionDecl>();
892 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000893 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000894 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000895 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000896 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000897 break;
898 }
899 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000900 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
901 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000902
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000903 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000904 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000905 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000906
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000907 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000908 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000909 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000910 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000911 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000912 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000913 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000914 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000915 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000916
917 LAngleLoc = ReadSourceLocation();
918 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000919 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000920
921 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000922
Douglas Gregor4163aca2011-09-09 21:34:22 +0000923 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000924 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000925 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000926 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000927 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000928 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000929 FunctionTemplateSpecializationInfo *FTInfo
930 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
931 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000932 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
933 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000934 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000935 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000936
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000937 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000938 // The template that contains the specializations set. It's not safe to
939 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000940 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000941 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
942 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
943 // FunctionTemplateSpecializationInfo's Profile().
944 // We avoid getASTContext because a decl in the parent hierarchy may
945 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000946 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000947 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000948 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000949 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000950 FunctionTemplateSpecializationInfo *ExistingInfo =
951 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000952 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000953 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
954 else {
955 assert(Reader.getContext().getLangOpts().Modules &&
956 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000957 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000958 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000959 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000960 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000961 }
962 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
963 // Templates.
964 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000965 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000966 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000967 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
968
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000969 // Templates args.
970 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000971 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000972 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000973 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000974 TemplArgs.setLAngleLoc(ReadSourceLocation());
975 TemplArgs.setRAngleLoc(ReadSourceLocation());
976
Douglas Gregor4163aca2011-09-09 21:34:22 +0000977 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000978 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000979 // These are not merged; we don't need to merge redeclarations of dependent
980 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000981 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000982 }
983 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000984
Chris Lattnerca025db2010-05-07 21:43:38 +0000985 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000986 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000987 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000988 Params.reserve(NumParams);
989 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000990 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000991 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000992}
993
Sebastian Redlb3298c32010-08-18 23:56:48 +0000994void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000995 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000996 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000997 // Load the body on-demand. Most clients won't care, because method
998 // definitions rarely show up in headers.
999 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1000 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001001 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1002 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001003 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001004 MD->setInstanceMethod(Record.readInt());
1005 MD->setVariadic(Record.readInt());
1006 MD->setPropertyAccessor(Record.readInt());
1007 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001008 MD->setOverriding(Record.readInt());
1009 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001010
Erich Keane9b18eca2018-08-01 21:31:08 +00001011 MD->setIsRedeclaration(Record.readInt());
1012 MD->setHasRedeclaration(Record.readInt());
1013 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001014 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001015 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001016
David L. Jonesbe1557a2016-12-21 00:17:49 +00001017 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1018 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001019 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001020 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001021 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1022 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001023 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001024 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001025 Params.reserve(NumParams);
1026 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001027 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001028
Erich Keane9b18eca2018-08-01 21:31:08 +00001029 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001030 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001031 SmallVector<SourceLocation, 16> SelLocs;
1032 SelLocs.reserve(NumStoredSelLocs);
1033 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001034 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001035
1036 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001037}
1038
Douglas Gregor85f3f952015-07-07 03:57:15 +00001039void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001040 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001041
David L. Jonesbe1557a2016-12-21 00:17:49 +00001042 D->Variance = Record.readInt();
1043 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001044 D->VarianceLoc = ReadSourceLocation();
1045 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001046}
1047
Sebastian Redlb3298c32010-08-18 23:56:48 +00001048void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001049 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001050 CD->setAtStartLoc(ReadSourceLocation());
1051 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001052}
1053
Douglas Gregor85f3f952015-07-07 03:57:15 +00001054ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001055 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001056 if (numParams == 0)
1057 return nullptr;
1058
1059 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1060 typeParams.reserve(numParams);
1061 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001062 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001063 if (!typeParam)
1064 return nullptr;
1065
1066 typeParams.push_back(typeParam);
1067 }
1068
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001069 SourceLocation lAngleLoc = ReadSourceLocation();
1070 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001071
1072 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1073 typeParams, rAngleLoc);
1074}
1075
Manman Renec315f12016-09-09 23:48:27 +00001076void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001077 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001078 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001079 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001080
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001081 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001082 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001083
Manman Renec315f12016-09-09 23:48:27 +00001084 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001085 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001086 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1087 Protocols.reserve(NumProtocols);
1088 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001089 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001090 SmallVector<SourceLocation, 16> ProtoLocs;
1091 ProtoLocs.reserve(NumProtocols);
1092 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001093 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001094 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1095 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001096
Manman Renec315f12016-09-09 23:48:27 +00001097 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001098 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001099 Protocols.clear();
1100 Protocols.reserve(NumProtocols);
1101 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001102 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001103 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1104 Reader.getContext());
1105}
1106
1107void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1108 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1109 // FIXME: odr checking?
1110}
1111
Sebastian Redlb3298c32010-08-18 23:56:48 +00001112void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001113 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001114 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001115 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001116 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001117
1118 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001119 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001120 // Read the definition.
1121 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001122
David L. Jonesbe1557a2016-12-21 00:17:49 +00001123 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001124 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1125 if (Canon->Data.getPointer()) {
1126 // If we already have a definition, keep the definition invariant and
1127 // merge the data.
1128 MergeDefinitionData(Canon, std::move(ID->data()));
1129 ID->Data = Canon->Data;
1130 } else {
1131 // Set the definition data of the canonical declaration, so other
1132 // redeclarations will see it.
1133 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001134
Manman Renec315f12016-09-09 23:48:27 +00001135 // We will rebuild this list lazily.
1136 ID->setIvarList(nullptr);
1137 }
Craig Toppera13603a2014-05-22 05:54:18 +00001138
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001139 // Note that we have deserialized a definition.
1140 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001141
Douglas Gregor404cdde2012-01-27 01:47:08 +00001142 // Note that we've loaded this Objective-C class.
1143 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001144 } else {
1145 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001146 }
Chris Lattner487412d2009-04-27 05:27:42 +00001147}
1148
Sebastian Redlb3298c32010-08-18 23:56:48 +00001149void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001150 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001151 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001152 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001153 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001154 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001155 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001156}
1157
Graydon Hoaree0a68352017-06-28 18:36:27 +00001158void ASTDeclReader::ReadObjCDefinitionData(
1159 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001160 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001161 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1162 ProtoRefs.reserve(NumProtoRefs);
1163 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001164 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001165 SmallVector<SourceLocation, 16> ProtoLocs;
1166 ProtoLocs.reserve(NumProtoRefs);
1167 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001168 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001169 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1170 ProtoLocs.data(), Reader.getContext());
1171}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001172
Graydon Hoaree0a68352017-06-28 18:36:27 +00001173void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1174 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1175 // FIXME: odr checking?
1176}
1177
1178void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1179 RedeclarableResult Redecl = VisitRedeclarable(PD);
1180 VisitObjCContainerDecl(PD);
1181 mergeRedeclarable(PD, Redecl);
1182
1183 if (Record.readInt()) {
1184 // Read the definition.
1185 PD->allocateDefinitionData();
1186
1187 ReadObjCDefinitionData(PD->data());
1188
1189 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1190 if (Canon->Data.getPointer()) {
1191 // If we already have a definition, keep the definition invariant and
1192 // merge the data.
1193 MergeDefinitionData(Canon, std::move(PD->data()));
1194 PD->Data = Canon->Data;
1195 } else {
1196 // Set the definition data of the canonical declaration, so other
1197 // redeclarations will see it.
1198 PD->getCanonicalDecl()->Data = PD->Data;
1199 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001200 // Note that we have deserialized a definition.
1201 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001202 } else {
1203 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001204 }
Chris Lattner487412d2009-04-27 05:27:42 +00001205}
1206
Sebastian Redlb3298c32010-08-18 23:56:48 +00001207void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001208 VisitFieldDecl(FD);
1209}
1210
Sebastian Redlb3298c32010-08-18 23:56:48 +00001211void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001212 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001213 CD->setCategoryNameLoc(ReadSourceLocation());
1214 CD->setIvarLBraceLoc(ReadSourceLocation());
1215 CD->setIvarRBraceLoc(ReadSourceLocation());
1216
Douglas Gregor404cdde2012-01-27 01:47:08 +00001217 // Note that this category has been deserialized. We do this before
1218 // deserializing the interface declaration, so that it will consider this
1219 /// category.
1220 Reader.CategoriesDeserialized.insert(CD);
1221
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001222 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001223 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001224 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001225 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001226 ProtoRefs.reserve(NumProtoRefs);
1227 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001228 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001229 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001230 ProtoLocs.reserve(NumProtoRefs);
1231 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001232 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001233 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001234 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001235
1236 // Protocols in the class extension belong to the class.
1237 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1238 CD->ClassInterface->mergeClassExtensionProtocolList(
1239 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1240 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001241}
1242
Sebastian Redlb3298c32010-08-18 23:56:48 +00001243void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001244 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001245 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001246}
1247
Sebastian Redlb3298c32010-08-18 23:56:48 +00001248void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001249 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001250 D->setAtLoc(ReadSourceLocation());
1251 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001252 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001253 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001254 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001255 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001256 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001257 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001258 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001259 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001260 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001261 DeclarationName GetterName = Record.readDeclarationName();
1262 SourceLocation GetterLoc = ReadSourceLocation();
1263 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1264 DeclarationName SetterName = Record.readDeclarationName();
1265 SourceLocation SetterLoc = ReadSourceLocation();
1266 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001267 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1268 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1269 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001270}
1271
Sebastian Redlb3298c32010-08-18 23:56:48 +00001272void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001273 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001274 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001275}
1276
Sebastian Redlb3298c32010-08-18 23:56:48 +00001277void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001278 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001279 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001280}
1281
Sebastian Redlb3298c32010-08-18 23:56:48 +00001282void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001283 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001284 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1285 D->SuperLoc = ReadSourceLocation();
1286 D->setIvarLBraceLoc(ReadSourceLocation());
1287 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001288 D->setHasNonZeroConstructors(Record.readInt());
1289 D->setHasDestructors(Record.readInt());
1290 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001291 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001292 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001293}
1294
Sebastian Redlb3298c32010-08-18 23:56:48 +00001295void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001296 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001297 D->setAtLoc(ReadSourceLocation());
1298 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1299 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1300 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001301 D->setGetterCXXConstructor(Record.readExpr());
1302 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001303}
1304
Sebastian Redlb3298c32010-08-18 23:56:48 +00001305void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001306 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001307 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001308
1309 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1310 FD->InitStorage.setInt(ISK);
1311 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1312 ? Record.readType().getAsOpaquePtr()
1313 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001314 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001315
1316 if (auto *BW = Record.readExpr())
1317 FD->setBitWidth(BW);
1318
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001319 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001320 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001321 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001322 }
Richard Smith0b87e072013-10-07 08:02:11 +00001323 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001324}
1325
John McCall5e77d762013-04-16 07:28:30 +00001326void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1327 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001328 PD->GetterId = Record.getIdentifierInfo();
1329 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001330}
1331
Francois Pichet783dd6e2010-11-21 06:08:52 +00001332void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1333 VisitValueDecl(FD);
1334
David L. Jonesbe1557a2016-12-21 00:17:49 +00001335 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001336 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001337 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001338
1339 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001340 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001341
1342 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001343}
1344
Larisse Voufo39a1e502013-08-06 01:03:05 +00001345ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001346 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001347 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001348
David L. Jonesbe1557a2016-12-21 00:17:49 +00001349 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1350 VD->VarDeclBits.TSCSpec = Record.readInt();
1351 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001352 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001353 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1354 Record.readInt();
1355 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001356 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001357 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001358 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001359 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1360 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1361 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1362 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1363 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1364 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001365 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001366 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001367 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001368 VD->setCachedLinkage(VarLinkage);
1369
1370 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001371 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001372 VD->getLexicalDeclContext()->isFunctionOrMethod())
1373 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001374
David L. Jonesbe1557a2016-12-21 00:17:49 +00001375 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001376 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001377 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001378 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1379 Eval->CheckedICE = true;
1380 Eval->IsICE = Val == 3;
1381 }
1382 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001383
Richard Smitha4653622017-09-06 20:01:14 +00001384 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1385 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1386
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001387 enum VarKind {
1388 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1389 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001390 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001391 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001392 // Only true variables (not parameters or implicit parameters) can be
1393 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001394 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1395 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001396 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001397 break;
1398 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001399 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001400 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001401 break;
1402 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001403 auto *Tmpl = ReadDeclAs<VarDecl>();
1404 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001405 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001406 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001407 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001408 break;
1409 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001410 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001411
1412 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001413}
1414
Sebastian Redlb3298c32010-08-18 23:56:48 +00001415void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001416 VisitVarDecl(PD);
1417}
1418
Sebastian Redlb3298c32010-08-18 23:56:48 +00001419void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001420 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001421 unsigned isObjCMethodParam = Record.readInt();
1422 unsigned scopeDepth = Record.readInt();
1423 unsigned scopeIndex = Record.readInt();
1424 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001425 if (isObjCMethodParam) {
1426 assert(scopeDepth == 0);
1427 PD->setObjCMethodScopeInfo(scopeIndex);
1428 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1429 } else {
1430 PD->setScopeInfo(scopeDepth, scopeIndex);
1431 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001432 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1433 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1434 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001435 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001436
1437 // FIXME: If this is a redeclaration of a function from another module, handle
1438 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001439}
1440
Richard Smith7b76d812016-08-12 02:21:25 +00001441void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1442 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001443 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith7b76d812016-08-12 02:21:25 +00001444 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001445 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001446}
1447
1448void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1449 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001450 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001451}
1452
Sebastian Redlb3298c32010-08-18 23:56:48 +00001453void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001454 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001455 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001456 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001457}
1458
Sebastian Redlb3298c32010-08-18 23:56:48 +00001459void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001460 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001461 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001462 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001463 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001464 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001465 Params.reserve(NumParams);
1466 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001467 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001468 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001469
David L. Jonesbe1557a2016-12-21 00:17:49 +00001470 BD->setIsVariadic(Record.readInt());
1471 BD->setBlockMissingReturnType(Record.readInt());
1472 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001473 BD->setDoesNotEscape(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);
Erich Keanef92f31c2018-08-01 20:48:16 +00001747 MergeDD.Definition->setCompleteDefinition(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.
Erich Keanef92f31c2018-08-01 20:48:16 +00001887 D->setCompleteDefinition(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();
Erich Keanef92f31c2018-08-01 20:48:16 +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);
Erich Keane9c665062018-08-01 21:02:40 +00001961 D->setIsCopyDeductionCandidate(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 Krusedc5ce722018-08-03 01:21:16 +00002835 // Return false if any of the enable_if expressions of A and B are different.
George Burgess IV95845082017-02-15 22:43:27 +00002836 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00002837 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
2838 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
2839 auto AEnableIf = AEnableIfAttrs.begin();
2840 auto BEnableIf = BEnableIfAttrs.begin();
2841 for (; AEnableIf != AEnableIfAttrs.end() && BEnableIf != BEnableIfAttrs.end();
2842 ++BEnableIf, ++AEnableIf) {
George Burgess IV95845082017-02-15 22:43:27 +00002843 Cand1ID.clear();
2844 Cand2ID.clear();
2845
Michael Krusedc5ce722018-08-03 01:21:16 +00002846 AEnableIf->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2847 BEnableIf->getCond()->Profile(Cand2ID, B->getASTContext(), true);
George Burgess IV95845082017-02-15 22:43:27 +00002848 if (Cand1ID != Cand2ID)
2849 return false;
2850 }
2851
Michael Krusedc5ce722018-08-03 01:21:16 +00002852 // Return false if the number of enable_if attributes was different.
2853 return AEnableIf == AEnableIfAttrs.end() && BEnableIf == BEnableIfAttrs.end();
George Burgess IV95845082017-02-15 22:43:27 +00002854}
2855
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002856/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002857static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2858 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002859
Douglas Gregor022857e2011-12-22 01:48:48 +00002860 if (X == Y)
2861 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002862
Douglas Gregor022857e2011-12-22 01:48:48 +00002863 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00002864 //
2865 // Note that we can't use DeclContext::Equals here, because the DeclContexts
2866 // could be two different declarations of the same function. (We will fix the
2867 // semantic DC to refer to the primary definition after merging.)
2868 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
2869 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00002870 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002871
2872 // Two typedefs refer to the same entity if they have the same underlying
2873 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002874 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
2875 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002876 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2877 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002878
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002879 // Must have the same kind.
2880 if (X->getKind() != Y->getKind())
2881 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002882
Douglas Gregorda389302012-01-01 21:47:52 +00002883 // Objective-C classes and protocols with the same name always match.
2884 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002885 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002886
2887 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002888 // No need to handle these here: we merge them when adding them to the
2889 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002890 return false;
2891 }
Richard Smithd55889a2013-09-09 16:55:27 +00002892
Douglas Gregor2009cee2012-01-03 22:46:00 +00002893 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002894 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
2895 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00002896 return (TagX->getTagKind() == TagY->getTagKind()) ||
2897 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2898 TagX->getTagKind() == TTK_Interface) &&
2899 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2900 TagY->getTagKind() == TTK_Interface));
2901 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002902
Joao Matose9a3ed42012-08-31 22:18:20 +00002903 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002904 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2905 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002906 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
2907 const auto *FuncY = cast<FunctionDecl>(Y);
2908 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2909 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002910 if (CtorX->getInheritedConstructor() &&
2911 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2912 CtorY->getInheritedConstructor().getConstructor()))
2913 return false;
2914 }
Erich Keane281d20b2018-01-08 21:34:17 +00002915
2916 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
2917 return false;
2918
2919 // Multiversioned functions with different feature strings are represented
2920 // as separate declarations.
2921 if (FuncX->isMultiVersion()) {
2922 const auto *TAX = FuncX->getAttr<TargetAttr>();
2923 const auto *TAY = FuncY->getAttr<TargetAttr>();
2924 assert(TAX && TAY && "Multiversion Function without target attribute");
2925
2926 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
2927 return false;
2928 }
2929
Richard Smitha54d3242017-03-08 23:00:26 +00002930 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00002931 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
2932 // Map to the first declaration that we've already merged into this one.
2933 // The TSI of redeclarations might not match (due to calling conventions
2934 // being inherited onto the type but not the TSI), but the TSI type of
2935 // the first declaration of the function should match across modules.
2936 FD = FD->getCanonicalDecl();
2937 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
2938 : FD->getType();
2939 };
2940 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
2941 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00002942 // We can get functions with different types on the redecl chain in C++17
2943 // if they have differing exception specifications and at least one of
2944 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00002945 auto *XFPT = XT->getAs<FunctionProtoType>();
2946 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002947 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00002948 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2949 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00002950 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00002951 return true;
2952 return false;
2953 }
George Burgess IV95845082017-02-15 22:43:27 +00002954 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002955 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002956 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002957
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002958 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002959 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
2960 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002961 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2962 ASTContext &C = VarX->getASTContext();
2963 if (C.hasSameType(VarX->getType(), VarY->getType()))
2964 return true;
2965
2966 // We can get decls with different types on the redecl chain. Eg.
2967 // template <typename T> struct S { static T Var[]; }; // #1
2968 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2969 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002970 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002971 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2972 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2973 if (!VarXTy || !VarYTy)
2974 return false;
2975 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2976 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2977 }
2978 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002979 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002980
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002981 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002982 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2983 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002984 return NamespaceX->isInline() == NamespaceY->isInline();
2985 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002986
Richard Smith8f8f05c2013-06-24 04:45:28 +00002987 // Identical template names and kinds match if their template parameter lists
2988 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002989 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
2990 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002991 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002992 TemplateY->getTemplatedDecl()) &&
2993 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2994 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002995 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002996
Richard Smith0b87e072013-10-07 08:02:11 +00002997 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002998 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
2999 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003000 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003001 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3002 }
3003
Richard Smith8cbd8952015-08-04 02:05:09 +00003004 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003005 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3006 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003007 return IFDX->getAnonField()->getCanonicalDecl() ==
3008 IFDY->getAnonField()->getCanonicalDecl();
3009 }
3010
Richard Smith01a73372013-10-15 22:02:41 +00003011 // Enumerators with the same name match.
3012 if (isa<EnumConstantDecl>(X))
3013 // FIXME: Also check the value is odr-equivalent.
3014 return true;
3015
Richard Smithfd8634a2013-10-23 02:17:46 +00003016 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003017 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3018 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003019 return USX->getTargetDecl() == USY->getTargetDecl();
3020 }
3021
Richard Smith32952e12014-10-14 02:00:47 +00003022 // Using declarations with the same qualifier match. (We already know that
3023 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003024 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3025 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003026 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3027 UX->hasTypename() == UY->hasTypename() &&
3028 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3029 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003030 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3031 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003032 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3033 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3034 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003035 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003036 return isSameQualifier(
3037 UX->getQualifier(),
3038 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3039
Richard Smithf4634362014-09-03 23:11:22 +00003040 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003041 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3042 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003043 return NAX->getNamespace()->Equals(NAY->getNamespace());
3044 }
3045
Douglas Gregor022857e2011-12-22 01:48:48 +00003046 return false;
3047}
3048
Richard Smithd55889a2013-09-09 16:55:27 +00003049/// Find the context in which we should search for previous declarations when
3050/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003051DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3052 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003053 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003054 return ND->getOriginalNamespace();
3055
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003056 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003057 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003058 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003059 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003060 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003061
3062 // If there's no definition yet, then DC's definition is added by an update
3063 // record, but we've not yet loaded that update record. In this case, we
3064 // commit to DC being the canonical definition now, and will fix this when
3065 // we load the update record.
3066 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003067 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003068 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003069 RD->DefinitionData = DD;
3070 RD->getCanonicalDecl()->DefinitionData = DD;
3071
3072 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003073 Reader.PendingFakeDefinitionData.insert(
3074 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003075 }
3076
3077 return DD->Definition;
3078 }
Richard Smithd55889a2013-09-09 16:55:27 +00003079
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003080 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003081 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3082 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003083
Richard Smith4eca9b92015-02-04 23:37:59 +00003084 // We can see the TU here only if we have no Sema object. In that case,
3085 // there's no TU scope to look in, so using the DC alone is sufficient.
3086 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3087 return TU;
3088
Craig Toppera13603a2014-05-22 05:54:18 +00003089 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003090}
3091
Douglas Gregor022857e2011-12-22 01:48:48 +00003092ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003093 // Record that we had a typedef name for linkage whether or not we merge
3094 // with that declaration.
3095 if (TypedefNameForLinkage) {
3096 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3097 Reader.ImportedTypedefNamesForLinkage.insert(
3098 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3099 return;
3100 }
3101
Douglas Gregor9b47f942012-01-09 17:38:47 +00003102 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003103 return;
Richard Smith95d99302013-07-13 02:00:19 +00003104
Richard Smith70d58502014-08-30 00:04:23 +00003105 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003106 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003107 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003108 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3109 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003110 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003111 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003112 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003113 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3114 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003115 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003116 // Add the declaration to its redeclaration context so later merging
3117 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003118 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003119 }
3120}
3121
Richard Smith88ebade2014-08-23 01:45:27 +00003122/// Find the declaration that should be merged into, given the declaration found
3123/// by name lookup. If we're merging an anonymous declaration within a typedef,
3124/// we need a matching typedef, and we merge with the type inside it.
3125static NamedDecl *getDeclForMerging(NamedDecl *Found,
3126 bool IsTypedefNameForLinkage) {
3127 if (!IsTypedefNameForLinkage)
3128 return Found;
3129
3130 // If we found a typedef declaration that gives a name to some other
3131 // declaration, then we want that inner declaration. Declarations from
3132 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003133 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003134 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003135
3136 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003137 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003138
Hans Wennborgdcfba332015-10-06 23:40:43 +00003139 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003140}
3141
Richard Smith600adef2018-07-04 02:25:38 +00003142/// Find the declaration to use to populate the anonymous declaration table
3143/// for the given lexical DeclContext. We only care about finding local
3144/// definitions of the context; we'll merge imported ones as we go.
3145DeclContext *
3146ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3147 // For classes, we track the definition as we merge.
3148 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3149 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3150 return DD ? DD->Definition : nullptr;
3151 }
3152
3153 // For anything else, walk its merged redeclarations looking for a definition.
3154 // Note that we can't just call getDefinition here because the redeclaration
3155 // chain isn't wired up.
3156 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3157 if (auto *FD = dyn_cast<FunctionDecl>(D))
3158 if (FD->isThisDeclarationADefinition())
3159 return FD;
3160 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3161 if (MD->isThisDeclarationADefinition())
3162 return MD;
3163 }
3164
3165 // No merged definition yet.
3166 return nullptr;
3167}
3168
Richard Smithd08aeb62014-08-28 01:33:39 +00003169NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3170 DeclContext *DC,
3171 unsigned Index) {
3172 // If the lexical context has been merged, look into the now-canonical
3173 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003174 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003175
3176 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003177 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003178 if (Index < Previous.size() && Previous[Index])
3179 return Previous[Index];
3180
3181 // If this is the first time, but we have parsed a declaration of the context,
3182 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003183 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3184 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3185 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003186 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003187 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3188 else
Richard Smith2b560572015-02-07 03:11:11 +00003189 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3190 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003191 }
3192
3193 return Index < Previous.size() ? Previous[Index] : nullptr;
3194}
3195
3196void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3197 DeclContext *DC, unsigned Index,
3198 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003199 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003200
Richard Smith600adef2018-07-04 02:25:38 +00003201 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003202 if (Index >= Previous.size())
3203 Previous.resize(Index + 1);
3204 if (!Previous[Index])
3205 Previous[Index] = D;
3206}
3207
Douglas Gregor022857e2011-12-22 01:48:48 +00003208ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003209 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3210 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003211
Richard Smithd08aeb62014-08-28 01:33:39 +00003212 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3213 // Don't bother trying to find unnamed declarations that are in
3214 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003215 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3216 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003217 Result.suppress();
3218 return Result;
3219 }
Richard Smith95d99302013-07-13 02:00:19 +00003220
Douglas Gregor022857e2011-12-22 01:48:48 +00003221 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003222 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003223 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003224 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003225 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3226 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003227 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3228 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003229 // Go on to check in other places in case an existing typedef name
3230 // was not imported.
3231 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003232
Richard Smith2b560572015-02-07 03:11:11 +00003233 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003234 // This is an anonymous declaration that we may need to merge. Look it up
3235 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003236 if (auto *Existing = getAnonymousDeclForMerging(
3237 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3238 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003239 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3240 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003241 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003242 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003243 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003244
3245 // Temporarily consider the identifier to be up-to-date. We don't want to
3246 // cause additional lookups here.
3247 class UpToDateIdentifierRAII {
3248 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003249 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003250
3251 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003252 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003253 if (II) {
3254 WasOutToDate = II->isOutOfDate();
3255 if (WasOutToDate)
3256 II->setOutOfDate(false);
3257 }
3258 }
3259
3260 ~UpToDateIdentifierRAII() {
3261 if (WasOutToDate)
3262 II->setOutOfDate(true);
3263 }
3264 } UpToDate(Name.getAsIdentifierInfo());
3265
Fangrui Song6907ce22018-07-30 19:24:48 +00003266 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003267 IEnd = IdResolver.end();
3268 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003269 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003270 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003271 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3272 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003273 }
Richard Smith8a639892015-01-24 01:07:20 +00003274 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003275 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003276 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003277 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003278 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003279 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3280 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003281 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003282 } else {
3283 // Not in a mergeable context.
3284 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003285 }
Richard Smithd55889a2013-09-09 16:55:27 +00003286
Richard Smith2b9e3e32013-10-18 06:05:18 +00003287 // If this declaration is from a merged context, make a note that we need to
3288 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003289 //
3290 // FIXME: We should do something similar if we merge two definitions of the
3291 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003292 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3293 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3294 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003295 Reader.PendingOdrMergeChecks.push_back(D);
3296
Richard Smithd08aeb62014-08-28 01:33:39 +00003297 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003298 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003299}
3300
Richard Smithb321ecb2014-05-13 01:15:00 +00003301template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003302Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3303 return D->RedeclLink.getLatestNotUpdated();
3304}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003305
Richard Smithc3a53252015-02-28 05:57:02 +00003306Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3307 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3308}
3309
3310Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3311 assert(D);
3312
3313 switch (D->getKind()) {
3314#define ABSTRACT_DECL(TYPE)
3315#define DECL(TYPE, BASE) \
3316 case Decl::TYPE: \
3317 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3318#include "clang/AST/DeclNodes.inc"
3319 }
3320 llvm_unreachable("unknown decl kind");
3321}
3322
Richard Smith8ce51082015-03-11 01:44:51 +00003323Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3324 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3325}
3326
Richard Smithc3a53252015-02-28 05:57:02 +00003327template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003328void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3329 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003330 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003331 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003332 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003333}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003334
Richard Smith24d166c2014-07-31 23:52:38 +00003335namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003336
Richard Smith6de7a242014-07-31 23:46:44 +00003337template<>
3338void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003339 Redeclarable<VarDecl> *D,
3340 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003341 auto *VD = static_cast<VarDecl *>(D);
3342 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003343 D->RedeclLink.setPrevious(PrevVD);
3344 D->First = PrevVD->First;
3345
3346 // We should keep at most one definition on the chain.
3347 // FIXME: Cache the definition once we've found it. Building a chain with
3348 // N definitions currently takes O(N^2) time here.
3349 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3350 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3351 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3352 Reader.mergeDefinitionVisibility(CurD, VD);
3353 VD->demoteThisDefinitionToDeclaration();
3354 break;
3355 }
3356 }
3357 }
3358}
3359
Richard Smitha62d1982018-08-03 01:00:01 +00003360static bool isUndeducedReturnType(QualType T) {
3361 auto *DT = T->getContainedDeducedType();
3362 return DT && !DT->isDeduced();
3363}
3364
Richard Smithedbc6e92016-10-14 21:41:24 +00003365template<>
3366void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003367 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003368 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003369 auto *FD = static_cast<FunctionDecl *>(D);
3370 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003371
3372 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003373 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003374
3375 // If the previous declaration is an inline function declaration, then this
3376 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003377 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003378 // FIXME: [dcl.fct.spec]p4:
3379 // If a function with external linkage is declared inline in one
3380 // translation unit, it shall be declared inline in all translation
3381 // units in which it appears.
3382 //
3383 // Be careful of this case:
3384 //
3385 // module A:
3386 // template<typename T> struct X { void f(); };
3387 // template<typename T> inline void X<T>::f() {}
3388 //
3389 // module B instantiates the declaration of X<int>::f
3390 // module C instantiates the definition of X<int>::f
3391 //
3392 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003393 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003394 }
3395
Richard Smith6de7a242014-07-31 23:46:44 +00003396 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3397 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003398 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003399 // If we need to propagate an exception specification along the redecl
3400 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003401 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3402 bool WasUnresolved =
3403 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3404 if (IsUnresolved != WasUnresolved)
3405 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003406 {Canon, IsUnresolved ? PrevFD : FD});
3407
3408 // If we need to propagate a deduced return type along the redecl chain,
3409 // make a note of that so that we can do it later.
3410 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3411 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3412 if (IsUndeduced != WasUndeduced)
3413 Reader.PendingDeducedTypeUpdates.insert(
3414 {cast<FunctionDecl>(Canon),
3415 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003416 }
3417}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003418
3419} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003420
Richard Smith6de7a242014-07-31 23:46:44 +00003421void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003422 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3423}
3424
Richard Smith8346e522015-06-10 01:47:58 +00003425/// Inherit the default template argument from \p From to \p To. Returns
3426/// \c false if there is no default template for \p From.
3427template <typename ParmDecl>
3428static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3429 Decl *ToD) {
3430 auto *To = cast<ParmDecl>(ToD);
3431 if (!From->hasDefaultArgument())
3432 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003433 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003434 return true;
3435}
3436
3437static void inheritDefaultTemplateArguments(ASTContext &Context,
3438 TemplateDecl *From,
3439 TemplateDecl *To) {
3440 auto *FromTP = From->getTemplateParameters();
3441 auto *ToTP = To->getTemplateParameters();
3442 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3443
3444 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003445 NamedDecl *FromParam = FromTP->getParam(I);
3446 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003447
Richard Smith559cc692018-07-31 21:01:53 +00003448 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3449 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3450 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3451 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3452 else
3453 inheritDefaultTemplateArgument(
3454 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003455 }
3456}
3457
Richard Smith6de7a242014-07-31 23:46:44 +00003458void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003459 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003460 assert(D && Previous);
3461
3462 switch (D->getKind()) {
3463#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003464#define DECL(TYPE, BASE) \
3465 case Decl::TYPE: \
3466 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003467 break;
3468#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003469 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003470
3471 // If the declaration was visible in one module, a redeclaration of it in
3472 // another module remains visible even if it wouldn't be visible by itself.
3473 //
3474 // FIXME: In this case, the declaration should only be visible if a module
3475 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003476 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003477 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003478 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003479
Richard Smith8346e522015-06-10 01:47:58 +00003480 // If the declaration declares a template, it may inherit default arguments
3481 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003482 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003483 inheritDefaultTemplateArguments(Reader.getContext(),
3484 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003485}
3486
Richard Smithb321ecb2014-05-13 01:15:00 +00003487template<typename DeclT>
3488void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003489 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003490}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003491
Richard Smithb321ecb2014-05-13 01:15:00 +00003492void ASTDeclReader::attachLatestDeclImpl(...) {
3493 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3494}
3495
Douglas Gregor05f10352011-12-17 23:38:30 +00003496void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3497 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003498
3499 switch (D->getKind()) {
3500#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003501#define DECL(TYPE, BASE) \
3502 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003503 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3504 break;
3505#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003506 }
3507}
3508
Richard Smith851072e2014-05-19 20:59:20 +00003509template<typename DeclT>
3510void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3511 D->RedeclLink.markIncomplete();
3512}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003513
Richard Smith851072e2014-05-19 20:59:20 +00003514void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3515 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3516}
3517
3518void ASTReader::markIncompleteDeclChain(Decl *D) {
3519 switch (D->getKind()) {
3520#define ABSTRACT_DECL(TYPE)
3521#define DECL(TYPE, BASE) \
3522 case Decl::TYPE: \
3523 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3524 break;
3525#include "clang/AST/DeclNodes.inc"
3526 }
3527}
3528
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003529/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003530Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003531 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003532 SourceLocation DeclLoc;
3533 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003534 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003535 // Keep track of where we are in the stream, then jump back there
3536 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003537 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003538
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003539 ReadingKindTracker ReadingKind(Read_Decl, *this);
3540
Douglas Gregor1342e842009-07-06 18:54:52 +00003541 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003542 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003543
Sebastian Redl2c373b92010-10-05 15:59:54 +00003544 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003545 ASTRecordReader Record(*this, *Loc.F);
3546 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003547 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003548
Richard Smithdbafb6c2017-06-29 23:23:46 +00003549 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003550 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003551 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003552 case DECL_CONTEXT_LEXICAL:
3553 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003554 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003555 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003556 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003557 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003558 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003559 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003560 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003561 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003562 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003563 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003564 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003565 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003566 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003567 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003568 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003569 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003570 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003571 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003572 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003573 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003574 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003575 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003576 case DECL_EXPORT:
3577 D = ExportDecl::CreateDeserialized(Context, ID);
3578 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003579 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003580 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003581 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003582 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003583 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003584 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003585 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003586 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003587 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003588 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003589 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003590 break;
Richard Smith151c4562016-12-20 21:35:28 +00003591 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003592 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003593 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003594 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003595 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003596 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003597 case DECL_CONSTRUCTOR_USING_SHADOW:
3598 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3599 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003600 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003601 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003602 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003603 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003604 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003605 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003606 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003607 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003608 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003609 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003610 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003611 break;
Richard Smithbc491202017-02-17 20:05:37 +00003612 case DECL_CXX_DEDUCTION_GUIDE:
3613 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3614 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003615 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003616 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003617 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003618 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003619 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3620 break;
3621 case DECL_CXX_INHERITED_CONSTRUCTOR:
3622 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003623 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003624 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003625 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003626 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003627 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003628 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003629 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003630 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003631 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003632 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003633 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003634 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003635 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003636 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003637 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003638 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003639 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003640 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003641 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003642 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003643 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003644 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003645 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003646 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003647 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003648 case DECL_VAR_TEMPLATE:
3649 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3650 break;
3651 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3652 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3653 break;
3654 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3655 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3656 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003657 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003658 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003659 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003660 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003661 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003662 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003663 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003664 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003665 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003666 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003667 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003668 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003669 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003670 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3671 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003672 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003673 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003674 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003675 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003676 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3677 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003678 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003679 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003680 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003681 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003682 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003683 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003684 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003685 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003686 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003687 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003688 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003689 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003690 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003691 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003692 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003693 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003694 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003695 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003696 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003697 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003698 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003699 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003700 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003701 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003702 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003703 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003704 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003705 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003706 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003707 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003708 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003709 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003710 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003711 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003712 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003713 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003714 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003715 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003716 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003717 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003718 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003719 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003720 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003721 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003722 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003723 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003724 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003725 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003726 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003727 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003728 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003729 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003730 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003731 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003732 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003733 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003734 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003735 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003736 break;
3737 case DECL_BINDING:
3738 D = BindingDecl::CreateDeserialized(Context, ID);
3739 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003740 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003741 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003742 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003743 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003744 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003745 break;
John McCall5e77d762013-04-16 07:28:30 +00003746 case DECL_MS_PROPERTY:
3747 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3748 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003749 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003750 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003751 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003752 case DECL_CXX_BASE_SPECIFIERS:
3753 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003754 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003755 case DECL_CXX_CTOR_INITIALIZERS:
3756 Error("attempt to read a C++ ctor initializer record as a declaration");
3757 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003758 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003759 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003760 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003761 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003762 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003763 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003764 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003765 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003766 case DECL_OMP_DECLARE_REDUCTION:
3767 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3768 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003769 case DECL_OMP_CAPTUREDEXPR:
3770 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003771 break;
Nico Weber66220292016-03-02 17:28:48 +00003772 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003773 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003774 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003775 case DECL_PRAGMA_DETECT_MISMATCH:
3776 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003777 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003778 break;
Michael Han84324352013-02-22 17:15:32 +00003779 case DECL_EMPTY:
3780 D = EmptyDecl::CreateDeserialized(Context, ID);
3781 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003782 case DECL_OBJC_TYPE_PARAM:
3783 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3784 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003785 }
Chris Lattner487412d2009-04-27 05:27:42 +00003786
Sebastian Redlb3298c32010-08-18 23:56:48 +00003787 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003788 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003789 // Set the DeclContext before doing any deserialization, to make sure internal
3790 // calls to Decl::getASTContext() by Decl's methods will find the
3791 // TranslationUnitDecl without crashing.
3792 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003793 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003794
3795 // If this declaration is also a declaration context, get the
3796 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003797 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003798 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003799 if (Offsets.first &&
3800 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3801 return nullptr;
3802 if (Offsets.second &&
3803 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3804 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003805 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003806 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003807
Douglas Gregordab42432011-08-12 00:15:20 +00003808 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003809 PendingUpdateRecords.push_back(
3810 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003811
Douglas Gregor404cdde2012-01-27 01:47:08 +00003812 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003813 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003814 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3815 // we put the Decl in PendingDefinitions so we can pull the categories here.
3816 if (Class->isThisDeclarationADefinition() ||
3817 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003818 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00003819
Richard Smith13fb8602016-07-15 21:33:46 +00003820 // If we have deserialized a declaration that has a definition the
3821 // AST consumer might need to know about, queue it.
3822 // We don't pass it to the consumer immediately because we may be in recursive
3823 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003824 PotentiallyInterestingDecls.push_back(
3825 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003826
Douglas Gregordab42432011-08-12 00:15:20 +00003827 return D;
3828}
3829
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003830void ASTReader::PassInterestingDeclsToConsumer() {
3831 assert(Consumer);
3832
3833 if (PassingDeclsToConsumer)
3834 return;
3835
3836 // Guard variable to avoid recursively redoing the process of passing
3837 // decls to consumer.
3838 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3839 true);
3840
3841 // Ensure that we've loaded all potentially-interesting declarations
3842 // that need to be eagerly loaded.
3843 for (auto ID : EagerlyDeserializedDecls)
3844 GetDecl(ID);
3845 EagerlyDeserializedDecls.clear();
3846
3847 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003848 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003849 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003850 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3851 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003852 }
3853}
3854
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003855void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003856 // The declaration may have been modified by files later in the chain.
3857 // If this is the case, read the record containing the updates from each file
3858 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003859 serialization::GlobalDeclID ID = Record.ID;
3860 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003861 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003862 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003863
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003864 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00003865
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003866 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003867 auto UpdateOffsets = std::move(UpdI->second);
3868 DeclUpdateOffsets.erase(UpdI);
3869
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003870 // Check if this decl was interesting to the consumer. If we just loaded
3871 // the declaration, then we know it was interesting and we skip the call
3872 // to isConsumerInterestedIn because it is unsafe to call in the
3873 // current ASTReader state.
3874 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003875 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003876 for (auto &FileAndOffset : UpdateOffsets) {
3877 ModuleFile *F = FileAndOffset.first;
3878 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003879 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3880 SavedStreamPosition SavedPosition(Cursor);
3881 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003882 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003883 ASTRecordReader Record(*this, *F);
3884 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003885 (void)RecCode;
3886 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003887
David L. Jonesbe1557a2016-12-21 00:17:49 +00003888 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3889 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003890 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003891
3892 // We might have made this declaration interesting. If so, remember that
3893 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003894 if (!WasInteresting &&
3895 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
3896 PotentiallyInterestingDecls.push_back(
3897 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003898 WasInteresting = true;
3899 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003900 }
3901 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00003902 // Add the lazy specializations to the template.
3903 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3904 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3905 "Must not have pending specializations");
3906 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3907 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3908 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3909 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3910 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3911 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
3912 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00003913
3914 // Load the pending visible updates for this decl context, if it has any.
3915 auto I = PendingVisibleUpdates.find(ID);
3916 if (I != PendingVisibleUpdates.end()) {
3917 auto VisibleUpdates = std::move(I->second);
3918 PendingVisibleUpdates.erase(I);
3919
3920 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003921 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00003922 Lookups[DC].Table.add(
3923 Update.Mod, Update.Data,
3924 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3925 DC->setHasExternalVisibleStorage(true);
3926 }
Chris Lattner487412d2009-04-27 05:27:42 +00003927}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003928
Richard Smithd61d4ac2015-08-22 20:13:39 +00003929void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3930 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003931 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003932 if (FirstLocal != CanonDecl) {
3933 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3934 ASTDeclReader::attachPreviousDecl(
3935 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3936 CanonDecl);
3937 }
3938
3939 if (!LocalOffset) {
3940 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3941 return;
3942 }
3943
3944 // Load the list of other redeclarations from this module file.
3945 ModuleFile *M = getOwningModuleFile(FirstLocal);
3946 assert(M && "imported decl from no module file");
3947
3948 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3949 SavedStreamPosition SavedPosition(Cursor);
3950 Cursor.JumpToBit(LocalOffset);
3951
3952 RecordData Record;
3953 unsigned Code = Cursor.ReadCode();
3954 unsigned RecCode = Cursor.readRecord(Code, Record);
3955 (void)RecCode;
3956 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3957
3958 // FIXME: We have several different dispatches on decl kind here; maybe
3959 // we should instead generate one loop per kind and dispatch up-front?
3960 Decl *MostRecent = FirstLocal;
3961 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3962 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003963 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3964 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003965 }
Richard Smithc3a53252015-02-28 05:57:02 +00003966 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003967}
3968
3969namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003970
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003971 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003972 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003973 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003974 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003975 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003976 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003977 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003978 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003979 serialization::GlobalDeclID InterfaceID;
3980 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00003981
Douglas Gregor404cdde2012-01-27 01:47:08 +00003982 void add(ObjCCategoryDecl *Cat) {
3983 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003984 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003985 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00003986
Douglas Gregor404cdde2012-01-27 01:47:08 +00003987 // Check for duplicate categories.
3988 if (Cat->getDeclName()) {
3989 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00003990 if (Existing &&
3991 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003992 != Reader.getOwningModuleFile(Cat)) {
3993 // FIXME: We should not warn for duplicates in diamond:
3994 //
3995 // MT //
3996 // / \ //
3997 // ML MR //
3998 // \ / //
3999 // MB //
4000 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004001 // If there are duplicates in ML/MR, there will be warning when
4002 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004003 // importing.
4004 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4005 << Interface->getDeclName() << Cat->getDeclName();
4006 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4007 } else if (!Existing) {
4008 // Record this category.
4009 Existing = Cat;
4010 }
4011 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004012
Douglas Gregor404cdde2012-01-27 01:47:08 +00004013 // Add this category to the end of the chain.
4014 if (Tail)
4015 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4016 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004017 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004018 Tail = Cat;
4019 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004020
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004021 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004022 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004023 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004024 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4025 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004026 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004027 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4028 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004029 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004030 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004031 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004032 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004033
Douglas Gregor404cdde2012-01-27 01:47:08 +00004034 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004035 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004036 }
4037 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004038
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004039 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004040 // If we've loaded all of the category information we care about from
4041 // this module file, we're done.
4042 if (M.Generation <= PreviousGeneration)
4043 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004044
4045 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004046 // module file. If there is no such mapping, we'll find nothing here
4047 // (or in any module it imports).
4048 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4049 if (!LocalID)
4050 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004051
Douglas Gregor404cdde2012-01-27 01:47:08 +00004052 // Perform a binary search to find the local redeclarations for this
4053 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004054 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004055 const ObjCCategoriesInfo *Result
4056 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004057 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004058 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004059 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4060 Result->DefinitionID != LocalID) {
4061 // We didn't find anything. If the class definition is in this module
4062 // file, then the module files it depends on cannot have any categories,
4063 // so suppress further lookup.
4064 return Reader.isDeclIDFromModule(InterfaceID, M);
4065 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004066
Douglas Gregor404cdde2012-01-27 01:47:08 +00004067 // We found something. Dig out all of the categories.
4068 unsigned Offset = Result->Offset;
4069 unsigned N = M.ObjCCategories[Offset];
4070 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4071 for (unsigned I = 0; I != N; ++I)
4072 add(cast_or_null<ObjCCategoryDecl>(
4073 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4074 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004075 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004076 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004077
4078} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004079
Douglas Gregor404cdde2012-01-27 01:47:08 +00004080void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4081 ObjCInterfaceDecl *D,
4082 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004083 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004084 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004085 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004086}
Douglas Gregordab42432011-08-12 00:15:20 +00004087
Richard Smithd6db68c2014-08-07 20:58:41 +00004088template<typename DeclT, typename Fn>
4089static void forAllLaterRedecls(DeclT *D, Fn F) {
4090 F(D);
4091
4092 // Check whether we've already merged D into its redeclaration chain.
4093 // MostRecent may or may not be nullptr if D has not been merged. If
4094 // not, walk the merged redecl chain and see if it's there.
4095 auto *MostRecent = D->getMostRecentDecl();
4096 bool Found = false;
4097 for (auto *Redecl = MostRecent; Redecl && !Found;
4098 Redecl = Redecl->getPreviousDecl())
4099 Found = (Redecl == D);
4100
4101 // If this declaration is merged, apply the functor to all later decls.
4102 if (Found) {
4103 for (auto *Redecl = MostRecent; Redecl != D;
4104 Redecl = Redecl->getPreviousDecl())
4105 F(Redecl);
4106 }
4107}
4108
Vassil Vassilev7d264622017-06-30 22:40:17 +00004109void ASTDeclReader::UpdateDecl(Decl *D,
4110 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004111 while (Record.getIdx() < Record.size()) {
4112 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004113 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004114 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004115 // FIXME: If we also have an update record for instantiating the
4116 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004117 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004118 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004119 // FIXME: We should call addHiddenDecl instead, to add the member
4120 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004121 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004122 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004123 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004124
4125 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004126 // It will be added to the template's lazy specialization set.
4127 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004128 break;
4129
4130 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004131 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004132
Douglas Gregor540fd812012-01-09 18:07:24 +00004133 // Each module has its own anonymous namespace, which is disjoint from
4134 // any other module's anonymous namespaces, so don't attach the anonymous
4135 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004136 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004137 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004138 TU->setAnonymousNamespace(Anon);
4139 else
4140 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4141 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004142 break;
4143 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004144
Richard Smith891fc7f2017-12-05 01:31:47 +00004145 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004146 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004147 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4148 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004149 uint64_t Val = Record.readInt();
4150 if (Val && !VD->getInit()) {
4151 VD->setInit(Record.readExpr());
4152 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4153 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4154 Eval->CheckedICE = true;
4155 Eval->IsICE = Val == 3;
4156 }
4157 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004158 break;
Richard Smith05a21352017-06-22 22:18:46 +00004159 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004160
Richard Smith891fc7f2017-12-05 01:31:47 +00004161 case UPD_CXX_POINT_OF_INSTANTIATION: {
4162 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004163 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004164 VTSD->setPointOfInstantiation(POI);
4165 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4166 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4167 } else {
4168 auto *FD = cast<FunctionDecl>(D);
4169 if (auto *FTSInfo = FD->TemplateOrSpecialization
4170 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4171 FTSInfo->setPointOfInstantiation(POI);
4172 else
4173 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4174 ->setPointOfInstantiation(POI);
4175 }
4176 break;
4177 }
4178
John McCall32791cc2016-01-06 22:34:54 +00004179 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004180 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004181
4182 // We have to read the default argument regardless of whether we use it
4183 // so that hypothetical further update records aren't messed up.
4184 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004185 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004186
4187 // Only apply the update if the parameter still has an uninstantiated
4188 // default argument.
4189 if (Param->hasUninstantiatedDefaultArg())
4190 Param->setDefaultArg(DefaultArg);
4191 break;
4192 }
4193
Richard Smith4b054b22016-08-24 21:25:37 +00004194 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004195 auto *FD = cast<FieldDecl>(D);
4196 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004197
4198 // Only apply the update if the field still has an uninstantiated
4199 // default member initializer.
4200 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4201 if (DefaultInit)
4202 FD->setInClassInitializer(DefaultInit);
4203 else
4204 // Instantiation failed. We can get here if we serialized an AST for
4205 // an invalid program.
4206 FD->removeInClassInitializer();
4207 }
4208 break;
4209 }
4210
Richard Smith4d235792014-08-07 18:53:08 +00004211 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004212 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004213 if (Reader.PendingBodies[FD]) {
4214 // FIXME: Maybe check for ODR violations.
4215 // It's safe to stop now because this update record is always last.
4216 return;
4217 }
4218
David L. Jonesbe1557a2016-12-21 00:17:49 +00004219 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004220 // Maintain AST consistency: any later redeclarations of this function
4221 // are inline if this one is. (We might have merged another declaration
4222 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004223 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4224 FD->setImplicitlyInline();
4225 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004226 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004227 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004228 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004229 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004230 break;
4231 }
4232
Richard Smithcd45dbc2014-04-19 03:48:30 +00004233 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4234 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004235 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004236 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004237 OldDD && (OldDD->Definition != RD ||
4238 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004239 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004240 RD->setArgPassingRestrictions(
4241 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004242 ReadCXXRecordDefinition(RD, /*Update*/true);
4243
Richard Smithcd45dbc2014-04-19 03:48:30 +00004244 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004245 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004246 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004247 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004248 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004249 }
4250
David L. Jonesbe1557a2016-12-21 00:17:49 +00004251 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004252 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004253 if (MemberSpecializationInfo *MSInfo =
4254 RD->getMemberSpecializationInfo()) {
4255 MSInfo->setTemplateSpecializationKind(TSK);
4256 MSInfo->setPointOfInstantiation(POI);
4257 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004258 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004259 Spec->setTemplateSpecializationKind(TSK);
4260 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004261
David L. Jonesbe1557a2016-12-21 00:17:49 +00004262 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004263 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004264 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004265 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004266 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004267 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004268 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004269
4270 // FIXME: If we already have a partial specialization set,
4271 // check that it matches.
4272 if (!Spec->getSpecializedTemplateOrPartial()
4273 .is<ClassTemplatePartialSpecializationDecl *>())
4274 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004275 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004276 }
4277
David L. Jonesbe1557a2016-12-21 00:17:49 +00004278 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004279 RD->setLocation(ReadSourceLocation());
4280 RD->setLocStart(ReadSourceLocation());
4281 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004282
David L. Jonesbe1557a2016-12-21 00:17:49 +00004283 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004284 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004285 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004286 // If the declaration already has attributes, we assume that some other
4287 // AST file already loaded them.
4288 if (!D->hasAttrs())
4289 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004290 }
4291 break;
4292 }
4293
Richard Smithf8134002015-03-10 01:41:22 +00004294 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4295 // Set the 'operator delete' directly to avoid emitting another update
4296 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004297 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004298 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004299 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004300 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004301 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004302 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004303 First->OperatorDeleteThisArg = ThisArg;
4304 }
Richard Smithf8134002015-03-10 01:41:22 +00004305 break;
4306 }
4307
Richard Smith564417a2014-03-20 21:47:22 +00004308 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004309 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004310 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004311 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004312
4313 // Update this declaration's exception specification, if needed.
4314 auto *FD = cast<FunctionDecl>(D);
4315 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4316 // FIXME: If the exception specification is already present, check that it
4317 // matches.
4318 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004319 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004320 FPT->getReturnType(), FPT->getParamTypes(),
4321 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004322
4323 // When we get to the end of deserializing, see if there are other decls
4324 // that we need to propagate this exception specification onto.
4325 Reader.PendingExceptionSpecUpdates.insert(
4326 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004327 }
Richard Smith564417a2014-03-20 21:47:22 +00004328 break;
4329 }
4330
Richard Smith1fa5d642013-05-11 05:45:24 +00004331 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004332 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004333 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004334 Reader.PendingDeducedTypeUpdates.insert(
4335 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004336 break;
4337 }
Eli Friedman276dd182013-09-05 00:02:25 +00004338
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004339 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004340 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004341 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004342 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004343
4344 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004345 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4346 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004347 break;
4348
4349 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004350 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4351 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004352 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004353
Alexey Bataev97720002014-11-11 04:05:39 +00004354 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004355 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4356 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004357 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004358
Alex Denisovfde64952015-06-26 05:28:36 +00004359 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004360 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004361 auto *Exported = cast<NamedDecl>(D);
4362 if (auto *TD = dyn_cast<TagDecl>(Exported))
4363 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004364 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004365 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004366 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4367 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004368 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4369 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004370 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004371 // If Owner is made visible at some later point, make this declaration
4372 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004373 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004374 } else {
4375 // The declaration is now visible.
Richard Smith90dc5252017-06-23 01:04:34 +00004376 Exported->setVisibleDespiteOwningModule();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004377 }
4378 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004379 }
Alex Denisovfde64952015-06-26 05:28:36 +00004380
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004381 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004382 case UPD_ADDED_ATTR_TO_RECORD:
4383 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004384 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004385 assert(Attrs.size() == 1);
4386 D->addAttr(Attrs[0]);
4387 break;
4388 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004389 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004390}