blob: 1c56cdbf9489bd218a13fc4dd37de16cc76e3654 [file] [log] [blame]
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001//===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner487412d2009-04-27 05:27:42 +00006//
7//===----------------------------------------------------------------------===//
8//
Sebastian Redl2c499f62010-08-18 23:56:43 +00009// This file implements the ASTReader::ReadDeclRecord method, which is the
Chris Lattner487412d2009-04-27 05:27:42 +000010// entrypoint for loading a decl.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000014#include "ASTCommon.h"
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +000015#include "ASTReaderInternals.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/AttrIterator.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclCXX.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000022#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclOpenMP.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/DeclVisitor.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000027#include "clang/AST/DeclarationName.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "clang/AST/Expr.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000029#include "clang/AST/ExternalASTSource.h"
30#include "clang/AST/LambdaCapture.h"
31#include "clang/AST/NestedNameSpecifier.h"
Kelvin Li1408f912018-09-26 04:28:39 +000032#include "clang/AST/OpenMPClause.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000033#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"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000050#include "clang/Serialization/ASTBitCodes.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000051#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000052#include "clang/Serialization/ContinuousRangeMap.h"
53#include "clang/Serialization/Module.h"
54#include "llvm/ADT/DenseMap.h"
55#include "llvm/ADT/FoldingSet.h"
56#include "llvm/ADT/STLExtras.h"
57#include "llvm/ADT/SmallPtrSet.h"
58#include "llvm/ADT/SmallVector.h"
59#include "llvm/ADT/iterator_range.h"
60#include "llvm/Bitcode/BitstreamReader.h"
61#include "llvm/Support/Casting.h"
62#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000063#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000064#include <algorithm>
65#include <cassert>
66#include <cstdint>
67#include <cstring>
68#include <string>
69#include <utility>
Hans Wennborgdcfba332015-10-06 23:40:43 +000070
Chris Lattner487412d2009-04-27 05:27:42 +000071using namespace clang;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000072using namespace serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000073
74//===----------------------------------------------------------------------===//
75// Declaration deserialization
76//===----------------------------------------------------------------------===//
77
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000078namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000079
Sebastian Redlb3298c32010-08-18 23:56:48 +000080 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000081 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000082 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000083 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000084 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000085 const SourceLocation ThisDeclLoc;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000086
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000087 using RecordData = ASTReader::RecordData;
88
Richard Smith600adef2018-07-04 02:25:38 +000089 TypeID DeferredTypeID = 0;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000090 unsigned AnonymousDeclNumber;
91 GlobalDeclID NamedDeclForTagDecl = 0;
92 IdentifierInfo *TypedefNameForLinkage = nullptr;
93
94 bool HasPendingBody = false;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +000095
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000096 ///A flag to carry the information for a decl from the entity is
Vassil Vassilev928c8252016-04-28 14:13:28 +000097 /// used. We use it to delay the marking of the canonical decl as used until
98 /// the entire declaration is deserialized and merged.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000099 bool IsDeclMarkedUsed = false;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000100
Sebastian Redlc67764e2010-07-22 22:43:28 +0000101 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000102
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000103 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000104 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000105 assert(LocalOffset < Loc.Offset && "offset point after current record");
106 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +0000107 }
108
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000109 uint64_t ReadGlobalOffset() {
110 uint64_t Local = ReadLocalOffset();
111 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +0000112 }
113
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000114 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000115 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000116 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000117
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000118 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000119 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000120 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000121
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000122 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000123 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000124 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000125
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000126 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000127 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000128 }
Richard Smith0b884372015-02-27 00:25:58 +0000129
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000130 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000131 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +0000132 }
133
Richard Smith0b884372015-02-27 00:25:58 +0000134 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000135 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000136 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +0000137 }
138
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000139 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000140 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000141 }
142
143 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000144 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000145 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000146 }
147
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000148 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000149 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000150 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000151
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000152 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000153 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000154 }
155
156 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000157 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000158 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000159
David L. Jonesbe1557a2016-12-21 00:17:49 +0000160 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000161 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000162
163 Module *readModule() {
164 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000165 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000166
Richard Smith2a9e5c52015-02-03 03:32:14 +0000167 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000168 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
169 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000170 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000171 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000172 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000173 void MergeDefinitionData(ObjCInterfaceDecl *D,
174 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000175 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
176 void MergeDefinitionData(ObjCProtocolDecl *D,
177 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000178
Richard Smith600adef2018-07-04 02:25:38 +0000179 static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
180
Richard Smithd08aeb62014-08-28 01:33:39 +0000181 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
182 DeclContext *DC,
183 unsigned Index);
184 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
185 unsigned Index, NamedDecl *D);
186
Richard Smith0144f512015-08-22 02:09:38 +0000187 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000188 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000189 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000190 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000191 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000192
Douglas Gregor022857e2011-12-22 01:48:48 +0000193 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000194 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000195 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000197 /// Retrieve the first ID.
Douglas Gregor022857e2011-12-22 01:48:48 +0000198 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000200 /// Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000201 bool isKeyDecl() const { return IsKeyDecl; }
202
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000203 /// Get a known declaration that this should be merged with, if
Richard Smith5a4737c2015-02-06 02:42:59 +0000204 /// any.
205 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000206 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000207
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000208 /// Class used to capture the result of searching for an existing
Douglas Gregor022857e2011-12-22 01:48:48 +0000209 /// declaration of a specific kind and name, along with the ability
210 /// to update the place where this result was found (the declaration
211 /// chain hanging off an identifier or the DeclContext we searched in)
212 /// if requested.
213 class FindExistingResult {
214 ASTReader &Reader;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000215 NamedDecl *New = nullptr;
216 NamedDecl *Existing = nullptr;
217 bool AddResult = false;
218 unsigned AnonymousDeclNumber = 0;
219 IdentifierInfo *TypedefNameForLinkage = nullptr;
Richard Smithd08aeb62014-08-28 01:33:39 +0000220
Douglas Gregor022857e2011-12-22 01:48:48 +0000221 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000222 FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000223
Richard Smithd08aeb62014-08-28 01:33:39 +0000224 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000225 unsigned AnonymousDeclNumber,
226 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000227 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000228 AnonymousDeclNumber(AnonymousDeclNumber),
229 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000230
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000231 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000232 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
233 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000234 AnonymousDeclNumber(Other.AnonymousDeclNumber),
235 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000236 Other.AddResult = false;
237 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000238
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000239 FindExistingResult &operator=(FindExistingResult &&) = delete;
Douglas Gregor022857e2011-12-22 01:48:48 +0000240 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000241
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000242 /// Suppress the addition of this result into the known set of
Douglas Gregor2009cee2012-01-03 22:46:00 +0000243 /// names.
244 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000245
Douglas Gregor022857e2011-12-22 01:48:48 +0000246 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000247
Douglas Gregor022857e2011-12-22 01:48:48 +0000248 template<typename T>
249 operator T*() const { return dyn_cast_or_null<T>(Existing); }
250 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000251
Richard Smith8a639892015-01-24 01:07:20 +0000252 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
253 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000254 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000255
Chris Lattner487412d2009-04-27 05:27:42 +0000256 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000257 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
258 ASTReader::RecordLocation Loc,
259 DeclID thisDeclID, SourceLocation ThisDeclLoc)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000260 : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
261 ThisDeclLoc(ThisDeclLoc) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000262
Vassil Vassilev7d264622017-06-30 22:40:17 +0000263 template <typename T> static
264 void AddLazySpecializations(T *D,
265 SmallVectorImpl<serialization::DeclID>& IDs) {
266 if (IDs.empty())
267 return;
268
269 // FIXME: We should avoid this pattern of getting the ASTContext.
270 ASTContext &C = D->getASTContext();
271
272 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
273
274 if (auto &Old = LazySpecializations) {
275 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Fangrui Song55fab262018-09-26 22:16:28 +0000276 llvm::sort(IDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +0000277 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
278 }
279
280 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
281 *Result = IDs.size();
282 std::copy(IDs.begin(), IDs.end(), Result + 1);
283
284 LazySpecializations = Result;
285 }
286
Richard Smithb321ecb2014-05-13 01:15:00 +0000287 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000288 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
289 static Decl *getMostRecentDeclImpl(...);
290 static Decl *getMostRecentDecl(Decl *D);
291
292 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000293 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000294 Redeclarable<DeclT> *D, Decl *Previous,
295 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000296 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000297 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
298 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000299
300 template <typename DeclT>
301 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
302 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000303 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000304
Richard Smith851072e2014-05-19 20:59:20 +0000305 template <typename DeclT>
306 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
307 static void markIncompleteDeclChainImpl(...);
308
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000309 /// Determine whether this declaration has a pending body.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000310 bool hasPendingBody() const { return HasPendingBody; }
311
David Blaikieac4345c2017-02-12 18:45:31 +0000312 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000313 void Visit(Decl *D);
314
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000315 void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000316
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000317 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
318 ObjCCategoryDecl *Next) {
319 Cat->NextClassCategory = Next;
320 }
321
Chris Lattner487412d2009-04-27 05:27:42 +0000322 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000323 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000324 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000325 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
326 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000327 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000328 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000329 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
330 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000331 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000332 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000333 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000334 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000335 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000336 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000337 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000338 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
339 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
340 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
341 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
342 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000343 ClassTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000344
Richard Smith1d209d02013-05-23 01:49:11 +0000345 void VisitClassTemplateSpecializationDecl(
346 ClassTemplateSpecializationDecl *D) {
347 VisitClassTemplateSpecializationDeclImpl(D);
348 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000349
Chris Lattnerca025db2010-05-07 21:43:38 +0000350 void VisitClassTemplatePartialSpecializationDecl(
351 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000352 void VisitClassScopeFunctionSpecializationDecl(
353 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000354 RedeclarableResult
355 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000356
Larisse Voufo39a1e502013-08-06 01:03:05 +0000357 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
358 VisitVarTemplateSpecializationDeclImpl(D);
359 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000360
Larisse Voufo39a1e502013-08-06 01:03:05 +0000361 void VisitVarTemplatePartialSpecializationDecl(
362 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000363 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000364 void VisitValueDecl(ValueDecl *VD);
365 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000366 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000367 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000368 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000369 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000370 void VisitCXXMethodDecl(CXXMethodDecl *D);
371 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
372 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
373 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000374 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000375 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000376 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000377 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
378 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000379 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
380 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000381 void VisitDecompositionDecl(DecompositionDecl *DD);
382 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000383 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000384 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000385 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000386 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000387 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000388 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000389 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000390 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000391 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000392 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000393 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000394 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000395 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000396 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000397 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000398 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000399 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000400 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000401 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000402 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
403 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000404 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000405 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000406 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000407
Chris Lattner487412d2009-04-27 05:27:42 +0000408 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000409
410 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000411 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000412
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000413 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000414 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
415 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000416
417 template<typename T>
418 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000419 RedeclarableResult &Redecl,
420 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000421
Richard Smith0b87e072013-10-07 08:02:11 +0000422 template<typename T>
423 void mergeMergeable(Mergeable<T> *D);
424
Richard Smithf17fdbd2014-04-24 02:25:27 +0000425 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
426 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000427 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000428
Douglas Gregor85f3f952015-07-07 03:57:15 +0000429 ObjCTypeParamList *ReadObjCTypeParamList();
430
Alexis Hunted053252010-05-30 07:21:58 +0000431 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000432 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000433 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000434 void VisitObjCContainerDecl(ObjCContainerDecl *D);
435 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
436 void VisitObjCIvarDecl(ObjCIvarDecl *D);
437 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
438 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000439 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
440 void VisitObjCImplDecl(ObjCImplDecl *D);
441 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
442 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
443 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
444 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
445 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000446 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000447 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Michael Kruse251e1482019-02-01 20:25:04 +0000448 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
Kelvin Li1408f912018-09-26 04:28:39 +0000449 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000450 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000451 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000452
453} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000454
Richard Smith86dfc1e2015-06-18 22:07:00 +0000455namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000456
Richard Smith86dfc1e2015-06-18 22:07:00 +0000457/// Iterator over the redeclarations of a declaration that have already
458/// been merged into the same redeclaration chain.
459template<typename DeclT>
460class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000461 DeclT *Start;
462 DeclT *Canonical = nullptr;
463 DeclT *Current = nullptr;
464
Richard Smith86dfc1e2015-06-18 22:07:00 +0000465public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000466 MergedRedeclIterator() = default;
467 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000468
469 DeclT *operator*() { return Current; }
470
471 MergedRedeclIterator &operator++() {
472 if (Current->isFirstDecl()) {
473 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000474 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000475 } else
476 Current = Current->getPreviousDecl();
477
478 // If we started in the merged portion, we'll reach our start position
479 // eventually. Otherwise, we'll never reach it, but the second declaration
480 // we reached was the canonical declaration, so stop when we see that one
481 // again.
482 if (Current == Start || Current == Canonical)
483 Current = nullptr;
484 return *this;
485 }
486
487 friend bool operator!=(const MergedRedeclIterator &A,
488 const MergedRedeclIterator &B) {
489 return A.Current != B.Current;
490 }
491};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000492
493} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000494
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000495template <typename DeclT>
496static llvm::iterator_range<MergedRedeclIterator<DeclT>>
497merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000498 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
499 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000500}
501
Sebastian Redlb3298c32010-08-18 23:56:48 +0000502uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000503 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000504}
505
David Blaikieac4345c2017-02-12 18:45:31 +0000506void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000507 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000508 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000509 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Erich Keane9c665062018-08-01 21:02:40 +0000510 CD->setNumCtorInitializers(Record.readInt());
511 if (CD->getNumCtorInitializers())
David Blaikieac4345c2017-02-12 18:45:31 +0000512 CD->CtorInitializers = ReadGlobalOffset();
513 }
514 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000515 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
516 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000517}
518
Sebastian Redlb3298c32010-08-18 23:56:48 +0000519void ASTDeclReader::Visit(Decl *D) {
520 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000521
Vassil Vassilev928c8252016-04-28 14:13:28 +0000522 // At this point we have deserialized and merged the decl and it is safe to
523 // update its canonical decl to signal that the entire entity is used.
524 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
525 IsDeclMarkedUsed = false;
526
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000527 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000528 if (auto *TInfo = DD->getTypeSourceInfo())
529 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000530 }
531
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000532 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000533 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000534 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000535
536 // If this is a tag declaration with a typedef name for linkage, it's safe
537 // to load that typedef now.
538 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000539 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
540 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000541 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000542 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000543 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000544 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000545 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000546 // We only read it if FD doesn't already have a body (e.g., from another
547 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000548 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000549 if (Record.readInt())
550 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000551 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000552}
553
Sebastian Redlb3298c32010-08-18 23:56:48 +0000554void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000555 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
556 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000557 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000558 // parameter or of a parameter of a function template immediately. These
559 // entities might be used in the formulation of its DeclContext (for
560 // example, a function parameter can be used in decltype() in trailing
561 // return type of the function). Use the translation unit DeclContext as a
562 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000563 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
564 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000565 if (!LexicalDCIDForTemplateParmDecl)
566 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000567 Reader.addPendingDeclContextInfo(D,
568 SemaDCIDForTemplateParmDecl,
569 LexicalDCIDForTemplateParmDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000570 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000571 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000572 auto *SemaDC = ReadDeclAs<DeclContext>();
573 auto *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000574 if (!LexicalDC)
575 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000576 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000577 // Avoid calling setLexicalDeclContext() directly because it uses
578 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000579 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
580 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000581 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000582 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000583 D->setInvalidDecl(Record.readInt());
584 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000585 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000586 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000587 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
588 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000589 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000590 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000591 D->setImplicit(Record.readInt());
592 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000593 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000594 D->setReferenced(Record.readInt());
595 D->setTopLevelDeclInObjCContainer(Record.readInt());
596 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000597 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000598 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000599
Douglas Gregorcf68c582011-12-01 22:20:10 +0000600 // Determine whether this declaration is part of a (sub)module. If so, it
601 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000602 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000603 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000604 D->setModuleOwnershipKind(
605 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
606 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000607 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000608
Richard Smith90dc5252017-06-23 01:04:34 +0000609 if (ModulePrivate) {
610 // Module-private declarations are never visible, so there is no work to
611 // do.
Richard Smith42413142015-05-15 20:05:43 +0000612 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
613 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000614 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000615 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000616 // Mark the declaration as visible when its owning module becomes visible.
617 if (Owner->NameVisibility == Module::AllVisible)
618 D->setVisibleDespiteOwningModule();
619 else
Richard Smith42413142015-05-15 20:05:43 +0000620 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000621 }
Richard Smithd19389a2017-07-05 07:47:11 +0000622 } else if (ModulePrivate) {
623 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000624 }
Chris Lattner487412d2009-04-27 05:27:42 +0000625}
626
Nico Weber66220292016-03-02 17:28:48 +0000627void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
628 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000629 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000630 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000631 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000632 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
633 D->getTrailingObjects<char>()[Arg.size()] = '\0';
634}
635
Nico Webercbbaeb12016-03-02 19:28:54 +0000636void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
637 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000638 D->setLocation(ReadSourceLocation());
639 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000640 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
641 D->getTrailingObjects<char>()[Name.size()] = '\0';
642
643 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000644 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000645 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
646 Value.size());
647 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
648}
649
Sebastian Redlb3298c32010-08-18 23:56:48 +0000650void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000651 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000652}
653
Sebastian Redlb3298c32010-08-18 23:56:48 +0000654void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000655 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000656 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000657 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000658}
659
Sebastian Redlb3298c32010-08-18 23:56:48 +0000660void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000661 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000662 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000663 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000664 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000665}
666
Richard Smith43ccec8e2014-08-26 03:52:16 +0000667ASTDeclReader::RedeclarableResult
668ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000669 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000670 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000671 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000672 if (Record.readInt()) { // isModed
673 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000674 TD->setModedTypeSourceInfo(TInfo, modedT);
675 } else
676 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000677 // Read and discard the declaration for which this is a typedef name for
678 // linkage, if it exists. We cannot rely on our type to pull in this decl,
679 // because it might have been merged with a type from another module and
680 // thus might not refer to our version of the declaration.
681 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000682 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000683}
684
685void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000686 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
687 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000688}
689
Richard Smithdda56e42011-04-15 14:24:37 +0000690void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000691 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000692 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000693 // Merged when we merge the template.
694 TD->setDescribedAliasTemplate(Template);
695 else
696 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000697}
698
Richard Smith1d209d02013-05-23 01:49:11 +0000699ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000700 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000701 VisitTypeDecl(TD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000702
David L. Jonesbe1557a2016-12-21 00:17:49 +0000703 TD->IdentifierNamespace = Record.readInt();
704 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000705 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000706 TD->setCompleteDefinition(Record.readInt());
707 TD->setEmbeddedInDeclarator(Record.readInt());
708 TD->setFreeStanding(Record.readInt());
709 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000710 TD->setBraceRange(ReadSourceRange());
Fangrui Song6907ce22018-07-30 19:24:48 +0000711
David L. Jonesbe1557a2016-12-21 00:17:49 +0000712 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000713 case 0:
714 break;
715 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000716 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000717 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000718 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000719 break;
720 }
721 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000722 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000723 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000724 break;
Richard Smith70d58502014-08-30 00:04:23 +0000725 default:
726 llvm_unreachable("unexpected tag info kind");
727 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000728
Richard Smithcd45dbc2014-04-19 03:48:30 +0000729 if (!isa<CXXRecordDecl>(TD))
730 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000731 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000732}
733
Sebastian Redlb3298c32010-08-18 23:56:48 +0000734void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000735 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000736 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000737 ED->setIntegerTypeSourceInfo(TI);
738 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000739 ED->setIntegerType(Record.readType());
740 ED->setPromotionType(Record.readType());
741 ED->setNumPositiveBits(Record.readInt());
742 ED->setNumNegativeBits(Record.readInt());
Erich Keanef92f31c2018-08-01 20:48:16 +0000743 ED->setScoped(Record.readInt());
744 ED->setScopedUsingClassTag(Record.readInt());
745 ED->setFixed(Record.readInt());
Richard Smith4b38ded2012-03-14 23:13:10 +0000746
Erich Keanef92f31c2018-08-01 20:48:16 +0000747 ED->setHasODRHash(true);
Richard Trieuab4d7302018-07-25 22:52:05 +0000748 ED->ODRHash = Record.readInt();
749
Richard Smith01a73372013-10-15 22:02:41 +0000750 // If this is a definition subject to the ODR, and we already have a
751 // definition, merge this one into it.
Erich Keanef92f31c2018-08-01 20:48:16 +0000752 if (ED->isCompleteDefinition() &&
Richard Smith01a73372013-10-15 22:02:41 +0000753 Reader.getContext().getLangOpts().Modules &&
754 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000755 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
756 if (!OldDef) {
757 // This is the first time we've seen an imported definition. Look for a
758 // local definition before deciding that we are the first definition.
759 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
760 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
761 OldDef = D;
762 break;
763 }
764 }
765 }
766 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000767 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
Erich Keanef92f31c2018-08-01 20:48:16 +0000768 ED->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +0000769 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000770 if (OldDef->getODRHash() != ED->getODRHash())
771 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000772 } else {
773 OldDef = ED;
774 }
775 }
776
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000777 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
778 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000779 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000780 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
781 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
782 }
Chris Lattner487412d2009-04-27 05:27:42 +0000783}
784
Richard Smith1d209d02013-05-23 01:49:11 +0000785ASTDeclReader::RedeclarableResult
786ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
787 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000788 RD->setHasFlexibleArrayMember(Record.readInt());
789 RD->setAnonymousStructOrUnion(Record.readInt());
790 RD->setHasObjectMember(Record.readInt());
791 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000792 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
793 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
794 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000795 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000796 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000797 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000798}
799
Sebastian Redlb3298c32010-08-18 23:56:48 +0000800void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000801 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000802 // For function declarations, defer reading the type in case the function has
803 // a deduced return type that references an entity declared within the
804 // function.
805 if (isa<FunctionDecl>(VD))
806 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
807 else
808 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000809}
810
Sebastian Redlb3298c32010-08-18 23:56:48 +0000811void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000812 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000813 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000814 ECD->setInitExpr(Record.readExpr());
815 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000816 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000817}
818
Sebastian Redlb3298c32010-08-18 23:56:48 +0000819void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000820 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000821 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000822 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000823 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000824 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000825 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000826 }
Richard Smithc23d7342018-06-29 20:46:25 +0000827 QualType TSIType = Record.readType();
828 DD->setTypeSourceInfo(
829 TSIType.isNull() ? nullptr
830 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000831}
832
Sebastian Redlb3298c32010-08-18 23:56:48 +0000833void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000834 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000835 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000836
Richard Smith600adef2018-07-04 02:25:38 +0000837 // Attach a type to this function. Use the real type if possible, but fall
838 // back to the type as written if it involves a deduced return type.
839 if (FD->getTypeSourceInfo() &&
840 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
841 ->getReturnType()->getContainedAutoType()) {
842 // We'll set up the real type in Visit, once we've finished loading the
843 // function.
844 FD->setType(FD->getTypeSourceInfo()->getType());
Richard Smitha62d1982018-08-03 01:00:01 +0000845 Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
Richard Smith600adef2018-07-04 02:25:38 +0000846 } else {
847 FD->setType(Reader.GetType(DeferredTypeID));
Richard Smith600adef2018-07-04 02:25:38 +0000848 }
Richard Smitha62d1982018-08-03 01:00:01 +0000849 DeferredTypeID = 0;
Richard Smith600adef2018-07-04 02:25:38 +0000850
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000851 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000852 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000853
Douglas Gregorb2585692012-01-04 17:13:46 +0000854 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
855 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000856
Erich Keane9c665062018-08-01 21:02:40 +0000857 FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
858 FD->setInlineSpecified(Record.readInt());
859 FD->setImplicitlyInline(Record.readInt());
860 FD->setExplicitSpecified(Record.readInt());
861 FD->setVirtualAsWritten(Record.readInt());
862 FD->setPure(Record.readInt());
863 FD->setHasInheritedPrototype(Record.readInt());
864 FD->setHasWrittenPrototype(Record.readInt());
865 FD->setDeletedAsWritten(Record.readInt());
866 FD->setTrivial(Record.readInt());
867 FD->setTrivialForCall(Record.readInt());
868 FD->setDefaulted(Record.readInt());
869 FD->setExplicitlyDefaulted(Record.readInt());
870 FD->setHasImplicitReturnZero(Record.readInt());
871 FD->setConstexpr(Record.readInt());
872 FD->setUsesSEHTry(Record.readInt());
873 FD->setHasSkippedBody(Record.readInt());
874 FD->setIsMultiVersion(Record.readInt());
875 FD->setLateTemplateParsed(Record.readInt());
876
877 FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000878 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000879
Richard Trieue6caa262017-12-23 00:41:01 +0000880 FD->ODRHash = Record.readInt();
Erich Keane9c665062018-08-01 21:02:40 +0000881 FD->setHasODRHash(true);
Richard Trieue6caa262017-12-23 00:41:01 +0000882
David L. Jonesbe1557a2016-12-21 00:17:49 +0000883 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000884 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000885 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000886 break;
887 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000888 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000889 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000890 break;
891 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000892 auto *InstFD = ReadDeclAs<FunctionDecl>();
893 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000894 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000895 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000896 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000897 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000898 break;
899 }
900 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000901 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
902 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000903
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000904 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000905 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000906 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000907
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000908 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000909 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000910 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000911 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000912 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000913 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000914 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000915 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000916 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000917
918 LAngleLoc = ReadSourceLocation();
919 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000920 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000921
922 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000923
Douglas Gregor4163aca2011-09-09 21:34:22 +0000924 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000925 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000926 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000927 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000928 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000929 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000930 FunctionTemplateSpecializationInfo *FTInfo
931 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
932 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000933 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
934 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000935 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000936 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000937
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000938 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000939 // The template that contains the specializations set. It's not safe to
940 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000941 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000942 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
943 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
944 // FunctionTemplateSpecializationInfo's Profile().
945 // We avoid getASTContext because a decl in the parent hierarchy may
946 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000947 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000948 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000949 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000950 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000951 FunctionTemplateSpecializationInfo *ExistingInfo =
952 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000953 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000954 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
955 else {
956 assert(Reader.getContext().getLangOpts().Modules &&
957 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000958 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000959 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000960 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000961 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000962 }
963 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
964 // Templates.
965 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000966 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000967 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000968 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
969
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000970 // Templates args.
971 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000972 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000973 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000974 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000975 TemplArgs.setLAngleLoc(ReadSourceLocation());
976 TemplArgs.setRAngleLoc(ReadSourceLocation());
977
Douglas Gregor4163aca2011-09-09 21:34:22 +0000978 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000979 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000980 // These are not merged; we don't need to merge redeclarations of dependent
981 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000982 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000983 }
984 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000985
Chris Lattnerca025db2010-05-07 21:43:38 +0000986 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000987 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000988 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000989 Params.reserve(NumParams);
990 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000991 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000992 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000993}
994
Sebastian Redlb3298c32010-08-18 23:56:48 +0000995void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000996 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000997 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000998 // Load the body on-demand. Most clients won't care, because method
999 // definitions rarely show up in headers.
1000 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1001 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001002 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1003 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001004 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001005 MD->setInstanceMethod(Record.readInt());
1006 MD->setVariadic(Record.readInt());
1007 MD->setPropertyAccessor(Record.readInt());
1008 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001009 MD->setOverriding(Record.readInt());
1010 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001011
Erich Keane9b18eca2018-08-01 21:31:08 +00001012 MD->setIsRedeclaration(Record.readInt());
1013 MD->setHasRedeclaration(Record.readInt());
1014 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001015 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001016 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001017
David L. Jonesbe1557a2016-12-21 00:17:49 +00001018 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1019 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001020 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001021 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001022 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1023 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001024 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001025 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001026 Params.reserve(NumParams);
1027 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001028 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001029
Erich Keane9b18eca2018-08-01 21:31:08 +00001030 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001031 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001032 SmallVector<SourceLocation, 16> SelLocs;
1033 SelLocs.reserve(NumStoredSelLocs);
1034 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001035 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001036
1037 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001038}
1039
Douglas Gregor85f3f952015-07-07 03:57:15 +00001040void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001041 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001042
David L. Jonesbe1557a2016-12-21 00:17:49 +00001043 D->Variance = Record.readInt();
1044 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001045 D->VarianceLoc = ReadSourceLocation();
1046 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001047}
1048
Sebastian Redlb3298c32010-08-18 23:56:48 +00001049void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001050 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001051 CD->setAtStartLoc(ReadSourceLocation());
1052 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001053}
1054
Douglas Gregor85f3f952015-07-07 03:57:15 +00001055ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001056 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001057 if (numParams == 0)
1058 return nullptr;
1059
1060 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1061 typeParams.reserve(numParams);
1062 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001063 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001064 if (!typeParam)
1065 return nullptr;
1066
1067 typeParams.push_back(typeParam);
1068 }
1069
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001070 SourceLocation lAngleLoc = ReadSourceLocation();
1071 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001072
1073 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1074 typeParams, rAngleLoc);
1075}
1076
Manman Renec315f12016-09-09 23:48:27 +00001077void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001078 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001079 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001080 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001081
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001082 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001083 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001084
Manman Renec315f12016-09-09 23:48:27 +00001085 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001086 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001087 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1088 Protocols.reserve(NumProtocols);
1089 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001090 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001091 SmallVector<SourceLocation, 16> ProtoLocs;
1092 ProtoLocs.reserve(NumProtocols);
1093 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001094 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001095 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1096 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001097
Manman Renec315f12016-09-09 23:48:27 +00001098 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001099 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001100 Protocols.clear();
1101 Protocols.reserve(NumProtocols);
1102 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001103 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001104 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1105 Reader.getContext());
1106}
1107
1108void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1109 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1110 // FIXME: odr checking?
1111}
1112
Sebastian Redlb3298c32010-08-18 23:56:48 +00001113void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001114 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001115 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001116 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001117 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001118
1119 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001120 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001121 // Read the definition.
1122 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001123
David L. Jonesbe1557a2016-12-21 00:17:49 +00001124 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001125 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1126 if (Canon->Data.getPointer()) {
1127 // If we already have a definition, keep the definition invariant and
1128 // merge the data.
1129 MergeDefinitionData(Canon, std::move(ID->data()));
1130 ID->Data = Canon->Data;
1131 } else {
1132 // Set the definition data of the canonical declaration, so other
1133 // redeclarations will see it.
1134 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001135
Manman Renec315f12016-09-09 23:48:27 +00001136 // We will rebuild this list lazily.
1137 ID->setIvarList(nullptr);
1138 }
Craig Toppera13603a2014-05-22 05:54:18 +00001139
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001140 // Note that we have deserialized a definition.
1141 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001142
Douglas Gregor404cdde2012-01-27 01:47:08 +00001143 // Note that we've loaded this Objective-C class.
1144 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001145 } else {
1146 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001147 }
Chris Lattner487412d2009-04-27 05:27:42 +00001148}
1149
Sebastian Redlb3298c32010-08-18 23:56:48 +00001150void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001151 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001152 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001153 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001154 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001155 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001156 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001157}
1158
Graydon Hoaree0a68352017-06-28 18:36:27 +00001159void ASTDeclReader::ReadObjCDefinitionData(
1160 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001161 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001162 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1163 ProtoRefs.reserve(NumProtoRefs);
1164 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001165 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001166 SmallVector<SourceLocation, 16> ProtoLocs;
1167 ProtoLocs.reserve(NumProtoRefs);
1168 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001169 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001170 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1171 ProtoLocs.data(), Reader.getContext());
1172}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001173
Graydon Hoaree0a68352017-06-28 18:36:27 +00001174void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1175 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1176 // FIXME: odr checking?
1177}
1178
1179void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1180 RedeclarableResult Redecl = VisitRedeclarable(PD);
1181 VisitObjCContainerDecl(PD);
1182 mergeRedeclarable(PD, Redecl);
1183
1184 if (Record.readInt()) {
1185 // Read the definition.
1186 PD->allocateDefinitionData();
1187
1188 ReadObjCDefinitionData(PD->data());
1189
1190 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1191 if (Canon->Data.getPointer()) {
1192 // If we already have a definition, keep the definition invariant and
1193 // merge the data.
1194 MergeDefinitionData(Canon, std::move(PD->data()));
1195 PD->Data = Canon->Data;
1196 } else {
1197 // Set the definition data of the canonical declaration, so other
1198 // redeclarations will see it.
1199 PD->getCanonicalDecl()->Data = PD->Data;
1200 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001201 // Note that we have deserialized a definition.
1202 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001203 } else {
1204 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001205 }
Chris Lattner487412d2009-04-27 05:27:42 +00001206}
1207
Sebastian Redlb3298c32010-08-18 23:56:48 +00001208void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001209 VisitFieldDecl(FD);
1210}
1211
Sebastian Redlb3298c32010-08-18 23:56:48 +00001212void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001213 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001214 CD->setCategoryNameLoc(ReadSourceLocation());
1215 CD->setIvarLBraceLoc(ReadSourceLocation());
1216 CD->setIvarRBraceLoc(ReadSourceLocation());
1217
Douglas Gregor404cdde2012-01-27 01:47:08 +00001218 // Note that this category has been deserialized. We do this before
1219 // deserializing the interface declaration, so that it will consider this
1220 /// category.
1221 Reader.CategoriesDeserialized.insert(CD);
1222
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001223 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001224 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001225 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001226 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001227 ProtoRefs.reserve(NumProtoRefs);
1228 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001229 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001230 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001231 ProtoLocs.reserve(NumProtoRefs);
1232 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001233 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001234 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001235 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001236
1237 // Protocols in the class extension belong to the class.
1238 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1239 CD->ClassInterface->mergeClassExtensionProtocolList(
1240 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1241 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001242}
1243
Sebastian Redlb3298c32010-08-18 23:56:48 +00001244void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001245 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001246 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001247}
1248
Sebastian Redlb3298c32010-08-18 23:56:48 +00001249void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001250 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001251 D->setAtLoc(ReadSourceLocation());
1252 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001253 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001254 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001255 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001256 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001257 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001258 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001259 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001260 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001261 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001262 DeclarationName GetterName = Record.readDeclarationName();
1263 SourceLocation GetterLoc = ReadSourceLocation();
1264 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1265 DeclarationName SetterName = Record.readDeclarationName();
1266 SourceLocation SetterLoc = ReadSourceLocation();
1267 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001268 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1269 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1270 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001271}
1272
Sebastian Redlb3298c32010-08-18 23:56:48 +00001273void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001274 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001275 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001276}
1277
Sebastian Redlb3298c32010-08-18 23:56:48 +00001278void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001279 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001280 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001281}
1282
Sebastian Redlb3298c32010-08-18 23:56:48 +00001283void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001284 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001285 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1286 D->SuperLoc = ReadSourceLocation();
1287 D->setIvarLBraceLoc(ReadSourceLocation());
1288 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001289 D->setHasNonZeroConstructors(Record.readInt());
1290 D->setHasDestructors(Record.readInt());
1291 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001292 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001293 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001294}
1295
Sebastian Redlb3298c32010-08-18 23:56:48 +00001296void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001297 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001298 D->setAtLoc(ReadSourceLocation());
1299 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1300 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1301 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001302 D->setGetterCXXConstructor(Record.readExpr());
1303 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001304}
1305
Sebastian Redlb3298c32010-08-18 23:56:48 +00001306void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001307 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001308 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001309
1310 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1311 FD->InitStorage.setInt(ISK);
1312 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1313 ? Record.readType().getAsOpaquePtr()
1314 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001315 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001316
1317 if (auto *BW = Record.readExpr())
1318 FD->setBitWidth(BW);
1319
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001320 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001321 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001322 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001323 }
Richard Smith0b87e072013-10-07 08:02:11 +00001324 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001325}
1326
John McCall5e77d762013-04-16 07:28:30 +00001327void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1328 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001329 PD->GetterId = Record.getIdentifierInfo();
1330 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001331}
1332
Francois Pichet783dd6e2010-11-21 06:08:52 +00001333void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1334 VisitValueDecl(FD);
1335
David L. Jonesbe1557a2016-12-21 00:17:49 +00001336 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001337 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001338 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001339
1340 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001341 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001342
1343 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001344}
1345
Larisse Voufo39a1e502013-08-06 01:03:05 +00001346ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001347 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001348 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001349
David L. Jonesbe1557a2016-12-21 00:17:49 +00001350 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1351 VD->VarDeclBits.TSCSpec = Record.readInt();
1352 VD->VarDeclBits.InitStyle = Record.readInt();
Erik Pilkington1e368822019-01-04 18:33:06 +00001353 VD->VarDeclBits.ARCPseudoStrong = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001354 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001355 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1356 Record.readInt();
1357 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001358 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001359 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001360 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001361 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1362 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1363 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1364 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1365 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001366 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
Akira Hatanaka8e57b072018-10-01 21:51:28 +00001367 VD->NonParmVarDeclBits.EscapingByref = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001368 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001369 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001370 VD->setCachedLinkage(VarLinkage);
1371
1372 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001373 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001374 VD->getLexicalDeclContext()->isFunctionOrMethod())
1375 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001376
David L. Jonesbe1557a2016-12-21 00:17:49 +00001377 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001378 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001379 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001380 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1381 Eval->CheckedICE = true;
1382 Eval->IsICE = Val == 3;
1383 }
1384 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001385
Akira Hatanaka9978da32018-08-10 15:09:24 +00001386 if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1387 Expr *CopyExpr = Record.readExpr();
1388 if (CopyExpr)
1389 Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1390 }
1391
Richard Smitha4653622017-09-06 20:01:14 +00001392 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1393 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1394
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001395 enum VarKind {
1396 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1397 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001398 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001399 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001400 // Only true variables (not parameters or implicit parameters) can be
1401 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001402 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1403 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001404 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001405 break;
1406 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001407 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001408 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001409 break;
1410 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001411 auto *Tmpl = ReadDeclAs<VarDecl>();
1412 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001413 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001414 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001415 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001416 break;
1417 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001418 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001419
1420 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001421}
1422
Sebastian Redlb3298c32010-08-18 23:56:48 +00001423void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001424 VisitVarDecl(PD);
1425}
1426
Sebastian Redlb3298c32010-08-18 23:56:48 +00001427void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001428 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001429 unsigned isObjCMethodParam = Record.readInt();
1430 unsigned scopeDepth = Record.readInt();
1431 unsigned scopeIndex = Record.readInt();
1432 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001433 if (isObjCMethodParam) {
1434 assert(scopeDepth == 0);
1435 PD->setObjCMethodScopeInfo(scopeIndex);
1436 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1437 } else {
1438 PD->setScopeInfo(scopeDepth, scopeIndex);
1439 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001440 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1441 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1442 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001443 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001444
1445 // FIXME: If this is a redeclaration of a function from another module, handle
1446 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001447}
1448
Richard Smith7b76d812016-08-12 02:21:25 +00001449void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1450 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001451 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith7b76d812016-08-12 02:21:25 +00001452 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001453 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001454}
1455
1456void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1457 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001458 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001459}
1460
Sebastian Redlb3298c32010-08-18 23:56:48 +00001461void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001462 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001463 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001464 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001465}
1466
Sebastian Redlb3298c32010-08-18 23:56:48 +00001467void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001468 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001469 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001470 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001471 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001472 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001473 Params.reserve(NumParams);
1474 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001475 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001476 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001477
David L. Jonesbe1557a2016-12-21 00:17:49 +00001478 BD->setIsVariadic(Record.readInt());
1479 BD->setBlockMissingReturnType(Record.readInt());
1480 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001481 BD->setDoesNotEscape(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001482
David L. Jonesbe1557a2016-12-21 00:17:49 +00001483 bool capturesCXXThis = Record.readInt();
1484 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001485 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001486 captures.reserve(numCaptures);
1487 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001488 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001489 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001490 bool byRef = (flags & 1);
1491 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001492 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001493
1494 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1495 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001496 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001497}
1498
Ben Langmuirce914fc2013-05-03 19:20:19 +00001499void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1500 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001501 unsigned ContextParamPos = Record.readInt();
1502 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001503 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001504 for (unsigned I = 0; I < CD->NumParams; ++I) {
1505 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001506 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001507 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001508 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001509 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001510}
1511
Sebastian Redlb3298c32010-08-18 23:56:48 +00001512void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001513 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001514 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001515 D->setExternLoc(ReadSourceLocation());
1516 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001517}
1518
Richard Smith8df390f2016-09-08 23:14:54 +00001519void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1520 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001521 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001522}
1523
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001524void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1525 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001526 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001527}
1528
Sebastian Redlb3298c32010-08-18 23:56:48 +00001529void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001530 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001531 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001532 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001533 D->LocStart = ReadSourceLocation();
1534 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001535
Richard Smith15e32fd2015-03-17 02:23:11 +00001536 // Defer loading the anonymous namespace until we've finished merging
1537 // this namespace; loading it might load a later declaration of the
1538 // same namespace, and we have an invariant that older declarations
1539 // get merged before newer ones try to merge.
1540 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001541 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001542 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001543 } else {
1544 // Link this namespace back to the first declaration, which has already
1545 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001546 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001547 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001548
1549 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001550
1551 if (AnonNamespace) {
1552 // Each module has its own anonymous namespace, which is disjoint from
1553 // any other module's anonymous namespaces, so don't attach the anonymous
1554 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001555 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001556 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001557 D->setAnonymousNamespace(Anon);
1558 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001559}
1560
Sebastian Redlb3298c32010-08-18 23:56:48 +00001561void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001562 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001563 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001564 D->NamespaceLoc = ReadSourceLocation();
1565 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001566 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001567 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001568 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001569}
1570
Sebastian Redlb3298c32010-08-18 23:56:48 +00001571void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001572 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001573 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001574 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001575 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1576 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001577 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001578 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001579 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001580 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001581}
1582
Richard Smith151c4562016-12-20 21:35:28 +00001583void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1584 VisitNamedDecl(D);
1585 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001586 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001587 for (unsigned I = 0; I != D->NumExpansions; ++I)
1588 Expansions[I] = ReadDeclAs<NamedDecl>();
1589 mergeMergeable(D);
1590}
1591
Sebastian Redlb3298c32010-08-18 23:56:48 +00001592void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001593 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001594 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001595 D->Underlying = ReadDeclAs<NamedDecl>();
1596 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001597 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001598 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001599 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001600 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001601 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001602}
1603
Richard Smith5179eb72016-06-28 19:03:57 +00001604void ASTDeclReader::VisitConstructorUsingShadowDecl(
1605 ConstructorUsingShadowDecl *D) {
1606 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001607 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1608 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001609 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001610}
1611
Sebastian Redlb3298c32010-08-18 23:56:48 +00001612void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001613 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001614 D->UsingLoc = ReadSourceLocation();
1615 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001616 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001617 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1618 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001619}
1620
Sebastian Redlb3298c32010-08-18 23:56:48 +00001621void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001622 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001623 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001624 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001625 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001626 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001627 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001628}
1629
Sebastian Redlb3298c32010-08-18 23:56:48 +00001630void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001631 UnresolvedUsingTypenameDecl *D) {
1632 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001633 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001634 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001635 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001636 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001637}
1638
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001639void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001640 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001641 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001642 Data.UserDeclaredConstructor = Record.readInt();
1643 Data.UserDeclaredSpecialMembers = Record.readInt();
1644 Data.Aggregate = Record.readInt();
1645 Data.PlainOldData = Record.readInt();
1646 Data.Empty = Record.readInt();
1647 Data.Polymorphic = Record.readInt();
1648 Data.Abstract = Record.readInt();
1649 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001650 Data.IsCXX11StandardLayout = Record.readInt();
1651 Data.HasBasesWithFields = Record.readInt();
1652 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001653 Data.HasPrivateFields = Record.readInt();
1654 Data.HasProtectedFields = Record.readInt();
1655 Data.HasPublicFields = Record.readInt();
1656 Data.HasMutableFields = Record.readInt();
1657 Data.HasVariantMembers = Record.readInt();
1658 Data.HasOnlyCMembers = Record.readInt();
1659 Data.HasInClassInitializer = Record.readInt();
1660 Data.HasUninitializedReferenceMember = Record.readInt();
1661 Data.HasUninitializedFields = Record.readInt();
1662 Data.HasInheritedConstructor = Record.readInt();
1663 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001664 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001665 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1666 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1667 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001668 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001669 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1670 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1671 Data.DefaultedDestructorIsDeleted = Record.readInt();
1672 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001673 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001674 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001675 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001676 Data.HasIrrelevantDestructor = Record.readInt();
1677 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1678 Data.HasDefaultedDefaultConstructor = Record.readInt();
1679 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1680 Data.HasConstexprDefaultConstructor = Record.readInt();
1681 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1682 Data.ComputedVisibleConversions = Record.readInt();
1683 Data.UserProvidedDefaultConstructor = Record.readInt();
1684 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001685 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1686 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001687 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1688 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1689 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001690 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001691 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001692
Richard Smithcd4a7a42017-09-07 00:55:55 +00001693 if (Record.readInt())
1694 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001695
David L. Jonesbe1557a2016-12-21 00:17:49 +00001696 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001697 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001698 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001699 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001700 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001701 Data.VBases = ReadGlobalOffset();
1702
David L. Jonesb6a8f022016-12-21 04:34:52 +00001703 Record.readUnresolvedSet(Data.Conversions);
1704 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001705 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001706 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001707
Douglas Gregor99ae8062012-02-14 17:54:36 +00001708 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001709 using Capture = LambdaCapture;
1710
1711 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001712 Lambda.Dependent = Record.readInt();
1713 Lambda.IsGenericLambda = Record.readInt();
1714 Lambda.CaptureDefault = Record.readInt();
1715 Lambda.NumCaptures = Record.readInt();
1716 Lambda.NumExplicitCaptures = Record.readInt();
1717 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001718 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001719 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1720 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001721 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001722 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001723 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001724 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001725 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001726 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001727 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001728 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001729 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001730 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001731 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001732 break;
1733 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001734 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001735 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001736 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001737 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1738 break;
1739 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001740 }
1741 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001742}
1743
Richard Smithcd45dbc2014-04-19 03:48:30 +00001744void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001745 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001746 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001747 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001748 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001749
Richard Smith02793752015-03-27 21:16:39 +00001750 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001751 // Track that we merged the definitions.
1752 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1753 DD.Definition));
1754 Reader.PendingDefinitions.erase(MergeDD.Definition);
Erich Keanef92f31c2018-08-01 20:48:16 +00001755 MergeDD.Definition->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +00001756 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001757 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1758 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001759 }
1760
Richard Smith2a9e5c52015-02-03 03:32:14 +00001761 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1762 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1763 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001764 // We faked up this definition data because we found a class for which we'd
1765 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001766 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001767 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001768
1769 // Don't change which declaration is the definition; that is required
1770 // to be invariant once we select it.
1771 auto *Def = DD.Definition;
1772 DD = std::move(MergeDD);
1773 DD.Definition = Def;
1774 return;
1775 }
1776
Richard Smithcd45dbc2014-04-19 03:48:30 +00001777 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001778 bool DetectedOdrViolation = false;
1779#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1780#define MATCH_FIELD(Field) \
1781 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1782 OR_FIELD(Field)
1783 MATCH_FIELD(UserDeclaredConstructor)
1784 MATCH_FIELD(UserDeclaredSpecialMembers)
1785 MATCH_FIELD(Aggregate)
1786 MATCH_FIELD(PlainOldData)
1787 MATCH_FIELD(Empty)
1788 MATCH_FIELD(Polymorphic)
1789 MATCH_FIELD(Abstract)
1790 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001791 MATCH_FIELD(IsCXX11StandardLayout)
1792 MATCH_FIELD(HasBasesWithFields)
1793 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001794 MATCH_FIELD(HasPrivateFields)
1795 MATCH_FIELD(HasProtectedFields)
1796 MATCH_FIELD(HasPublicFields)
1797 MATCH_FIELD(HasMutableFields)
1798 MATCH_FIELD(HasVariantMembers)
1799 MATCH_FIELD(HasOnlyCMembers)
1800 MATCH_FIELD(HasInClassInitializer)
1801 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001802 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001803 MATCH_FIELD(HasInheritedConstructor)
1804 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001805 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001806 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1807 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1808 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001809 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001810 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1811 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1812 MATCH_FIELD(DefaultedDestructorIsDeleted)
1813 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001814 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001815 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001816 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001817 MATCH_FIELD(HasIrrelevantDestructor)
1818 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001819 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001820 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1821 OR_FIELD(HasConstexprDefaultConstructor)
1822 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1823 // ComputedVisibleConversions is handled below.
1824 MATCH_FIELD(UserProvidedDefaultConstructor)
1825 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001826 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1827 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001828 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1829 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1830 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1831 MATCH_FIELD(IsLambda)
1832#undef OR_FIELD
1833#undef MATCH_FIELD
1834
1835 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1836 DetectedOdrViolation = true;
1837 // FIXME: Issue a diagnostic if the base classes don't match when we come
1838 // to lazily load them.
1839
1840 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1841 // match when we come to lazily load them.
1842 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1843 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1844 DD.ComputedVisibleConversions = true;
1845 }
1846
1847 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1848 // lazily load it.
1849
1850 if (DD.IsLambda) {
1851 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1852 // when they occur within the body of a function template specialization).
1853 }
1854
Richard Trieufd1acbb2017-04-11 21:31:00 +00001855 if (D->getODRHash() != MergeDD.ODRHash) {
1856 DetectedOdrViolation = true;
1857 }
1858
Richard Smithcd45dbc2014-04-19 03:48:30 +00001859 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001860 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1861 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001862}
1863
Richard Smith2a9e5c52015-02-03 03:32:14 +00001864void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001865 struct CXXRecordDecl::DefinitionData *DD;
1866 ASTContext &C = Reader.getContext();
1867
1868 // Determine whether this is a lambda closure type, so that we can
1869 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001870 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001871 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001872 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001873 LCD_None);
1874 else
1875 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1876
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001877 CXXRecordDecl *Canon = D->getCanonicalDecl();
1878 // Set decl definition data before reading it, so that during deserialization
1879 // when we read CXXRecordDecl, it already has definition data and we don't
1880 // set fake one.
1881 if (!Canon->DefinitionData)
1882 Canon->DefinitionData = DD;
1883 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001884 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001885
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001886 // We might already have a different definition for this record. This can
1887 // happen either because we're reading an update record, or because we've
1888 // already done some merging. Either way, just merge into it.
1889 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001890 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001891 return;
1892 }
1893
Richard Smith97100e32015-06-20 00:22:34 +00001894 // Mark this declaration as being a definition.
Erich Keanef92f31c2018-08-01 20:48:16 +00001895 D->setCompleteDefinition(true);
Richard Smith2a9e5c52015-02-03 03:32:14 +00001896
Richard Smith97100e32015-06-20 00:22:34 +00001897 // If this is not the first declaration or is an update record, we can have
1898 // other redeclarations already. Make a note that we need to propagate the
1899 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001900 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001901 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001902}
1903
Richard Smith1d209d02013-05-23 01:49:11 +00001904ASTDeclReader::RedeclarableResult
1905ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1906 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001907
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001908 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001909
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001910 enum CXXRecKind {
1911 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1912 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001913 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001914 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001915 // Merged when we merge the folding set entry in the primary template.
1916 if (!isa<ClassTemplateSpecializationDecl>(D))
1917 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001918 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001919 case CXXRecTemplate: {
1920 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001921 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001922 D->TemplateOrInstantiation = Template;
1923 if (!Template->getTemplatedDecl()) {
1924 // We've not actually loaded the ClassTemplateDecl yet, because we're
1925 // currently being loaded as its pattern. Rely on it to set up our
1926 // TypeForDecl (see VisitClassTemplateDecl).
1927 //
1928 // Beware: we do not yet know our canonical declaration, and may still
1929 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001930 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001931 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001932 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001933 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001934 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001935 auto *RD = ReadDeclAs<CXXRecordDecl>();
1936 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001937 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001938 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1939 MSI->setPointOfInstantiation(POI);
1940 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001941 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001942 break;
1943 }
1944 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001945
David L. Jonesbe1557a2016-12-21 00:17:49 +00001946 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001947 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001948 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001949 else
1950 // Propagate DefinitionData pointer from the canonical declaration.
1951 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1952
Richard Smith676c4042013-08-29 23:59:27 +00001953 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001954 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001955 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001956 DeclID KeyFn = ReadDeclID();
Erich Keanef92f31c2018-08-01 20:48:16 +00001957 if (KeyFn && D->isCompleteDefinition())
Richard Smitha9a1c682014-07-07 06:38:20 +00001958 // FIXME: This is wrong for the ARM ABI, where some other module may have
1959 // made this function no longer be a key function. We need an update
1960 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001961 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001962 }
Richard Smith1d209d02013-05-23 01:49:11 +00001963
1964 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001965}
1966
Richard Smithbc491202017-02-17 20:05:37 +00001967void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1968 VisitFunctionDecl(D);
Erich Keane9c665062018-08-01 21:02:40 +00001969 D->setIsCopyDeductionCandidate(Record.readInt());
Richard Smithbc491202017-02-17 20:05:37 +00001970}
1971
Sebastian Redlb3298c32010-08-18 23:56:48 +00001972void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001973 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001974
David L. Jonesbe1557a2016-12-21 00:17:49 +00001975 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001976 if (D->isCanonicalDecl()) {
1977 while (NumOverridenMethods--) {
1978 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1979 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001980 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001981 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1982 }
1983 } else {
1984 // We don't care about which declarations this used to override; we get
1985 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001986 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001987 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001988}
1989
Sebastian Redlb3298c32010-08-18 23:56:48 +00001990void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001991 // We need the inherited constructor information to merge the declaration,
1992 // so we have to read it before we call VisitCXXMethodDecl.
1993 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001994 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1995 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001996 *D->getTrailingObjects<InheritedConstructor>() =
1997 InheritedConstructor(Shadow, Ctor);
1998 }
1999
Chris Lattnerca025db2010-05-07 21:43:38 +00002000 VisitCXXMethodDecl(D);
2001}
2002
Sebastian Redlb3298c32010-08-18 23:56:48 +00002003void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002004 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00002005
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002006 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002007 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00002008 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00002009 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00002010 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00002011 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00002012 Canon->OperatorDeleteThisArg = ThisArg;
2013 }
Richard Smithf8134002015-03-10 01:41:22 +00002014 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002015}
2016
Sebastian Redlb3298c32010-08-18 23:56:48 +00002017void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002018 VisitCXXMethodDecl(D);
2019}
2020
Douglas Gregorba345522011-12-02 23:23:56 +00002021void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2022 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002023 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002024 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002025 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002026 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002027 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002028 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002029}
2030
Sebastian Redlb3298c32010-08-18 23:56:48 +00002031void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002032 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002033 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002034}
2035
Sebastian Redlb3298c32010-08-18 23:56:48 +00002036void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002037 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002038 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002039 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002040 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002041 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002042 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002043 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002044 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002045 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002046 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002047 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002048}
2049
Sebastian Redlb3298c32010-08-18 23:56:48 +00002050void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002051 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002052 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002053 D->NumParams = NumParams;
2054 D->Params = new TemplateParameterList*[NumParams];
2055 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002056 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002057 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002058 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002059 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002060 D->Friend = GetTypeSourceInfo();
2061 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002062}
2063
Richard Smithf17fdbd2014-04-24 02:25:27 +00002064DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002065 VisitNamedDecl(D);
2066
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002067 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002068 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002069 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002070 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002071 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002072
Richard Smithf17fdbd2014-04-24 02:25:27 +00002073 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002074}
2075
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002076ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002077ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002078 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002079
Douglas Gregor68444de2012-01-14 15:13:49 +00002080 // Make sure we've allocated the Common pointer first. We do this before
2081 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2082 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2083 if (!CanonD->Common) {
2084 CanonD->Common = CanonD->newCommon(Reader.getContext());
2085 Reader.PendingDefinitions.insert(CanonD);
2086 }
2087 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002088
Douglas Gregor68444de2012-01-14 15:13:49 +00002089 // If this is the first declaration of the template, fill in the information
2090 // for the 'common' pointer.
2091 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002092 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002093 assert(RTD->getKind() == D->getKind() &&
2094 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002095 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002096 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002097 D->setMemberSpecialization();
2098 }
2099 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002100
Richard Smithf17fdbd2014-04-24 02:25:27 +00002101 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002102 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002103
Richard Smithf17fdbd2014-04-24 02:25:27 +00002104 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002105
Richard Smithd46d6de2013-10-13 23:50:45 +00002106 // If we merged the template with a prior declaration chain, merge the common
2107 // pointer.
2108 // FIXME: Actually merge here, don't just overwrite.
2109 D->Common = D->getCanonicalDecl()->Common;
2110
Douglas Gregor68444de2012-01-14 15:13:49 +00002111 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002112}
2113
Sebastian Redlb3298c32010-08-18 23:56:48 +00002114void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002115 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002116
Douglas Gregor68444de2012-01-14 15:13:49 +00002117 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002118 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2119 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002120 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002121 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002122 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002123 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002124
2125 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2126 // We were loaded before our templated declaration was. We've not set up
2127 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2128 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002129 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002130 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002131 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002132}
2133
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002134void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2135 llvm_unreachable("BuiltinTemplates are not serialized");
2136}
2137
Larisse Voufo30616382013-08-23 22:21:36 +00002138/// TODO: Unify with ClassTemplateDecl version?
2139/// May require unifying ClassTemplateDecl and
2140/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002141void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2142 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2143
2144 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002145 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002146 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002147 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002148 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002149 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002150 }
2151}
2152
Richard Smith1d209d02013-05-23 01:49:11 +00002153ASTDeclReader::RedeclarableResult
2154ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2155 ClassTemplateSpecializationDecl *D) {
2156 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002157
Douglas Gregor4163aca2011-09-09 21:34:22 +00002158 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002159 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002160 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002161 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002162 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002163 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002164 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002165 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002166 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002167 auto *PS =
2168 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002169 SpecializedPartialSpecialization();
2170 PS->PartialSpecialization
2171 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2172 PS->TemplateArgs = ArgList;
2173 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002174 }
2175 }
2176
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002177 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002178 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002179 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002180 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002181 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002182
David L. Jonesbe1557a2016-12-21 00:17:49 +00002183 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002184 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002185 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002186 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002187 // Set this as, or find, the canonical declaration for this specialization
2188 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002189 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002190 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002191 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002192 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002193 CanonSpec =
2194 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2195 }
2196 // If there was already a canonical specialization, merge into it.
2197 if (CanonSpec != D) {
2198 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2199
2200 // This declaration might be a definition. Merge with any existing
2201 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002202 if (auto *DDD = D->DefinitionData) {
2203 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002204 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002205 else
Richard Smith053f6c62014-05-16 23:01:30 +00002206 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002207 }
Richard Smith547864d2014-07-11 18:22:58 +00002208 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002209 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002210 }
2211 }
Richard Smith1d209d02013-05-23 01:49:11 +00002212
Richard Smithd55889a2013-09-09 16:55:27 +00002213 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002214 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002215 auto *ExplicitInfo =
2216 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002217 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002218 ExplicitInfo->ExternLoc = ReadSourceLocation();
2219 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002220 D->ExplicitInfo = ExplicitInfo;
2221 }
2222
Richard Smith1d209d02013-05-23 01:49:11 +00002223 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002224}
2225
Sebastian Redlb3298c32010-08-18 23:56:48 +00002226void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002227 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002228 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002229
David L. Jonesb6a8f022016-12-21 04:34:52 +00002230 D->TemplateParams = Record.readTemplateParameterList();
2231 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002232
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002233 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002234 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002235 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002236 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002237 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002238 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002239}
2240
Sebastian Redlb7448632011-08-31 13:59:56 +00002241void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2242 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002243 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002244 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002245}
2246
Sebastian Redlb3298c32010-08-18 23:56:48 +00002247void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002248 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002249
Douglas Gregor68444de2012-01-14 15:13:49 +00002250 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002251 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002252 SmallVector<serialization::DeclID, 32> SpecIDs;
2253 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002254 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002255 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002256}
2257
Larisse Voufo30616382013-08-23 22:21:36 +00002258/// TODO: Unify with ClassTemplateSpecializationDecl version?
2259/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2260/// VarTemplate(Partial)SpecializationDecl with a new data
2261/// structure Template(Partial)SpecializationDecl, and
2262/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002263ASTDeclReader::RedeclarableResult
2264ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2265 VarTemplateSpecializationDecl *D) {
2266 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2267
2268 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002269 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002270 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002271 D->SpecializedTemplate = VTD;
2272 } else {
2273 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002274 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002275 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002276 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002277 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002278 new (C)
2279 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2280 PS->PartialSpecialization =
2281 cast<VarTemplatePartialSpecializationDecl>(InstD);
2282 PS->TemplateArgs = ArgList;
2283 D->SpecializedTemplate = PS;
2284 }
2285 }
2286
2287 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002288 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002289 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002290 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2291 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002292 ExplicitInfo->ExternLoc = ReadSourceLocation();
2293 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002294 D->ExplicitInfo = ExplicitInfo;
2295 }
2296
2297 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002298 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002299 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002300 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002301 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002302 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002303
David L. Jonesbe1557a2016-12-21 00:17:49 +00002304 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002305 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002306 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002307 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002308 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002309 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002310 CanonPattern->getCommonPtr()->PartialSpecializations
2311 .GetOrInsertNode(Partial);
2312 } else {
2313 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2314 }
2315 }
2316 }
2317
2318 return Redecl;
2319}
2320
Larisse Voufo30616382013-08-23 22:21:36 +00002321/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2322/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2323/// VarTemplate(Partial)SpecializationDecl with a new data
2324/// structure Template(Partial)SpecializationDecl, and
2325/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002326void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2327 VarTemplatePartialSpecializationDecl *D) {
2328 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2329
David L. Jonesb6a8f022016-12-21 04:34:52 +00002330 D->TemplateParams = Record.readTemplateParameterList();
2331 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002332
2333 // These are read/set from/to the first declaration.
2334 if (ThisDeclID == Redecl.getFirstID()) {
2335 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002336 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002337 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002338 }
2339}
2340
Sebastian Redlb3298c32010-08-18 23:56:48 +00002341void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002342 VisitTypeDecl(D);
2343
David L. Jonesbe1557a2016-12-21 00:17:49 +00002344 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002345
David L. Jonesbe1557a2016-12-21 00:17:49 +00002346 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002347 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002348}
2349
Sebastian Redlb3298c32010-08-18 23:56:48 +00002350void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002351 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002352 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002353 D->setDepth(Record.readInt());
2354 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002355 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002356 auto TypesAndInfos =
2357 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002358 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002359 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002360 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002361 }
2362 } else {
2363 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002364 D->ParameterPack = Record.readInt();
2365 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002366 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002367 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002368}
2369
Sebastian Redlb3298c32010-08-18 23:56:48 +00002370void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002371 VisitTemplateDecl(D);
2372 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002373 D->setDepth(Record.readInt());
2374 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002375 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002376 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002377 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2378 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002379 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002380 } else {
2381 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002382 D->ParameterPack = Record.readInt();
2383 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002384 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002385 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002386 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002387}
2388
Richard Smith3f1b5d02011-05-05 21:57:07 +00002389void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2390 VisitRedeclarableTemplateDecl(D);
2391}
2392
Sebastian Redlb3298c32010-08-18 23:56:48 +00002393void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002394 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002395 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002396 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002397 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002398 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002399}
2400
Michael Han84324352013-02-22 17:15:32 +00002401void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2402 VisitDecl(D);
2403}
2404
Mike Stump11289f42009-09-09 15:08:12 +00002405std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002406ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002407 uint64_t LexicalOffset = ReadLocalOffset();
2408 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002409 return std::make_pair(LexicalOffset, VisibleOffset);
2410}
2411
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002412template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002413ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002414ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002415 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002416 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002417
Richard Smith5fc18a92015-07-12 23:43:21 +00002418 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002419 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002420
Richard Smithd61d4ac2015-08-22 20:13:39 +00002421 uint64_t RedeclOffset = 0;
2422
Douglas Gregor358cd442012-01-15 16:58:34 +00002423 // 0 indicates that this declaration was the only declaration of its entity,
2424 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002425 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002426 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002427 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002428 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002429 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002430 // This declaration was the first local declaration, but may have imported
2431 // other declarations.
2432 IsKeyDecl = N == 1;
2433 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002434
Richard Smithfe620d22015-03-05 23:24:12 +00002435 // We have some declarations that must be before us in our redeclaration
2436 // chain. Read them now, and remember that we ought to merge with one of
2437 // them.
2438 // FIXME: Provide a known merge target to the second and subsequent such
2439 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002440 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002441 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002442
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002443 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002444 } else {
2445 // This declaration was not the first local declaration. Read the first
2446 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002447 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002448 }
2449
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002450 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002451 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002452 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2453 // We temporarily set the first (canonical) declaration as the previous one
2454 // which is the one that matters and mark the real previous DeclID to be
2455 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002456 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002457 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002458 }
Richard Smithd8a83712015-08-22 01:47:18 +00002459
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002460 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002461
2462 // Note that we need to load local redeclarations of this decl and build a
2463 // decl chain for them. This must happen *after* we perform the preloading
2464 // above; this ensures that the redeclaration chain is built in the correct
2465 // order.
2466 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002467 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002468
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002469 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002470}
2471
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002472/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002473/// of the same entity.
2474template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002475void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002476 RedeclarableResult &Redecl,
2477 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002478 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002479 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002480 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002481
Richard Smith202850a2015-03-10 02:57:50 +00002482 // If we're not the canonical declaration, we don't need to merge.
2483 if (!DBase->isFirstDecl())
2484 return;
2485
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002486 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002487
Richard Smith5a4737c2015-02-06 02:42:59 +00002488 if (auto *Existing = Redecl.getKnownMergeTarget())
2489 // We already know of an existing declaration we should merge with.
2490 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2491 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002492 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002493 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2494}
2495
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002496/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002497/// We use this to put code in a template that will only be valid for certain
2498/// instantiations.
2499template<typename T> static T assert_cast(T t) { return t; }
2500template<typename T> static T assert_cast(...) {
2501 llvm_unreachable("bad assert_cast");
2502}
2503
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002504/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002505/// declarations.
2506void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2507 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002508 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002509 auto *DPattern = D->getTemplatedDecl();
2510 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002511 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2512 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002513 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002514
Richard Smith547864d2014-07-11 18:22:58 +00002515 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2516 // Merge with any existing definition.
2517 // FIXME: This is duplicated in several places. Refactor.
2518 auto *ExistingClass =
2519 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002520 if (auto *DDD = DClass->DefinitionData) {
2521 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002522 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002523 } else {
2524 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002525 // We may have skipped this before because we thought that DClass
2526 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002527 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002528 }
2529 }
2530 DClass->DefinitionData = ExistingClass->DefinitionData;
2531
Richard Smithf17fdbd2014-04-24 02:25:27 +00002532 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2533 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002534 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002535 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2536 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2537 Result);
2538 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2539 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002540 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2541 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2542 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002543 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002544}
2545
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002546/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002547/// of the same entity.
2548template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002549void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2550 RedeclarableResult &Redecl,
2551 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002552 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002553 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002554 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002555 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002556 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2557 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002558
Richard Smithd55889a2013-09-09 16:55:27 +00002559 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002560 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002561 // appropriate canonical declaration.
2562 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002563 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002564 ExistingCanon->Used |= D->Used;
2565 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002566
2567 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002568 // We cannot have loaded any redeclarations of this declaration yet, so
2569 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002570 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002571 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002572 assert_cast<NamespaceDecl*>(ExistingCanon));
2573
2574 // When we merge a template, merge its pattern.
2575 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2576 mergeTemplatePattern(
2577 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002578 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002579
Richard Smith5fc18a92015-07-12 23:43:21 +00002580 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002581 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002582 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002583 }
2584}
2585
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002586/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2587/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2588/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2589/// that some types are mergeable during deserialization, otherwise name
2590/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002591static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002592 if (!ND)
2593 return false;
2594 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002595 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002596 return true;
2597 return false;
2598}
2599
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002600/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002601/// of the same entity, for the case where the entity is not actually
2602/// redeclarable. This happens, for instance, when merging the fields of
2603/// identical class definitions from two different modules.
2604template<typename T>
2605void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2606 // If modules are not available, there is no reason to perform this merge.
2607 if (!Reader.getContext().getLangOpts().Modules)
2608 return;
2609
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002610 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2611 // Note that C identically-named things in different translation units are
2612 // not redeclarations, but may still have compatible types, where ODR-like
2613 // semantics may apply.
2614 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2615 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002616 return;
2617
Richard Smith0b87e072013-10-07 08:02:11 +00002618 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2619 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002620 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2621 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002622}
2623
Alexey Bataeva769e072013-03-22 06:34:35 +00002624void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2625 VisitDecl(D);
2626 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002627 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002628 Vars.reserve(NumVars);
2629 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002630 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002631 }
2632 D->setVars(Vars);
2633}
2634
Kelvin Li1408f912018-09-26 04:28:39 +00002635void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
2636 VisitDecl(D);
2637 unsigned NumClauses = D->clauselist_size();
2638 SmallVector<OMPClause *, 8> Clauses;
2639 Clauses.reserve(NumClauses);
2640 OMPClauseReader ClauseReader(Record);
2641 for (unsigned I = 0; I != NumClauses; ++I)
2642 Clauses.push_back(ClauseReader.readClause());
2643 D->setClauses(Clauses);
2644}
2645
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002646void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002647 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002648 D->setLocation(ReadSourceLocation());
Alexey Bataeve6aa4692018-09-13 16:54:05 +00002649 Expr *In = Record.readExpr();
2650 Expr *Out = Record.readExpr();
2651 D->setCombinerData(In, Out);
2652 Expr *Combiner = Record.readExpr();
2653 D->setCombiner(Combiner);
2654 Expr *Orig = Record.readExpr();
2655 Expr *Priv = Record.readExpr();
2656 D->setInitializerData(Orig, Priv);
2657 Expr *Init = Record.readExpr();
2658 auto IK = static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt());
2659 D->setInitializer(Init, IK);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002660 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002661}
2662
Michael Kruse251e1482019-02-01 20:25:04 +00002663void ASTDeclReader::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
2664 VisitValueDecl(D);
2665 D->setLocation(ReadSourceLocation());
2666 Expr *MapperVarRefE = Record.readExpr();
2667 D->setMapperVarRef(MapperVarRefE);
2668 D->VarName = Record.readDeclarationName();
2669 D->PrevDeclInScope = ReadDeclID();
2670 unsigned NumClauses = D->clauselist_size();
2671 SmallVector<OMPClause *, 8> Clauses;
2672 Clauses.reserve(NumClauses);
2673 OMPClauseReader ClauseReader(Record);
2674 for (unsigned I = 0; I != NumClauses; ++I)
2675 Clauses.push_back(ClauseReader.readClause());
2676 D->setClauses(Clauses);
2677}
2678
Alexey Bataev4244be22016-02-11 05:35:55 +00002679void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002680 VisitVarDecl(D);
2681}
2682
Chris Lattner487412d2009-04-27 05:27:42 +00002683//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002684// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002685//===----------------------------------------------------------------------===//
2686
Richard Smithe43e2b32018-08-20 21:47:29 +00002687namespace {
2688class AttrReader {
2689 ModuleFile *F;
2690 ASTReader *Reader;
2691 const ASTReader::RecordData &Record;
2692 unsigned &Idx;
2693
2694public:
2695 AttrReader(ModuleFile &F, ASTReader &Reader,
2696 const ASTReader::RecordData &Record, unsigned &Idx)
2697 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
2698
2699 const uint64_t &readInt() { return Record[Idx++]; }
2700
2701 SourceRange readSourceRange() {
2702 return Reader->ReadSourceRange(*F, Record, Idx);
2703 }
2704
2705 Expr *readExpr() { return Reader->ReadExpr(*F); }
2706
2707 std::string readString() {
2708 return Reader->ReadString(Record, Idx);
2709 }
2710
2711 TypeSourceInfo *getTypeSourceInfo() {
2712 return Reader->GetTypeSourceInfo(*F, Record, Idx);
2713 }
2714
2715 IdentifierInfo *getIdentifierInfo() {
2716 return Reader->GetIdentifierInfo(*F, Record, Idx);
2717 }
2718
2719 VersionTuple readVersionTuple() {
2720 return ASTReader::ReadVersionTuple(Record, Idx);
2721 }
2722
2723 template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
2724 return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
2725 }
2726};
2727}
2728
2729Attr *ASTReader::ReadAttr(ModuleFile &M, const RecordData &Rec,
2730 unsigned &Idx) {
2731 AttrReader Record(M, *this, Rec, Idx);
2732 auto V = Record.readInt();
2733 if (!V)
2734 return nullptr;
2735
2736 Attr *New = nullptr;
2737 // Kind is stored as a 1-based integer because 0 is used to indicate a null
2738 // Attr pointer.
2739 auto Kind = static_cast<attr::Kind>(V - 1);
2740 SourceRange Range = Record.readSourceRange();
2741 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002742
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002743#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002744
Richard Smithe43e2b32018-08-20 21:47:29 +00002745 assert(New && "Unable to decode attribute?");
2746 return New;
2747}
2748
2749/// Reads attributes from the current stream position.
2750void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2751 for (unsigned I = 0, E = Record.readInt(); I != E; ++I)
2752 Attrs.push_back(Record.readAttr());
Chris Lattner8f63ab52009-04-27 06:01:06 +00002753}
2754
2755//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002756// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002757//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002758
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002759/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002760/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002761///
Chris Lattner487412d2009-04-27 05:27:42 +00002762/// This routine notes that this declaration has already been loaded,
2763/// so that future GetDecl calls will return this declaration rather
2764/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002765inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002766 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2767 DeclsLoaded[Index] = D;
2768}
2769
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002770/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002771/// this declaration (via HandleTopLevelDecl).
2772///
2773/// This routine should return true for anything that might affect
2774/// code generation, e.g., inline function definitions, Objective-C
2775/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002776static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002777 // An ObjCMethodDecl is never considered as "interesting" because its
2778 // implementation container always is.
2779
Richard Smithcd4a7a42017-09-07 00:55:55 +00002780 // An ImportDecl or VarDecl imported from a module map module will get
2781 // emitted when we import the relevant module.
2782 if (isa<ImportDecl>(D) || isa<VarDecl>(D)) {
2783 auto *M = D->getImportedOwningModule();
2784 if (M && M->Kind == Module::ModuleMapModule &&
2785 Ctx.DeclMustBeEmitted(D))
2786 return false;
2787 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002788
Fangrui Song6907ce22018-07-30 19:24:48 +00002789 if (isa<FileScopeAsmDecl>(D) ||
2790 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002791 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002792 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002793 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002794 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002795 return true;
Michael Kruse251e1482019-02-01 20:25:04 +00002796 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D) ||
2797 isa<OMPDeclareMapperDecl>(D))
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002798 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002799 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002800 return Var->isFileVarDecl() &&
Alexey Bataevd01b7492018-08-15 19:45:12 +00002801 (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
2802 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002803 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002804 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002805
2806 if (auto *ES = D->getASTContext().getExternalSource())
2807 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2808 return true;
2809
Douglas Gregor87d81242011-09-10 00:22:34 +00002810 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002811}
2812
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002813/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002814ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002815ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002816 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2817 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002818 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002819 const DeclOffset &DOffs =
2820 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002821 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002822 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002823}
2824
Douglas Gregord32f0352011-07-22 06:10:01 +00002825ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002826 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002827
2828 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2829 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2830}
2831
Douglas Gregorde3ef502011-11-30 23:21:26 +00002832uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002833 return LocalOffset + M.GlobalBitOffset;
2834}
2835
Richard Smithbf78e642013-06-24 22:51:00 +00002836static bool isSameTemplateParameterList(const TemplateParameterList *X,
2837 const TemplateParameterList *Y);
2838
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002839/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002840/// that they may be used in declarations of the same template.
2841static bool isSameTemplateParameter(const NamedDecl *X,
2842 const NamedDecl *Y) {
2843 if (X->getKind() != Y->getKind())
2844 return false;
2845
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002846 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2847 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002848 return TX->isParameterPack() == TY->isParameterPack();
2849 }
2850
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002851 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2852 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002853 return TX->isParameterPack() == TY->isParameterPack() &&
2854 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2855 }
2856
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002857 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2858 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002859 return TX->isParameterPack() == TY->isParameterPack() &&
2860 isSameTemplateParameterList(TX->getTemplateParameters(),
2861 TY->getTemplateParameters());
2862}
2863
Richard Smith32952e12014-10-14 02:00:47 +00002864static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2865 if (auto *NS = X->getAsNamespace())
2866 return NS;
2867 if (auto *NAS = X->getAsNamespaceAlias())
2868 return NAS->getNamespace();
2869 return nullptr;
2870}
2871
2872static bool isSameQualifier(const NestedNameSpecifier *X,
2873 const NestedNameSpecifier *Y) {
2874 if (auto *NSX = getNamespace(X)) {
2875 auto *NSY = getNamespace(Y);
2876 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2877 return false;
2878 } else if (X->getKind() != Y->getKind())
2879 return false;
2880
2881 // FIXME: For namespaces and types, we're permitted to check that the entity
2882 // is named via the same tokens. We should probably do so.
2883 switch (X->getKind()) {
2884 case NestedNameSpecifier::Identifier:
2885 if (X->getAsIdentifier() != Y->getAsIdentifier())
2886 return false;
2887 break;
2888 case NestedNameSpecifier::Namespace:
2889 case NestedNameSpecifier::NamespaceAlias:
2890 // We've already checked that we named the same namespace.
2891 break;
2892 case NestedNameSpecifier::TypeSpec:
2893 case NestedNameSpecifier::TypeSpecWithTemplate:
2894 if (X->getAsType()->getCanonicalTypeInternal() !=
2895 Y->getAsType()->getCanonicalTypeInternal())
2896 return false;
2897 break;
2898 case NestedNameSpecifier::Global:
2899 case NestedNameSpecifier::Super:
2900 return true;
2901 }
2902
2903 // Recurse into earlier portion of NNS, if any.
2904 auto *PX = X->getPrefix();
2905 auto *PY = Y->getPrefix();
2906 if (PX && PY)
2907 return isSameQualifier(PX, PY);
2908 return !PX && !PY;
2909}
2910
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002911/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002912/// that they may be used in declarations of the same template.
2913static bool isSameTemplateParameterList(const TemplateParameterList *X,
2914 const TemplateParameterList *Y) {
2915 if (X->size() != Y->size())
2916 return false;
2917
2918 for (unsigned I = 0, N = X->size(); I != N; ++I)
2919 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2920 return false;
2921
2922 return true;
2923}
2924
George Burgess IV95845082017-02-15 22:43:27 +00002925/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002926/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002927static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2928 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002929 // Note that pass_object_size attributes are represented in the function's
2930 // ExtParameterInfo, so we don't need to check them here.
2931
George Burgess IV95845082017-02-15 22:43:27 +00002932 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00002933 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
2934 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
Michael Kruse157a3552018-12-10 15:16:37 +00002935
2936 for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) {
2937 Optional<EnableIfAttr *> Cand1A = std::get<0>(Pair);
2938 Optional<EnableIfAttr *> Cand2A = std::get<1>(Pair);
2939
2940 // Return false if the number of enable_if attributes is different.
2941 if (!Cand1A || !Cand2A)
2942 return false;
2943
George Burgess IV95845082017-02-15 22:43:27 +00002944 Cand1ID.clear();
2945 Cand2ID.clear();
2946
Michael Kruse157a3552018-12-10 15:16:37 +00002947 (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2948 (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2949
2950 // Return false if any of the enable_if expressions of A and B are
2951 // different.
George Burgess IV95845082017-02-15 22:43:27 +00002952 if (Cand1ID != Cand2ID)
2953 return false;
2954 }
Michael Kruse157a3552018-12-10 15:16:37 +00002955 return true;
George Burgess IV95845082017-02-15 22:43:27 +00002956}
2957
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002958/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002959static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2960 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002961
Douglas Gregor022857e2011-12-22 01:48:48 +00002962 if (X == Y)
2963 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002964
Douglas Gregor022857e2011-12-22 01:48:48 +00002965 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00002966 //
2967 // Note that we can't use DeclContext::Equals here, because the DeclContexts
2968 // could be two different declarations of the same function. (We will fix the
2969 // semantic DC to refer to the primary definition after merging.)
2970 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
2971 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00002972 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002973
2974 // Two typedefs refer to the same entity if they have the same underlying
2975 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002976 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
2977 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002978 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2979 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002980
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002981 // Must have the same kind.
2982 if (X->getKind() != Y->getKind())
2983 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002984
Douglas Gregorda389302012-01-01 21:47:52 +00002985 // Objective-C classes and protocols with the same name always match.
2986 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002987 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002988
2989 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002990 // No need to handle these here: we merge them when adding them to the
2991 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002992 return false;
2993 }
Richard Smithd55889a2013-09-09 16:55:27 +00002994
Douglas Gregor2009cee2012-01-03 22:46:00 +00002995 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002996 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
2997 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00002998 return (TagX->getTagKind() == TagY->getTagKind()) ||
2999 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
3000 TagX->getTagKind() == TTK_Interface) &&
3001 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
3002 TagY->getTagKind() == TTK_Interface));
3003 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003004
Joao Matose9a3ed42012-08-31 22:18:20 +00003005 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00003006 // FIXME: This needs to cope with merging of prototyped/non-prototyped
3007 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003008 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
3009 const auto *FuncY = cast<FunctionDecl>(Y);
3010 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
3011 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00003012 if (CtorX->getInheritedConstructor() &&
3013 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
3014 CtorY->getInheritedConstructor().getConstructor()))
3015 return false;
3016 }
Erich Keane281d20b2018-01-08 21:34:17 +00003017
3018 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
3019 return false;
3020
3021 // Multiversioned functions with different feature strings are represented
3022 // as separate declarations.
3023 if (FuncX->isMultiVersion()) {
3024 const auto *TAX = FuncX->getAttr<TargetAttr>();
3025 const auto *TAY = FuncY->getAttr<TargetAttr>();
3026 assert(TAX && TAY && "Multiversion Function without target attribute");
3027
3028 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
3029 return false;
3030 }
3031
Richard Smitha54d3242017-03-08 23:00:26 +00003032 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00003033 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
3034 // Map to the first declaration that we've already merged into this one.
3035 // The TSI of redeclarations might not match (due to calling conventions
3036 // being inherited onto the type but not the TSI), but the TSI type of
3037 // the first declaration of the function should match across modules.
3038 FD = FD->getCanonicalDecl();
3039 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
3040 : FD->getType();
3041 };
3042 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
3043 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00003044 // We can get functions with different types on the redecl chain in C++17
3045 // if they have differing exception specifications and at least one of
3046 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00003047 auto *XFPT = XT->getAs<FunctionProtoType>();
3048 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003049 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00003050 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
3051 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00003052 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00003053 return true;
3054 return false;
3055 }
George Burgess IV95845082017-02-15 22:43:27 +00003056 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00003057 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00003058 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003059
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003060 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003061 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
3062 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00003063 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
3064 ASTContext &C = VarX->getASTContext();
3065 if (C.hasSameType(VarX->getType(), VarY->getType()))
3066 return true;
3067
3068 // We can get decls with different types on the redecl chain. Eg.
3069 // template <typename T> struct S { static T Var[]; }; // #1
3070 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
3071 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00003072 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00003073 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
3074 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
3075 if (!VarXTy || !VarYTy)
3076 return false;
3077 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
3078 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
3079 }
3080 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003081 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003082
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003083 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003084 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
3085 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003086 return NamespaceX->isInline() == NamespaceY->isInline();
3087 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003088
Richard Smith8f8f05c2013-06-24 04:45:28 +00003089 // Identical template names and kinds match if their template parameter lists
3090 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003091 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
3092 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00003093 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00003094 TemplateY->getTemplatedDecl()) &&
3095 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3096 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003097 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003098
Richard Smith0b87e072013-10-07 08:02:11 +00003099 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003100 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3101 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003102 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003103 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3104 }
3105
Richard Smith8cbd8952015-08-04 02:05:09 +00003106 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003107 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3108 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003109 return IFDX->getAnonField()->getCanonicalDecl() ==
3110 IFDY->getAnonField()->getCanonicalDecl();
3111 }
3112
Richard Smith01a73372013-10-15 22:02:41 +00003113 // Enumerators with the same name match.
3114 if (isa<EnumConstantDecl>(X))
3115 // FIXME: Also check the value is odr-equivalent.
3116 return true;
3117
Richard Smithfd8634a2013-10-23 02:17:46 +00003118 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003119 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3120 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003121 return USX->getTargetDecl() == USY->getTargetDecl();
3122 }
3123
Richard Smith32952e12014-10-14 02:00:47 +00003124 // Using declarations with the same qualifier match. (We already know that
3125 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003126 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3127 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003128 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3129 UX->hasTypename() == UY->hasTypename() &&
3130 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3131 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003132 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3133 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003134 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3135 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3136 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003137 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003138 return isSameQualifier(
3139 UX->getQualifier(),
3140 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3141
Richard Smithf4634362014-09-03 23:11:22 +00003142 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003143 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3144 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003145 return NAX->getNamespace()->Equals(NAY->getNamespace());
3146 }
3147
Douglas Gregor022857e2011-12-22 01:48:48 +00003148 return false;
3149}
3150
Richard Smithd55889a2013-09-09 16:55:27 +00003151/// Find the context in which we should search for previous declarations when
3152/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003153DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3154 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003155 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003156 return ND->getOriginalNamespace();
3157
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003158 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003159 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003160 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003161 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003162 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003163
3164 // If there's no definition yet, then DC's definition is added by an update
3165 // record, but we've not yet loaded that update record. In this case, we
3166 // commit to DC being the canonical definition now, and will fix this when
3167 // we load the update record.
3168 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003169 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003170 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003171 RD->DefinitionData = DD;
3172 RD->getCanonicalDecl()->DefinitionData = DD;
3173
3174 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003175 Reader.PendingFakeDefinitionData.insert(
3176 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003177 }
3178
3179 return DD->Definition;
3180 }
Richard Smithd55889a2013-09-09 16:55:27 +00003181
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003182 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003183 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3184 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003185
Richard Smith4eca9b92015-02-04 23:37:59 +00003186 // We can see the TU here only if we have no Sema object. In that case,
3187 // there's no TU scope to look in, so using the DC alone is sufficient.
3188 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3189 return TU;
3190
Craig Toppera13603a2014-05-22 05:54:18 +00003191 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003192}
3193
Douglas Gregor022857e2011-12-22 01:48:48 +00003194ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003195 // Record that we had a typedef name for linkage whether or not we merge
3196 // with that declaration.
3197 if (TypedefNameForLinkage) {
3198 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3199 Reader.ImportedTypedefNamesForLinkage.insert(
3200 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3201 return;
3202 }
3203
Douglas Gregor9b47f942012-01-09 17:38:47 +00003204 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003205 return;
Richard Smith95d99302013-07-13 02:00:19 +00003206
Richard Smith70d58502014-08-30 00:04:23 +00003207 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003208 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003209 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003210 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3211 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003212 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003213 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003214 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003215 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3216 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003217 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003218 // Add the declaration to its redeclaration context so later merging
3219 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003220 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003221 }
3222}
3223
Richard Smith88ebade2014-08-23 01:45:27 +00003224/// Find the declaration that should be merged into, given the declaration found
3225/// by name lookup. If we're merging an anonymous declaration within a typedef,
3226/// we need a matching typedef, and we merge with the type inside it.
3227static NamedDecl *getDeclForMerging(NamedDecl *Found,
3228 bool IsTypedefNameForLinkage) {
3229 if (!IsTypedefNameForLinkage)
3230 return Found;
3231
3232 // If we found a typedef declaration that gives a name to some other
3233 // declaration, then we want that inner declaration. Declarations from
3234 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003235 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003236 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003237
3238 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003239 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003240
Hans Wennborgdcfba332015-10-06 23:40:43 +00003241 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003242}
3243
Richard Smith600adef2018-07-04 02:25:38 +00003244/// Find the declaration to use to populate the anonymous declaration table
3245/// for the given lexical DeclContext. We only care about finding local
3246/// definitions of the context; we'll merge imported ones as we go.
3247DeclContext *
3248ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3249 // For classes, we track the definition as we merge.
3250 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3251 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3252 return DD ? DD->Definition : nullptr;
3253 }
3254
3255 // For anything else, walk its merged redeclarations looking for a definition.
3256 // Note that we can't just call getDefinition here because the redeclaration
3257 // chain isn't wired up.
3258 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3259 if (auto *FD = dyn_cast<FunctionDecl>(D))
3260 if (FD->isThisDeclarationADefinition())
3261 return FD;
3262 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3263 if (MD->isThisDeclarationADefinition())
3264 return MD;
3265 }
3266
3267 // No merged definition yet.
3268 return nullptr;
3269}
3270
Richard Smithd08aeb62014-08-28 01:33:39 +00003271NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3272 DeclContext *DC,
3273 unsigned Index) {
3274 // If the lexical context has been merged, look into the now-canonical
3275 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003276 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003277
3278 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003279 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003280 if (Index < Previous.size() && Previous[Index])
3281 return Previous[Index];
3282
3283 // If this is the first time, but we have parsed a declaration of the context,
3284 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003285 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3286 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3287 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003288 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003289 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3290 else
Richard Smith2b560572015-02-07 03:11:11 +00003291 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3292 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003293 }
3294
3295 return Index < Previous.size() ? Previous[Index] : nullptr;
3296}
3297
3298void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3299 DeclContext *DC, unsigned Index,
3300 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003301 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003302
Richard Smith600adef2018-07-04 02:25:38 +00003303 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003304 if (Index >= Previous.size())
3305 Previous.resize(Index + 1);
3306 if (!Previous[Index])
3307 Previous[Index] = D;
3308}
3309
Douglas Gregor022857e2011-12-22 01:48:48 +00003310ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003311 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3312 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003313
Richard Smithd08aeb62014-08-28 01:33:39 +00003314 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3315 // Don't bother trying to find unnamed declarations that are in
3316 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003317 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3318 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003319 Result.suppress();
3320 return Result;
3321 }
Richard Smith95d99302013-07-13 02:00:19 +00003322
Douglas Gregor022857e2011-12-22 01:48:48 +00003323 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003324 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003325 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003326 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003327 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3328 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003329 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3330 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003331 // Go on to check in other places in case an existing typedef name
3332 // was not imported.
3333 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003334
Richard Smith2b560572015-02-07 03:11:11 +00003335 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003336 // This is an anonymous declaration that we may need to merge. Look it up
3337 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003338 if (auto *Existing = getAnonymousDeclForMerging(
3339 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3340 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003341 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3342 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003343 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003344 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003345 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003346
3347 // Temporarily consider the identifier to be up-to-date. We don't want to
3348 // cause additional lookups here.
3349 class UpToDateIdentifierRAII {
3350 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003351 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003352
3353 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003354 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003355 if (II) {
3356 WasOutToDate = II->isOutOfDate();
3357 if (WasOutToDate)
3358 II->setOutOfDate(false);
3359 }
3360 }
3361
3362 ~UpToDateIdentifierRAII() {
3363 if (WasOutToDate)
3364 II->setOutOfDate(true);
3365 }
3366 } UpToDate(Name.getAsIdentifierInfo());
3367
Fangrui Song6907ce22018-07-30 19:24:48 +00003368 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003369 IEnd = IdResolver.end();
3370 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003371 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003372 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003373 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3374 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003375 }
Richard Smith8a639892015-01-24 01:07:20 +00003376 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003377 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003378 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003379 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003380 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003381 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3382 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003383 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003384 } else {
3385 // Not in a mergeable context.
3386 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003387 }
Richard Smithd55889a2013-09-09 16:55:27 +00003388
Richard Smith2b9e3e32013-10-18 06:05:18 +00003389 // If this declaration is from a merged context, make a note that we need to
3390 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003391 //
3392 // FIXME: We should do something similar if we merge two definitions of the
3393 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003394 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3395 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3396 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003397 Reader.PendingOdrMergeChecks.push_back(D);
3398
Richard Smithd08aeb62014-08-28 01:33:39 +00003399 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003400 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003401}
3402
Richard Smithb321ecb2014-05-13 01:15:00 +00003403template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003404Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3405 return D->RedeclLink.getLatestNotUpdated();
3406}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003407
Richard Smithc3a53252015-02-28 05:57:02 +00003408Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3409 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3410}
3411
3412Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3413 assert(D);
3414
3415 switch (D->getKind()) {
3416#define ABSTRACT_DECL(TYPE)
3417#define DECL(TYPE, BASE) \
3418 case Decl::TYPE: \
3419 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3420#include "clang/AST/DeclNodes.inc"
3421 }
3422 llvm_unreachable("unknown decl kind");
3423}
3424
Richard Smith8ce51082015-03-11 01:44:51 +00003425Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3426 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3427}
3428
Richard Smithc3a53252015-02-28 05:57:02 +00003429template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003430void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3431 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003432 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003433 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003434 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003435}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003436
Richard Smith24d166c2014-07-31 23:52:38 +00003437namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003438
Richard Smith6de7a242014-07-31 23:46:44 +00003439template<>
3440void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003441 Redeclarable<VarDecl> *D,
3442 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003443 auto *VD = static_cast<VarDecl *>(D);
3444 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003445 D->RedeclLink.setPrevious(PrevVD);
3446 D->First = PrevVD->First;
3447
3448 // We should keep at most one definition on the chain.
3449 // FIXME: Cache the definition once we've found it. Building a chain with
3450 // N definitions currently takes O(N^2) time here.
3451 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3452 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3453 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3454 Reader.mergeDefinitionVisibility(CurD, VD);
3455 VD->demoteThisDefinitionToDeclaration();
3456 break;
3457 }
3458 }
3459 }
3460}
3461
Richard Smitha62d1982018-08-03 01:00:01 +00003462static bool isUndeducedReturnType(QualType T) {
3463 auto *DT = T->getContainedDeducedType();
3464 return DT && !DT->isDeduced();
3465}
3466
Richard Smithedbc6e92016-10-14 21:41:24 +00003467template<>
3468void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003469 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003470 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003471 auto *FD = static_cast<FunctionDecl *>(D);
3472 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003473
3474 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003475 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003476
3477 // If the previous declaration is an inline function declaration, then this
3478 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003479 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003480 // FIXME: [dcl.fct.spec]p4:
3481 // If a function with external linkage is declared inline in one
3482 // translation unit, it shall be declared inline in all translation
3483 // units in which it appears.
3484 //
3485 // Be careful of this case:
3486 //
3487 // module A:
3488 // template<typename T> struct X { void f(); };
3489 // template<typename T> inline void X<T>::f() {}
3490 //
3491 // module B instantiates the declaration of X<int>::f
3492 // module C instantiates the definition of X<int>::f
3493 //
3494 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003495 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003496 }
3497
Richard Smith6de7a242014-07-31 23:46:44 +00003498 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3499 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003500 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003501 // If we need to propagate an exception specification along the redecl
3502 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003503 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3504 bool WasUnresolved =
3505 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3506 if (IsUnresolved != WasUnresolved)
3507 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003508 {Canon, IsUnresolved ? PrevFD : FD});
3509
3510 // If we need to propagate a deduced return type along the redecl chain,
3511 // make a note of that so that we can do it later.
3512 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3513 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3514 if (IsUndeduced != WasUndeduced)
3515 Reader.PendingDeducedTypeUpdates.insert(
3516 {cast<FunctionDecl>(Canon),
3517 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003518 }
3519}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003520
3521} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003522
Richard Smith6de7a242014-07-31 23:46:44 +00003523void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003524 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3525}
3526
Richard Smith8346e522015-06-10 01:47:58 +00003527/// Inherit the default template argument from \p From to \p To. Returns
3528/// \c false if there is no default template for \p From.
3529template <typename ParmDecl>
3530static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3531 Decl *ToD) {
3532 auto *To = cast<ParmDecl>(ToD);
3533 if (!From->hasDefaultArgument())
3534 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003535 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003536 return true;
3537}
3538
3539static void inheritDefaultTemplateArguments(ASTContext &Context,
3540 TemplateDecl *From,
3541 TemplateDecl *To) {
3542 auto *FromTP = From->getTemplateParameters();
3543 auto *ToTP = To->getTemplateParameters();
3544 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3545
3546 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003547 NamedDecl *FromParam = FromTP->getParam(I);
3548 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003549
Richard Smith559cc692018-07-31 21:01:53 +00003550 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3551 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3552 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3553 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3554 else
3555 inheritDefaultTemplateArgument(
3556 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003557 }
3558}
3559
Richard Smith6de7a242014-07-31 23:46:44 +00003560void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003561 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003562 assert(D && Previous);
3563
3564 switch (D->getKind()) {
3565#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003566#define DECL(TYPE, BASE) \
3567 case Decl::TYPE: \
3568 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003569 break;
3570#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003571 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003572
3573 // If the declaration was visible in one module, a redeclaration of it in
3574 // another module remains visible even if it wouldn't be visible by itself.
3575 //
3576 // FIXME: In this case, the declaration should only be visible if a module
3577 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003578 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003579 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003580 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003581
Richard Smith8346e522015-06-10 01:47:58 +00003582 // If the declaration declares a template, it may inherit default arguments
3583 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003584 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003585 inheritDefaultTemplateArguments(Reader.getContext(),
3586 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003587}
3588
Richard Smithb321ecb2014-05-13 01:15:00 +00003589template<typename DeclT>
3590void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003591 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003592}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003593
Richard Smithb321ecb2014-05-13 01:15:00 +00003594void ASTDeclReader::attachLatestDeclImpl(...) {
3595 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3596}
3597
Douglas Gregor05f10352011-12-17 23:38:30 +00003598void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3599 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003600
3601 switch (D->getKind()) {
3602#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003603#define DECL(TYPE, BASE) \
3604 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003605 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3606 break;
3607#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003608 }
3609}
3610
Richard Smith851072e2014-05-19 20:59:20 +00003611template<typename DeclT>
3612void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3613 D->RedeclLink.markIncomplete();
3614}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003615
Richard Smith851072e2014-05-19 20:59:20 +00003616void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3617 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3618}
3619
3620void ASTReader::markIncompleteDeclChain(Decl *D) {
3621 switch (D->getKind()) {
3622#define ABSTRACT_DECL(TYPE)
3623#define DECL(TYPE, BASE) \
3624 case Decl::TYPE: \
3625 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3626 break;
3627#include "clang/AST/DeclNodes.inc"
3628 }
3629}
3630
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003631/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003632Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003633 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003634 SourceLocation DeclLoc;
3635 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003636 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003637 // Keep track of where we are in the stream, then jump back there
3638 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003639 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003640
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003641 ReadingKindTracker ReadingKind(Read_Decl, *this);
3642
Douglas Gregor1342e842009-07-06 18:54:52 +00003643 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003644 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003645
Sebastian Redl2c373b92010-10-05 15:59:54 +00003646 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003647 ASTRecordReader Record(*this, *Loc.F);
3648 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003649 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003650
Richard Smithdbafb6c2017-06-29 23:23:46 +00003651 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003652 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003653 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003654 case DECL_CONTEXT_LEXICAL:
3655 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003656 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003657 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003658 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003659 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003660 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003661 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003662 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003663 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003664 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003665 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003666 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003667 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003668 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003669 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003670 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003671 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003672 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003673 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003674 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003675 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003676 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003677 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003678 case DECL_EXPORT:
3679 D = ExportDecl::CreateDeserialized(Context, ID);
3680 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003681 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003682 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003683 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003684 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003685 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003686 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003687 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003688 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003689 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003690 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003691 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003692 break;
Richard Smith151c4562016-12-20 21:35:28 +00003693 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003694 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003695 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003696 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003697 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003698 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003699 case DECL_CONSTRUCTOR_USING_SHADOW:
3700 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3701 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003702 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003703 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003704 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003705 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003706 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003707 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003708 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003709 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003710 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003711 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003712 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003713 break;
Richard Smithbc491202017-02-17 20:05:37 +00003714 case DECL_CXX_DEDUCTION_GUIDE:
3715 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3716 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003717 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003718 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003719 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003720 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003721 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3722 break;
3723 case DECL_CXX_INHERITED_CONSTRUCTOR:
3724 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003725 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003726 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003727 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003728 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003729 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003730 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003731 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003732 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003733 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003734 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003735 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003736 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003737 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003738 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003739 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003740 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003741 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003742 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003743 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003744 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003745 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003746 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003747 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003748 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003749 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003750 case DECL_VAR_TEMPLATE:
3751 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3752 break;
3753 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3754 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3755 break;
3756 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3757 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3758 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003759 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003760 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003761 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003762 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003763 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003764 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003765 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003766 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003767 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003768 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003769 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003770 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003771 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003772 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3773 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003774 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003775 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003776 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003777 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003778 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3779 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003780 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003781 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003782 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003783 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003784 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003785 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003786 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003787 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003788 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003789 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003790 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003791 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003792 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003793 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003794 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003795 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003796 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003797 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003798 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003799 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003800 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003801 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003802 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003803 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003804 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003805 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003806 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003807 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003808 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003809 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003810 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003811 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003812 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003813 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003814 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003815 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003816 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003817 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003818 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003819 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003820 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003821 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003822 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003823 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003824 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003825 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003826 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003827 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003828 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003829 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003830 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003831 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003832 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003833 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003834 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003835 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003836 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003837 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003838 break;
3839 case DECL_BINDING:
3840 D = BindingDecl::CreateDeserialized(Context, ID);
3841 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003842 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003843 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003844 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003845 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003846 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003847 break;
John McCall5e77d762013-04-16 07:28:30 +00003848 case DECL_MS_PROPERTY:
3849 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3850 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003851 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003852 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003853 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003854 case DECL_CXX_BASE_SPECIFIERS:
3855 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003856 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003857 case DECL_CXX_CTOR_INITIALIZERS:
3858 Error("attempt to read a C++ ctor initializer record as a declaration");
3859 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003860 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003861 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003862 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003863 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003864 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003865 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003866 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003867 break;
Kelvin Li1408f912018-09-26 04:28:39 +00003868 case DECL_OMP_REQUIRES:
3869 D = OMPRequiresDecl::CreateDeserialized(Context, ID, Record.readInt());
3870 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003871 case DECL_OMP_DECLARE_REDUCTION:
3872 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3873 break;
Michael Kruse251e1482019-02-01 20:25:04 +00003874 case DECL_OMP_DECLARE_MAPPER:
3875 D = OMPDeclareMapperDecl::CreateDeserialized(Context, ID, Record.readInt());
3876 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003877 case DECL_OMP_CAPTUREDEXPR:
3878 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003879 break;
Nico Weber66220292016-03-02 17:28:48 +00003880 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003881 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003882 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003883 case DECL_PRAGMA_DETECT_MISMATCH:
3884 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003885 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003886 break;
Michael Han84324352013-02-22 17:15:32 +00003887 case DECL_EMPTY:
3888 D = EmptyDecl::CreateDeserialized(Context, ID);
3889 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003890 case DECL_OBJC_TYPE_PARAM:
3891 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3892 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003893 }
Chris Lattner487412d2009-04-27 05:27:42 +00003894
Sebastian Redlb3298c32010-08-18 23:56:48 +00003895 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003896 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003897 // Set the DeclContext before doing any deserialization, to make sure internal
3898 // calls to Decl::getASTContext() by Decl's methods will find the
3899 // TranslationUnitDecl without crashing.
3900 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003901 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003902
3903 // If this declaration is also a declaration context, get the
3904 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003905 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003906 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003907 if (Offsets.first &&
3908 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3909 return nullptr;
3910 if (Offsets.second &&
3911 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3912 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003913 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003914 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003915
Douglas Gregordab42432011-08-12 00:15:20 +00003916 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003917 PendingUpdateRecords.push_back(
3918 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003919
Douglas Gregor404cdde2012-01-27 01:47:08 +00003920 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003921 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003922 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3923 // we put the Decl in PendingDefinitions so we can pull the categories here.
3924 if (Class->isThisDeclarationADefinition() ||
3925 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003926 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00003927
Richard Smith13fb8602016-07-15 21:33:46 +00003928 // If we have deserialized a declaration that has a definition the
3929 // AST consumer might need to know about, queue it.
3930 // We don't pass it to the consumer immediately because we may be in recursive
3931 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003932 PotentiallyInterestingDecls.push_back(
3933 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003934
Douglas Gregordab42432011-08-12 00:15:20 +00003935 return D;
3936}
3937
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003938void ASTReader::PassInterestingDeclsToConsumer() {
3939 assert(Consumer);
3940
3941 if (PassingDeclsToConsumer)
3942 return;
3943
3944 // Guard variable to avoid recursively redoing the process of passing
3945 // decls to consumer.
3946 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3947 true);
3948
3949 // Ensure that we've loaded all potentially-interesting declarations
3950 // that need to be eagerly loaded.
3951 for (auto ID : EagerlyDeserializedDecls)
3952 GetDecl(ID);
3953 EagerlyDeserializedDecls.clear();
3954
3955 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003956 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003957 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003958 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3959 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003960 }
3961}
3962
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003963void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003964 // The declaration may have been modified by files later in the chain.
3965 // If this is the case, read the record containing the updates from each file
3966 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003967 serialization::GlobalDeclID ID = Record.ID;
3968 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003969 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003970 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003971
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003972 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00003973
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003974 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003975 auto UpdateOffsets = std::move(UpdI->second);
3976 DeclUpdateOffsets.erase(UpdI);
3977
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003978 // Check if this decl was interesting to the consumer. If we just loaded
3979 // the declaration, then we know it was interesting and we skip the call
3980 // to isConsumerInterestedIn because it is unsafe to call in the
3981 // current ASTReader state.
3982 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003983 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003984 for (auto &FileAndOffset : UpdateOffsets) {
3985 ModuleFile *F = FileAndOffset.first;
3986 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003987 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3988 SavedStreamPosition SavedPosition(Cursor);
3989 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003990 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003991 ASTRecordReader Record(*this, *F);
3992 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003993 (void)RecCode;
3994 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003995
David L. Jonesbe1557a2016-12-21 00:17:49 +00003996 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3997 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003998 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003999
4000 // We might have made this declaration interesting. If so, remember that
4001 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004002 if (!WasInteresting &&
4003 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
4004 PotentiallyInterestingDecls.push_back(
4005 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00004006 WasInteresting = true;
4007 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004008 }
4009 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00004010 // Add the lazy specializations to the template.
4011 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
4012 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
4013 "Must not have pending specializations");
4014 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
4015 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
4016 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
4017 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
4018 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
4019 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
4020 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00004021
4022 // Load the pending visible updates for this decl context, if it has any.
4023 auto I = PendingVisibleUpdates.find(ID);
4024 if (I != PendingVisibleUpdates.end()) {
4025 auto VisibleUpdates = std::move(I->second);
4026 PendingVisibleUpdates.erase(I);
4027
4028 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004029 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00004030 Lookups[DC].Table.add(
4031 Update.Mod, Update.Data,
4032 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
4033 DC->setHasExternalVisibleStorage(true);
4034 }
Chris Lattner487412d2009-04-27 05:27:42 +00004035}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004036
Richard Smithd61d4ac2015-08-22 20:13:39 +00004037void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
4038 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00004039 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00004040 if (FirstLocal != CanonDecl) {
4041 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
4042 ASTDeclReader::attachPreviousDecl(
4043 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
4044 CanonDecl);
4045 }
4046
4047 if (!LocalOffset) {
4048 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
4049 return;
4050 }
4051
4052 // Load the list of other redeclarations from this module file.
4053 ModuleFile *M = getOwningModuleFile(FirstLocal);
4054 assert(M && "imported decl from no module file");
4055
4056 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
4057 SavedStreamPosition SavedPosition(Cursor);
4058 Cursor.JumpToBit(LocalOffset);
4059
4060 RecordData Record;
4061 unsigned Code = Cursor.ReadCode();
4062 unsigned RecCode = Cursor.readRecord(Code, Record);
4063 (void)RecCode;
4064 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
4065
4066 // FIXME: We have several different dispatches on decl kind here; maybe
4067 // we should instead generate one loop per kind and dispatch up-front?
4068 Decl *MostRecent = FirstLocal;
4069 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4070 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00004071 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
4072 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00004073 }
Richard Smithc3a53252015-02-28 05:57:02 +00004074 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00004075}
4076
4077namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004078
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004079 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004080 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004081 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004082 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004083 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00004084 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004085 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004086 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004087 serialization::GlobalDeclID InterfaceID;
4088 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00004089
Douglas Gregor404cdde2012-01-27 01:47:08 +00004090 void add(ObjCCategoryDecl *Cat) {
4091 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00004092 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004093 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00004094
Douglas Gregor404cdde2012-01-27 01:47:08 +00004095 // Check for duplicate categories.
4096 if (Cat->getDeclName()) {
4097 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00004098 if (Existing &&
4099 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00004100 != Reader.getOwningModuleFile(Cat)) {
4101 // FIXME: We should not warn for duplicates in diamond:
4102 //
4103 // MT //
4104 // / \ //
4105 // ML MR //
4106 // \ / //
4107 // MB //
4108 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004109 // If there are duplicates in ML/MR, there will be warning when
4110 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004111 // importing.
4112 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4113 << Interface->getDeclName() << Cat->getDeclName();
4114 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4115 } else if (!Existing) {
4116 // Record this category.
4117 Existing = Cat;
4118 }
4119 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004120
Douglas Gregor404cdde2012-01-27 01:47:08 +00004121 // Add this category to the end of the chain.
4122 if (Tail)
4123 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4124 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004125 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004126 Tail = Cat;
4127 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004128
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004129 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004130 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004131 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004132 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4133 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004134 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004135 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4136 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004137 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004138 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004139 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004140 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004141
Douglas Gregor404cdde2012-01-27 01:47:08 +00004142 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004143 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004144 }
4145 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004146
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004147 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004148 // If we've loaded all of the category information we care about from
4149 // this module file, we're done.
4150 if (M.Generation <= PreviousGeneration)
4151 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004152
4153 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004154 // module file. If there is no such mapping, we'll find nothing here
4155 // (or in any module it imports).
4156 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4157 if (!LocalID)
4158 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004159
Douglas Gregor404cdde2012-01-27 01:47:08 +00004160 // Perform a binary search to find the local redeclarations for this
4161 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004162 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004163 const ObjCCategoriesInfo *Result
4164 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004165 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004166 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004167 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4168 Result->DefinitionID != LocalID) {
4169 // We didn't find anything. If the class definition is in this module
4170 // file, then the module files it depends on cannot have any categories,
4171 // so suppress further lookup.
4172 return Reader.isDeclIDFromModule(InterfaceID, M);
4173 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004174
Douglas Gregor404cdde2012-01-27 01:47:08 +00004175 // We found something. Dig out all of the categories.
4176 unsigned Offset = Result->Offset;
4177 unsigned N = M.ObjCCategories[Offset];
4178 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4179 for (unsigned I = 0; I != N; ++I)
4180 add(cast_or_null<ObjCCategoryDecl>(
4181 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4182 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004183 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004184 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004185
4186} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004187
Douglas Gregor404cdde2012-01-27 01:47:08 +00004188void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4189 ObjCInterfaceDecl *D,
4190 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004191 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004192 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004193 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004194}
Douglas Gregordab42432011-08-12 00:15:20 +00004195
Richard Smithd6db68c2014-08-07 20:58:41 +00004196template<typename DeclT, typename Fn>
4197static void forAllLaterRedecls(DeclT *D, Fn F) {
4198 F(D);
4199
4200 // Check whether we've already merged D into its redeclaration chain.
4201 // MostRecent may or may not be nullptr if D has not been merged. If
4202 // not, walk the merged redecl chain and see if it's there.
4203 auto *MostRecent = D->getMostRecentDecl();
4204 bool Found = false;
4205 for (auto *Redecl = MostRecent; Redecl && !Found;
4206 Redecl = Redecl->getPreviousDecl())
4207 Found = (Redecl == D);
4208
4209 // If this declaration is merged, apply the functor to all later decls.
4210 if (Found) {
4211 for (auto *Redecl = MostRecent; Redecl != D;
4212 Redecl = Redecl->getPreviousDecl())
4213 F(Redecl);
4214 }
4215}
4216
Vassil Vassilev7d264622017-06-30 22:40:17 +00004217void ASTDeclReader::UpdateDecl(Decl *D,
4218 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004219 while (Record.getIdx() < Record.size()) {
4220 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004221 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004222 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004223 // FIXME: If we also have an update record for instantiating the
4224 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004225 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004226 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004227 // FIXME: We should call addHiddenDecl instead, to add the member
4228 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004229 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004230 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004231 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004232
4233 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004234 // It will be added to the template's lazy specialization set.
4235 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004236 break;
4237
4238 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004239 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004240
Douglas Gregor540fd812012-01-09 18:07:24 +00004241 // Each module has its own anonymous namespace, which is disjoint from
4242 // any other module's anonymous namespaces, so don't attach the anonymous
4243 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004244 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004245 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004246 TU->setAnonymousNamespace(Anon);
4247 else
4248 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4249 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004250 break;
4251 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004252
Richard Smith891fc7f2017-12-05 01:31:47 +00004253 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004254 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004255 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4256 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004257 uint64_t Val = Record.readInt();
4258 if (Val && !VD->getInit()) {
4259 VD->setInit(Record.readExpr());
4260 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4261 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4262 Eval->CheckedICE = true;
4263 Eval->IsICE = Val == 3;
4264 }
4265 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004266 break;
Richard Smith05a21352017-06-22 22:18:46 +00004267 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004268
Richard Smith891fc7f2017-12-05 01:31:47 +00004269 case UPD_CXX_POINT_OF_INSTANTIATION: {
4270 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004271 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004272 VTSD->setPointOfInstantiation(POI);
4273 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4274 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4275 } else {
4276 auto *FD = cast<FunctionDecl>(D);
4277 if (auto *FTSInfo = FD->TemplateOrSpecialization
4278 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4279 FTSInfo->setPointOfInstantiation(POI);
4280 else
4281 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4282 ->setPointOfInstantiation(POI);
4283 }
4284 break;
4285 }
4286
John McCall32791cc2016-01-06 22:34:54 +00004287 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004288 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004289
4290 // We have to read the default argument regardless of whether we use it
4291 // so that hypothetical further update records aren't messed up.
4292 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004293 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004294
4295 // Only apply the update if the parameter still has an uninstantiated
4296 // default argument.
4297 if (Param->hasUninstantiatedDefaultArg())
4298 Param->setDefaultArg(DefaultArg);
4299 break;
4300 }
4301
Richard Smith4b054b22016-08-24 21:25:37 +00004302 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004303 auto *FD = cast<FieldDecl>(D);
4304 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004305
4306 // Only apply the update if the field still has an uninstantiated
4307 // default member initializer.
4308 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4309 if (DefaultInit)
4310 FD->setInClassInitializer(DefaultInit);
4311 else
4312 // Instantiation failed. We can get here if we serialized an AST for
4313 // an invalid program.
4314 FD->removeInClassInitializer();
4315 }
4316 break;
4317 }
4318
Richard Smith4d235792014-08-07 18:53:08 +00004319 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004320 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004321 if (Reader.PendingBodies[FD]) {
4322 // FIXME: Maybe check for ODR violations.
4323 // It's safe to stop now because this update record is always last.
4324 return;
4325 }
4326
David L. Jonesbe1557a2016-12-21 00:17:49 +00004327 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004328 // Maintain AST consistency: any later redeclarations of this function
4329 // are inline if this one is. (We might have merged another declaration
4330 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004331 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4332 FD->setImplicitlyInline();
4333 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004334 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004335 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004336 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004337 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004338 break;
4339 }
4340
Richard Smithcd45dbc2014-04-19 03:48:30 +00004341 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4342 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004343 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004344 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004345 OldDD && (OldDD->Definition != RD ||
4346 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004347 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004348 RD->setArgPassingRestrictions(
4349 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004350 ReadCXXRecordDefinition(RD, /*Update*/true);
4351
Richard Smithcd45dbc2014-04-19 03:48:30 +00004352 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004353 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004354 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004355 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004356 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004357 }
4358
David L. Jonesbe1557a2016-12-21 00:17:49 +00004359 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004360 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004361 if (MemberSpecializationInfo *MSInfo =
4362 RD->getMemberSpecializationInfo()) {
4363 MSInfo->setTemplateSpecializationKind(TSK);
4364 MSInfo->setPointOfInstantiation(POI);
4365 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004366 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004367 Spec->setTemplateSpecializationKind(TSK);
4368 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004369
David L. Jonesbe1557a2016-12-21 00:17:49 +00004370 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004371 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004372 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004373 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004374 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004375 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004376 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004377
4378 // FIXME: If we already have a partial specialization set,
4379 // check that it matches.
4380 if (!Spec->getSpecializedTemplateOrPartial()
4381 .is<ClassTemplatePartialSpecializationDecl *>())
4382 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004383 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004384 }
4385
David L. Jonesbe1557a2016-12-21 00:17:49 +00004386 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004387 RD->setLocation(ReadSourceLocation());
4388 RD->setLocStart(ReadSourceLocation());
4389 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004390
David L. Jonesbe1557a2016-12-21 00:17:49 +00004391 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004392 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004393 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004394 // If the declaration already has attributes, we assume that some other
4395 // AST file already loaded them.
4396 if (!D->hasAttrs())
4397 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004398 }
4399 break;
4400 }
4401
Richard Smithf8134002015-03-10 01:41:22 +00004402 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4403 // Set the 'operator delete' directly to avoid emitting another update
4404 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004405 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004406 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004407 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004408 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004409 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004410 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004411 First->OperatorDeleteThisArg = ThisArg;
4412 }
Richard Smithf8134002015-03-10 01:41:22 +00004413 break;
4414 }
4415
Richard Smith564417a2014-03-20 21:47:22 +00004416 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004417 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004418 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004419 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004420
4421 // Update this declaration's exception specification, if needed.
4422 auto *FD = cast<FunctionDecl>(D);
4423 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4424 // FIXME: If the exception specification is already present, check that it
4425 // matches.
4426 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004427 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004428 FPT->getReturnType(), FPT->getParamTypes(),
4429 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004430
4431 // When we get to the end of deserializing, see if there are other decls
4432 // that we need to propagate this exception specification onto.
4433 Reader.PendingExceptionSpecUpdates.insert(
4434 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004435 }
Richard Smith564417a2014-03-20 21:47:22 +00004436 break;
4437 }
4438
Richard Smith1fa5d642013-05-11 05:45:24 +00004439 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004440 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004441 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004442 Reader.PendingDeducedTypeUpdates.insert(
4443 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004444 break;
4445 }
Eli Friedman276dd182013-09-05 00:02:25 +00004446
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004447 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004448 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004449 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004450 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004451
4452 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004453 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4454 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004455 break;
4456
4457 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004458 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4459 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004460 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004461
Alexey Bataev97720002014-11-11 04:05:39 +00004462 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004463 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4464 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004465 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004466
Alex Denisovfde64952015-06-26 05:28:36 +00004467 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004468 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004469 auto *Exported = cast<NamedDecl>(D);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004470 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith13897eb2018-09-12 23:37:00 +00004471 Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
4472 Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004473 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004474 }
Alex Denisovfde64952015-06-26 05:28:36 +00004475
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004476 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00004477 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
4478 Reader.getContext(),
4479 static_cast<OMPDeclareTargetDeclAttr::MapTypeTy>(Record.readInt()),
4480 ReadSourceRange()));
4481 break;
4482
Alex Denisovfde64952015-06-26 05:28:36 +00004483 case UPD_ADDED_ATTR_TO_RECORD:
4484 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004485 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004486 assert(Attrs.size() == 1);
4487 D->addAttr(Attrs[0]);
4488 break;
4489 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004490 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004491}