blob: 359c86d0199c00984e53287f299c3fdd261d00f3 [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 Bataev25ed0c02019-03-07 17:54:44 +0000447 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000448 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Michael Kruse251e1482019-02-01 20:25:04 +0000449 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
Kelvin Li1408f912018-09-26 04:28:39 +0000450 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000451 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000452 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000453
454} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000455
Richard Smith86dfc1e2015-06-18 22:07:00 +0000456namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000457
Richard Smith86dfc1e2015-06-18 22:07:00 +0000458/// Iterator over the redeclarations of a declaration that have already
459/// been merged into the same redeclaration chain.
460template<typename DeclT>
461class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000462 DeclT *Start;
463 DeclT *Canonical = nullptr;
464 DeclT *Current = nullptr;
465
Richard Smith86dfc1e2015-06-18 22:07:00 +0000466public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000467 MergedRedeclIterator() = default;
468 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000469
470 DeclT *operator*() { return Current; }
471
472 MergedRedeclIterator &operator++() {
473 if (Current->isFirstDecl()) {
474 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000475 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000476 } else
477 Current = Current->getPreviousDecl();
478
479 // If we started in the merged portion, we'll reach our start position
480 // eventually. Otherwise, we'll never reach it, but the second declaration
481 // we reached was the canonical declaration, so stop when we see that one
482 // again.
483 if (Current == Start || Current == Canonical)
484 Current = nullptr;
485 return *this;
486 }
487
488 friend bool operator!=(const MergedRedeclIterator &A,
489 const MergedRedeclIterator &B) {
490 return A.Current != B.Current;
491 }
492};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000493
494} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000495
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000496template <typename DeclT>
497static llvm::iterator_range<MergedRedeclIterator<DeclT>>
498merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000499 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
500 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000501}
502
Sebastian Redlb3298c32010-08-18 23:56:48 +0000503uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000504 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000505}
506
David Blaikieac4345c2017-02-12 18:45:31 +0000507void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000508 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000509 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000510 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Erich Keane9c665062018-08-01 21:02:40 +0000511 CD->setNumCtorInitializers(Record.readInt());
512 if (CD->getNumCtorInitializers())
David Blaikieac4345c2017-02-12 18:45:31 +0000513 CD->CtorInitializers = ReadGlobalOffset();
514 }
515 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000516 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
517 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000518}
519
Sebastian Redlb3298c32010-08-18 23:56:48 +0000520void ASTDeclReader::Visit(Decl *D) {
521 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000522
Vassil Vassilev928c8252016-04-28 14:13:28 +0000523 // At this point we have deserialized and merged the decl and it is safe to
524 // update its canonical decl to signal that the entire entity is used.
525 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
526 IsDeclMarkedUsed = false;
527
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000528 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000529 if (auto *TInfo = DD->getTypeSourceInfo())
530 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000531 }
532
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000533 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000534 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000535 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000536
537 // If this is a tag declaration with a typedef name for linkage, it's safe
538 // to load that typedef now.
539 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000540 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
541 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000542 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000543 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000544 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000545 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000546 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000547 // We only read it if FD doesn't already have a body (e.g., from another
548 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000549 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000550 if (Record.readInt())
551 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000552 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000553}
554
Sebastian Redlb3298c32010-08-18 23:56:48 +0000555void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000556 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
557 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000558 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000559 // parameter or of a parameter of a function template immediately. These
560 // entities might be used in the formulation of its DeclContext (for
561 // example, a function parameter can be used in decltype() in trailing
562 // return type of the function). Use the translation unit DeclContext as a
563 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000564 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
565 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000566 if (!LexicalDCIDForTemplateParmDecl)
567 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000568 Reader.addPendingDeclContextInfo(D,
569 SemaDCIDForTemplateParmDecl,
570 LexicalDCIDForTemplateParmDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000571 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000572 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000573 auto *SemaDC = ReadDeclAs<DeclContext>();
574 auto *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000575 if (!LexicalDC)
576 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000577 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000578 // Avoid calling setLexicalDeclContext() directly because it uses
579 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000580 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
581 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000582 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000583 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000584 D->setInvalidDecl(Record.readInt());
585 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000586 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000587 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000588 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
589 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000590 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000591 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000592 D->setImplicit(Record.readInt());
593 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000594 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000595 D->setReferenced(Record.readInt());
596 D->setTopLevelDeclInObjCContainer(Record.readInt());
597 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000598 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000599 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000600
Douglas Gregorcf68c582011-12-01 22:20:10 +0000601 // Determine whether this declaration is part of a (sub)module. If so, it
602 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000603 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000604 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000605 D->setModuleOwnershipKind(
606 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
607 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000608 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000609
Richard Smith90dc5252017-06-23 01:04:34 +0000610 if (ModulePrivate) {
611 // Module-private declarations are never visible, so there is no work to
612 // do.
Richard Smith42413142015-05-15 20:05:43 +0000613 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
614 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000615 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000616 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000617 // Mark the declaration as visible when its owning module becomes visible.
618 if (Owner->NameVisibility == Module::AllVisible)
619 D->setVisibleDespiteOwningModule();
620 else
Richard Smith42413142015-05-15 20:05:43 +0000621 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000622 }
Richard Smithd19389a2017-07-05 07:47:11 +0000623 } else if (ModulePrivate) {
624 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000625 }
Chris Lattner487412d2009-04-27 05:27:42 +0000626}
627
Nico Weber66220292016-03-02 17:28:48 +0000628void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
629 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000630 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000631 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000632 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000633 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
634 D->getTrailingObjects<char>()[Arg.size()] = '\0';
635}
636
Nico Webercbbaeb12016-03-02 19:28:54 +0000637void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
638 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000639 D->setLocation(ReadSourceLocation());
640 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000641 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
642 D->getTrailingObjects<char>()[Name.size()] = '\0';
643
644 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000645 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000646 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
647 Value.size());
648 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
649}
650
Sebastian Redlb3298c32010-08-18 23:56:48 +0000651void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000652 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000653}
654
Sebastian Redlb3298c32010-08-18 23:56:48 +0000655void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000656 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000657 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000658 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000659}
660
Sebastian Redlb3298c32010-08-18 23:56:48 +0000661void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000662 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000663 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000664 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000665 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000666}
667
Richard Smith43ccec8e2014-08-26 03:52:16 +0000668ASTDeclReader::RedeclarableResult
669ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000670 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000671 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000672 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000673 if (Record.readInt()) { // isModed
674 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000675 TD->setModedTypeSourceInfo(TInfo, modedT);
676 } else
677 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000678 // Read and discard the declaration for which this is a typedef name for
679 // linkage, if it exists. We cannot rely on our type to pull in this decl,
680 // because it might have been merged with a type from another module and
681 // thus might not refer to our version of the declaration.
682 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000683 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000684}
685
686void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000687 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
688 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000689}
690
Richard Smithdda56e42011-04-15 14:24:37 +0000691void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000692 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000693 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000694 // Merged when we merge the template.
695 TD->setDescribedAliasTemplate(Template);
696 else
697 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000698}
699
Richard Smith1d209d02013-05-23 01:49:11 +0000700ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000701 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000702 VisitTypeDecl(TD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000703
David L. Jonesbe1557a2016-12-21 00:17:49 +0000704 TD->IdentifierNamespace = Record.readInt();
705 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000706 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000707 TD->setCompleteDefinition(Record.readInt());
708 TD->setEmbeddedInDeclarator(Record.readInt());
709 TD->setFreeStanding(Record.readInt());
710 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000711 TD->setBraceRange(ReadSourceRange());
Fangrui Song6907ce22018-07-30 19:24:48 +0000712
David L. Jonesbe1557a2016-12-21 00:17:49 +0000713 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000714 case 0:
715 break;
716 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000717 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000718 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000719 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000720 break;
721 }
722 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000723 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000724 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000725 break;
Richard Smith70d58502014-08-30 00:04:23 +0000726 default:
727 llvm_unreachable("unexpected tag info kind");
728 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000729
Richard Smithcd45dbc2014-04-19 03:48:30 +0000730 if (!isa<CXXRecordDecl>(TD))
731 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000732 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000733}
734
Sebastian Redlb3298c32010-08-18 23:56:48 +0000735void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000736 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000737 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000738 ED->setIntegerTypeSourceInfo(TI);
739 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000740 ED->setIntegerType(Record.readType());
741 ED->setPromotionType(Record.readType());
742 ED->setNumPositiveBits(Record.readInt());
743 ED->setNumNegativeBits(Record.readInt());
Erich Keanef92f31c2018-08-01 20:48:16 +0000744 ED->setScoped(Record.readInt());
745 ED->setScopedUsingClassTag(Record.readInt());
746 ED->setFixed(Record.readInt());
Richard Smith4b38ded2012-03-14 23:13:10 +0000747
Erich Keanef92f31c2018-08-01 20:48:16 +0000748 ED->setHasODRHash(true);
Richard Trieuab4d7302018-07-25 22:52:05 +0000749 ED->ODRHash = Record.readInt();
750
Richard Smith01a73372013-10-15 22:02:41 +0000751 // If this is a definition subject to the ODR, and we already have a
752 // definition, merge this one into it.
Erich Keanef92f31c2018-08-01 20:48:16 +0000753 if (ED->isCompleteDefinition() &&
Richard Smith01a73372013-10-15 22:02:41 +0000754 Reader.getContext().getLangOpts().Modules &&
755 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000756 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
757 if (!OldDef) {
758 // This is the first time we've seen an imported definition. Look for a
759 // local definition before deciding that we are the first definition.
760 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
761 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
762 OldDef = D;
763 break;
764 }
765 }
766 }
767 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000768 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
Erich Keanef92f31c2018-08-01 20:48:16 +0000769 ED->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +0000770 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000771 if (OldDef->getODRHash() != ED->getODRHash())
772 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000773 } else {
774 OldDef = ED;
775 }
776 }
777
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000778 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
779 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000780 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000781 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
782 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
783 }
Chris Lattner487412d2009-04-27 05:27:42 +0000784}
785
Richard Smith1d209d02013-05-23 01:49:11 +0000786ASTDeclReader::RedeclarableResult
787ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
788 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000789 RD->setHasFlexibleArrayMember(Record.readInt());
790 RD->setAnonymousStructOrUnion(Record.readInt());
791 RD->setHasObjectMember(Record.readInt());
792 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000793 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
794 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
795 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000796 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000797 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000798 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000799}
800
Sebastian Redlb3298c32010-08-18 23:56:48 +0000801void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000802 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000803 // For function declarations, defer reading the type in case the function has
804 // a deduced return type that references an entity declared within the
805 // function.
806 if (isa<FunctionDecl>(VD))
807 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
808 else
809 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000810}
811
Sebastian Redlb3298c32010-08-18 23:56:48 +0000812void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000813 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000814 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000815 ECD->setInitExpr(Record.readExpr());
816 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000817 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000818}
819
Sebastian Redlb3298c32010-08-18 23:56:48 +0000820void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000821 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000822 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000823 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000824 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000825 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000826 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000827 }
Richard Smithc23d7342018-06-29 20:46:25 +0000828 QualType TSIType = Record.readType();
829 DD->setTypeSourceInfo(
830 TSIType.isNull() ? nullptr
831 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000832}
833
Sebastian Redlb3298c32010-08-18 23:56:48 +0000834void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000835 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000836 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000837
Richard Smith600adef2018-07-04 02:25:38 +0000838 // Attach a type to this function. Use the real type if possible, but fall
839 // back to the type as written if it involves a deduced return type.
840 if (FD->getTypeSourceInfo() &&
841 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
842 ->getReturnType()->getContainedAutoType()) {
843 // We'll set up the real type in Visit, once we've finished loading the
844 // function.
845 FD->setType(FD->getTypeSourceInfo()->getType());
Richard Smitha62d1982018-08-03 01:00:01 +0000846 Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
Richard Smith600adef2018-07-04 02:25:38 +0000847 } else {
848 FD->setType(Reader.GetType(DeferredTypeID));
Richard Smith600adef2018-07-04 02:25:38 +0000849 }
Richard Smitha62d1982018-08-03 01:00:01 +0000850 DeferredTypeID = 0;
Richard Smith600adef2018-07-04 02:25:38 +0000851
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000852 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000853 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000854
Douglas Gregorb2585692012-01-04 17:13:46 +0000855 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
856 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000857
Erich Keane9c665062018-08-01 21:02:40 +0000858 FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
859 FD->setInlineSpecified(Record.readInt());
860 FD->setImplicitlyInline(Record.readInt());
Erich Keane9c665062018-08-01 21:02:40 +0000861 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());
Gauthier Harnisch796ed032019-06-14 08:56:20 +0000871 FD->setConstexprKind(static_cast<ConstexprSpecKind>(Record.readInt()));
Erich Keane9c665062018-08-01 21:02:40 +0000872 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]);
Richard Smithf19a8b02019-05-02 00:49:14 +0000930
931 MemberSpecializationInfo *MSInfo = nullptr;
932 if (Record.readInt()) {
933 auto *FD = ReadDeclAs<FunctionDecl>();
934 auto TSK = (TemplateSpecializationKind)Record.readInt();
935 SourceLocation POI = ReadSourceLocation();
936
937 MSInfo = new (C) MemberSpecializationInfo(FD, TSK);
938 MSInfo->setPointOfInstantiation(POI);
939 }
940
941 FunctionTemplateSpecializationInfo *FTInfo =
942 FunctionTemplateSpecializationInfo::Create(
943 C, FD, Template, TSK, TemplArgList,
944 HasTemplateArgumentsAsWritten ? &TemplArgsInfo : nullptr, POI,
945 MSInfo);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000946 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000947
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000948 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000949 // The template that contains the specializations set. It's not safe to
950 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000951 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000952 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
953 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
954 // FunctionTemplateSpecializationInfo's Profile().
955 // We avoid getASTContext because a decl in the parent hierarchy may
956 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000957 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000958 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000959 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000960 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000961 FunctionTemplateSpecializationInfo *ExistingInfo =
962 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000963 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000964 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
965 else {
966 assert(Reader.getContext().getLangOpts().Modules &&
967 "already deserialized this template specialization");
Richard Smithf19a8b02019-05-02 00:49:14 +0000968 mergeRedeclarable(FD, ExistingInfo->getFunction(), Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000969 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000970 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000971 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000972 }
973 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
974 // Templates.
975 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000976 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000977 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000978 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
979
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000980 // Templates args.
981 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000982 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000983 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000984 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000985 TemplArgs.setLAngleLoc(ReadSourceLocation());
986 TemplArgs.setRAngleLoc(ReadSourceLocation());
987
Douglas Gregor4163aca2011-09-09 21:34:22 +0000988 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000989 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000990 // These are not merged; we don't need to merge redeclarations of dependent
991 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000992 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000993 }
994 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000995
Chris Lattnerca025db2010-05-07 21:43:38 +0000996 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000997 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000998 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000999 Params.reserve(NumParams);
1000 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001001 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001002 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +00001003}
1004
Sebastian Redlb3298c32010-08-18 23:56:48 +00001005void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001006 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001007 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00001008 // Load the body on-demand. Most clients won't care, because method
1009 // definitions rarely show up in headers.
1010 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1011 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001012 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1013 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001014 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001015 MD->setInstanceMethod(Record.readInt());
1016 MD->setVariadic(Record.readInt());
1017 MD->setPropertyAccessor(Record.readInt());
1018 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001019 MD->setOverriding(Record.readInt());
1020 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001021
Erich Keane9b18eca2018-08-01 21:31:08 +00001022 MD->setIsRedeclaration(Record.readInt());
1023 MD->setHasRedeclaration(Record.readInt());
1024 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001025 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001026 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001027
David L. Jonesbe1557a2016-12-21 00:17:49 +00001028 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1029 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001030 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001031 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001032 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1033 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001034 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001035 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001036 Params.reserve(NumParams);
1037 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001038 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001039
Erich Keane9b18eca2018-08-01 21:31:08 +00001040 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001041 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001042 SmallVector<SourceLocation, 16> SelLocs;
1043 SelLocs.reserve(NumStoredSelLocs);
1044 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001045 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001046
1047 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001048}
1049
Douglas Gregor85f3f952015-07-07 03:57:15 +00001050void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001051 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001052
David L. Jonesbe1557a2016-12-21 00:17:49 +00001053 D->Variance = Record.readInt();
1054 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001055 D->VarianceLoc = ReadSourceLocation();
1056 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001057}
1058
Sebastian Redlb3298c32010-08-18 23:56:48 +00001059void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001060 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001061 CD->setAtStartLoc(ReadSourceLocation());
1062 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001063}
1064
Douglas Gregor85f3f952015-07-07 03:57:15 +00001065ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001066 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001067 if (numParams == 0)
1068 return nullptr;
1069
1070 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1071 typeParams.reserve(numParams);
1072 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001073 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001074 if (!typeParam)
1075 return nullptr;
1076
1077 typeParams.push_back(typeParam);
1078 }
1079
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001080 SourceLocation lAngleLoc = ReadSourceLocation();
1081 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001082
1083 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1084 typeParams, rAngleLoc);
1085}
1086
Manman Renec315f12016-09-09 23:48:27 +00001087void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001088 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001089 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001090 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001091
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001092 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001093 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001094
Manman Renec315f12016-09-09 23:48:27 +00001095 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001096 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001097 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1098 Protocols.reserve(NumProtocols);
1099 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001100 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001101 SmallVector<SourceLocation, 16> ProtoLocs;
1102 ProtoLocs.reserve(NumProtocols);
1103 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001104 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001105 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1106 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001107
Manman Renec315f12016-09-09 23:48:27 +00001108 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001109 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001110 Protocols.clear();
1111 Protocols.reserve(NumProtocols);
1112 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001113 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001114 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1115 Reader.getContext());
1116}
1117
1118void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1119 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1120 // FIXME: odr checking?
1121}
1122
Sebastian Redlb3298c32010-08-18 23:56:48 +00001123void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001124 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001125 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001126 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001127 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001128
1129 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001130 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001131 // Read the definition.
1132 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001133
David L. Jonesbe1557a2016-12-21 00:17:49 +00001134 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001135 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1136 if (Canon->Data.getPointer()) {
1137 // If we already have a definition, keep the definition invariant and
1138 // merge the data.
1139 MergeDefinitionData(Canon, std::move(ID->data()));
1140 ID->Data = Canon->Data;
1141 } else {
1142 // Set the definition data of the canonical declaration, so other
1143 // redeclarations will see it.
1144 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001145
Manman Renec315f12016-09-09 23:48:27 +00001146 // We will rebuild this list lazily.
1147 ID->setIvarList(nullptr);
1148 }
Craig Toppera13603a2014-05-22 05:54:18 +00001149
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001150 // Note that we have deserialized a definition.
1151 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001152
Douglas Gregor404cdde2012-01-27 01:47:08 +00001153 // Note that we've loaded this Objective-C class.
1154 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001155 } else {
1156 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001157 }
Chris Lattner487412d2009-04-27 05:27:42 +00001158}
1159
Sebastian Redlb3298c32010-08-18 23:56:48 +00001160void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001161 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001162 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001163 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001164 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001165 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001166 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001167}
1168
Graydon Hoaree0a68352017-06-28 18:36:27 +00001169void ASTDeclReader::ReadObjCDefinitionData(
1170 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001171 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001172 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1173 ProtoRefs.reserve(NumProtoRefs);
1174 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001175 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001176 SmallVector<SourceLocation, 16> ProtoLocs;
1177 ProtoLocs.reserve(NumProtoRefs);
1178 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001179 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001180 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1181 ProtoLocs.data(), Reader.getContext());
1182}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001183
Graydon Hoaree0a68352017-06-28 18:36:27 +00001184void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1185 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1186 // FIXME: odr checking?
1187}
1188
1189void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1190 RedeclarableResult Redecl = VisitRedeclarable(PD);
1191 VisitObjCContainerDecl(PD);
1192 mergeRedeclarable(PD, Redecl);
1193
1194 if (Record.readInt()) {
1195 // Read the definition.
1196 PD->allocateDefinitionData();
1197
1198 ReadObjCDefinitionData(PD->data());
1199
1200 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1201 if (Canon->Data.getPointer()) {
1202 // If we already have a definition, keep the definition invariant and
1203 // merge the data.
1204 MergeDefinitionData(Canon, std::move(PD->data()));
1205 PD->Data = Canon->Data;
1206 } else {
1207 // Set the definition data of the canonical declaration, so other
1208 // redeclarations will see it.
1209 PD->getCanonicalDecl()->Data = PD->Data;
1210 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001211 // Note that we have deserialized a definition.
1212 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001213 } else {
1214 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001215 }
Chris Lattner487412d2009-04-27 05:27:42 +00001216}
1217
Sebastian Redlb3298c32010-08-18 23:56:48 +00001218void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001219 VisitFieldDecl(FD);
1220}
1221
Sebastian Redlb3298c32010-08-18 23:56:48 +00001222void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001223 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001224 CD->setCategoryNameLoc(ReadSourceLocation());
1225 CD->setIvarLBraceLoc(ReadSourceLocation());
1226 CD->setIvarRBraceLoc(ReadSourceLocation());
1227
Douglas Gregor404cdde2012-01-27 01:47:08 +00001228 // Note that this category has been deserialized. We do this before
1229 // deserializing the interface declaration, so that it will consider this
1230 /// category.
1231 Reader.CategoriesDeserialized.insert(CD);
1232
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001233 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001234 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001235 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001236 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001237 ProtoRefs.reserve(NumProtoRefs);
1238 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001239 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001240 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001241 ProtoLocs.reserve(NumProtoRefs);
1242 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001243 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001244 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001245 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001246
1247 // Protocols in the class extension belong to the class.
1248 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1249 CD->ClassInterface->mergeClassExtensionProtocolList(
1250 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1251 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001252}
1253
Sebastian Redlb3298c32010-08-18 23:56:48 +00001254void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001255 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001256 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001257}
1258
Sebastian Redlb3298c32010-08-18 23:56:48 +00001259void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001260 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001261 D->setAtLoc(ReadSourceLocation());
1262 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001263 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001264 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001265 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001266 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001267 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001268 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001269 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001270 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001271 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001272 DeclarationName GetterName = Record.readDeclarationName();
1273 SourceLocation GetterLoc = ReadSourceLocation();
1274 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1275 DeclarationName SetterName = Record.readDeclarationName();
1276 SourceLocation SetterLoc = ReadSourceLocation();
1277 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001278 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1279 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1280 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001281}
1282
Sebastian Redlb3298c32010-08-18 23:56:48 +00001283void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001284 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001285 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001286}
1287
Sebastian Redlb3298c32010-08-18 23:56:48 +00001288void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001289 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001290 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001291}
1292
Sebastian Redlb3298c32010-08-18 23:56:48 +00001293void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001294 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001295 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1296 D->SuperLoc = ReadSourceLocation();
1297 D->setIvarLBraceLoc(ReadSourceLocation());
1298 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001299 D->setHasNonZeroConstructors(Record.readInt());
1300 D->setHasDestructors(Record.readInt());
1301 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001302 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001303 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001304}
1305
Sebastian Redlb3298c32010-08-18 23:56:48 +00001306void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001307 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001308 D->setAtLoc(ReadSourceLocation());
1309 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1310 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1311 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001312 D->setGetterCXXConstructor(Record.readExpr());
1313 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001314}
1315
Sebastian Redlb3298c32010-08-18 23:56:48 +00001316void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001317 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001318 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001319
1320 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1321 FD->InitStorage.setInt(ISK);
1322 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1323 ? Record.readType().getAsOpaquePtr()
1324 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001325 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001326
1327 if (auto *BW = Record.readExpr())
1328 FD->setBitWidth(BW);
1329
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001330 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001331 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001332 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001333 }
Richard Smith0b87e072013-10-07 08:02:11 +00001334 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001335}
1336
John McCall5e77d762013-04-16 07:28:30 +00001337void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1338 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001339 PD->GetterId = Record.getIdentifierInfo();
1340 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001341}
1342
Francois Pichet783dd6e2010-11-21 06:08:52 +00001343void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1344 VisitValueDecl(FD);
1345
David L. Jonesbe1557a2016-12-21 00:17:49 +00001346 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001347 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001348 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001349
1350 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001351 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001352
1353 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001354}
1355
Larisse Voufo39a1e502013-08-06 01:03:05 +00001356ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001357 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001358 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001359
David L. Jonesbe1557a2016-12-21 00:17:49 +00001360 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1361 VD->VarDeclBits.TSCSpec = Record.readInt();
1362 VD->VarDeclBits.InitStyle = Record.readInt();
Erik Pilkington1e368822019-01-04 18:33:06 +00001363 VD->VarDeclBits.ARCPseudoStrong = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001364 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001365 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1366 Record.readInt();
1367 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001368 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001369 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001370 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001371 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1372 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1373 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1374 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1375 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001376 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
Akira Hatanaka8e57b072018-10-01 21:51:28 +00001377 VD->NonParmVarDeclBits.EscapingByref = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001378 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001379 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001380 VD->setCachedLinkage(VarLinkage);
1381
1382 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001383 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001384 VD->getLexicalDeclContext()->isFunctionOrMethod())
1385 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001386
David L. Jonesbe1557a2016-12-21 00:17:49 +00001387 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001388 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001389 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001390 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1391 Eval->CheckedICE = true;
1392 Eval->IsICE = Val == 3;
1393 }
1394 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001395
Akira Hatanaka9978da32018-08-10 15:09:24 +00001396 if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1397 Expr *CopyExpr = Record.readExpr();
1398 if (CopyExpr)
1399 Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1400 }
1401
Richard Smitha4653622017-09-06 20:01:14 +00001402 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1403 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1404
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001405 enum VarKind {
1406 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1407 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001408 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001409 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001410 // Only true variables (not parameters or implicit parameters) can be
1411 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001412 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1413 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001414 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001415 break;
1416 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001417 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001418 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001419 break;
1420 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001421 auto *Tmpl = ReadDeclAs<VarDecl>();
1422 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001423 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001424 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001425 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001426 break;
1427 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001428 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001429
1430 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001431}
1432
Sebastian Redlb3298c32010-08-18 23:56:48 +00001433void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001434 VisitVarDecl(PD);
1435}
1436
Sebastian Redlb3298c32010-08-18 23:56:48 +00001437void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001438 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001439 unsigned isObjCMethodParam = Record.readInt();
1440 unsigned scopeDepth = Record.readInt();
1441 unsigned scopeIndex = Record.readInt();
1442 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001443 if (isObjCMethodParam) {
1444 assert(scopeDepth == 0);
1445 PD->setObjCMethodScopeInfo(scopeIndex);
1446 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1447 } else {
1448 PD->setScopeInfo(scopeDepth, scopeIndex);
1449 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001450 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1451 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1452 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001453 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001454
1455 // FIXME: If this is a redeclaration of a function from another module, handle
1456 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001457}
1458
Richard Smith7b76d812016-08-12 02:21:25 +00001459void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1460 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001461 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith0353e5a2019-05-25 01:04:17 +00001462 for (unsigned I = 0; I != DD->NumBindings; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001463 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith0353e5a2019-05-25 01:04:17 +00001464 BDs[I]->setDecomposedDecl(DD);
1465 }
Richard Smith7b76d812016-08-12 02:21:25 +00001466}
1467
1468void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1469 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001470 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001471}
1472
Sebastian Redlb3298c32010-08-18 23:56:48 +00001473void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001474 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001475 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001476 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001477}
1478
Sebastian Redlb3298c32010-08-18 23:56:48 +00001479void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001480 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001481 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001482 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001483 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001484 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001485 Params.reserve(NumParams);
1486 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001487 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001488 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001489
David L. Jonesbe1557a2016-12-21 00:17:49 +00001490 BD->setIsVariadic(Record.readInt());
1491 BD->setBlockMissingReturnType(Record.readInt());
1492 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001493 BD->setDoesNotEscape(Record.readInt());
Akira Hatanakac5792aa2019-02-27 18:17:16 +00001494 BD->setCanAvoidCopyToHeap(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001495
David L. Jonesbe1557a2016-12-21 00:17:49 +00001496 bool capturesCXXThis = Record.readInt();
1497 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001498 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001499 captures.reserve(numCaptures);
1500 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001501 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001502 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001503 bool byRef = (flags & 1);
1504 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001505 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001506
1507 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1508 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001509 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001510}
1511
Ben Langmuirce914fc2013-05-03 19:20:19 +00001512void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1513 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001514 unsigned ContextParamPos = Record.readInt();
1515 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001516 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001517 for (unsigned I = 0; I < CD->NumParams; ++I) {
1518 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001519 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001520 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001521 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001522 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001523}
1524
Sebastian Redlb3298c32010-08-18 23:56:48 +00001525void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001526 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001527 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001528 D->setExternLoc(ReadSourceLocation());
1529 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001530}
1531
Richard Smith8df390f2016-09-08 23:14:54 +00001532void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1533 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001534 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001535}
1536
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001537void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1538 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001539 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001540}
1541
Sebastian Redlb3298c32010-08-18 23:56:48 +00001542void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001543 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001544 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001545 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001546 D->LocStart = ReadSourceLocation();
1547 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001548
Richard Smith15e32fd2015-03-17 02:23:11 +00001549 // Defer loading the anonymous namespace until we've finished merging
1550 // this namespace; loading it might load a later declaration of the
1551 // same namespace, and we have an invariant that older declarations
1552 // get merged before newer ones try to merge.
1553 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001554 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001555 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001556 } else {
1557 // Link this namespace back to the first declaration, which has already
1558 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001559 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001560 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001561
1562 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001563
1564 if (AnonNamespace) {
1565 // Each module has its own anonymous namespace, which is disjoint from
1566 // any other module's anonymous namespaces, so don't attach the anonymous
1567 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001568 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001569 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001570 D->setAnonymousNamespace(Anon);
1571 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001572}
1573
Sebastian Redlb3298c32010-08-18 23:56:48 +00001574void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001575 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001576 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001577 D->NamespaceLoc = ReadSourceLocation();
1578 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001579 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001580 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001581 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001582}
1583
Sebastian Redlb3298c32010-08-18 23:56:48 +00001584void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001585 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001586 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001587 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001588 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1589 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001590 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001591 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001592 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001593 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001594}
1595
Richard Smith151c4562016-12-20 21:35:28 +00001596void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1597 VisitNamedDecl(D);
1598 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001599 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001600 for (unsigned I = 0; I != D->NumExpansions; ++I)
1601 Expansions[I] = ReadDeclAs<NamedDecl>();
1602 mergeMergeable(D);
1603}
1604
Sebastian Redlb3298c32010-08-18 23:56:48 +00001605void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001606 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001607 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001608 D->Underlying = ReadDeclAs<NamedDecl>();
1609 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001610 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001611 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001612 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001613 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001614 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001615}
1616
Richard Smith5179eb72016-06-28 19:03:57 +00001617void ASTDeclReader::VisitConstructorUsingShadowDecl(
1618 ConstructorUsingShadowDecl *D) {
1619 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001620 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1621 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001622 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001623}
1624
Sebastian Redlb3298c32010-08-18 23:56:48 +00001625void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001626 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001627 D->UsingLoc = ReadSourceLocation();
1628 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001629 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001630 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1631 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001632}
1633
Sebastian Redlb3298c32010-08-18 23:56:48 +00001634void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001635 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001636 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001637 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001638 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001639 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001640 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001641}
1642
Sebastian Redlb3298c32010-08-18 23:56:48 +00001643void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001644 UnresolvedUsingTypenameDecl *D) {
1645 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001646 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001647 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001648 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001649 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001650}
1651
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001652void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001653 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001654 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001655 Data.UserDeclaredConstructor = Record.readInt();
1656 Data.UserDeclaredSpecialMembers = Record.readInt();
1657 Data.Aggregate = Record.readInt();
1658 Data.PlainOldData = Record.readInt();
1659 Data.Empty = Record.readInt();
1660 Data.Polymorphic = Record.readInt();
1661 Data.Abstract = Record.readInt();
1662 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001663 Data.IsCXX11StandardLayout = Record.readInt();
1664 Data.HasBasesWithFields = Record.readInt();
1665 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001666 Data.HasPrivateFields = Record.readInt();
1667 Data.HasProtectedFields = Record.readInt();
1668 Data.HasPublicFields = Record.readInt();
1669 Data.HasMutableFields = Record.readInt();
1670 Data.HasVariantMembers = Record.readInt();
1671 Data.HasOnlyCMembers = Record.readInt();
1672 Data.HasInClassInitializer = Record.readInt();
1673 Data.HasUninitializedReferenceMember = Record.readInt();
1674 Data.HasUninitializedFields = Record.readInt();
1675 Data.HasInheritedConstructor = Record.readInt();
1676 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001677 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001678 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1679 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1680 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001681 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001682 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1683 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1684 Data.DefaultedDestructorIsDeleted = Record.readInt();
1685 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001686 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001687 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001688 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001689 Data.HasIrrelevantDestructor = Record.readInt();
1690 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1691 Data.HasDefaultedDefaultConstructor = Record.readInt();
1692 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1693 Data.HasConstexprDefaultConstructor = Record.readInt();
1694 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1695 Data.ComputedVisibleConversions = Record.readInt();
1696 Data.UserProvidedDefaultConstructor = Record.readInt();
1697 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001698 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1699 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001700 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1701 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1702 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001703 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001704 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001705
Richard Smithcd4a7a42017-09-07 00:55:55 +00001706 if (Record.readInt())
1707 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001708
David L. Jonesbe1557a2016-12-21 00:17:49 +00001709 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001710 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001711 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001712 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001713 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001714 Data.VBases = ReadGlobalOffset();
1715
David L. Jonesb6a8f022016-12-21 04:34:52 +00001716 Record.readUnresolvedSet(Data.Conversions);
1717 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001718 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001719 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001720
Douglas Gregor99ae8062012-02-14 17:54:36 +00001721 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001722 using Capture = LambdaCapture;
1723
1724 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001725 Lambda.Dependent = Record.readInt();
1726 Lambda.IsGenericLambda = Record.readInt();
1727 Lambda.CaptureDefault = Record.readInt();
1728 Lambda.NumCaptures = Record.readInt();
1729 Lambda.NumExplicitCaptures = Record.readInt();
1730 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001731 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001732 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1733 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001734 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001735 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001736 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001737 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001738 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001739 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001740 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001741 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001742 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001743 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001744 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001745 break;
1746 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001747 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001748 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001749 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001750 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1751 break;
1752 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001753 }
1754 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001755}
1756
Richard Smithcd45dbc2014-04-19 03:48:30 +00001757void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001758 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001759 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001760 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001761 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001762
Richard Smith02793752015-03-27 21:16:39 +00001763 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001764 // Track that we merged the definitions.
1765 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1766 DD.Definition));
1767 Reader.PendingDefinitions.erase(MergeDD.Definition);
Erich Keanef92f31c2018-08-01 20:48:16 +00001768 MergeDD.Definition->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +00001769 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001770 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1771 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001772 }
1773
Richard Smith2a9e5c52015-02-03 03:32:14 +00001774 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1775 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1776 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001777 // We faked up this definition data because we found a class for which we'd
1778 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001779 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001780 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001781
1782 // Don't change which declaration is the definition; that is required
1783 // to be invariant once we select it.
1784 auto *Def = DD.Definition;
1785 DD = std::move(MergeDD);
1786 DD.Definition = Def;
1787 return;
1788 }
1789
Richard Smithcd45dbc2014-04-19 03:48:30 +00001790 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001791 bool DetectedOdrViolation = false;
1792#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1793#define MATCH_FIELD(Field) \
1794 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1795 OR_FIELD(Field)
1796 MATCH_FIELD(UserDeclaredConstructor)
1797 MATCH_FIELD(UserDeclaredSpecialMembers)
1798 MATCH_FIELD(Aggregate)
1799 MATCH_FIELD(PlainOldData)
1800 MATCH_FIELD(Empty)
1801 MATCH_FIELD(Polymorphic)
1802 MATCH_FIELD(Abstract)
1803 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001804 MATCH_FIELD(IsCXX11StandardLayout)
1805 MATCH_FIELD(HasBasesWithFields)
1806 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001807 MATCH_FIELD(HasPrivateFields)
1808 MATCH_FIELD(HasProtectedFields)
1809 MATCH_FIELD(HasPublicFields)
1810 MATCH_FIELD(HasMutableFields)
1811 MATCH_FIELD(HasVariantMembers)
1812 MATCH_FIELD(HasOnlyCMembers)
1813 MATCH_FIELD(HasInClassInitializer)
1814 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001815 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001816 MATCH_FIELD(HasInheritedConstructor)
1817 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001818 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001819 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1820 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1821 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001822 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001823 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1824 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1825 MATCH_FIELD(DefaultedDestructorIsDeleted)
1826 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001827 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001828 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001829 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001830 MATCH_FIELD(HasIrrelevantDestructor)
1831 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001832 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001833 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1834 OR_FIELD(HasConstexprDefaultConstructor)
1835 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1836 // ComputedVisibleConversions is handled below.
1837 MATCH_FIELD(UserProvidedDefaultConstructor)
1838 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001839 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1840 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001841 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1842 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1843 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1844 MATCH_FIELD(IsLambda)
1845#undef OR_FIELD
1846#undef MATCH_FIELD
1847
1848 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1849 DetectedOdrViolation = true;
1850 // FIXME: Issue a diagnostic if the base classes don't match when we come
1851 // to lazily load them.
1852
1853 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1854 // match when we come to lazily load them.
1855 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1856 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1857 DD.ComputedVisibleConversions = true;
1858 }
1859
1860 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1861 // lazily load it.
1862
1863 if (DD.IsLambda) {
1864 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1865 // when they occur within the body of a function template specialization).
1866 }
1867
Richard Trieufd1acbb2017-04-11 21:31:00 +00001868 if (D->getODRHash() != MergeDD.ODRHash) {
1869 DetectedOdrViolation = true;
1870 }
1871
Richard Smithcd45dbc2014-04-19 03:48:30 +00001872 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001873 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1874 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001875}
1876
Richard Smith2a9e5c52015-02-03 03:32:14 +00001877void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001878 struct CXXRecordDecl::DefinitionData *DD;
1879 ASTContext &C = Reader.getContext();
1880
1881 // Determine whether this is a lambda closure type, so that we can
1882 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001883 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001884 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001885 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001886 LCD_None);
1887 else
1888 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1889
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001890 CXXRecordDecl *Canon = D->getCanonicalDecl();
1891 // Set decl definition data before reading it, so that during deserialization
1892 // when we read CXXRecordDecl, it already has definition data and we don't
1893 // set fake one.
1894 if (!Canon->DefinitionData)
1895 Canon->DefinitionData = DD;
1896 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001897 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001898
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001899 // We might already have a different definition for this record. This can
1900 // happen either because we're reading an update record, or because we've
1901 // already done some merging. Either way, just merge into it.
1902 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001903 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001904 return;
1905 }
1906
Richard Smith97100e32015-06-20 00:22:34 +00001907 // Mark this declaration as being a definition.
Erich Keanef92f31c2018-08-01 20:48:16 +00001908 D->setCompleteDefinition(true);
Richard Smith2a9e5c52015-02-03 03:32:14 +00001909
Richard Smith97100e32015-06-20 00:22:34 +00001910 // If this is not the first declaration or is an update record, we can have
1911 // other redeclarations already. Make a note that we need to propagate the
1912 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001913 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001914 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001915}
1916
Richard Smith1d209d02013-05-23 01:49:11 +00001917ASTDeclReader::RedeclarableResult
1918ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1919 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001920
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001921 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001922
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001923 enum CXXRecKind {
1924 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1925 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001926 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001927 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001928 // Merged when we merge the folding set entry in the primary template.
1929 if (!isa<ClassTemplateSpecializationDecl>(D))
1930 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001931 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001932 case CXXRecTemplate: {
1933 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001934 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001935 D->TemplateOrInstantiation = Template;
1936 if (!Template->getTemplatedDecl()) {
1937 // We've not actually loaded the ClassTemplateDecl yet, because we're
1938 // currently being loaded as its pattern. Rely on it to set up our
1939 // TypeForDecl (see VisitClassTemplateDecl).
1940 //
1941 // Beware: we do not yet know our canonical declaration, and may still
1942 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001943 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001944 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001945 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001946 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001947 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001948 auto *RD = ReadDeclAs<CXXRecordDecl>();
1949 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001950 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001951 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1952 MSI->setPointOfInstantiation(POI);
1953 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001954 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001955 break;
1956 }
1957 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001958
David L. Jonesbe1557a2016-12-21 00:17:49 +00001959 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001960 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001961 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001962 else
1963 // Propagate DefinitionData pointer from the canonical declaration.
1964 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1965
Richard Smith676c4042013-08-29 23:59:27 +00001966 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001967 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001968 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001969 DeclID KeyFn = ReadDeclID();
Erich Keanef92f31c2018-08-01 20:48:16 +00001970 if (KeyFn && D->isCompleteDefinition())
Richard Smitha9a1c682014-07-07 06:38:20 +00001971 // FIXME: This is wrong for the ARM ABI, where some other module may have
1972 // made this function no longer be a key function. We need an update
1973 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001974 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001975 }
Richard Smith1d209d02013-05-23 01:49:11 +00001976
1977 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001978}
1979
Richard Smithbc491202017-02-17 20:05:37 +00001980void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
Richard Smith76b90272019-05-09 03:59:21 +00001981 D->setExplicitSpecifier(Record.readExplicitSpec());
Richard Smithbc491202017-02-17 20:05:37 +00001982 VisitFunctionDecl(D);
Erich Keane9c665062018-08-01 21:02:40 +00001983 D->setIsCopyDeductionCandidate(Record.readInt());
Richard Smithbc491202017-02-17 20:05:37 +00001984}
1985
Sebastian Redlb3298c32010-08-18 23:56:48 +00001986void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001987 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001988
David L. Jonesbe1557a2016-12-21 00:17:49 +00001989 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001990 if (D->isCanonicalDecl()) {
1991 while (NumOverridenMethods--) {
1992 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1993 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001994 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001995 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1996 }
1997 } else {
1998 // We don't care about which declarations this used to override; we get
1999 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002000 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002001 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002002}
2003
Sebastian Redlb3298c32010-08-18 23:56:48 +00002004void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00002005 // We need the inherited constructor information to merge the declaration,
2006 // so we have to read it before we call VisitCXXMethodDecl.
Richard Smith76b90272019-05-09 03:59:21 +00002007 D->setExplicitSpecifier(Record.readExplicitSpec());
Richard Smith5179eb72016-06-28 19:03:57 +00002008 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002009 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
2010 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00002011 *D->getTrailingObjects<InheritedConstructor>() =
2012 InheritedConstructor(Shadow, Ctor);
2013 }
2014
Chris Lattnerca025db2010-05-07 21:43:38 +00002015 VisitCXXMethodDecl(D);
2016}
2017
Sebastian Redlb3298c32010-08-18 23:56:48 +00002018void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002019 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00002020
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002021 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002022 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00002023 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00002024 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00002025 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00002026 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00002027 Canon->OperatorDeleteThisArg = ThisArg;
2028 }
Richard Smithf8134002015-03-10 01:41:22 +00002029 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002030}
2031
Sebastian Redlb3298c32010-08-18 23:56:48 +00002032void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Richard Smith76b90272019-05-09 03:59:21 +00002033 D->setExplicitSpecifier(Record.readExplicitSpec());
Chris Lattnerca025db2010-05-07 21:43:38 +00002034 VisitCXXMethodDecl(D);
2035}
2036
Douglas Gregorba345522011-12-02 23:23:56 +00002037void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2038 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002039 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002040 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002041 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002042 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002043 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002044 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002045}
2046
Sebastian Redlb3298c32010-08-18 23:56:48 +00002047void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002048 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002049 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002050}
2051
Sebastian Redlb3298c32010-08-18 23:56:48 +00002052void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002053 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002054 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002055 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002056 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002057 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002058 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002059 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002060 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002061 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002062 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002063 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002064}
2065
Sebastian Redlb3298c32010-08-18 23:56:48 +00002066void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002067 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002068 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002069 D->NumParams = NumParams;
2070 D->Params = new TemplateParameterList*[NumParams];
2071 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002072 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002073 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002074 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002075 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002076 D->Friend = GetTypeSourceInfo();
2077 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002078}
2079
Richard Smithf17fdbd2014-04-24 02:25:27 +00002080DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002081 VisitNamedDecl(D);
2082
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002083 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002084 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002085 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002086 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002087 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002088
Richard Smithf17fdbd2014-04-24 02:25:27 +00002089 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002090}
2091
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002092ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002093ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002094 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002095
Douglas Gregor68444de2012-01-14 15:13:49 +00002096 // Make sure we've allocated the Common pointer first. We do this before
2097 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2098 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2099 if (!CanonD->Common) {
2100 CanonD->Common = CanonD->newCommon(Reader.getContext());
2101 Reader.PendingDefinitions.insert(CanonD);
2102 }
2103 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002104
Douglas Gregor68444de2012-01-14 15:13:49 +00002105 // If this is the first declaration of the template, fill in the information
2106 // for the 'common' pointer.
2107 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002108 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002109 assert(RTD->getKind() == D->getKind() &&
2110 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002111 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002112 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002113 D->setMemberSpecialization();
2114 }
2115 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002116
Richard Smithf17fdbd2014-04-24 02:25:27 +00002117 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002118 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002119
Richard Smithf17fdbd2014-04-24 02:25:27 +00002120 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002121
Richard Smithd46d6de2013-10-13 23:50:45 +00002122 // If we merged the template with a prior declaration chain, merge the common
2123 // pointer.
2124 // FIXME: Actually merge here, don't just overwrite.
2125 D->Common = D->getCanonicalDecl()->Common;
2126
Douglas Gregor68444de2012-01-14 15:13:49 +00002127 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002128}
2129
Sebastian Redlb3298c32010-08-18 23:56:48 +00002130void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002131 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002132
Douglas Gregor68444de2012-01-14 15:13:49 +00002133 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002134 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2135 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002136 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002137 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002138 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002139 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002140
2141 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2142 // We were loaded before our templated declaration was. We've not set up
2143 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2144 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002145 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002146 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002147 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002148}
2149
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002150void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2151 llvm_unreachable("BuiltinTemplates are not serialized");
2152}
2153
Larisse Voufo30616382013-08-23 22:21:36 +00002154/// TODO: Unify with ClassTemplateDecl version?
2155/// May require unifying ClassTemplateDecl and
2156/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002157void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2158 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2159
2160 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002161 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002162 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002163 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002164 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002165 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002166 }
2167}
2168
Richard Smith1d209d02013-05-23 01:49:11 +00002169ASTDeclReader::RedeclarableResult
2170ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2171 ClassTemplateSpecializationDecl *D) {
2172 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002173
Douglas Gregor4163aca2011-09-09 21:34:22 +00002174 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002175 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002176 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002177 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002178 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002179 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002180 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002181 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002182 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002183 auto *PS =
2184 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002185 SpecializedPartialSpecialization();
2186 PS->PartialSpecialization
2187 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2188 PS->TemplateArgs = ArgList;
2189 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002190 }
2191 }
2192
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002193 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002194 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002195 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002196 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002197 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002198
David L. Jonesbe1557a2016-12-21 00:17:49 +00002199 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002200 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002201 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002202 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002203 // Set this as, or find, the canonical declaration for this specialization
2204 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002205 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002206 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002207 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002208 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002209 CanonSpec =
2210 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2211 }
2212 // If there was already a canonical specialization, merge into it.
2213 if (CanonSpec != D) {
2214 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2215
2216 // This declaration might be a definition. Merge with any existing
2217 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002218 if (auto *DDD = D->DefinitionData) {
2219 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002220 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002221 else
Richard Smith053f6c62014-05-16 23:01:30 +00002222 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002223 }
Richard Smith547864d2014-07-11 18:22:58 +00002224 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002225 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002226 }
2227 }
Richard Smith1d209d02013-05-23 01:49:11 +00002228
Richard Smithd55889a2013-09-09 16:55:27 +00002229 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002230 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002231 auto *ExplicitInfo =
2232 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002233 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002234 ExplicitInfo->ExternLoc = ReadSourceLocation();
2235 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002236 D->ExplicitInfo = ExplicitInfo;
2237 }
2238
Richard Smith1d209d02013-05-23 01:49:11 +00002239 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002240}
2241
Sebastian Redlb3298c32010-08-18 23:56:48 +00002242void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002243 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002244 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002245
David L. Jonesb6a8f022016-12-21 04:34:52 +00002246 D->TemplateParams = Record.readTemplateParameterList();
2247 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002248
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002249 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002250 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002251 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002252 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002253 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002254 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002255}
2256
Sebastian Redlb7448632011-08-31 13:59:56 +00002257void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2258 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002259 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002260 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Richard Smithf19a8b02019-05-02 00:49:14 +00002261 if (Record.readInt())
2262 D->TemplateArgs = Record.readASTTemplateArgumentListInfo();
Francois Pichet09af8c32011-08-17 01:06:54 +00002263}
2264
Sebastian Redlb3298c32010-08-18 23:56:48 +00002265void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002266 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002267
Douglas Gregor68444de2012-01-14 15:13:49 +00002268 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002269 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002270 SmallVector<serialization::DeclID, 32> SpecIDs;
2271 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002272 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002273 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002274}
2275
Larisse Voufo30616382013-08-23 22:21:36 +00002276/// TODO: Unify with ClassTemplateSpecializationDecl version?
2277/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2278/// VarTemplate(Partial)SpecializationDecl with a new data
2279/// structure Template(Partial)SpecializationDecl, and
2280/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002281ASTDeclReader::RedeclarableResult
2282ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2283 VarTemplateSpecializationDecl *D) {
2284 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2285
2286 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002287 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002288 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002289 D->SpecializedTemplate = VTD;
2290 } else {
2291 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002292 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002293 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002294 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002295 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002296 new (C)
2297 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2298 PS->PartialSpecialization =
2299 cast<VarTemplatePartialSpecializationDecl>(InstD);
2300 PS->TemplateArgs = ArgList;
2301 D->SpecializedTemplate = PS;
2302 }
2303 }
2304
2305 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002306 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002307 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002308 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2309 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002310 ExplicitInfo->ExternLoc = ReadSourceLocation();
2311 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002312 D->ExplicitInfo = ExplicitInfo;
2313 }
2314
2315 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002316 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002317 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002318 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002319 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002320 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002321
David L. Jonesbe1557a2016-12-21 00:17:49 +00002322 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002323 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002324 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002325 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002326 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002327 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002328 CanonPattern->getCommonPtr()->PartialSpecializations
2329 .GetOrInsertNode(Partial);
2330 } else {
2331 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2332 }
2333 }
2334 }
2335
2336 return Redecl;
2337}
2338
Larisse Voufo30616382013-08-23 22:21:36 +00002339/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2340/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2341/// VarTemplate(Partial)SpecializationDecl with a new data
2342/// structure Template(Partial)SpecializationDecl, and
2343/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002344void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2345 VarTemplatePartialSpecializationDecl *D) {
2346 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2347
David L. Jonesb6a8f022016-12-21 04:34:52 +00002348 D->TemplateParams = Record.readTemplateParameterList();
2349 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002350
2351 // These are read/set from/to the first declaration.
2352 if (ThisDeclID == Redecl.getFirstID()) {
2353 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002354 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002355 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002356 }
2357}
2358
Sebastian Redlb3298c32010-08-18 23:56:48 +00002359void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002360 VisitTypeDecl(D);
2361
David L. Jonesbe1557a2016-12-21 00:17:49 +00002362 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002363
David L. Jonesbe1557a2016-12-21 00:17:49 +00002364 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002365 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002366}
2367
Sebastian Redlb3298c32010-08-18 23:56:48 +00002368void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002369 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002370 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002371 D->setDepth(Record.readInt());
2372 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002373 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002374 auto TypesAndInfos =
2375 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002376 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002377 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002378 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002379 }
2380 } else {
2381 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002382 D->ParameterPack = Record.readInt();
2383 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002384 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002385 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002386}
2387
Sebastian Redlb3298c32010-08-18 23:56:48 +00002388void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002389 VisitTemplateDecl(D);
2390 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002391 D->setDepth(Record.readInt());
2392 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002393 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002394 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002395 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2396 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002397 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002398 } else {
2399 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002400 D->ParameterPack = Record.readInt();
2401 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002402 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002403 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002404 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002405}
2406
Richard Smith3f1b5d02011-05-05 21:57:07 +00002407void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2408 VisitRedeclarableTemplateDecl(D);
2409}
2410
Sebastian Redlb3298c32010-08-18 23:56:48 +00002411void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002412 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002413 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002414 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002415 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002416 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002417}
2418
Michael Han84324352013-02-22 17:15:32 +00002419void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2420 VisitDecl(D);
2421}
2422
Mike Stump11289f42009-09-09 15:08:12 +00002423std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002424ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002425 uint64_t LexicalOffset = ReadLocalOffset();
2426 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002427 return std::make_pair(LexicalOffset, VisibleOffset);
2428}
2429
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002430template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002431ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002432ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002433 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002434 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002435
Richard Smith5fc18a92015-07-12 23:43:21 +00002436 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002437 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002438
Richard Smithd61d4ac2015-08-22 20:13:39 +00002439 uint64_t RedeclOffset = 0;
2440
Douglas Gregor358cd442012-01-15 16:58:34 +00002441 // 0 indicates that this declaration was the only declaration of its entity,
2442 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002443 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002444 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002445 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002446 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002447 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002448 // This declaration was the first local declaration, but may have imported
2449 // other declarations.
2450 IsKeyDecl = N == 1;
2451 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002452
Richard Smithfe620d22015-03-05 23:24:12 +00002453 // We have some declarations that must be before us in our redeclaration
2454 // chain. Read them now, and remember that we ought to merge with one of
2455 // them.
2456 // FIXME: Provide a known merge target to the second and subsequent such
2457 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002458 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002459 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002460
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002461 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002462 } else {
2463 // This declaration was not the first local declaration. Read the first
2464 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002465 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002466 }
2467
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002468 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002469 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002470 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2471 // We temporarily set the first (canonical) declaration as the previous one
2472 // which is the one that matters and mark the real previous DeclID to be
2473 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002474 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002475 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002476 }
Richard Smithd8a83712015-08-22 01:47:18 +00002477
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002478 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002479
2480 // Note that we need to load local redeclarations of this decl and build a
2481 // decl chain for them. This must happen *after* we perform the preloading
2482 // above; this ensures that the redeclaration chain is built in the correct
2483 // order.
2484 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002485 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002486
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002487 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002488}
2489
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002490/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002491/// of the same entity.
2492template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002493void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002494 RedeclarableResult &Redecl,
2495 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002496 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002497 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002498 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002499
Richard Smith202850a2015-03-10 02:57:50 +00002500 // If we're not the canonical declaration, we don't need to merge.
2501 if (!DBase->isFirstDecl())
2502 return;
2503
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002504 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002505
Richard Smith5a4737c2015-02-06 02:42:59 +00002506 if (auto *Existing = Redecl.getKnownMergeTarget())
2507 // We already know of an existing declaration we should merge with.
2508 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2509 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002510 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002511 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2512}
2513
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002514/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002515/// We use this to put code in a template that will only be valid for certain
2516/// instantiations.
2517template<typename T> static T assert_cast(T t) { return t; }
2518template<typename T> static T assert_cast(...) {
2519 llvm_unreachable("bad assert_cast");
2520}
2521
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002522/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002523/// declarations.
2524void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2525 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002526 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002527 auto *DPattern = D->getTemplatedDecl();
2528 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002529 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2530 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002531 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002532
Richard Smith547864d2014-07-11 18:22:58 +00002533 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2534 // Merge with any existing definition.
2535 // FIXME: This is duplicated in several places. Refactor.
2536 auto *ExistingClass =
2537 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002538 if (auto *DDD = DClass->DefinitionData) {
2539 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002540 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002541 } else {
2542 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002543 // We may have skipped this before because we thought that DClass
2544 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002545 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002546 }
2547 }
2548 DClass->DefinitionData = ExistingClass->DefinitionData;
2549
Richard Smithf17fdbd2014-04-24 02:25:27 +00002550 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2551 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002552 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002553 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2554 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2555 Result);
2556 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2557 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002558 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2559 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2560 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002561 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002562}
2563
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002564/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002565/// of the same entity.
2566template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002567void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2568 RedeclarableResult &Redecl,
2569 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002570 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002571 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002572 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002573 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002574 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2575 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002576
Richard Smithd55889a2013-09-09 16:55:27 +00002577 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002578 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002579 // appropriate canonical declaration.
2580 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002581 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002582 ExistingCanon->Used |= D->Used;
2583 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002584
2585 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002586 // We cannot have loaded any redeclarations of this declaration yet, so
2587 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002588 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002589 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002590 assert_cast<NamespaceDecl*>(ExistingCanon));
2591
2592 // When we merge a template, merge its pattern.
2593 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2594 mergeTemplatePattern(
2595 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002596 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002597
Richard Smith5fc18a92015-07-12 23:43:21 +00002598 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002599 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002600 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002601 }
2602}
2603
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002604/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2605/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2606/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2607/// that some types are mergeable during deserialization, otherwise name
2608/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002609static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002610 if (!ND)
2611 return false;
2612 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002613 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002614 return true;
2615 return false;
2616}
2617
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002618/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002619/// of the same entity, for the case where the entity is not actually
2620/// redeclarable. This happens, for instance, when merging the fields of
2621/// identical class definitions from two different modules.
2622template<typename T>
2623void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2624 // If modules are not available, there is no reason to perform this merge.
2625 if (!Reader.getContext().getLangOpts().Modules)
2626 return;
2627
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002628 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2629 // Note that C identically-named things in different translation units are
2630 // not redeclarations, but may still have compatible types, where ODR-like
2631 // semantics may apply.
2632 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2633 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002634 return;
2635
Richard Smith0b87e072013-10-07 08:02:11 +00002636 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2637 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002638 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2639 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002640}
2641
Alexey Bataeva769e072013-03-22 06:34:35 +00002642void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2643 VisitDecl(D);
2644 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002645 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002646 Vars.reserve(NumVars);
2647 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002648 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002649 }
2650 D->setVars(Vars);
2651}
2652
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002653void ASTDeclReader::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
2654 VisitDecl(D);
2655 unsigned NumVars = D->varlist_size();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002656 unsigned NumClauses = D->clauselist_size();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002657 SmallVector<Expr *, 16> Vars;
2658 Vars.reserve(NumVars);
2659 for (unsigned i = 0; i != NumVars; ++i) {
2660 Vars.push_back(Record.readExpr());
2661 }
2662 D->setVars(Vars);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002663 SmallVector<OMPClause *, 8> Clauses;
2664 Clauses.reserve(NumClauses);
2665 OMPClauseReader ClauseReader(Record);
2666 for (unsigned I = 0; I != NumClauses; ++I)
2667 Clauses.push_back(ClauseReader.readClause());
2668 D->setClauses(Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002669}
2670
Kelvin Li1408f912018-09-26 04:28:39 +00002671void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
2672 VisitDecl(D);
2673 unsigned NumClauses = D->clauselist_size();
2674 SmallVector<OMPClause *, 8> Clauses;
2675 Clauses.reserve(NumClauses);
2676 OMPClauseReader ClauseReader(Record);
2677 for (unsigned I = 0; I != NumClauses; ++I)
2678 Clauses.push_back(ClauseReader.readClause());
2679 D->setClauses(Clauses);
2680}
2681
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002682void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002683 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002684 D->setLocation(ReadSourceLocation());
Alexey Bataeve6aa4692018-09-13 16:54:05 +00002685 Expr *In = Record.readExpr();
2686 Expr *Out = Record.readExpr();
2687 D->setCombinerData(In, Out);
2688 Expr *Combiner = Record.readExpr();
2689 D->setCombiner(Combiner);
2690 Expr *Orig = Record.readExpr();
2691 Expr *Priv = Record.readExpr();
2692 D->setInitializerData(Orig, Priv);
2693 Expr *Init = Record.readExpr();
2694 auto IK = static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt());
2695 D->setInitializer(Init, IK);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002696 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002697}
2698
Michael Kruse251e1482019-02-01 20:25:04 +00002699void ASTDeclReader::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
2700 VisitValueDecl(D);
2701 D->setLocation(ReadSourceLocation());
2702 Expr *MapperVarRefE = Record.readExpr();
2703 D->setMapperVarRef(MapperVarRefE);
2704 D->VarName = Record.readDeclarationName();
2705 D->PrevDeclInScope = ReadDeclID();
2706 unsigned NumClauses = D->clauselist_size();
2707 SmallVector<OMPClause *, 8> Clauses;
2708 Clauses.reserve(NumClauses);
2709 OMPClauseReader ClauseReader(Record);
2710 for (unsigned I = 0; I != NumClauses; ++I)
2711 Clauses.push_back(ClauseReader.readClause());
2712 D->setClauses(Clauses);
2713}
2714
Alexey Bataev4244be22016-02-11 05:35:55 +00002715void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002716 VisitVarDecl(D);
2717}
2718
Chris Lattner487412d2009-04-27 05:27:42 +00002719//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002720// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002721//===----------------------------------------------------------------------===//
2722
Richard Smithe43e2b32018-08-20 21:47:29 +00002723namespace {
2724class AttrReader {
2725 ModuleFile *F;
2726 ASTReader *Reader;
2727 const ASTReader::RecordData &Record;
2728 unsigned &Idx;
2729
2730public:
2731 AttrReader(ModuleFile &F, ASTReader &Reader,
2732 const ASTReader::RecordData &Record, unsigned &Idx)
2733 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
2734
2735 const uint64_t &readInt() { return Record[Idx++]; }
2736
2737 SourceRange readSourceRange() {
2738 return Reader->ReadSourceRange(*F, Record, Idx);
2739 }
2740
2741 Expr *readExpr() { return Reader->ReadExpr(*F); }
2742
2743 std::string readString() {
2744 return Reader->ReadString(Record, Idx);
2745 }
2746
2747 TypeSourceInfo *getTypeSourceInfo() {
2748 return Reader->GetTypeSourceInfo(*F, Record, Idx);
2749 }
2750
2751 IdentifierInfo *getIdentifierInfo() {
2752 return Reader->GetIdentifierInfo(*F, Record, Idx);
2753 }
2754
2755 VersionTuple readVersionTuple() {
2756 return ASTReader::ReadVersionTuple(Record, Idx);
2757 }
2758
2759 template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
2760 return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
2761 }
2762};
2763}
2764
2765Attr *ASTReader::ReadAttr(ModuleFile &M, const RecordData &Rec,
2766 unsigned &Idx) {
2767 AttrReader Record(M, *this, Rec, Idx);
2768 auto V = Record.readInt();
2769 if (!V)
2770 return nullptr;
2771
2772 Attr *New = nullptr;
2773 // Kind is stored as a 1-based integer because 0 is used to indicate a null
2774 // Attr pointer.
2775 auto Kind = static_cast<attr::Kind>(V - 1);
2776 SourceRange Range = Record.readSourceRange();
2777 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002778
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002779#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002780
Richard Smithe43e2b32018-08-20 21:47:29 +00002781 assert(New && "Unable to decode attribute?");
2782 return New;
2783}
2784
2785/// Reads attributes from the current stream position.
2786void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2787 for (unsigned I = 0, E = Record.readInt(); I != E; ++I)
2788 Attrs.push_back(Record.readAttr());
Chris Lattner8f63ab52009-04-27 06:01:06 +00002789}
2790
2791//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002792// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002793//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002794
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002795/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002796/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002797///
Chris Lattner487412d2009-04-27 05:27:42 +00002798/// This routine notes that this declaration has already been loaded,
2799/// so that future GetDecl calls will return this declaration rather
2800/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002801inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002802 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2803 DeclsLoaded[Index] = D;
2804}
2805
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002806/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002807/// this declaration (via HandleTopLevelDecl).
2808///
2809/// This routine should return true for anything that might affect
2810/// code generation, e.g., inline function definitions, Objective-C
2811/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002812static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002813 // An ObjCMethodDecl is never considered as "interesting" because its
2814 // implementation container always is.
2815
Richard Smithcd4a7a42017-09-07 00:55:55 +00002816 // An ImportDecl or VarDecl imported from a module map module will get
2817 // emitted when we import the relevant module.
Richard Smith520a37f2019-02-05 23:37:13 +00002818 if (isPartOfPerModuleInitializer(D)) {
Richard Smithcd4a7a42017-09-07 00:55:55 +00002819 auto *M = D->getImportedOwningModule();
2820 if (M && M->Kind == Module::ModuleMapModule &&
2821 Ctx.DeclMustBeEmitted(D))
2822 return false;
2823 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002824
Fangrui Song6907ce22018-07-30 19:24:48 +00002825 if (isa<FileScopeAsmDecl>(D) ||
2826 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002827 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002828 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002829 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002830 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002831 return true;
Michael Kruse251e1482019-02-01 20:25:04 +00002832 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D) ||
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002833 isa<OMPDeclareMapperDecl>(D) || isa<OMPAllocateDecl>(D))
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002834 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002835 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002836 return Var->isFileVarDecl() &&
Alexey Bataevd01b7492018-08-15 19:45:12 +00002837 (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
2838 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002839 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002840 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002841
2842 if (auto *ES = D->getASTContext().getExternalSource())
2843 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2844 return true;
2845
Douglas Gregor87d81242011-09-10 00:22:34 +00002846 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002847}
2848
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002849/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002850ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002851ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002852 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2853 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002854 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002855 const DeclOffset &DOffs =
2856 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002857 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002858 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002859}
2860
Douglas Gregord32f0352011-07-22 06:10:01 +00002861ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002862 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002863
2864 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2865 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2866}
2867
Douglas Gregorde3ef502011-11-30 23:21:26 +00002868uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002869 return LocalOffset + M.GlobalBitOffset;
2870}
2871
Richard Smithbf78e642013-06-24 22:51:00 +00002872static bool isSameTemplateParameterList(const TemplateParameterList *X,
2873 const TemplateParameterList *Y);
2874
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002875/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002876/// that they may be used in declarations of the same template.
2877static bool isSameTemplateParameter(const NamedDecl *X,
2878 const NamedDecl *Y) {
2879 if (X->getKind() != Y->getKind())
2880 return false;
2881
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002882 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2883 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002884 return TX->isParameterPack() == TY->isParameterPack();
2885 }
2886
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002887 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2888 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002889 return TX->isParameterPack() == TY->isParameterPack() &&
2890 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2891 }
2892
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002893 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2894 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002895 return TX->isParameterPack() == TY->isParameterPack() &&
2896 isSameTemplateParameterList(TX->getTemplateParameters(),
2897 TY->getTemplateParameters());
2898}
2899
Richard Smith32952e12014-10-14 02:00:47 +00002900static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2901 if (auto *NS = X->getAsNamespace())
2902 return NS;
2903 if (auto *NAS = X->getAsNamespaceAlias())
2904 return NAS->getNamespace();
2905 return nullptr;
2906}
2907
2908static bool isSameQualifier(const NestedNameSpecifier *X,
2909 const NestedNameSpecifier *Y) {
2910 if (auto *NSX = getNamespace(X)) {
2911 auto *NSY = getNamespace(Y);
2912 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2913 return false;
2914 } else if (X->getKind() != Y->getKind())
2915 return false;
2916
2917 // FIXME: For namespaces and types, we're permitted to check that the entity
2918 // is named via the same tokens. We should probably do so.
2919 switch (X->getKind()) {
2920 case NestedNameSpecifier::Identifier:
2921 if (X->getAsIdentifier() != Y->getAsIdentifier())
2922 return false;
2923 break;
2924 case NestedNameSpecifier::Namespace:
2925 case NestedNameSpecifier::NamespaceAlias:
2926 // We've already checked that we named the same namespace.
2927 break;
2928 case NestedNameSpecifier::TypeSpec:
2929 case NestedNameSpecifier::TypeSpecWithTemplate:
2930 if (X->getAsType()->getCanonicalTypeInternal() !=
2931 Y->getAsType()->getCanonicalTypeInternal())
2932 return false;
2933 break;
2934 case NestedNameSpecifier::Global:
2935 case NestedNameSpecifier::Super:
2936 return true;
2937 }
2938
2939 // Recurse into earlier portion of NNS, if any.
2940 auto *PX = X->getPrefix();
2941 auto *PY = Y->getPrefix();
2942 if (PX && PY)
2943 return isSameQualifier(PX, PY);
2944 return !PX && !PY;
2945}
2946
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002947/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002948/// that they may be used in declarations of the same template.
2949static bool isSameTemplateParameterList(const TemplateParameterList *X,
2950 const TemplateParameterList *Y) {
2951 if (X->size() != Y->size())
2952 return false;
2953
2954 for (unsigned I = 0, N = X->size(); I != N; ++I)
2955 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2956 return false;
2957
2958 return true;
2959}
2960
George Burgess IV95845082017-02-15 22:43:27 +00002961/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002962/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002963static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2964 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002965 // Note that pass_object_size attributes are represented in the function's
2966 // ExtParameterInfo, so we don't need to check them here.
2967
George Burgess IV95845082017-02-15 22:43:27 +00002968 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00002969 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
2970 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
Michael Kruse157a3552018-12-10 15:16:37 +00002971
2972 for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) {
2973 Optional<EnableIfAttr *> Cand1A = std::get<0>(Pair);
2974 Optional<EnableIfAttr *> Cand2A = std::get<1>(Pair);
2975
2976 // Return false if the number of enable_if attributes is different.
2977 if (!Cand1A || !Cand2A)
2978 return false;
2979
George Burgess IV95845082017-02-15 22:43:27 +00002980 Cand1ID.clear();
2981 Cand2ID.clear();
2982
Michael Kruse157a3552018-12-10 15:16:37 +00002983 (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2984 (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2985
2986 // Return false if any of the enable_if expressions of A and B are
2987 // different.
George Burgess IV95845082017-02-15 22:43:27 +00002988 if (Cand1ID != Cand2ID)
2989 return false;
2990 }
Michael Kruse157a3552018-12-10 15:16:37 +00002991 return true;
George Burgess IV95845082017-02-15 22:43:27 +00002992}
2993
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002994/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002995static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2996 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002997
Douglas Gregor022857e2011-12-22 01:48:48 +00002998 if (X == Y)
2999 return true;
Richard Smithf4634362014-09-03 23:11:22 +00003000
Douglas Gregor022857e2011-12-22 01:48:48 +00003001 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00003002 //
3003 // Note that we can't use DeclContext::Equals here, because the DeclContexts
3004 // could be two different declarations of the same function. (We will fix the
3005 // semantic DC to refer to the primary definition after merging.)
3006 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
3007 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00003008 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003009
3010 // Two typedefs refer to the same entity if they have the same underlying
3011 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003012 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
3013 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003014 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
3015 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00003016
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003017 // Must have the same kind.
3018 if (X->getKind() != Y->getKind())
3019 return false;
Richard Smithf4634362014-09-03 23:11:22 +00003020
Douglas Gregorda389302012-01-01 21:47:52 +00003021 // Objective-C classes and protocols with the same name always match.
3022 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00003023 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00003024
3025 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003026 // No need to handle these here: we merge them when adding them to the
3027 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00003028 return false;
3029 }
Richard Smithd55889a2013-09-09 16:55:27 +00003030
Douglas Gregor2009cee2012-01-03 22:46:00 +00003031 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003032 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
3033 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00003034 return (TagX->getTagKind() == TagY->getTagKind()) ||
3035 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
3036 TagX->getTagKind() == TTK_Interface) &&
3037 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
3038 TagY->getTagKind() == TTK_Interface));
3039 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003040
Joao Matose9a3ed42012-08-31 22:18:20 +00003041 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00003042 // FIXME: This needs to cope with merging of prototyped/non-prototyped
3043 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003044 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
3045 const auto *FuncY = cast<FunctionDecl>(Y);
3046 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
3047 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00003048 if (CtorX->getInheritedConstructor() &&
3049 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
3050 CtorY->getInheritedConstructor().getConstructor()))
3051 return false;
3052 }
Erich Keane281d20b2018-01-08 21:34:17 +00003053
3054 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
3055 return false;
3056
3057 // Multiversioned functions with different feature strings are represented
3058 // as separate declarations.
3059 if (FuncX->isMultiVersion()) {
3060 const auto *TAX = FuncX->getAttr<TargetAttr>();
3061 const auto *TAY = FuncY->getAttr<TargetAttr>();
3062 assert(TAX && TAY && "Multiversion Function without target attribute");
3063
3064 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
3065 return false;
3066 }
3067
Richard Smitha54d3242017-03-08 23:00:26 +00003068 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00003069 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
3070 // Map to the first declaration that we've already merged into this one.
3071 // The TSI of redeclarations might not match (due to calling conventions
3072 // being inherited onto the type but not the TSI), but the TSI type of
3073 // the first declaration of the function should match across modules.
3074 FD = FD->getCanonicalDecl();
3075 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
3076 : FD->getType();
3077 };
3078 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
3079 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00003080 // We can get functions with different types on the redecl chain in C++17
3081 // if they have differing exception specifications and at least one of
3082 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00003083 auto *XFPT = XT->getAs<FunctionProtoType>();
3084 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003085 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00003086 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
3087 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00003088 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00003089 return true;
3090 return false;
3091 }
George Burgess IV95845082017-02-15 22:43:27 +00003092 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00003093 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00003094 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003095
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003096 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003097 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
3098 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00003099 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
3100 ASTContext &C = VarX->getASTContext();
3101 if (C.hasSameType(VarX->getType(), VarY->getType()))
3102 return true;
3103
3104 // We can get decls with different types on the redecl chain. Eg.
3105 // template <typename T> struct S { static T Var[]; }; // #1
3106 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
3107 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00003108 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00003109 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
3110 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
3111 if (!VarXTy || !VarYTy)
3112 return false;
3113 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
3114 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
3115 }
3116 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003117 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003118
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003119 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003120 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
3121 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003122 return NamespaceX->isInline() == NamespaceY->isInline();
3123 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003124
Richard Smith8f8f05c2013-06-24 04:45:28 +00003125 // Identical template names and kinds match if their template parameter lists
3126 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003127 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
3128 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00003129 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00003130 TemplateY->getTemplatedDecl()) &&
3131 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3132 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003133 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003134
Richard Smith0b87e072013-10-07 08:02:11 +00003135 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003136 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3137 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003138 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003139 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3140 }
3141
Richard Smith8cbd8952015-08-04 02:05:09 +00003142 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003143 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3144 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003145 return IFDX->getAnonField()->getCanonicalDecl() ==
3146 IFDY->getAnonField()->getCanonicalDecl();
3147 }
3148
Richard Smith01a73372013-10-15 22:02:41 +00003149 // Enumerators with the same name match.
3150 if (isa<EnumConstantDecl>(X))
3151 // FIXME: Also check the value is odr-equivalent.
3152 return true;
3153
Richard Smithfd8634a2013-10-23 02:17:46 +00003154 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003155 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3156 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003157 return USX->getTargetDecl() == USY->getTargetDecl();
3158 }
3159
Richard Smith32952e12014-10-14 02:00:47 +00003160 // Using declarations with the same qualifier match. (We already know that
3161 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003162 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3163 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003164 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3165 UX->hasTypename() == UY->hasTypename() &&
3166 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3167 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003168 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3169 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003170 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3171 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3172 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003173 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003174 return isSameQualifier(
3175 UX->getQualifier(),
3176 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3177
Richard Smithf4634362014-09-03 23:11:22 +00003178 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003179 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3180 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003181 return NAX->getNamespace()->Equals(NAY->getNamespace());
3182 }
3183
Douglas Gregor022857e2011-12-22 01:48:48 +00003184 return false;
3185}
3186
Richard Smithd55889a2013-09-09 16:55:27 +00003187/// Find the context in which we should search for previous declarations when
3188/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003189DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3190 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003191 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003192 return ND->getOriginalNamespace();
3193
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003194 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003195 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003196 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003197 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003198 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003199
3200 // If there's no definition yet, then DC's definition is added by an update
3201 // record, but we've not yet loaded that update record. In this case, we
3202 // commit to DC being the canonical definition now, and will fix this when
3203 // we load the update record.
3204 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003205 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003206 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003207 RD->DefinitionData = DD;
3208 RD->getCanonicalDecl()->DefinitionData = DD;
3209
3210 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003211 Reader.PendingFakeDefinitionData.insert(
3212 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003213 }
3214
3215 return DD->Definition;
3216 }
Richard Smithd55889a2013-09-09 16:55:27 +00003217
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003218 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003219 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3220 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003221
Richard Smith4eca9b92015-02-04 23:37:59 +00003222 // We can see the TU here only if we have no Sema object. In that case,
3223 // there's no TU scope to look in, so using the DC alone is sufficient.
3224 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3225 return TU;
3226
Craig Toppera13603a2014-05-22 05:54:18 +00003227 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003228}
3229
Douglas Gregor022857e2011-12-22 01:48:48 +00003230ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003231 // Record that we had a typedef name for linkage whether or not we merge
3232 // with that declaration.
3233 if (TypedefNameForLinkage) {
3234 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3235 Reader.ImportedTypedefNamesForLinkage.insert(
3236 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3237 return;
3238 }
3239
Douglas Gregor9b47f942012-01-09 17:38:47 +00003240 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003241 return;
Richard Smith95d99302013-07-13 02:00:19 +00003242
Richard Smith70d58502014-08-30 00:04:23 +00003243 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003244 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003245 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003246 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3247 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003248 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003249 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003250 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003251 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3252 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003253 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003254 // Add the declaration to its redeclaration context so later merging
3255 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003256 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003257 }
3258}
3259
Richard Smith88ebade2014-08-23 01:45:27 +00003260/// Find the declaration that should be merged into, given the declaration found
3261/// by name lookup. If we're merging an anonymous declaration within a typedef,
3262/// we need a matching typedef, and we merge with the type inside it.
3263static NamedDecl *getDeclForMerging(NamedDecl *Found,
3264 bool IsTypedefNameForLinkage) {
3265 if (!IsTypedefNameForLinkage)
3266 return Found;
3267
3268 // If we found a typedef declaration that gives a name to some other
3269 // declaration, then we want that inner declaration. Declarations from
3270 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003271 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003272 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003273
3274 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003275 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003276
Hans Wennborgdcfba332015-10-06 23:40:43 +00003277 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003278}
3279
Richard Smith600adef2018-07-04 02:25:38 +00003280/// Find the declaration to use to populate the anonymous declaration table
3281/// for the given lexical DeclContext. We only care about finding local
3282/// definitions of the context; we'll merge imported ones as we go.
3283DeclContext *
3284ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3285 // For classes, we track the definition as we merge.
3286 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3287 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3288 return DD ? DD->Definition : nullptr;
3289 }
3290
3291 // For anything else, walk its merged redeclarations looking for a definition.
3292 // Note that we can't just call getDefinition here because the redeclaration
3293 // chain isn't wired up.
3294 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3295 if (auto *FD = dyn_cast<FunctionDecl>(D))
3296 if (FD->isThisDeclarationADefinition())
3297 return FD;
3298 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3299 if (MD->isThisDeclarationADefinition())
3300 return MD;
3301 }
3302
3303 // No merged definition yet.
3304 return nullptr;
3305}
3306
Richard Smithd08aeb62014-08-28 01:33:39 +00003307NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3308 DeclContext *DC,
3309 unsigned Index) {
3310 // If the lexical context has been merged, look into the now-canonical
3311 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003312 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003313
3314 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003315 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003316 if (Index < Previous.size() && Previous[Index])
3317 return Previous[Index];
3318
3319 // If this is the first time, but we have parsed a declaration of the context,
3320 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003321 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3322 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3323 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003324 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003325 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3326 else
Richard Smith2b560572015-02-07 03:11:11 +00003327 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3328 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003329 }
3330
3331 return Index < Previous.size() ? Previous[Index] : nullptr;
3332}
3333
3334void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3335 DeclContext *DC, unsigned Index,
3336 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003337 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003338
Richard Smith600adef2018-07-04 02:25:38 +00003339 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003340 if (Index >= Previous.size())
3341 Previous.resize(Index + 1);
3342 if (!Previous[Index])
3343 Previous[Index] = D;
3344}
3345
Douglas Gregor022857e2011-12-22 01:48:48 +00003346ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003347 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3348 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003349
Richard Smithd08aeb62014-08-28 01:33:39 +00003350 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3351 // Don't bother trying to find unnamed declarations that are in
3352 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003353 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3354 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003355 Result.suppress();
3356 return Result;
3357 }
Richard Smith95d99302013-07-13 02:00:19 +00003358
Douglas Gregor022857e2011-12-22 01:48:48 +00003359 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003360 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003361 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003362 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003363 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3364 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003365 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3366 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003367 // Go on to check in other places in case an existing typedef name
3368 // was not imported.
3369 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003370
Richard Smith2b560572015-02-07 03:11:11 +00003371 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003372 // This is an anonymous declaration that we may need to merge. Look it up
3373 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003374 if (auto *Existing = getAnonymousDeclForMerging(
3375 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3376 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003377 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3378 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003379 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003380 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003381 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003382
3383 // Temporarily consider the identifier to be up-to-date. We don't want to
3384 // cause additional lookups here.
3385 class UpToDateIdentifierRAII {
3386 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003387 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003388
3389 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003390 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003391 if (II) {
3392 WasOutToDate = II->isOutOfDate();
3393 if (WasOutToDate)
3394 II->setOutOfDate(false);
3395 }
3396 }
3397
3398 ~UpToDateIdentifierRAII() {
3399 if (WasOutToDate)
3400 II->setOutOfDate(true);
3401 }
3402 } UpToDate(Name.getAsIdentifierInfo());
3403
Fangrui Song6907ce22018-07-30 19:24:48 +00003404 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003405 IEnd = IdResolver.end();
3406 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003407 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003408 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003409 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3410 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003411 }
Richard Smith8a639892015-01-24 01:07:20 +00003412 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003413 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003414 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003415 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003416 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003417 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3418 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003419 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003420 } else {
3421 // Not in a mergeable context.
3422 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003423 }
Richard Smithd55889a2013-09-09 16:55:27 +00003424
Richard Smith2b9e3e32013-10-18 06:05:18 +00003425 // If this declaration is from a merged context, make a note that we need to
3426 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003427 //
3428 // FIXME: We should do something similar if we merge two definitions of the
3429 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003430 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3431 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3432 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003433 Reader.PendingOdrMergeChecks.push_back(D);
3434
Richard Smithd08aeb62014-08-28 01:33:39 +00003435 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003436 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003437}
3438
Richard Smithb321ecb2014-05-13 01:15:00 +00003439template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003440Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3441 return D->RedeclLink.getLatestNotUpdated();
3442}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003443
Richard Smithc3a53252015-02-28 05:57:02 +00003444Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3445 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3446}
3447
3448Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3449 assert(D);
3450
3451 switch (D->getKind()) {
3452#define ABSTRACT_DECL(TYPE)
3453#define DECL(TYPE, BASE) \
3454 case Decl::TYPE: \
3455 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3456#include "clang/AST/DeclNodes.inc"
3457 }
3458 llvm_unreachable("unknown decl kind");
3459}
3460
Richard Smith8ce51082015-03-11 01:44:51 +00003461Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3462 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3463}
3464
Richard Smithc3a53252015-02-28 05:57:02 +00003465template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003466void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3467 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003468 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003469 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003470 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003471}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003472
Richard Smith24d166c2014-07-31 23:52:38 +00003473namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003474
Richard Smith6de7a242014-07-31 23:46:44 +00003475template<>
3476void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003477 Redeclarable<VarDecl> *D,
3478 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003479 auto *VD = static_cast<VarDecl *>(D);
3480 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003481 D->RedeclLink.setPrevious(PrevVD);
3482 D->First = PrevVD->First;
3483
3484 // We should keep at most one definition on the chain.
3485 // FIXME: Cache the definition once we've found it. Building a chain with
3486 // N definitions currently takes O(N^2) time here.
3487 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3488 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3489 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3490 Reader.mergeDefinitionVisibility(CurD, VD);
3491 VD->demoteThisDefinitionToDeclaration();
3492 break;
3493 }
3494 }
3495 }
3496}
3497
Richard Smitha62d1982018-08-03 01:00:01 +00003498static bool isUndeducedReturnType(QualType T) {
3499 auto *DT = T->getContainedDeducedType();
3500 return DT && !DT->isDeduced();
3501}
3502
Richard Smithedbc6e92016-10-14 21:41:24 +00003503template<>
3504void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003505 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003506 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003507 auto *FD = static_cast<FunctionDecl *>(D);
3508 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003509
3510 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003511 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003512
3513 // If the previous declaration is an inline function declaration, then this
3514 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003515 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003516 // FIXME: [dcl.fct.spec]p4:
3517 // If a function with external linkage is declared inline in one
3518 // translation unit, it shall be declared inline in all translation
3519 // units in which it appears.
3520 //
3521 // Be careful of this case:
3522 //
3523 // module A:
3524 // template<typename T> struct X { void f(); };
3525 // template<typename T> inline void X<T>::f() {}
3526 //
3527 // module B instantiates the declaration of X<int>::f
3528 // module C instantiates the definition of X<int>::f
3529 //
3530 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003531 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003532 }
3533
Richard Smith6de7a242014-07-31 23:46:44 +00003534 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3535 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003536 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003537 // If we need to propagate an exception specification along the redecl
3538 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003539 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3540 bool WasUnresolved =
3541 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3542 if (IsUnresolved != WasUnresolved)
3543 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003544 {Canon, IsUnresolved ? PrevFD : FD});
3545
3546 // If we need to propagate a deduced return type along the redecl chain,
3547 // make a note of that so that we can do it later.
3548 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3549 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3550 if (IsUndeduced != WasUndeduced)
3551 Reader.PendingDeducedTypeUpdates.insert(
3552 {cast<FunctionDecl>(Canon),
3553 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003554 }
3555}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003556
3557} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003558
Richard Smith6de7a242014-07-31 23:46:44 +00003559void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003560 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3561}
3562
Richard Smith8346e522015-06-10 01:47:58 +00003563/// Inherit the default template argument from \p From to \p To. Returns
3564/// \c false if there is no default template for \p From.
3565template <typename ParmDecl>
3566static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3567 Decl *ToD) {
3568 auto *To = cast<ParmDecl>(ToD);
3569 if (!From->hasDefaultArgument())
3570 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003571 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003572 return true;
3573}
3574
3575static void inheritDefaultTemplateArguments(ASTContext &Context,
3576 TemplateDecl *From,
3577 TemplateDecl *To) {
3578 auto *FromTP = From->getTemplateParameters();
3579 auto *ToTP = To->getTemplateParameters();
3580 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3581
3582 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003583 NamedDecl *FromParam = FromTP->getParam(I);
3584 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003585
Richard Smith559cc692018-07-31 21:01:53 +00003586 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3587 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3588 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3589 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3590 else
3591 inheritDefaultTemplateArgument(
3592 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003593 }
3594}
3595
Richard Smith6de7a242014-07-31 23:46:44 +00003596void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003597 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003598 assert(D && Previous);
3599
3600 switch (D->getKind()) {
3601#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003602#define DECL(TYPE, BASE) \
3603 case Decl::TYPE: \
3604 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003605 break;
3606#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003607 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003608
3609 // If the declaration was visible in one module, a redeclaration of it in
3610 // another module remains visible even if it wouldn't be visible by itself.
3611 //
3612 // FIXME: In this case, the declaration should only be visible if a module
3613 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003614 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003615 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003616 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003617
Richard Smith8346e522015-06-10 01:47:58 +00003618 // If the declaration declares a template, it may inherit default arguments
3619 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003620 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003621 inheritDefaultTemplateArguments(Reader.getContext(),
3622 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003623}
3624
Richard Smithb321ecb2014-05-13 01:15:00 +00003625template<typename DeclT>
3626void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003627 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003628}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003629
Richard Smithb321ecb2014-05-13 01:15:00 +00003630void ASTDeclReader::attachLatestDeclImpl(...) {
3631 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3632}
3633
Douglas Gregor05f10352011-12-17 23:38:30 +00003634void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3635 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003636
3637 switch (D->getKind()) {
3638#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003639#define DECL(TYPE, BASE) \
3640 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003641 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3642 break;
3643#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003644 }
3645}
3646
Richard Smith851072e2014-05-19 20:59:20 +00003647template<typename DeclT>
3648void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3649 D->RedeclLink.markIncomplete();
3650}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003651
Richard Smith851072e2014-05-19 20:59:20 +00003652void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3653 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3654}
3655
3656void ASTReader::markIncompleteDeclChain(Decl *D) {
3657 switch (D->getKind()) {
3658#define ABSTRACT_DECL(TYPE)
3659#define DECL(TYPE, BASE) \
3660 case Decl::TYPE: \
3661 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3662 break;
3663#include "clang/AST/DeclNodes.inc"
3664 }
3665}
3666
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003667/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003668Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003669 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003670 SourceLocation DeclLoc;
3671 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003672 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003673 // Keep track of where we are in the stream, then jump back there
3674 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003675 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003676
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003677 ReadingKindTracker ReadingKind(Read_Decl, *this);
3678
Douglas Gregor1342e842009-07-06 18:54:52 +00003679 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003680 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003681
Sebastian Redl2c373b92010-10-05 15:59:54 +00003682 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003683 ASTRecordReader Record(*this, *Loc.F);
3684 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003685 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003686
Richard Smithdbafb6c2017-06-29 23:23:46 +00003687 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003688 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003689 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003690 case DECL_CONTEXT_LEXICAL:
3691 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003692 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003693 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003694 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003695 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003696 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003697 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003698 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003699 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003700 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003701 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003702 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003703 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003704 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003705 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003706 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003707 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003708 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003709 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003710 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003711 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003712 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003713 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003714 case DECL_EXPORT:
3715 D = ExportDecl::CreateDeserialized(Context, ID);
3716 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003717 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003718 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003719 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003720 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003721 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003722 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003723 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003724 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003725 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003726 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003727 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003728 break;
Richard Smith151c4562016-12-20 21:35:28 +00003729 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003730 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003731 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003732 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003733 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003734 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003735 case DECL_CONSTRUCTOR_USING_SHADOW:
3736 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3737 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003738 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003739 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003740 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003741 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003742 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003743 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003744 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003745 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003746 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003747 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003748 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003749 break;
Richard Smithbc491202017-02-17 20:05:37 +00003750 case DECL_CXX_DEDUCTION_GUIDE:
3751 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3752 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003753 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003754 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003755 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003756 case DECL_CXX_CONSTRUCTOR:
Richard Smith76b90272019-05-09 03:59:21 +00003757 D = CXXConstructorDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003758 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003759 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003760 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003761 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003762 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003763 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003764 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003765 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003766 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003767 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003768 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003769 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003770 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003771 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003772 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003773 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003774 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003775 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003776 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003777 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003778 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003779 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003780 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003781 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003782 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003783 case DECL_VAR_TEMPLATE:
3784 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3785 break;
3786 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3787 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3788 break;
3789 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3790 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3791 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003792 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003793 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003794 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003795 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003796 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003797 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003798 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003799 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003800 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003801 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003802 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003803 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003804 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003805 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3806 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003807 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003808 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003809 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003810 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003811 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3812 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003813 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003814 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003815 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003816 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003817 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003818 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003819 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003820 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003821 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003822 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003823 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003824 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003825 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003826 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003827 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003828 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003829 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003830 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003831 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003832 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003833 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003834 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003835 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003836 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003837 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003838 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003839 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003840 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003841 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003842 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003843 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003844 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003845 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003846 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003847 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003848 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003849 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003850 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003851 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003852 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003853 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003854 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003855 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003856 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003857 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003858 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003859 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003860 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003861 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003862 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003863 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003864 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003865 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003866 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003867 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003868 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003869 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003870 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003871 break;
3872 case DECL_BINDING:
3873 D = BindingDecl::CreateDeserialized(Context, ID);
3874 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003875 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003876 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003877 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003878 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003879 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003880 break;
John McCall5e77d762013-04-16 07:28:30 +00003881 case DECL_MS_PROPERTY:
3882 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3883 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003884 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003885 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003886 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003887 case DECL_CXX_BASE_SPECIFIERS:
3888 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003889 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003890 case DECL_CXX_CTOR_INITIALIZERS:
3891 Error("attempt to read a C++ ctor initializer record as a declaration");
3892 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003893 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003894 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003895 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003896 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003897 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003898 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003899 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003900 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00003901 case DECL_OMP_ALLOCATE: {
3902 unsigned NumVars = Record.readInt();
3903 unsigned NumClauses = Record.readInt();
3904 D = OMPAllocateDecl::CreateDeserialized(Context, ID, NumVars, NumClauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00003905 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00003906 }
Kelvin Li1408f912018-09-26 04:28:39 +00003907 case DECL_OMP_REQUIRES:
3908 D = OMPRequiresDecl::CreateDeserialized(Context, ID, Record.readInt());
3909 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003910 case DECL_OMP_DECLARE_REDUCTION:
3911 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3912 break;
Michael Kruse251e1482019-02-01 20:25:04 +00003913 case DECL_OMP_DECLARE_MAPPER:
3914 D = OMPDeclareMapperDecl::CreateDeserialized(Context, ID, Record.readInt());
3915 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003916 case DECL_OMP_CAPTUREDEXPR:
3917 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003918 break;
Nico Weber66220292016-03-02 17:28:48 +00003919 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003920 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003921 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003922 case DECL_PRAGMA_DETECT_MISMATCH:
3923 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003924 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003925 break;
Michael Han84324352013-02-22 17:15:32 +00003926 case DECL_EMPTY:
3927 D = EmptyDecl::CreateDeserialized(Context, ID);
3928 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003929 case DECL_OBJC_TYPE_PARAM:
3930 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3931 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003932 }
Chris Lattner487412d2009-04-27 05:27:42 +00003933
Sebastian Redlb3298c32010-08-18 23:56:48 +00003934 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003935 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003936 // Set the DeclContext before doing any deserialization, to make sure internal
3937 // calls to Decl::getASTContext() by Decl's methods will find the
3938 // TranslationUnitDecl without crashing.
3939 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003940 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003941
3942 // If this declaration is also a declaration context, get the
3943 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003944 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003945 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003946 if (Offsets.first &&
3947 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3948 return nullptr;
3949 if (Offsets.second &&
3950 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3951 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003952 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003953 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003954
Douglas Gregordab42432011-08-12 00:15:20 +00003955 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003956 PendingUpdateRecords.push_back(
3957 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003958
Douglas Gregor404cdde2012-01-27 01:47:08 +00003959 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003960 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003961 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3962 // we put the Decl in PendingDefinitions so we can pull the categories here.
3963 if (Class->isThisDeclarationADefinition() ||
3964 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003965 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00003966
Richard Smith13fb8602016-07-15 21:33:46 +00003967 // If we have deserialized a declaration that has a definition the
3968 // AST consumer might need to know about, queue it.
3969 // We don't pass it to the consumer immediately because we may be in recursive
3970 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003971 PotentiallyInterestingDecls.push_back(
3972 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003973
Douglas Gregordab42432011-08-12 00:15:20 +00003974 return D;
3975}
3976
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003977void ASTReader::PassInterestingDeclsToConsumer() {
3978 assert(Consumer);
3979
3980 if (PassingDeclsToConsumer)
3981 return;
3982
3983 // Guard variable to avoid recursively redoing the process of passing
3984 // decls to consumer.
3985 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3986 true);
3987
3988 // Ensure that we've loaded all potentially-interesting declarations
3989 // that need to be eagerly loaded.
3990 for (auto ID : EagerlyDeserializedDecls)
3991 GetDecl(ID);
3992 EagerlyDeserializedDecls.clear();
3993
3994 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003995 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003996 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003997 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3998 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003999 }
4000}
4001
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004002void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004003 // The declaration may have been modified by files later in the chain.
4004 // If this is the case, read the record containing the updates from each file
4005 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004006 serialization::GlobalDeclID ID = Record.ID;
4007 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004008 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004009 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00004010
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004011 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00004012
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004013 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00004014 auto UpdateOffsets = std::move(UpdI->second);
4015 DeclUpdateOffsets.erase(UpdI);
4016
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004017 // Check if this decl was interesting to the consumer. If we just loaded
4018 // the declaration, then we know it was interesting and we skip the call
4019 // to isConsumerInterestedIn because it is unsafe to call in the
4020 // current ASTReader state.
4021 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004022 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00004023 for (auto &FileAndOffset : UpdateOffsets) {
4024 ModuleFile *F = FileAndOffset.first;
4025 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004026 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
4027 SavedStreamPosition SavedPosition(Cursor);
4028 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004029 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00004030 ASTRecordReader Record(*this, *F);
4031 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004032 (void)RecCode;
4033 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00004034
David L. Jonesbe1557a2016-12-21 00:17:49 +00004035 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
4036 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00004037 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00004038
4039 // We might have made this declaration interesting. If so, remember that
4040 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004041 if (!WasInteresting &&
4042 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
4043 PotentiallyInterestingDecls.push_back(
4044 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00004045 WasInteresting = true;
4046 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004047 }
4048 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00004049 // Add the lazy specializations to the template.
4050 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
4051 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
4052 "Must not have pending specializations");
4053 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
4054 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
4055 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
4056 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
4057 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
4058 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
4059 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00004060
4061 // Load the pending visible updates for this decl context, if it has any.
4062 auto I = PendingVisibleUpdates.find(ID);
4063 if (I != PendingVisibleUpdates.end()) {
4064 auto VisibleUpdates = std::move(I->second);
4065 PendingVisibleUpdates.erase(I);
4066
4067 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004068 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00004069 Lookups[DC].Table.add(
4070 Update.Mod, Update.Data,
4071 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
4072 DC->setHasExternalVisibleStorage(true);
4073 }
Chris Lattner487412d2009-04-27 05:27:42 +00004074}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004075
Richard Smithd61d4ac2015-08-22 20:13:39 +00004076void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
4077 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00004078 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00004079 if (FirstLocal != CanonDecl) {
4080 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
4081 ASTDeclReader::attachPreviousDecl(
4082 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
4083 CanonDecl);
4084 }
4085
4086 if (!LocalOffset) {
4087 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
4088 return;
4089 }
4090
4091 // Load the list of other redeclarations from this module file.
4092 ModuleFile *M = getOwningModuleFile(FirstLocal);
4093 assert(M && "imported decl from no module file");
4094
4095 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
4096 SavedStreamPosition SavedPosition(Cursor);
4097 Cursor.JumpToBit(LocalOffset);
4098
4099 RecordData Record;
4100 unsigned Code = Cursor.ReadCode();
4101 unsigned RecCode = Cursor.readRecord(Code, Record);
4102 (void)RecCode;
4103 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
4104
4105 // FIXME: We have several different dispatches on decl kind here; maybe
4106 // we should instead generate one loop per kind and dispatch up-front?
4107 Decl *MostRecent = FirstLocal;
4108 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4109 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00004110 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
4111 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00004112 }
Richard Smithc3a53252015-02-28 05:57:02 +00004113 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00004114}
4115
4116namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004117
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004118 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004119 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004120 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004121 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004122 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00004123 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004124 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004125 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004126 serialization::GlobalDeclID InterfaceID;
4127 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00004128
Douglas Gregor404cdde2012-01-27 01:47:08 +00004129 void add(ObjCCategoryDecl *Cat) {
4130 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00004131 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004132 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00004133
Douglas Gregor404cdde2012-01-27 01:47:08 +00004134 // Check for duplicate categories.
4135 if (Cat->getDeclName()) {
4136 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00004137 if (Existing &&
4138 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00004139 != Reader.getOwningModuleFile(Cat)) {
4140 // FIXME: We should not warn for duplicates in diamond:
4141 //
4142 // MT //
4143 // / \ //
4144 // ML MR //
4145 // \ / //
4146 // MB //
4147 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004148 // If there are duplicates in ML/MR, there will be warning when
4149 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004150 // importing.
4151 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4152 << Interface->getDeclName() << Cat->getDeclName();
4153 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4154 } else if (!Existing) {
4155 // Record this category.
4156 Existing = Cat;
4157 }
4158 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004159
Douglas Gregor404cdde2012-01-27 01:47:08 +00004160 // Add this category to the end of the chain.
4161 if (Tail)
4162 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4163 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004164 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004165 Tail = Cat;
4166 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004167
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004168 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004169 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004170 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004171 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4172 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004173 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004174 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4175 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004176 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004177 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004178 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004179 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004180
Douglas Gregor404cdde2012-01-27 01:47:08 +00004181 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004182 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004183 }
4184 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004185
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004186 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004187 // If we've loaded all of the category information we care about from
4188 // this module file, we're done.
4189 if (M.Generation <= PreviousGeneration)
4190 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004191
4192 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004193 // module file. If there is no such mapping, we'll find nothing here
4194 // (or in any module it imports).
4195 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4196 if (!LocalID)
4197 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004198
Douglas Gregor404cdde2012-01-27 01:47:08 +00004199 // Perform a binary search to find the local redeclarations for this
4200 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004201 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004202 const ObjCCategoriesInfo *Result
4203 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004204 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004205 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004206 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4207 Result->DefinitionID != LocalID) {
4208 // We didn't find anything. If the class definition is in this module
4209 // file, then the module files it depends on cannot have any categories,
4210 // so suppress further lookup.
4211 return Reader.isDeclIDFromModule(InterfaceID, M);
4212 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004213
Douglas Gregor404cdde2012-01-27 01:47:08 +00004214 // We found something. Dig out all of the categories.
4215 unsigned Offset = Result->Offset;
4216 unsigned N = M.ObjCCategories[Offset];
4217 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4218 for (unsigned I = 0; I != N; ++I)
4219 add(cast_or_null<ObjCCategoryDecl>(
4220 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4221 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004222 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004223 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004224
4225} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004226
Douglas Gregor404cdde2012-01-27 01:47:08 +00004227void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4228 ObjCInterfaceDecl *D,
4229 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004230 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004231 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004232 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004233}
Douglas Gregordab42432011-08-12 00:15:20 +00004234
Richard Smithd6db68c2014-08-07 20:58:41 +00004235template<typename DeclT, typename Fn>
4236static void forAllLaterRedecls(DeclT *D, Fn F) {
4237 F(D);
4238
4239 // Check whether we've already merged D into its redeclaration chain.
4240 // MostRecent may or may not be nullptr if D has not been merged. If
4241 // not, walk the merged redecl chain and see if it's there.
4242 auto *MostRecent = D->getMostRecentDecl();
4243 bool Found = false;
4244 for (auto *Redecl = MostRecent; Redecl && !Found;
4245 Redecl = Redecl->getPreviousDecl())
4246 Found = (Redecl == D);
4247
4248 // If this declaration is merged, apply the functor to all later decls.
4249 if (Found) {
4250 for (auto *Redecl = MostRecent; Redecl != D;
4251 Redecl = Redecl->getPreviousDecl())
4252 F(Redecl);
4253 }
4254}
4255
Vassil Vassilev7d264622017-06-30 22:40:17 +00004256void ASTDeclReader::UpdateDecl(Decl *D,
4257 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004258 while (Record.getIdx() < Record.size()) {
4259 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004260 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004261 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004262 // FIXME: If we also have an update record for instantiating the
4263 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004264 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004265 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004266 // FIXME: We should call addHiddenDecl instead, to add the member
4267 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004268 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004269 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004270 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004271
4272 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004273 // It will be added to the template's lazy specialization set.
4274 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004275 break;
4276
4277 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004278 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004279
Douglas Gregor540fd812012-01-09 18:07:24 +00004280 // Each module has its own anonymous namespace, which is disjoint from
4281 // any other module's anonymous namespaces, so don't attach the anonymous
4282 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004283 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004284 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004285 TU->setAnonymousNamespace(Anon);
4286 else
4287 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4288 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004289 break;
4290 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004291
Richard Smith891fc7f2017-12-05 01:31:47 +00004292 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004293 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004294 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4295 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004296 uint64_t Val = Record.readInt();
4297 if (Val && !VD->getInit()) {
4298 VD->setInit(Record.readExpr());
4299 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4300 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4301 Eval->CheckedICE = true;
4302 Eval->IsICE = Val == 3;
4303 }
4304 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004305 break;
Richard Smith05a21352017-06-22 22:18:46 +00004306 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004307
Richard Smith891fc7f2017-12-05 01:31:47 +00004308 case UPD_CXX_POINT_OF_INSTANTIATION: {
4309 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004310 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004311 VTSD->setPointOfInstantiation(POI);
4312 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4313 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4314 } else {
4315 auto *FD = cast<FunctionDecl>(D);
4316 if (auto *FTSInfo = FD->TemplateOrSpecialization
4317 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4318 FTSInfo->setPointOfInstantiation(POI);
4319 else
4320 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4321 ->setPointOfInstantiation(POI);
4322 }
4323 break;
4324 }
4325
John McCall32791cc2016-01-06 22:34:54 +00004326 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004327 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004328
4329 // We have to read the default argument regardless of whether we use it
4330 // so that hypothetical further update records aren't messed up.
4331 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004332 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004333
4334 // Only apply the update if the parameter still has an uninstantiated
4335 // default argument.
4336 if (Param->hasUninstantiatedDefaultArg())
4337 Param->setDefaultArg(DefaultArg);
4338 break;
4339 }
4340
Richard Smith4b054b22016-08-24 21:25:37 +00004341 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004342 auto *FD = cast<FieldDecl>(D);
4343 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004344
4345 // Only apply the update if the field still has an uninstantiated
4346 // default member initializer.
4347 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4348 if (DefaultInit)
4349 FD->setInClassInitializer(DefaultInit);
4350 else
4351 // Instantiation failed. We can get here if we serialized an AST for
4352 // an invalid program.
4353 FD->removeInClassInitializer();
4354 }
4355 break;
4356 }
4357
Richard Smith4d235792014-08-07 18:53:08 +00004358 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004359 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004360 if (Reader.PendingBodies[FD]) {
4361 // FIXME: Maybe check for ODR violations.
4362 // It's safe to stop now because this update record is always last.
4363 return;
4364 }
4365
David L. Jonesbe1557a2016-12-21 00:17:49 +00004366 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004367 // Maintain AST consistency: any later redeclarations of this function
4368 // are inline if this one is. (We might have merged another declaration
4369 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004370 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4371 FD->setImplicitlyInline();
4372 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004373 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004374 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004375 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004376 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004377 break;
4378 }
4379
Richard Smithcd45dbc2014-04-19 03:48:30 +00004380 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4381 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004382 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004383 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004384 OldDD && (OldDD->Definition != RD ||
4385 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004386 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004387 RD->setArgPassingRestrictions(
4388 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004389 ReadCXXRecordDefinition(RD, /*Update*/true);
4390
Richard Smithcd45dbc2014-04-19 03:48:30 +00004391 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004392 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004393 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004394 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004395 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004396 }
4397
David L. Jonesbe1557a2016-12-21 00:17:49 +00004398 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004399 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004400 if (MemberSpecializationInfo *MSInfo =
4401 RD->getMemberSpecializationInfo()) {
4402 MSInfo->setTemplateSpecializationKind(TSK);
4403 MSInfo->setPointOfInstantiation(POI);
4404 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004405 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004406 Spec->setTemplateSpecializationKind(TSK);
4407 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004408
David L. Jonesbe1557a2016-12-21 00:17:49 +00004409 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004410 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004411 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004412 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004413 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004414 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004415 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004416
4417 // FIXME: If we already have a partial specialization set,
4418 // check that it matches.
4419 if (!Spec->getSpecializedTemplateOrPartial()
4420 .is<ClassTemplatePartialSpecializationDecl *>())
4421 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004422 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004423 }
4424
David L. Jonesbe1557a2016-12-21 00:17:49 +00004425 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004426 RD->setLocation(ReadSourceLocation());
4427 RD->setLocStart(ReadSourceLocation());
4428 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004429
David L. Jonesbe1557a2016-12-21 00:17:49 +00004430 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004431 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004432 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004433 // If the declaration already has attributes, we assume that some other
4434 // AST file already loaded them.
4435 if (!D->hasAttrs())
4436 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004437 }
4438 break;
4439 }
4440
Richard Smithf8134002015-03-10 01:41:22 +00004441 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4442 // Set the 'operator delete' directly to avoid emitting another update
4443 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004444 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004445 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004446 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004447 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004448 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004449 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004450 First->OperatorDeleteThisArg = ThisArg;
4451 }
Richard Smithf8134002015-03-10 01:41:22 +00004452 break;
4453 }
4454
Richard Smith564417a2014-03-20 21:47:22 +00004455 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004456 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004457 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004458 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004459
4460 // Update this declaration's exception specification, if needed.
4461 auto *FD = cast<FunctionDecl>(D);
4462 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4463 // FIXME: If the exception specification is already present, check that it
4464 // matches.
4465 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004466 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004467 FPT->getReturnType(), FPT->getParamTypes(),
4468 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004469
4470 // When we get to the end of deserializing, see if there are other decls
4471 // that we need to propagate this exception specification onto.
4472 Reader.PendingExceptionSpecUpdates.insert(
4473 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004474 }
Richard Smith564417a2014-03-20 21:47:22 +00004475 break;
4476 }
4477
Richard Smith1fa5d642013-05-11 05:45:24 +00004478 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004479 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004480 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004481 Reader.PendingDeducedTypeUpdates.insert(
4482 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004483 break;
4484 }
Eli Friedman276dd182013-09-05 00:02:25 +00004485
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004486 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004487 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004488 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004489 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004490
4491 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004492 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4493 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004494 break;
4495
4496 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004497 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4498 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004499 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004500
Alexey Bataev97720002014-11-11 04:05:39 +00004501 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004502 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4503 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004504 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004505
Alexey Bataev27ef9512019-03-20 20:14:22 +00004506 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
4507 auto AllocatorKind =
4508 static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(Record.readInt());
4509 Expr *Allocator = Record.readExpr();
4510 SourceRange SR = ReadSourceRange();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00004511 D->addAttr(OMPAllocateDeclAttr::CreateImplicit(
Alexey Bataev27ef9512019-03-20 20:14:22 +00004512 Reader.getContext(), AllocatorKind, Allocator, SR));
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004513 break;
Alexey Bataev27ef9512019-03-20 20:14:22 +00004514 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004515
Alex Denisovfde64952015-06-26 05:28:36 +00004516 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004517 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004518 auto *Exported = cast<NamedDecl>(D);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004519 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith13897eb2018-09-12 23:37:00 +00004520 Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
4521 Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004522 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004523 }
Alex Denisovfde64952015-06-26 05:28:36 +00004524
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004525 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00004526 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
4527 Reader.getContext(),
4528 static_cast<OMPDeclareTargetDeclAttr::MapTypeTy>(Record.readInt()),
4529 ReadSourceRange()));
4530 break;
4531
Alex Denisovfde64952015-06-26 05:28:36 +00004532 case UPD_ADDED_ATTR_TO_RECORD:
4533 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004534 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004535 assert(Attrs.size() == 1);
4536 D->addAttr(Attrs[0]);
4537 break;
4538 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004539 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004540}