blob: 3bd7b825cdc82089f5cfbdcf93ff472295305dfc [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//
John McCall3ce3d232019-12-13 03:37:23 -05009// 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"
John McCallc2f18312019-12-14 03:01:28 -050051#include "clang/Serialization/ASTRecordReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000052#include "clang/Serialization/ContinuousRangeMap.h"
Duncan P. N. Exon Smithf7170d12019-11-21 18:49:05 -080053#include "clang/Serialization/ModuleFile.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000054#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"
Francis Visoiu Mistrihe0308272019-07-03 22:40:07 +000060#include "llvm/Bitstream/BitstreamReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000061#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
John McCall3ce3d232019-12-13 03:37:23 -0500114 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
John McCall3ce3d232019-12-13 03:37:23 -0500118 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
John McCall3ce3d232019-12-13 03:37:23 -0500122 TypeSourceInfo *readTypeSourceInfo() {
123 return Record.readTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000124 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000125
John McCall3ce3d232019-12-13 03:37:23 -0500126 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
John McCall3ce3d232019-12-13 03:37:23 -0500130 std::string readString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000131 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +0000132 }
133
John McCall3ce3d232019-12-13 03:37:23 -0500134 void readDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000135 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
John McCall3ce3d232019-12-13 03:37:23 -0500136 IDs.push_back(readDeclID());
Richard Smith0b884372015-02-27 00:25:58 +0000137 }
138
John McCall3ce3d232019-12-13 03:37:23 -0500139 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>
John McCall3ce3d232019-12-13 03:37:23 -0500144 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 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000149 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000150 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000151
David L. Jonesbe1557a2016-12-21 00:17:49 +0000152 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000153 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000154
155 Module *readModule() {
156 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000157 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000158
Richard Smith2a9e5c52015-02-03 03:32:14 +0000159 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000160 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
161 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000162 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000163 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000164 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000165 void MergeDefinitionData(ObjCInterfaceDecl *D,
166 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000167 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
168 void MergeDefinitionData(ObjCProtocolDecl *D,
169 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000170
Richard Smith600adef2018-07-04 02:25:38 +0000171 static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
172
Richard Smithd08aeb62014-08-28 01:33:39 +0000173 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
174 DeclContext *DC,
175 unsigned Index);
176 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
177 unsigned Index, NamedDecl *D);
178
Richard Smith0144f512015-08-22 02:09:38 +0000179 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000180 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000181 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000182 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000183 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000184
Douglas Gregor022857e2011-12-22 01:48:48 +0000185 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000186 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000187 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000188
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000189 /// Retrieve the first ID.
Douglas Gregor022857e2011-12-22 01:48:48 +0000190 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000191
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000192 /// Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000193 bool isKeyDecl() const { return IsKeyDecl; }
194
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000195 /// Get a known declaration that this should be merged with, if
Richard Smith5a4737c2015-02-06 02:42:59 +0000196 /// any.
197 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000198 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000200 /// Class used to capture the result of searching for an existing
Douglas Gregor022857e2011-12-22 01:48:48 +0000201 /// declaration of a specific kind and name, along with the ability
202 /// to update the place where this result was found (the declaration
203 /// chain hanging off an identifier or the DeclContext we searched in)
204 /// if requested.
205 class FindExistingResult {
206 ASTReader &Reader;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000207 NamedDecl *New = nullptr;
208 NamedDecl *Existing = nullptr;
209 bool AddResult = false;
210 unsigned AnonymousDeclNumber = 0;
211 IdentifierInfo *TypedefNameForLinkage = nullptr;
Richard Smithd08aeb62014-08-28 01:33:39 +0000212
Douglas Gregor022857e2011-12-22 01:48:48 +0000213 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000214 FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000215
Richard Smithd08aeb62014-08-28 01:33:39 +0000216 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000217 unsigned AnonymousDeclNumber,
218 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000219 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000220 AnonymousDeclNumber(AnonymousDeclNumber),
221 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000222
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000223 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000224 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
225 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000226 AnonymousDeclNumber(Other.AnonymousDeclNumber),
227 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000228 Other.AddResult = false;
229 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000230
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000231 FindExistingResult &operator=(FindExistingResult &&) = delete;
Douglas Gregor022857e2011-12-22 01:48:48 +0000232 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000233
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000234 /// Suppress the addition of this result into the known set of
Douglas Gregor2009cee2012-01-03 22:46:00 +0000235 /// names.
236 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000237
Douglas Gregor022857e2011-12-22 01:48:48 +0000238 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000239
Douglas Gregor022857e2011-12-22 01:48:48 +0000240 template<typename T>
241 operator T*() const { return dyn_cast_or_null<T>(Existing); }
242 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000243
Richard Smith8a639892015-01-24 01:07:20 +0000244 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
245 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000246 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000247
Chris Lattner487412d2009-04-27 05:27:42 +0000248 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000249 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
250 ASTReader::RecordLocation Loc,
251 DeclID thisDeclID, SourceLocation ThisDeclLoc)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000252 : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
253 ThisDeclLoc(ThisDeclLoc) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000254
Vassil Vassilev7d264622017-06-30 22:40:17 +0000255 template <typename T> static
256 void AddLazySpecializations(T *D,
257 SmallVectorImpl<serialization::DeclID>& IDs) {
258 if (IDs.empty())
259 return;
260
261 // FIXME: We should avoid this pattern of getting the ASTContext.
262 ASTContext &C = D->getASTContext();
263
264 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
265
266 if (auto &Old = LazySpecializations) {
267 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Fangrui Song55fab262018-09-26 22:16:28 +0000268 llvm::sort(IDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +0000269 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
270 }
271
272 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
273 *Result = IDs.size();
274 std::copy(IDs.begin(), IDs.end(), Result + 1);
275
276 LazySpecializations = Result;
277 }
278
Richard Smithb321ecb2014-05-13 01:15:00 +0000279 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000280 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
281 static Decl *getMostRecentDeclImpl(...);
282 static Decl *getMostRecentDecl(Decl *D);
283
284 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000285 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000286 Redeclarable<DeclT> *D, Decl *Previous,
287 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000288 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000289 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
290 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000291
292 template <typename DeclT>
293 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
294 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000295 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000296
Richard Smith851072e2014-05-19 20:59:20 +0000297 template <typename DeclT>
298 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
299 static void markIncompleteDeclChainImpl(...);
300
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000301 /// Determine whether this declaration has a pending body.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000302 bool hasPendingBody() const { return HasPendingBody; }
303
David Blaikieac4345c2017-02-12 18:45:31 +0000304 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000305 void Visit(Decl *D);
306
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000307 void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000308
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000309 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
310 ObjCCategoryDecl *Next) {
311 Cat->NextClassCategory = Next;
312 }
313
Chris Lattner487412d2009-04-27 05:27:42 +0000314 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000315 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000316 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000317 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
318 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000319 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000320 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000321 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
322 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000323 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000324 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000325 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000326 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000327 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000328 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000329 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000330 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
331 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
332 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
333 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
334 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000335 ClassTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000336
Richard Smith1d209d02013-05-23 01:49:11 +0000337 void VisitClassTemplateSpecializationDecl(
338 ClassTemplateSpecializationDecl *D) {
339 VisitClassTemplateSpecializationDeclImpl(D);
340 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000341
Chris Lattnerca025db2010-05-07 21:43:38 +0000342 void VisitClassTemplatePartialSpecializationDecl(
343 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000344 void VisitClassScopeFunctionSpecializationDecl(
345 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000346 RedeclarableResult
347 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000348
Larisse Voufo39a1e502013-08-06 01:03:05 +0000349 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
350 VisitVarTemplateSpecializationDeclImpl(D);
351 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000352
Larisse Voufo39a1e502013-08-06 01:03:05 +0000353 void VisitVarTemplatePartialSpecializationDecl(
354 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000355 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000356 void VisitValueDecl(ValueDecl *VD);
357 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000358 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000359 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000360 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000361 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000362 void VisitCXXMethodDecl(CXXMethodDecl *D);
363 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
364 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
365 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000366 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000367 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000368 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000369 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
370 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000371 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
372 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000373 void VisitDecompositionDecl(DecompositionDecl *DD);
374 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000375 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000376 DeclID VisitTemplateDecl(TemplateDecl *D);
Saar Razd7aae332019-07-10 21:25:49 +0000377 void VisitConceptDecl(ConceptDecl *D);
Saar Raza0f50d72020-01-18 09:11:43 +0200378 void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000379 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000380 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000381 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000382 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000383 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000384 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000385 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000386 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000387 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000388 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000389 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000390 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000391 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000392 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000393 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000394 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000395 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000396 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
397 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000398 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000399 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000400 void VisitEmptyDecl(EmptyDecl *D);
Tykerb0561b32019-11-17 11:41:55 +0100401 void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000402
Chris Lattner487412d2009-04-27 05:27:42 +0000403 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000404
405 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000406 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000407
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000408 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000409 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
410 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000411
412 template<typename T>
413 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000414 RedeclarableResult &Redecl,
415 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000416
Richard Smith0b87e072013-10-07 08:02:11 +0000417 template<typename T>
418 void mergeMergeable(Mergeable<T> *D);
419
Tyker9ec6d712019-11-30 16:42:33 +0100420 void mergeMergeable(LifetimeExtendedTemporaryDecl *D);
421
Richard Smithf17fdbd2014-04-24 02:25:27 +0000422 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
423 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000424 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000425
Douglas Gregor85f3f952015-07-07 03:57:15 +0000426 ObjCTypeParamList *ReadObjCTypeParamList();
427
Alexis Hunted053252010-05-30 07:21:58 +0000428 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000429 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000430 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000431 void VisitObjCContainerDecl(ObjCContainerDecl *D);
432 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
433 void VisitObjCIvarDecl(ObjCIvarDecl *D);
434 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
435 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000436 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
437 void VisitObjCImplDecl(ObjCImplDecl *D);
438 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
439 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
440 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
441 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
442 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000443 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000444 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000445 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Michael Kruse251e1482019-02-01 20:25:04 +0000446 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
Kelvin Li1408f912018-09-26 04:28:39 +0000447 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000448 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000449 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000450
451} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000452
Richard Smith86dfc1e2015-06-18 22:07:00 +0000453namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000454
Richard Smith86dfc1e2015-06-18 22:07:00 +0000455/// Iterator over the redeclarations of a declaration that have already
456/// been merged into the same redeclaration chain.
457template<typename DeclT>
458class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000459 DeclT *Start;
460 DeclT *Canonical = nullptr;
461 DeclT *Current = nullptr;
462
Richard Smith86dfc1e2015-06-18 22:07:00 +0000463public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000464 MergedRedeclIterator() = default;
465 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000466
467 DeclT *operator*() { return Current; }
468
469 MergedRedeclIterator &operator++() {
470 if (Current->isFirstDecl()) {
471 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000472 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000473 } else
474 Current = Current->getPreviousDecl();
475
476 // If we started in the merged portion, we'll reach our start position
477 // eventually. Otherwise, we'll never reach it, but the second declaration
478 // we reached was the canonical declaration, so stop when we see that one
479 // again.
480 if (Current == Start || Current == Canonical)
481 Current = nullptr;
482 return *this;
483 }
484
485 friend bool operator!=(const MergedRedeclIterator &A,
486 const MergedRedeclIterator &B) {
487 return A.Current != B.Current;
488 }
489};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000490
491} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000492
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000493template <typename DeclT>
494static llvm::iterator_range<MergedRedeclIterator<DeclT>>
495merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000496 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
497 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000498}
499
Sebastian Redlb3298c32010-08-18 23:56:48 +0000500uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000501 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000502}
503
David Blaikieac4345c2017-02-12 18:45:31 +0000504void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Hans Wennborg7ea9a6e2020-02-27 14:33:43 +0100505 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000506 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000507 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Erich Keane9c665062018-08-01 21:02:40 +0000508 CD->setNumCtorInitializers(Record.readInt());
509 if (CD->getNumCtorInitializers())
David Blaikieac4345c2017-02-12 18:45:31 +0000510 CD->CtorInitializers = ReadGlobalOffset();
511 }
512 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000513 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
514 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000515}
516
Sebastian Redlb3298c32010-08-18 23:56:48 +0000517void ASTDeclReader::Visit(Decl *D) {
518 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000519
Vassil Vassilev928c8252016-04-28 14:13:28 +0000520 // At this point we have deserialized and merged the decl and it is safe to
521 // update its canonical decl to signal that the entire entity is used.
522 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
523 IsDeclMarkedUsed = false;
524
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000525 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000526 if (auto *TInfo = DD->getTypeSourceInfo())
527 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000528 }
529
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000530 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000531 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000532 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000533
534 // If this is a tag declaration with a typedef name for linkage, it's safe
535 // to load that typedef now.
536 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000537 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
538 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000539 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000540 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000541 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000542 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000543 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000544 // We only read it if FD doesn't already have a body (e.g., from another
545 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000546 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000547 if (Record.readInt())
548 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000549 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000550}
551
Sebastian Redlb3298c32010-08-18 23:56:48 +0000552void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000553 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
John McCall77b2ffc2020-02-12 18:40:00 -0500554 isa<ParmVarDecl>(D) || isa<ObjCTypeParamDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000555 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000556 // parameter or of a parameter of a function template immediately. These
557 // entities might be used in the formulation of its DeclContext (for
558 // example, a function parameter can be used in decltype() in trailing
559 // return type of the function). Use the translation unit DeclContext as a
560 // placeholder.
John McCall3ce3d232019-12-13 03:37:23 -0500561 GlobalDeclID SemaDCIDForTemplateParmDecl = readDeclID();
562 GlobalDeclID LexicalDCIDForTemplateParmDecl = readDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000563 if (!LexicalDCIDForTemplateParmDecl)
564 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000565 Reader.addPendingDeclContextInfo(D,
566 SemaDCIDForTemplateParmDecl,
567 LexicalDCIDForTemplateParmDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000568 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000569 } else {
John McCall3ce3d232019-12-13 03:37:23 -0500570 auto *SemaDC = readDeclAs<DeclContext>();
571 auto *LexicalDC = readDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000572 if (!LexicalDC)
573 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000574 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000575 // Avoid calling setLexicalDeclContext() directly because it uses
576 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000577 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
578 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000579 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000580 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000581 D->setInvalidDecl(Record.readInt());
582 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000583 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000584 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000585 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
586 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000587 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000588 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000589 D->setImplicit(Record.readInt());
590 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000591 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000592 D->setReferenced(Record.readInt());
593 D->setTopLevelDeclInObjCContainer(Record.readInt());
594 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000595 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000596 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000597
Douglas Gregorcf68c582011-12-01 22:20:10 +0000598 // Determine whether this declaration is part of a (sub)module. If so, it
599 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000600 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000601 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000602 D->setModuleOwnershipKind(
603 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
604 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000605 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000606
Richard Smith90dc5252017-06-23 01:04:34 +0000607 if (ModulePrivate) {
608 // Module-private declarations are never visible, so there is no work to
609 // do.
Richard Smith42413142015-05-15 20:05:43 +0000610 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
611 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000612 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000613 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000614 // Mark the declaration as visible when its owning module becomes visible.
615 if (Owner->NameVisibility == Module::AllVisible)
616 D->setVisibleDespiteOwningModule();
617 else
Richard Smith42413142015-05-15 20:05:43 +0000618 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000619 }
Richard Smithd19389a2017-07-05 07:47:11 +0000620 } else if (ModulePrivate) {
621 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000622 }
Chris Lattner487412d2009-04-27 05:27:42 +0000623}
624
Nico Weber66220292016-03-02 17:28:48 +0000625void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
626 VisitDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -0500627 D->setLocation(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000628 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -0500629 std::string Arg = readString();
Nico Weber66220292016-03-02 17:28:48 +0000630 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
631 D->getTrailingObjects<char>()[Arg.size()] = '\0';
632}
633
Nico Webercbbaeb12016-03-02 19:28:54 +0000634void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
635 VisitDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -0500636 D->setLocation(readSourceLocation());
637 std::string Name = readString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000638 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
639 D->getTrailingObjects<char>()[Name.size()] = '\0';
640
641 D->ValueStart = Name.size() + 1;
John McCall3ce3d232019-12-13 03:37:23 -0500642 std::string Value = readString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000643 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
644 Value.size());
645 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
646}
647
Sebastian Redlb3298c32010-08-18 23:56:48 +0000648void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000649 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000650}
651
Sebastian Redlb3298c32010-08-18 23:56:48 +0000652void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000653 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000654 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000655 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000656}
657
Sebastian Redlb3298c32010-08-18 23:56:48 +0000658void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000659 VisitNamedDecl(TD);
John McCall3ce3d232019-12-13 03:37:23 -0500660 TD->setLocStart(readSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000661 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000662 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000663}
664
Richard Smith43ccec8e2014-08-26 03:52:16 +0000665ASTDeclReader::RedeclarableResult
666ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000667 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000668 VisitTypeDecl(TD);
John McCall3ce3d232019-12-13 03:37:23 -0500669 TypeSourceInfo *TInfo = readTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000670 if (Record.readInt()) { // isModed
671 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000672 TD->setModedTypeSourceInfo(TInfo, modedT);
673 } else
674 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000675 // Read and discard the declaration for which this is a typedef name for
676 // linkage, if it exists. We cannot rely on our type to pull in this decl,
677 // because it might have been merged with a type from another module and
678 // thus might not refer to our version of the declaration.
John McCall3ce3d232019-12-13 03:37:23 -0500679 readDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000680 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000681}
682
683void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000684 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
685 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000686}
687
Richard Smithdda56e42011-04-15 14:24:37 +0000688void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000689 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
John McCall3ce3d232019-12-13 03:37:23 -0500690 if (auto *Template = readDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000691 // Merged when we merge the template.
692 TD->setDescribedAliasTemplate(Template);
693 else
694 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000695}
696
Richard Smith1d209d02013-05-23 01:49:11 +0000697ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000698 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000699 VisitTypeDecl(TD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000700
David L. Jonesbe1557a2016-12-21 00:17:49 +0000701 TD->IdentifierNamespace = Record.readInt();
702 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000703 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000704 TD->setCompleteDefinition(Record.readInt());
705 TD->setEmbeddedInDeclarator(Record.readInt());
706 TD->setFreeStanding(Record.readInt());
707 TD->setCompleteDefinitionRequired(Record.readInt());
John McCall3ce3d232019-12-13 03:37:23 -0500708 TD->setBraceRange(readSourceRange());
Fangrui Song6907ce22018-07-30 19:24:48 +0000709
David L. Jonesbe1557a2016-12-21 00:17:49 +0000710 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000711 case 0:
712 break;
713 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000714 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
John McCall3ce3d232019-12-13 03:37:23 -0500715 Record.readQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000716 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000717 break;
718 }
719 case 2: // TypedefNameForAnonDecl
John McCall3ce3d232019-12-13 03:37:23 -0500720 NamedDeclForTagDecl = readDeclID();
721 TypedefNameForLinkage = Record.readIdentifier();
Richard Smith70d58502014-08-30 00:04:23 +0000722 break;
Richard Smith70d58502014-08-30 00:04:23 +0000723 default:
724 llvm_unreachable("unexpected tag info kind");
725 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000726
Richard Smithcd45dbc2014-04-19 03:48:30 +0000727 if (!isa<CXXRecordDecl>(TD))
728 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000729 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000730}
731
Sebastian Redlb3298c32010-08-18 23:56:48 +0000732void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000733 VisitTagDecl(ED);
John McCall3ce3d232019-12-13 03:37:23 -0500734 if (TypeSourceInfo *TI = readTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000735 ED->setIntegerTypeSourceInfo(TI);
736 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000737 ED->setIntegerType(Record.readType());
738 ED->setPromotionType(Record.readType());
739 ED->setNumPositiveBits(Record.readInt());
740 ED->setNumNegativeBits(Record.readInt());
Erich Keanef92f31c2018-08-01 20:48:16 +0000741 ED->setScoped(Record.readInt());
742 ED->setScopedUsingClassTag(Record.readInt());
743 ED->setFixed(Record.readInt());
Richard Smith4b38ded2012-03-14 23:13:10 +0000744
Erich Keanef92f31c2018-08-01 20:48:16 +0000745 ED->setHasODRHash(true);
Richard Trieuab4d7302018-07-25 22:52:05 +0000746 ED->ODRHash = Record.readInt();
747
Richard Smith01a73372013-10-15 22:02:41 +0000748 // If this is a definition subject to the ODR, and we already have a
749 // definition, merge this one into it.
Erich Keanef92f31c2018-08-01 20:48:16 +0000750 if (ED->isCompleteDefinition() &&
Richard Smith01a73372013-10-15 22:02:41 +0000751 Reader.getContext().getLangOpts().Modules &&
752 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000753 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
754 if (!OldDef) {
755 // This is the first time we've seen an imported definition. Look for a
756 // local definition before deciding that we are the first definition.
757 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
758 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
759 OldDef = D;
760 break;
761 }
762 }
763 }
764 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000765 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
Erich Keanef92f31c2018-08-01 20:48:16 +0000766 ED->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +0000767 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000768 if (OldDef->getODRHash() != ED->getODRHash())
769 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000770 } else {
771 OldDef = ED;
772 }
773 }
774
John McCall3ce3d232019-12-13 03:37:23 -0500775 if (auto *InstED = readDeclAs<EnumDecl>()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000776 auto TSK = (TemplateSpecializationKind)Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -0500777 SourceLocation POI = readSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000778 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
779 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
780 }
Chris Lattner487412d2009-04-27 05:27:42 +0000781}
782
Richard Smith1d209d02013-05-23 01:49:11 +0000783ASTDeclReader::RedeclarableResult
784ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
785 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000786 RD->setHasFlexibleArrayMember(Record.readInt());
787 RD->setAnonymousStructOrUnion(Record.readInt());
788 RD->setHasObjectMember(Record.readInt());
789 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000790 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
791 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
792 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanaka09051062019-09-07 00:34:43 +0000793 RD->setHasNonTrivialToPrimitiveDefaultInitializeCUnion(Record.readInt());
794 RD->setHasNonTrivialToPrimitiveDestructCUnion(Record.readInt());
795 RD->setHasNonTrivialToPrimitiveCopyCUnion(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);
John McCall3ce3d232019-12-13 03:37:23 -0500822 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();
John McCall3ce3d232019-12-13 03:37:23 -0500825 Record.readQualifierInfo(*Info);
Saar Razb65b1f32020-01-09 15:07:51 +0200826 Info->TrailingRequiresClause = Record.readExpr();
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000827 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000828 }
Richard Smithc23d7342018-06-29 20:46:25 +0000829 QualType TSIType = Record.readType();
830 DD->setTypeSourceInfo(
831 TSIType.isNull() ? nullptr
832 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000833}
834
Sebastian Redlb3298c32010-08-18 23:56:48 +0000835void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000836 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000837 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000838
Richard Smith600adef2018-07-04 02:25:38 +0000839 // Attach a type to this function. Use the real type if possible, but fall
840 // back to the type as written if it involves a deduced return type.
841 if (FD->getTypeSourceInfo() &&
842 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
843 ->getReturnType()->getContainedAutoType()) {
844 // We'll set up the real type in Visit, once we've finished loading the
845 // function.
846 FD->setType(FD->getTypeSourceInfo()->getType());
Richard Smitha62d1982018-08-03 01:00:01 +0000847 Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
Richard Smith600adef2018-07-04 02:25:38 +0000848 } else {
849 FD->setType(Reader.GetType(DeferredTypeID));
Richard Smith600adef2018-07-04 02:25:38 +0000850 }
Richard Smitha62d1982018-08-03 01:00:01 +0000851 DeferredTypeID = 0;
Richard Smith600adef2018-07-04 02:25:38 +0000852
John McCall3ce3d232019-12-13 03:37:23 -0500853 FD->DNLoc = Record.readDeclarationNameLoc(FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000854 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000855
Douglas Gregorb2585692012-01-04 17:13:46 +0000856 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
857 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000858
Erich Keane9c665062018-08-01 21:02:40 +0000859 FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
860 FD->setInlineSpecified(Record.readInt());
861 FD->setImplicitlyInline(Record.readInt());
Erich Keane9c665062018-08-01 21:02:40 +0000862 FD->setVirtualAsWritten(Record.readInt());
863 FD->setPure(Record.readInt());
864 FD->setHasInheritedPrototype(Record.readInt());
865 FD->setHasWrittenPrototype(Record.readInt());
866 FD->setDeletedAsWritten(Record.readInt());
867 FD->setTrivial(Record.readInt());
868 FD->setTrivialForCall(Record.readInt());
869 FD->setDefaulted(Record.readInt());
870 FD->setExplicitlyDefaulted(Record.readInt());
871 FD->setHasImplicitReturnZero(Record.readInt());
Gauthier Harnisch796ed032019-06-14 08:56:20 +0000872 FD->setConstexprKind(static_cast<ConstexprSpecKind>(Record.readInt()));
Erich Keane9c665062018-08-01 21:02:40 +0000873 FD->setUsesSEHTry(Record.readInt());
874 FD->setHasSkippedBody(Record.readInt());
875 FD->setIsMultiVersion(Record.readInt());
876 FD->setLateTemplateParsed(Record.readInt());
877
878 FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
John McCall3ce3d232019-12-13 03:37:23 -0500879 FD->EndRangeLoc = readSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000880
Richard Trieue6caa262017-12-23 00:41:01 +0000881 FD->ODRHash = Record.readInt();
Erich Keane9c665062018-08-01 21:02:40 +0000882 FD->setHasODRHash(true);
Melanie Blower7f9b5132019-12-04 12:23:46 -0800883 FD->setUsesFPIntrin(Record.readInt());
Richard Trieue6caa262017-12-23 00:41:01 +0000884
Richard Smith848934c2019-12-05 13:37:35 -0800885 if (FD->isDefaulted()) {
886 if (unsigned NumLookups = Record.readInt()) {
887 SmallVector<DeclAccessPair, 8> Lookups;
888 for (unsigned I = 0; I != NumLookups; ++I) {
889 NamedDecl *ND = Record.readDeclAs<NamedDecl>();
890 AccessSpecifier AS = (AccessSpecifier)Record.readInt();
891 Lookups.push_back(DeclAccessPair::make(ND, AS));
892 }
893 FD->setDefaultedFunctionInfo(FunctionDecl::DefaultedFunctionInfo::Create(
894 Reader.getContext(), Lookups));
895 }
896 }
897
David L. Jonesbe1557a2016-12-21 00:17:49 +0000898 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000899 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000900 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000901 break;
902 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000903 // Merged when we merge the template.
John McCall3ce3d232019-12-13 03:37:23 -0500904 FD->setDescribedFunctionTemplate(readDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000905 break;
906 case FunctionDecl::TK_MemberSpecialization: {
John McCall3ce3d232019-12-13 03:37:23 -0500907 auto *InstFD = readDeclAs<FunctionDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000908 auto TSK = (TemplateSpecializationKind)Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -0500909 SourceLocation POI = readSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000910 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000911 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000912 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000913 break;
914 }
915 case FunctionDecl::TK_FunctionTemplateSpecialization: {
John McCall3ce3d232019-12-13 03:37:23 -0500916 auto *Template = readDeclAs<FunctionTemplateDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000917 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000918
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000919 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000920 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000921 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000922
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000923 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000924 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000925 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000926 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000927 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000928 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000929 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000930 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000931 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000932
John McCall3ce3d232019-12-13 03:37:23 -0500933 LAngleLoc = readSourceLocation();
934 RAngleLoc = readSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000935 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000936
John McCall3ce3d232019-12-13 03:37:23 -0500937 SourceLocation POI = readSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000938
Douglas Gregor4163aca2011-09-09 21:34:22 +0000939 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000940 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000941 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000942 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000943 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000944 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Richard Smithf19a8b02019-05-02 00:49:14 +0000945
946 MemberSpecializationInfo *MSInfo = nullptr;
947 if (Record.readInt()) {
John McCall3ce3d232019-12-13 03:37:23 -0500948 auto *FD = readDeclAs<FunctionDecl>();
Richard Smithf19a8b02019-05-02 00:49:14 +0000949 auto TSK = (TemplateSpecializationKind)Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -0500950 SourceLocation POI = readSourceLocation();
Richard Smithf19a8b02019-05-02 00:49:14 +0000951
952 MSInfo = new (C) MemberSpecializationInfo(FD, TSK);
953 MSInfo->setPointOfInstantiation(POI);
954 }
955
956 FunctionTemplateSpecializationInfo *FTInfo =
957 FunctionTemplateSpecializationInfo::Create(
958 C, FD, Template, TSK, TemplArgList,
959 HasTemplateArgumentsAsWritten ? &TemplArgsInfo : nullptr, POI,
960 MSInfo);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000961 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000962
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000963 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000964 // The template that contains the specializations set. It's not safe to
965 // use getCanonicalDecl on Template since it may still be initializing.
John McCall3ce3d232019-12-13 03:37:23 -0500966 auto *CanonTemplate = readDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000967 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
968 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
969 // FunctionTemplateSpecializationInfo's Profile().
970 // We avoid getASTContext because a decl in the parent hierarchy may
971 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000972 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000973 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000974 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000975 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000976 FunctionTemplateSpecializationInfo *ExistingInfo =
977 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000978 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000979 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
980 else {
981 assert(Reader.getContext().getLangOpts().Modules &&
982 "already deserialized this template specialization");
Richard Smithf19a8b02019-05-02 00:49:14 +0000983 mergeRedeclarable(FD, ExistingInfo->getFunction(), Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000984 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000985 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000986 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000987 }
988 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
989 // Templates.
990 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000991 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000992 while (NumTemplates--)
John McCall3ce3d232019-12-13 03:37:23 -0500993 TemplDecls.addDecl(readDeclAs<NamedDecl>());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000994
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000995 // Templates args.
996 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000997 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000998 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000999 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
John McCall3ce3d232019-12-13 03:37:23 -05001000 TemplArgs.setLAngleLoc(readSourceLocation());
1001 TemplArgs.setRAngleLoc(readSourceLocation());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001002
Douglas Gregor4163aca2011-09-09 21:34:22 +00001003 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00001004 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +00001005 // These are not merged; we don't need to merge redeclarations of dependent
1006 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00001007 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00001008 }
1009 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +00001010
Chris Lattnerca025db2010-05-07 21:43:38 +00001011 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001012 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001013 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001014 Params.reserve(NumParams);
1015 for (unsigned I = 0; I != NumParams; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001016 Params.push_back(readDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001017 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +00001018}
1019
Sebastian Redlb3298c32010-08-18 23:56:48 +00001020void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001021 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001022 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00001023 // Load the body on-demand. Most clients won't care, because method
1024 // definitions rarely show up in headers.
1025 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1026 HasPendingBody = true;
Chris Lattner487412d2009-04-27 05:27:42 +00001027 }
John McCall3ce3d232019-12-13 03:37:23 -05001028 MD->setSelfDecl(readDeclAs<ImplicitParamDecl>());
1029 MD->setCmdDecl(readDeclAs<ImplicitParamDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001030 MD->setInstanceMethod(Record.readInt());
1031 MD->setVariadic(Record.readInt());
1032 MD->setPropertyAccessor(Record.readInt());
Adrian Prantl2073dd22019-11-04 14:28:14 -08001033 MD->setSynthesizedAccessorStub(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001034 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001035 MD->setOverriding(Record.readInt());
1036 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001037
Erich Keane9b18eca2018-08-01 21:31:08 +00001038 MD->setIsRedeclaration(Record.readInt());
1039 MD->setHasRedeclaration(Record.readInt());
1040 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001041 Reader.getContext().setObjCMethodRedeclaration(MD,
John McCall3ce3d232019-12-13 03:37:23 -05001042 readDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001043
David L. Jonesbe1557a2016-12-21 00:17:49 +00001044 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1045 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001046 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001047 MD->setReturnType(Record.readType());
John McCall3ce3d232019-12-13 03:37:23 -05001048 MD->setReturnTypeSourceInfo(readTypeSourceInfo());
1049 MD->DeclEndLoc = readSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001050 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001051 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001052 Params.reserve(NumParams);
1053 for (unsigned I = 0; I != NumParams; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001054 Params.push_back(readDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001055
Erich Keane9b18eca2018-08-01 21:31:08 +00001056 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001057 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001058 SmallVector<SourceLocation, 16> SelLocs;
1059 SelLocs.reserve(NumStoredSelLocs);
1060 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
John McCall3ce3d232019-12-13 03:37:23 -05001061 SelLocs.push_back(readSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001062
1063 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001064}
1065
Douglas Gregor85f3f952015-07-07 03:57:15 +00001066void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001067 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001068
David L. Jonesbe1557a2016-12-21 00:17:49 +00001069 D->Variance = Record.readInt();
1070 D->Index = Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -05001071 D->VarianceLoc = readSourceLocation();
1072 D->ColonLoc = readSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001073}
1074
Sebastian Redlb3298c32010-08-18 23:56:48 +00001075void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001076 VisitNamedDecl(CD);
John McCall3ce3d232019-12-13 03:37:23 -05001077 CD->setAtStartLoc(readSourceLocation());
1078 CD->setAtEndRange(readSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001079}
1080
Douglas Gregor85f3f952015-07-07 03:57:15 +00001081ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001082 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001083 if (numParams == 0)
1084 return nullptr;
1085
1086 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1087 typeParams.reserve(numParams);
1088 for (unsigned i = 0; i != numParams; ++i) {
John McCall3ce3d232019-12-13 03:37:23 -05001089 auto *typeParam = readDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001090 if (!typeParam)
1091 return nullptr;
1092
1093 typeParams.push_back(typeParam);
1094 }
1095
John McCall3ce3d232019-12-13 03:37:23 -05001096 SourceLocation lAngleLoc = readSourceLocation();
1097 SourceLocation rAngleLoc = readSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001098
1099 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1100 typeParams, rAngleLoc);
1101}
1102
Manman Renec315f12016-09-09 23:48:27 +00001103void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001104 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001105 // Read the superclass.
John McCall3ce3d232019-12-13 03:37:23 -05001106 Data.SuperClassTInfo = readTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001107
John McCall3ce3d232019-12-13 03:37:23 -05001108 Data.EndLoc = readSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001109 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001110
Manman Renec315f12016-09-09 23:48:27 +00001111 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001112 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001113 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1114 Protocols.reserve(NumProtocols);
1115 for (unsigned I = 0; I != NumProtocols; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001116 Protocols.push_back(readDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001117 SmallVector<SourceLocation, 16> ProtoLocs;
1118 ProtoLocs.reserve(NumProtocols);
1119 for (unsigned I = 0; I != NumProtocols; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001120 ProtoLocs.push_back(readSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001121 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1122 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001123
Manman Renec315f12016-09-09 23:48:27 +00001124 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001125 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001126 Protocols.clear();
1127 Protocols.reserve(NumProtocols);
1128 for (unsigned I = 0; I != NumProtocols; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001129 Protocols.push_back(readDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001130 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1131 Reader.getContext());
1132}
1133
1134void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1135 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1136 // FIXME: odr checking?
1137}
1138
Sebastian Redlb3298c32010-08-18 23:56:48 +00001139void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001140 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001141 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001142 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001143 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001144
1145 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001146 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001147 // Read the definition.
1148 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001149
David L. Jonesbe1557a2016-12-21 00:17:49 +00001150 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001151 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1152 if (Canon->Data.getPointer()) {
1153 // If we already have a definition, keep the definition invariant and
1154 // merge the data.
1155 MergeDefinitionData(Canon, std::move(ID->data()));
1156 ID->Data = Canon->Data;
1157 } else {
1158 // Set the definition data of the canonical declaration, so other
1159 // redeclarations will see it.
1160 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001161
Manman Renec315f12016-09-09 23:48:27 +00001162 // We will rebuild this list lazily.
1163 ID->setIvarList(nullptr);
1164 }
Craig Toppera13603a2014-05-22 05:54:18 +00001165
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001166 // Note that we have deserialized a definition.
1167 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001168
Douglas Gregor404cdde2012-01-27 01:47:08 +00001169 // Note that we've loaded this Objective-C class.
1170 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001171 } else {
1172 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001173 }
Chris Lattner487412d2009-04-27 05:27:42 +00001174}
1175
Sebastian Redlb3298c32010-08-18 23:56:48 +00001176void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001177 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001178 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001179 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001180 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001181 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001182 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001183}
1184
Graydon Hoaree0a68352017-06-28 18:36:27 +00001185void ASTDeclReader::ReadObjCDefinitionData(
1186 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001187 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001188 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1189 ProtoRefs.reserve(NumProtoRefs);
1190 for (unsigned I = 0; I != NumProtoRefs; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001191 ProtoRefs.push_back(readDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001192 SmallVector<SourceLocation, 16> ProtoLocs;
1193 ProtoLocs.reserve(NumProtoRefs);
1194 for (unsigned I = 0; I != NumProtoRefs; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001195 ProtoLocs.push_back(readSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001196 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1197 ProtoLocs.data(), Reader.getContext());
1198}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001199
Graydon Hoaree0a68352017-06-28 18:36:27 +00001200void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1201 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1202 // FIXME: odr checking?
1203}
1204
1205void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1206 RedeclarableResult Redecl = VisitRedeclarable(PD);
1207 VisitObjCContainerDecl(PD);
1208 mergeRedeclarable(PD, Redecl);
1209
1210 if (Record.readInt()) {
1211 // Read the definition.
1212 PD->allocateDefinitionData();
1213
1214 ReadObjCDefinitionData(PD->data());
1215
1216 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1217 if (Canon->Data.getPointer()) {
1218 // If we already have a definition, keep the definition invariant and
1219 // merge the data.
1220 MergeDefinitionData(Canon, std::move(PD->data()));
1221 PD->Data = Canon->Data;
1222 } else {
1223 // Set the definition data of the canonical declaration, so other
1224 // redeclarations will see it.
1225 PD->getCanonicalDecl()->Data = PD->Data;
1226 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001227 // Note that we have deserialized a definition.
1228 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001229 } else {
1230 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001231 }
Chris Lattner487412d2009-04-27 05:27:42 +00001232}
1233
Sebastian Redlb3298c32010-08-18 23:56:48 +00001234void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001235 VisitFieldDecl(FD);
1236}
1237
Sebastian Redlb3298c32010-08-18 23:56:48 +00001238void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001239 VisitObjCContainerDecl(CD);
John McCall3ce3d232019-12-13 03:37:23 -05001240 CD->setCategoryNameLoc(readSourceLocation());
1241 CD->setIvarLBraceLoc(readSourceLocation());
1242 CD->setIvarRBraceLoc(readSourceLocation());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001243
Douglas Gregor404cdde2012-01-27 01:47:08 +00001244 // Note that this category has been deserialized. We do this before
1245 // deserializing the interface declaration, so that it will consider this
1246 /// category.
1247 Reader.CategoriesDeserialized.insert(CD);
1248
John McCall3ce3d232019-12-13 03:37:23 -05001249 CD->ClassInterface = readDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001250 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001251 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001252 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001253 ProtoRefs.reserve(NumProtoRefs);
1254 for (unsigned I = 0; I != NumProtoRefs; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001255 ProtoRefs.push_back(readDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001256 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001257 ProtoLocs.reserve(NumProtoRefs);
1258 for (unsigned I = 0; I != NumProtoRefs; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001259 ProtoLocs.push_back(readSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001260 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001261 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001262
1263 // Protocols in the class extension belong to the class.
1264 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1265 CD->ClassInterface->mergeClassExtensionProtocolList(
1266 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1267 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001268}
1269
Sebastian Redlb3298c32010-08-18 23:56:48 +00001270void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001271 VisitNamedDecl(CAD);
John McCall3ce3d232019-12-13 03:37:23 -05001272 CAD->setClassInterface(readDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001273}
1274
Sebastian Redlb3298c32010-08-18 23:56:48 +00001275void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001276 VisitNamedDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001277 D->setAtLoc(readSourceLocation());
1278 D->setLParenLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001279 QualType T = Record.readType();
John McCall3ce3d232019-12-13 03:37:23 -05001280 TypeSourceInfo *TSI = readTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001281 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001282 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001283 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001284 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001285 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001286 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001287 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001288 DeclarationName GetterName = Record.readDeclarationName();
John McCall3ce3d232019-12-13 03:37:23 -05001289 SourceLocation GetterLoc = readSourceLocation();
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001290 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1291 DeclarationName SetterName = Record.readDeclarationName();
John McCall3ce3d232019-12-13 03:37:23 -05001292 SourceLocation SetterLoc = readSourceLocation();
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001293 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
John McCall3ce3d232019-12-13 03:37:23 -05001294 D->setGetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1295 D->setSetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1296 D->setPropertyIvarDecl(readDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001297}
1298
Sebastian Redlb3298c32010-08-18 23:56:48 +00001299void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001300 VisitObjCContainerDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001301 D->setClassInterface(readDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001302}
1303
Sebastian Redlb3298c32010-08-18 23:56:48 +00001304void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001305 VisitObjCImplDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001306 D->CategoryNameLoc = readSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001307}
1308
Sebastian Redlb3298c32010-08-18 23:56:48 +00001309void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001310 VisitObjCImplDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001311 D->setSuperClass(readDeclAs<ObjCInterfaceDecl>());
1312 D->SuperLoc = readSourceLocation();
1313 D->setIvarLBraceLoc(readSourceLocation());
1314 D->setIvarRBraceLoc(readSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001315 D->setHasNonZeroConstructors(Record.readInt());
1316 D->setHasDestructors(Record.readInt());
1317 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001318 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001319 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001320}
1321
Sebastian Redlb3298c32010-08-18 23:56:48 +00001322void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001323 VisitDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001324 D->setAtLoc(readSourceLocation());
1325 D->setPropertyDecl(readDeclAs<ObjCPropertyDecl>());
1326 D->PropertyIvarDecl = readDeclAs<ObjCIvarDecl>();
1327 D->IvarLoc = readSourceLocation();
1328 D->setGetterMethodDecl(readDeclAs<ObjCMethodDecl>());
1329 D->setSetterMethodDecl(readDeclAs<ObjCMethodDecl>());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001330 D->setGetterCXXConstructor(Record.readExpr());
1331 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001332}
1333
Sebastian Redlb3298c32010-08-18 23:56:48 +00001334void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001335 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001336 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001337
1338 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1339 FD->InitStorage.setInt(ISK);
1340 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1341 ? Record.readType().getAsOpaquePtr()
1342 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001343 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001344
1345 if (auto *BW = Record.readExpr())
1346 FD->setBitWidth(BW);
1347
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001348 if (!FD->getDeclName()) {
John McCall3ce3d232019-12-13 03:37:23 -05001349 if (auto *Tmpl = readDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001350 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001351 }
Richard Smith0b87e072013-10-07 08:02:11 +00001352 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001353}
1354
John McCall5e77d762013-04-16 07:28:30 +00001355void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1356 VisitDeclaratorDecl(PD);
John McCall3ce3d232019-12-13 03:37:23 -05001357 PD->GetterId = Record.readIdentifier();
1358 PD->SetterId = Record.readIdentifier();
John McCall5e77d762013-04-16 07:28:30 +00001359}
1360
Francois Pichet783dd6e2010-11-21 06:08:52 +00001361void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1362 VisitValueDecl(FD);
1363
David L. Jonesbe1557a2016-12-21 00:17:49 +00001364 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001365 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001366 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001367
1368 for (unsigned I = 0; I != FD->ChainingSize; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001369 FD->Chaining[I] = readDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001370
1371 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001372}
1373
Larisse Voufo39a1e502013-08-06 01:03:05 +00001374ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001375 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001376 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001377
David L. Jonesbe1557a2016-12-21 00:17:49 +00001378 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1379 VD->VarDeclBits.TSCSpec = Record.readInt();
1380 VD->VarDeclBits.InitStyle = Record.readInt();
Erik Pilkington1e368822019-01-04 18:33:06 +00001381 VD->VarDeclBits.ARCPseudoStrong = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001382 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001383 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1384 Record.readInt();
1385 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001386 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001387 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001388 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001389 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1390 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1391 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1392 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1393 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001394 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
Akira Hatanaka8e57b072018-10-01 21:51:28 +00001395 VD->NonParmVarDeclBits.EscapingByref = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001396 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001397 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001398 VD->setCachedLinkage(VarLinkage);
1399
1400 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001401 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001402 VD->getLexicalDeclContext()->isFunctionOrMethod())
1403 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001404
David L. Jonesbe1557a2016-12-21 00:17:49 +00001405 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001406 VD->setInit(Record.readExpr());
Richard Smith2b4fa532019-09-29 05:08:46 +00001407 if (Val > 1) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001408 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1409 Eval->CheckedICE = true;
Richard Smith2b4fa532019-09-29 05:08:46 +00001410 Eval->IsICE = (Val & 1) != 0;
1411 Eval->HasConstantDestruction = (Val & 4) != 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001412 }
1413 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001414
Akira Hatanaka9978da32018-08-10 15:09:24 +00001415 if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1416 Expr *CopyExpr = Record.readExpr();
1417 if (CopyExpr)
1418 Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1419 }
1420
Hans Wennborg7ea9a6e2020-02-27 14:33:43 +01001421 if (VD->getStorageDuration() == SD_Static && Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +00001422 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1423
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001424 enum VarKind {
1425 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1426 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001427 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001428 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001429 // Only true variables (not parameters or implicit parameters) can be
1430 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001431 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1432 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001433 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001434 break;
1435 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001436 // Merged when we merge the template.
John McCall3ce3d232019-12-13 03:37:23 -05001437 VD->setDescribedVarTemplate(readDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001438 break;
1439 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
John McCall3ce3d232019-12-13 03:37:23 -05001440 auto *Tmpl = readDeclAs<VarDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001441 auto TSK = (TemplateSpecializationKind)Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -05001442 SourceLocation POI = readSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001443 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001444 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001445 break;
1446 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001447 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001448
1449 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001450}
1451
Sebastian Redlb3298c32010-08-18 23:56:48 +00001452void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001453 VisitVarDecl(PD);
1454}
1455
Sebastian Redlb3298c32010-08-18 23:56:48 +00001456void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001457 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001458 unsigned isObjCMethodParam = Record.readInt();
1459 unsigned scopeDepth = Record.readInt();
1460 unsigned scopeIndex = Record.readInt();
1461 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001462 if (isObjCMethodParam) {
1463 assert(scopeDepth == 0);
1464 PD->setObjCMethodScopeInfo(scopeIndex);
1465 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1466 } else {
1467 PD->setScopeInfo(scopeDepth, scopeIndex);
1468 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001469 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1470 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1471 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001472 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001473
1474 // FIXME: If this is a redeclaration of a function from another module, handle
1475 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001476}
1477
Richard Smith7b76d812016-08-12 02:21:25 +00001478void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1479 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001480 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith0353e5a2019-05-25 01:04:17 +00001481 for (unsigned I = 0; I != DD->NumBindings; ++I) {
John McCall3ce3d232019-12-13 03:37:23 -05001482 BDs[I] = readDeclAs<BindingDecl>();
Richard Smith0353e5a2019-05-25 01:04:17 +00001483 BDs[I]->setDecomposedDecl(DD);
1484 }
Richard Smith7b76d812016-08-12 02:21:25 +00001485}
1486
1487void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1488 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001489 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001490}
1491
Sebastian Redlb3298c32010-08-18 23:56:48 +00001492void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001493 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001494 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
John McCall3ce3d232019-12-13 03:37:23 -05001495 AD->setRParenLoc(readSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001496}
1497
Sebastian Redlb3298c32010-08-18 23:56:48 +00001498void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001499 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001500 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
John McCall3ce3d232019-12-13 03:37:23 -05001501 BD->setSignatureAsWritten(readTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001502 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001503 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001504 Params.reserve(NumParams);
1505 for (unsigned I = 0; I != NumParams; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001506 Params.push_back(readDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001507 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001508
David L. Jonesbe1557a2016-12-21 00:17:49 +00001509 BD->setIsVariadic(Record.readInt());
1510 BD->setBlockMissingReturnType(Record.readInt());
1511 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001512 BD->setDoesNotEscape(Record.readInt());
Akira Hatanakac5792aa2019-02-27 18:17:16 +00001513 BD->setCanAvoidCopyToHeap(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001514
David L. Jonesbe1557a2016-12-21 00:17:49 +00001515 bool capturesCXXThis = Record.readInt();
1516 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001517 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001518 captures.reserve(numCaptures);
1519 for (unsigned i = 0; i != numCaptures; ++i) {
John McCall3ce3d232019-12-13 03:37:23 -05001520 auto *decl = readDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001521 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001522 bool byRef = (flags & 1);
1523 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001524 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001525
1526 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1527 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001528 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001529}
1530
Ben Langmuirce914fc2013-05-03 19:20:19 +00001531void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1532 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001533 unsigned ContextParamPos = Record.readInt();
1534 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001535 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001536 for (unsigned I = 0; I < CD->NumParams; ++I) {
1537 if (I != ContextParamPos)
John McCall3ce3d232019-12-13 03:37:23 -05001538 CD->setParam(I, readDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001539 else
John McCall3ce3d232019-12-13 03:37:23 -05001540 CD->setContextParam(I, readDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001541 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001542}
1543
Sebastian Redlb3298c32010-08-18 23:56:48 +00001544void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001545 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001546 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
John McCall3ce3d232019-12-13 03:37:23 -05001547 D->setExternLoc(readSourceLocation());
1548 D->setRBraceLoc(readSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001549}
1550
Richard Smith8df390f2016-09-08 23:14:54 +00001551void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1552 VisitDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001553 D->RBraceLoc = readSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001554}
1555
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001556void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1557 VisitNamedDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001558 D->setLocStart(readSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001559}
1560
Sebastian Redlb3298c32010-08-18 23:56:48 +00001561void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001562 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001563 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001564 D->setInline(Record.readInt());
John McCall3ce3d232019-12-13 03:37:23 -05001565 D->LocStart = readSourceLocation();
1566 D->RBraceLoc = readSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001567
Richard Smith15e32fd2015-03-17 02:23:11 +00001568 // Defer loading the anonymous namespace until we've finished merging
1569 // this namespace; loading it might load a later declaration of the
1570 // same namespace, and we have an invariant that older declarations
1571 // get merged before newer ones try to merge.
1572 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001573 if (Redecl.getFirstID() == ThisDeclID) {
John McCall3ce3d232019-12-13 03:37:23 -05001574 AnonNamespace = readDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001575 } else {
1576 // Link this namespace back to the first declaration, which has already
1577 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001578 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001579 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001580
1581 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001582
1583 if (AnonNamespace) {
1584 // Each module has its own anonymous namespace, which is disjoint from
1585 // any other module's anonymous namespaces, so don't attach the anonymous
1586 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001587 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001588 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001589 D->setAnonymousNamespace(Anon);
1590 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001591}
1592
Sebastian Redlb3298c32010-08-18 23:56:48 +00001593void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001594 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001595 VisitNamedDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001596 D->NamespaceLoc = readSourceLocation();
1597 D->IdentLoc = readSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001598 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -05001599 D->Namespace = readDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001600 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001601}
1602
Sebastian Redlb3298c32010-08-18 23:56:48 +00001603void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001604 VisitNamedDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001605 D->setUsingLoc(readSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001606 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -05001607 D->DNLoc = Record.readDeclarationNameLoc(D->getDeclName());
1608 D->FirstUsingShadow.setPointer(readDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001609 D->setTypename(Record.readInt());
John McCall3ce3d232019-12-13 03:37:23 -05001610 if (auto *Pattern = readDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001611 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001612 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001613}
1614
Richard Smith151c4562016-12-20 21:35:28 +00001615void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1616 VisitNamedDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001617 D->InstantiatedFrom = readDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001618 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001619 for (unsigned I = 0; I != D->NumExpansions; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001620 Expansions[I] = readDeclAs<NamedDecl>();
Richard Smith151c4562016-12-20 21:35:28 +00001621 mergeMergeable(D);
1622}
1623
Sebastian Redlb3298c32010-08-18 23:56:48 +00001624void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001625 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001626 VisitNamedDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001627 D->Underlying = readDeclAs<NamedDecl>();
Richard Smitha263c342018-01-06 01:07:05 +00001628 D->IdentifierNamespace = Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -05001629 D->UsingOrNextShadow = readDeclAs<NamedDecl>();
1630 auto *Pattern = readDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001631 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001632 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001633 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001634}
1635
Richard Smith5179eb72016-06-28 19:03:57 +00001636void ASTDeclReader::VisitConstructorUsingShadowDecl(
1637 ConstructorUsingShadowDecl *D) {
1638 VisitUsingShadowDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001639 D->NominatedBaseClassShadowDecl = readDeclAs<ConstructorUsingShadowDecl>();
1640 D->ConstructedBaseClassShadowDecl = readDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001641 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001642}
1643
Sebastian Redlb3298c32010-08-18 23:56:48 +00001644void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001645 VisitNamedDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001646 D->UsingLoc = readSourceLocation();
1647 D->NamespaceLoc = readSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001648 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -05001649 D->NominatedNamespace = readDeclAs<NamedDecl>();
1650 D->CommonAncestor = readDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001651}
1652
Sebastian Redlb3298c32010-08-18 23:56:48 +00001653void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001654 VisitValueDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001655 D->setUsingLoc(readSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001656 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -05001657 D->DNLoc = Record.readDeclarationNameLoc(D->getDeclName());
1658 D->EllipsisLoc = readSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001659 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001660}
1661
Sebastian Redlb3298c32010-08-18 23:56:48 +00001662void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001663 UnresolvedUsingTypenameDecl *D) {
1664 VisitTypeDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001665 D->TypenameLocation = readSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001666 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
John McCall3ce3d232019-12-13 03:37:23 -05001667 D->EllipsisLoc = readSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001668 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001669}
1670
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001671void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001672 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Richard Smith91aeacc2019-10-11 00:29:04 +00001673 #define FIELD(Name, Width, Merge) \
1674 Data.Name = Record.readInt();
1675 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
1676
Douglas Gregor99ae8062012-02-14 17:54:36 +00001677 // Note: the caller has deserialized the IsLambda bit already.
Richard Trieub6adf542017-02-18 02:09:28 +00001678 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001679 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001680
Hans Wennborg7ea9a6e2020-02-27 14:33:43 +01001681 if (Record.readInt())
Richard Smithcd4a7a42017-09-07 00:55:55 +00001682 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001683
David L. Jonesbe1557a2016-12-21 00:17:49 +00001684 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001685 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001686 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001687 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001688 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001689 Data.VBases = ReadGlobalOffset();
1690
David L. Jonesb6a8f022016-12-21 04:34:52 +00001691 Record.readUnresolvedSet(Data.Conversions);
Richard Smith91aeacc2019-10-11 00:29:04 +00001692 Data.ComputedVisibleConversions = Record.readInt();
1693 if (Data.ComputedVisibleConversions)
1694 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001695 assert(Data.Definition && "Data.Definition should be already set!");
John McCall3ce3d232019-12-13 03:37:23 -05001696 Data.FirstFriend = readDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001697
Douglas Gregor99ae8062012-02-14 17:54:36 +00001698 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001699 using Capture = LambdaCapture;
1700
1701 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001702 Lambda.Dependent = Record.readInt();
1703 Lambda.IsGenericLambda = Record.readInt();
1704 Lambda.CaptureDefault = Record.readInt();
1705 Lambda.NumCaptures = Record.readInt();
1706 Lambda.NumExplicitCaptures = Record.readInt();
Michael Liao243ebfb2019-10-19 00:15:19 +00001707 Lambda.HasKnownInternalLinkage = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001708 Lambda.ManglingNumber = Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -05001709 Lambda.ContextDecl = readDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001710 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1711 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001712 Capture *ToCapture = Lambda.Captures;
John McCall3ce3d232019-12-13 03:37:23 -05001713 Lambda.MethodTyInfo = readTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001714 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
John McCall3ce3d232019-12-13 03:37:23 -05001715 SourceLocation Loc = readSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001716 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001717 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001718 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001719 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001720 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001721 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001722 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001723 break;
1724 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001725 case LCK_ByRef:
John McCall3ce3d232019-12-13 03:37:23 -05001726 auto *Var = readDeclAs<VarDecl>();
1727 SourceLocation EllipsisLoc = readSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001728 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1729 break;
1730 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001731 }
1732 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001733}
1734
Richard Smithcd45dbc2014-04-19 03:48:30 +00001735void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001736 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001737 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001738 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001739 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001740
Richard Smith02793752015-03-27 21:16:39 +00001741 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001742 // Track that we merged the definitions.
1743 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1744 DD.Definition));
1745 Reader.PendingDefinitions.erase(MergeDD.Definition);
Erich Keanef92f31c2018-08-01 20:48:16 +00001746 MergeDD.Definition->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +00001747 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001748 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1749 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001750 }
1751
Richard Smith2a9e5c52015-02-03 03:32:14 +00001752 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1753 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1754 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001755 // We faked up this definition data because we found a class for which we'd
1756 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001757 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001758 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001759
1760 // Don't change which declaration is the definition; that is required
1761 // to be invariant once we select it.
1762 auto *Def = DD.Definition;
1763 DD = std::move(MergeDD);
1764 DD.Definition = Def;
1765 return;
1766 }
1767
Richard Smithcd45dbc2014-04-19 03:48:30 +00001768 bool DetectedOdrViolation = false;
Richard Smith91aeacc2019-10-11 00:29:04 +00001769
1770 #define FIELD(Name, Width, Merge) Merge(Name)
1771 #define MERGE_OR(Field) DD.Field |= MergeDD.Field;
1772 #define NO_MERGE(Field) \
Richard Smithcd45dbc2014-04-19 03:48:30 +00001773 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
Richard Smith91aeacc2019-10-11 00:29:04 +00001774 MERGE_OR(Field)
1775 #include "clang/AST/CXXRecordDeclDefinitionBits.def"
1776 NO_MERGE(IsLambda)
1777 #undef NO_MERGE
1778 #undef MERGE_OR
Richard Smithcd45dbc2014-04-19 03:48:30 +00001779
1780 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1781 DetectedOdrViolation = true;
1782 // FIXME: Issue a diagnostic if the base classes don't match when we come
1783 // to lazily load them.
1784
1785 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1786 // match when we come to lazily load them.
1787 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1788 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1789 DD.ComputedVisibleConversions = true;
1790 }
1791
1792 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1793 // lazily load it.
1794
1795 if (DD.IsLambda) {
1796 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1797 // when they occur within the body of a function template specialization).
1798 }
1799
Richard Trieufd1acbb2017-04-11 21:31:00 +00001800 if (D->getODRHash() != MergeDD.ODRHash) {
1801 DetectedOdrViolation = true;
1802 }
1803
Richard Smithcd45dbc2014-04-19 03:48:30 +00001804 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001805 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1806 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001807}
1808
Richard Smith2a9e5c52015-02-03 03:32:14 +00001809void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001810 struct CXXRecordDecl::DefinitionData *DD;
1811 ASTContext &C = Reader.getContext();
1812
1813 // Determine whether this is a lambda closure type, so that we can
1814 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001815 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001816 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001817 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001818 LCD_None);
1819 else
1820 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1821
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001822 CXXRecordDecl *Canon = D->getCanonicalDecl();
1823 // Set decl definition data before reading it, so that during deserialization
1824 // when we read CXXRecordDecl, it already has definition data and we don't
1825 // set fake one.
1826 if (!Canon->DefinitionData)
1827 Canon->DefinitionData = DD;
1828 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001829 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001830
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001831 // We might already have a different definition for this record. This can
1832 // happen either because we're reading an update record, or because we've
1833 // already done some merging. Either way, just merge into it.
1834 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001835 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001836 return;
1837 }
1838
Richard Smith97100e32015-06-20 00:22:34 +00001839 // Mark this declaration as being a definition.
Erich Keanef92f31c2018-08-01 20:48:16 +00001840 D->setCompleteDefinition(true);
Richard Smith2a9e5c52015-02-03 03:32:14 +00001841
Richard Smith97100e32015-06-20 00:22:34 +00001842 // If this is not the first declaration or is an update record, we can have
1843 // other redeclarations already. Make a note that we need to propagate the
1844 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001845 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001846 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001847}
1848
Richard Smith1d209d02013-05-23 01:49:11 +00001849ASTDeclReader::RedeclarableResult
1850ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1851 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001852
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001853 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001854
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001855 enum CXXRecKind {
1856 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1857 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001858 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001859 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001860 // Merged when we merge the folding set entry in the primary template.
1861 if (!isa<ClassTemplateSpecializationDecl>(D))
1862 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001863 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001864 case CXXRecTemplate: {
1865 // Merged when we merge the template.
John McCall3ce3d232019-12-13 03:37:23 -05001866 auto *Template = readDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001867 D->TemplateOrInstantiation = Template;
1868 if (!Template->getTemplatedDecl()) {
1869 // We've not actually loaded the ClassTemplateDecl yet, because we're
1870 // currently being loaded as its pattern. Rely on it to set up our
1871 // TypeForDecl (see VisitClassTemplateDecl).
1872 //
1873 // Beware: we do not yet know our canonical declaration, and may still
1874 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001875 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001876 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001877 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001878 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001879 case CXXRecMemberSpecialization: {
John McCall3ce3d232019-12-13 03:37:23 -05001880 auto *RD = readDeclAs<CXXRecordDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001881 auto TSK = (TemplateSpecializationKind)Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -05001882 SourceLocation POI = readSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001883 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1884 MSI->setPointOfInstantiation(POI);
1885 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001886 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001887 break;
1888 }
1889 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001890
David L. Jonesbe1557a2016-12-21 00:17:49 +00001891 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001892 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001893 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001894 else
1895 // Propagate DefinitionData pointer from the canonical declaration.
1896 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1897
Richard Smith676c4042013-08-29 23:59:27 +00001898 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001899 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001900 if (WasDefinition) {
John McCall3ce3d232019-12-13 03:37:23 -05001901 DeclID KeyFn = readDeclID();
Erich Keanef92f31c2018-08-01 20:48:16 +00001902 if (KeyFn && D->isCompleteDefinition())
Richard Smitha9a1c682014-07-07 06:38:20 +00001903 // FIXME: This is wrong for the ARM ABI, where some other module may have
1904 // made this function no longer be a key function. We need an update
1905 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001906 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001907 }
Richard Smith1d209d02013-05-23 01:49:11 +00001908
1909 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001910}
1911
Richard Smithbc491202017-02-17 20:05:37 +00001912void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
Richard Smith76b90272019-05-09 03:59:21 +00001913 D->setExplicitSpecifier(Record.readExplicitSpec());
Richard Smithbc491202017-02-17 20:05:37 +00001914 VisitFunctionDecl(D);
Erich Keane9c665062018-08-01 21:02:40 +00001915 D->setIsCopyDeductionCandidate(Record.readInt());
Richard Smithbc491202017-02-17 20:05:37 +00001916}
1917
Sebastian Redlb3298c32010-08-18 23:56:48 +00001918void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001919 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001920
David L. Jonesbe1557a2016-12-21 00:17:49 +00001921 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001922 if (D->isCanonicalDecl()) {
1923 while (NumOverridenMethods--) {
1924 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1925 // MD may be initializing.
John McCall3ce3d232019-12-13 03:37:23 -05001926 if (auto *MD = readDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001927 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1928 }
1929 } else {
1930 // We don't care about which declarations this used to override; we get
1931 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001932 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001933 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001934}
1935
Sebastian Redlb3298c32010-08-18 23:56:48 +00001936void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001937 // We need the inherited constructor information to merge the declaration,
1938 // so we have to read it before we call VisitCXXMethodDecl.
Richard Smith76b90272019-05-09 03:59:21 +00001939 D->setExplicitSpecifier(Record.readExplicitSpec());
Richard Smith5179eb72016-06-28 19:03:57 +00001940 if (D->isInheritingConstructor()) {
John McCall3ce3d232019-12-13 03:37:23 -05001941 auto *Shadow = readDeclAs<ConstructorUsingShadowDecl>();
1942 auto *Ctor = readDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001943 *D->getTrailingObjects<InheritedConstructor>() =
1944 InheritedConstructor(Shadow, Ctor);
1945 }
1946
Chris Lattnerca025db2010-05-07 21:43:38 +00001947 VisitCXXMethodDecl(D);
1948}
1949
Sebastian Redlb3298c32010-08-18 23:56:48 +00001950void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001951 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001952
John McCall3ce3d232019-12-13 03:37:23 -05001953 if (auto *OperatorDelete = readDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00001954 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00001955 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00001956 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00001957 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00001958 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00001959 Canon->OperatorDeleteThisArg = ThisArg;
1960 }
Richard Smithf8134002015-03-10 01:41:22 +00001961 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001962}
1963
Sebastian Redlb3298c32010-08-18 23:56:48 +00001964void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Richard Smith76b90272019-05-09 03:59:21 +00001965 D->setExplicitSpecifier(Record.readExplicitSpec());
Chris Lattnerca025db2010-05-07 21:43:38 +00001966 VisitCXXMethodDecl(D);
1967}
1968
Douglas Gregorba345522011-12-02 23:23:56 +00001969void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1970 VisitDecl(D);
Reid Klecknerc915cb92020-02-27 18:13:54 -08001971 D->ImportedModule = readModule();
1972 D->setImportComplete(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001973 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001974 for (unsigned I = 0, N = Record.back(); I != N; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05001975 StoredLocs[I] = readSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001976 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001977}
1978
Sebastian Redlb3298c32010-08-18 23:56:48 +00001979void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001980 VisitDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05001981 D->setColonLoc(readSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001982}
1983
Sebastian Redlb3298c32010-08-18 23:56:48 +00001984void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001985 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001986 if (Record.readInt()) // hasFriendDecl
John McCall3ce3d232019-12-13 03:37:23 -05001987 D->Friend = readDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001988 else
John McCall3ce3d232019-12-13 03:37:23 -05001989 D->Friend = readTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001990 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001991 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001992 Record.readTemplateParameterList();
John McCall3ce3d232019-12-13 03:37:23 -05001993 D->NextFriend = readDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001994 D->UnsupportedFriend = (Record.readInt() != 0);
John McCall3ce3d232019-12-13 03:37:23 -05001995 D->FriendLoc = readSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001996}
1997
Sebastian Redlb3298c32010-08-18 23:56:48 +00001998void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001999 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002000 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002001 D->NumParams = NumParams;
2002 D->Params = new TemplateParameterList*[NumParams];
2003 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002004 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002005 if (Record.readInt()) // HasFriendDecl
John McCall3ce3d232019-12-13 03:37:23 -05002006 D->Friend = readDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002007 else
John McCall3ce3d232019-12-13 03:37:23 -05002008 D->Friend = readTypeSourceInfo();
2009 D->FriendLoc = readSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002010}
2011
Richard Smithf17fdbd2014-04-24 02:25:27 +00002012DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002013 VisitNamedDecl(D);
2014
John McCall3ce3d232019-12-13 03:37:23 -05002015 DeclID PatternID = readDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002016 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002017 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002018 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002019
Richard Smithf17fdbd2014-04-24 02:25:27 +00002020 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002021}
2022
Saar Razd7aae332019-07-10 21:25:49 +00002023void ASTDeclReader::VisitConceptDecl(ConceptDecl *D) {
2024 VisitTemplateDecl(D);
2025 D->ConstraintExpr = Record.readExpr();
2026 mergeMergeable(D);
2027}
2028
Saar Raza0f50d72020-01-18 09:11:43 +02002029void ASTDeclReader::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {
2030}
2031
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002032ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002033ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002034 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002035
Douglas Gregor68444de2012-01-14 15:13:49 +00002036 // Make sure we've allocated the Common pointer first. We do this before
2037 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2038 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2039 if (!CanonD->Common) {
2040 CanonD->Common = CanonD->newCommon(Reader.getContext());
2041 Reader.PendingDefinitions.insert(CanonD);
2042 }
2043 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002044
Douglas Gregor68444de2012-01-14 15:13:49 +00002045 // If this is the first declaration of the template, fill in the information
2046 // for the 'common' pointer.
2047 if (ThisDeclID == Redecl.getFirstID()) {
John McCall3ce3d232019-12-13 03:37:23 -05002048 if (auto *RTD = readDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002049 assert(RTD->getKind() == D->getKind() &&
2050 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002051 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002052 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002053 D->setMemberSpecialization();
2054 }
2055 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002056
Richard Smithf17fdbd2014-04-24 02:25:27 +00002057 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002058 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002059
Richard Smithf17fdbd2014-04-24 02:25:27 +00002060 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002061
Richard Smithd46d6de2013-10-13 23:50:45 +00002062 // If we merged the template with a prior declaration chain, merge the common
2063 // pointer.
2064 // FIXME: Actually merge here, don't just overwrite.
2065 D->Common = D->getCanonicalDecl()->Common;
2066
Douglas Gregor68444de2012-01-14 15:13:49 +00002067 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002068}
2069
Sebastian Redlb3298c32010-08-18 23:56:48 +00002070void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002071 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002072
Douglas Gregor68444de2012-01-14 15:13:49 +00002073 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002074 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2075 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002076 SmallVector<serialization::DeclID, 32> SpecIDs;
John McCall3ce3d232019-12-13 03:37:23 -05002077 readDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002078 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002079 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002080
2081 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2082 // We were loaded before our templated declaration was. We've not set up
2083 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2084 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002085 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002086 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002087 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002088}
2089
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002090void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2091 llvm_unreachable("BuiltinTemplates are not serialized");
2092}
2093
Larisse Voufo30616382013-08-23 22:21:36 +00002094/// TODO: Unify with ClassTemplateDecl version?
2095/// May require unifying ClassTemplateDecl and
2096/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002097void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2098 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2099
2100 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002101 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002102 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002103 SmallVector<serialization::DeclID, 32> SpecIDs;
John McCall3ce3d232019-12-13 03:37:23 -05002104 readDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002105 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002106 }
2107}
2108
Richard Smith1d209d02013-05-23 01:49:11 +00002109ASTDeclReader::RedeclarableResult
2110ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2111 ClassTemplateSpecializationDecl *D) {
2112 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002113
Douglas Gregor4163aca2011-09-09 21:34:22 +00002114 ASTContext &C = Reader.getContext();
John McCall3ce3d232019-12-13 03:37:23 -05002115 if (Decl *InstD = readDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002116 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002117 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002118 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002119 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002120 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002121 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002122 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002123 auto *PS =
2124 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002125 SpecializedPartialSpecialization();
2126 PS->PartialSpecialization
2127 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2128 PS->TemplateArgs = ArgList;
2129 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002130 }
2131 }
2132
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002133 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002134 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002135 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
John McCall3ce3d232019-12-13 03:37:23 -05002136 D->PointOfInstantiation = readSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002137 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002138
David L. Jonesbe1557a2016-12-21 00:17:49 +00002139 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002140 if (writtenAsCanonicalDecl) {
John McCall3ce3d232019-12-13 03:37:23 -05002141 auto *CanonPattern = readDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002142 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002143 // Set this as, or find, the canonical declaration for this specialization
2144 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002145 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002146 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002147 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002148 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002149 CanonSpec =
2150 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2151 }
2152 // If there was already a canonical specialization, merge into it.
2153 if (CanonSpec != D) {
2154 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2155
2156 // This declaration might be a definition. Merge with any existing
2157 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002158 if (auto *DDD = D->DefinitionData) {
2159 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002160 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002161 else
Richard Smith053f6c62014-05-16 23:01:30 +00002162 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002163 }
Richard Smith547864d2014-07-11 18:22:58 +00002164 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002165 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002166 }
2167 }
Richard Smith1d209d02013-05-23 01:49:11 +00002168
Richard Smithd55889a2013-09-09 16:55:27 +00002169 // Explicit info.
John McCall3ce3d232019-12-13 03:37:23 -05002170 if (TypeSourceInfo *TyInfo = readTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002171 auto *ExplicitInfo =
2172 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002173 ExplicitInfo->TypeAsWritten = TyInfo;
John McCall3ce3d232019-12-13 03:37:23 -05002174 ExplicitInfo->ExternLoc = readSourceLocation();
2175 ExplicitInfo->TemplateKeywordLoc = readSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002176 D->ExplicitInfo = ExplicitInfo;
2177 }
2178
Richard Smith1d209d02013-05-23 01:49:11 +00002179 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002180}
2181
Sebastian Redlb3298c32010-08-18 23:56:48 +00002182void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002183 ClassTemplatePartialSpecializationDecl *D) {
Saar Razdf061c32019-12-23 08:37:35 +02002184 // We need to read the template params first because redeclarable is going to
2185 // need them for profiling
Saar Raz0330fba2019-10-15 18:44:06 +00002186 TemplateParameterList *Params = Record.readTemplateParameterList();
2187 D->TemplateParams = Params;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002188 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002189
Saar Razdf061c32019-12-23 08:37:35 +02002190 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
2191
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002192 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002193 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002194 D->InstantiatedFromMember.setPointer(
John McCall3ce3d232019-12-13 03:37:23 -05002195 readDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002196 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002197 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002198}
2199
Sebastian Redlb7448632011-08-31 13:59:56 +00002200void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2201 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002202 VisitDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05002203 D->Specialization = readDeclAs<CXXMethodDecl>();
Richard Smithf19a8b02019-05-02 00:49:14 +00002204 if (Record.readInt())
2205 D->TemplateArgs = Record.readASTTemplateArgumentListInfo();
Francois Pichet09af8c32011-08-17 01:06:54 +00002206}
2207
Sebastian Redlb3298c32010-08-18 23:56:48 +00002208void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002209 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002210
Douglas Gregor68444de2012-01-14 15:13:49 +00002211 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002212 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002213 SmallVector<serialization::DeclID, 32> SpecIDs;
John McCall3ce3d232019-12-13 03:37:23 -05002214 readDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002215 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002216 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002217}
2218
Larisse Voufo30616382013-08-23 22:21:36 +00002219/// TODO: Unify with ClassTemplateSpecializationDecl version?
2220/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2221/// VarTemplate(Partial)SpecializationDecl with a new data
2222/// structure Template(Partial)SpecializationDecl, and
2223/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002224ASTDeclReader::RedeclarableResult
2225ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2226 VarTemplateSpecializationDecl *D) {
2227 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2228
2229 ASTContext &C = Reader.getContext();
John McCall3ce3d232019-12-13 03:37:23 -05002230 if (Decl *InstD = readDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002231 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002232 D->SpecializedTemplate = VTD;
2233 } else {
2234 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002235 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002236 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002237 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002238 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002239 new (C)
2240 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2241 PS->PartialSpecialization =
2242 cast<VarTemplatePartialSpecializationDecl>(InstD);
2243 PS->TemplateArgs = ArgList;
2244 D->SpecializedTemplate = PS;
2245 }
2246 }
2247
2248 // Explicit info.
John McCall3ce3d232019-12-13 03:37:23 -05002249 if (TypeSourceInfo *TyInfo = readTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002250 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002251 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2252 ExplicitInfo->TypeAsWritten = TyInfo;
John McCall3ce3d232019-12-13 03:37:23 -05002253 ExplicitInfo->ExternLoc = readSourceLocation();
2254 ExplicitInfo->TemplateKeywordLoc = readSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002255 D->ExplicitInfo = ExplicitInfo;
2256 }
2257
2258 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002259 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002260 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
John McCall3ce3d232019-12-13 03:37:23 -05002261 D->PointOfInstantiation = readSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002262 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002263 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002264
David L. Jonesbe1557a2016-12-21 00:17:49 +00002265 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002266 if (writtenAsCanonicalDecl) {
John McCall3ce3d232019-12-13 03:37:23 -05002267 auto *CanonPattern = readDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002268 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002269 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002270 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002271 CanonPattern->getCommonPtr()->PartialSpecializations
2272 .GetOrInsertNode(Partial);
2273 } else {
2274 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2275 }
2276 }
2277 }
2278
2279 return Redecl;
2280}
2281
Larisse Voufo30616382013-08-23 22:21:36 +00002282/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2283/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2284/// VarTemplate(Partial)SpecializationDecl with a new data
2285/// structure Template(Partial)SpecializationDecl, and
2286/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002287void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2288 VarTemplatePartialSpecializationDecl *D) {
Saar Raz0330fba2019-10-15 18:44:06 +00002289 TemplateParameterList *Params = Record.readTemplateParameterList();
2290 D->TemplateParams = Params;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002291 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002292
Saar Razdf061c32019-12-23 08:37:35 +02002293 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2294
Larisse Voufo39a1e502013-08-06 01:03:05 +00002295 // These are read/set from/to the first declaration.
2296 if (ThisDeclID == Redecl.getFirstID()) {
2297 D->InstantiatedFromMember.setPointer(
John McCall3ce3d232019-12-13 03:37:23 -05002298 readDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002299 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002300 }
2301}
2302
Sebastian Redlb3298c32010-08-18 23:56:48 +00002303void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002304 VisitTypeDecl(D);
2305
David L. Jonesbe1557a2016-12-21 00:17:49 +00002306 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002307
Saar Razb481f022020-01-22 02:03:05 +02002308 if (Record.readBool()) {
Saar Razff1e0fc2020-01-15 02:48:42 +02002309 NestedNameSpecifierLoc NNS = Record.readNestedNameSpecifierLoc();
2310 DeclarationNameInfo DN = Record.readDeclarationNameInfo();
Saar Razb481f022020-01-22 02:03:05 +02002311 ConceptDecl *NamedConcept = Record.readDeclAs<ConceptDecl>();
Saar Razff1e0fc2020-01-15 02:48:42 +02002312 const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
Saar Razb481f022020-01-22 02:03:05 +02002313 if (Record.readBool())
Saar Razff1e0fc2020-01-15 02:48:42 +02002314 ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
2315 Expr *ImmediatelyDeclaredConstraint = Record.readExpr();
2316 D->setTypeConstraint(NNS, DN, /*FoundDecl=*/nullptr, NamedConcept,
2317 ArgsAsWritten, ImmediatelyDeclaredConstraint);
2318 if ((D->ExpandedParameterPack = Record.readInt()))
2319 D->NumExpanded = Record.readInt();
2320 }
2321
David L. Jonesbe1557a2016-12-21 00:17:49 +00002322 if (Record.readInt())
John McCall3ce3d232019-12-13 03:37:23 -05002323 D->setDefaultArgument(readTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002324}
2325
Sebastian Redlb3298c32010-08-18 23:56:48 +00002326void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002327 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002328 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002329 D->setDepth(Record.readInt());
2330 D->setPosition(Record.readInt());
Saar Razb481f022020-01-22 02:03:05 +02002331 if (D->hasPlaceholderTypeConstraint())
2332 D->setPlaceholderTypeConstraint(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002333 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002334 auto TypesAndInfos =
2335 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002336 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002337 new (&TypesAndInfos[I].first) QualType(Record.readType());
John McCall3ce3d232019-12-13 03:37:23 -05002338 TypesAndInfos[I].second = readTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002339 }
2340 } else {
2341 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002342 D->ParameterPack = Record.readInt();
2343 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002344 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002345 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002346}
2347
Sebastian Redlb3298c32010-08-18 23:56:48 +00002348void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002349 VisitTemplateDecl(D);
2350 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002351 D->setDepth(Record.readInt());
2352 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002353 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002354 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002355 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2356 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002357 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002358 } else {
2359 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002360 D->ParameterPack = Record.readInt();
2361 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002362 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002363 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002364 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002365}
2366
Richard Smith3f1b5d02011-05-05 21:57:07 +00002367void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2368 VisitRedeclarableTemplateDecl(D);
2369}
2370
Sebastian Redlb3298c32010-08-18 23:56:48 +00002371void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002372 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002373 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002374 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002375 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
John McCall3ce3d232019-12-13 03:37:23 -05002376 D->RParenLoc = readSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002377}
2378
Michael Han84324352013-02-22 17:15:32 +00002379void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2380 VisitDecl(D);
2381}
2382
Tykerb0561b32019-11-17 11:41:55 +01002383void ASTDeclReader::VisitLifetimeExtendedTemporaryDecl(
2384 LifetimeExtendedTemporaryDecl *D) {
2385 VisitDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05002386 D->ExtendingDecl = readDeclAs<ValueDecl>();
Tykerb0561b32019-11-17 11:41:55 +01002387 D->ExprWithTemporary = Record.readStmt();
2388 if (Record.readInt())
2389 D->Value = new (D->getASTContext()) APValue(Record.readAPValue());
2390 D->ManglingNumber = Record.readInt();
Tyker9ec6d712019-11-30 16:42:33 +01002391 mergeMergeable(D);
Tykerb0561b32019-11-17 11:41:55 +01002392}
2393
Mike Stump11289f42009-09-09 15:08:12 +00002394std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002395ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002396 uint64_t LexicalOffset = ReadLocalOffset();
2397 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002398 return std::make_pair(LexicalOffset, VisibleOffset);
2399}
2400
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002401template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002402ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002403ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
John McCall3ce3d232019-12-13 03:37:23 -05002404 DeclID FirstDeclID = readDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002405 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002406
Richard Smith5fc18a92015-07-12 23:43:21 +00002407 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002408 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002409
Richard Smithd61d4ac2015-08-22 20:13:39 +00002410 uint64_t RedeclOffset = 0;
2411
Douglas Gregor358cd442012-01-15 16:58:34 +00002412 // 0 indicates that this declaration was the only declaration of its entity,
2413 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002414 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002415 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002416 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002417 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002418 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002419 // This declaration was the first local declaration, but may have imported
2420 // other declarations.
2421 IsKeyDecl = N == 1;
2422 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002423
Richard Smithfe620d22015-03-05 23:24:12 +00002424 // We have some declarations that must be before us in our redeclaration
2425 // chain. Read them now, and remember that we ought to merge with one of
2426 // them.
2427 // FIXME: Provide a known merge target to the second and subsequent such
2428 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002429 for (unsigned I = 0; I != N - 1; ++I)
John McCall3ce3d232019-12-13 03:37:23 -05002430 MergeWith = readDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002431
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002432 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002433 } else {
2434 // This declaration was not the first local declaration. Read the first
2435 // local declaration now, to trigger the import of other redeclarations.
John McCall3ce3d232019-12-13 03:37:23 -05002436 (void)readDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002437 }
2438
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002439 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002440 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002441 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2442 // We temporarily set the first (canonical) declaration as the previous one
2443 // which is the one that matters and mark the real previous DeclID to be
2444 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002445 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002446 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002447 }
Richard Smithd8a83712015-08-22 01:47:18 +00002448
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002449 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002450
2451 // Note that we need to load local redeclarations of this decl and build a
2452 // decl chain for them. This must happen *after* we perform the preloading
2453 // above; this ensures that the redeclaration chain is built in the correct
2454 // order.
2455 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002456 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002457
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002458 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002459}
2460
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002461/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002462/// of the same entity.
2463template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002464void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002465 RedeclarableResult &Redecl,
2466 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002467 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002468 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002469 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002470
Richard Smith202850a2015-03-10 02:57:50 +00002471 // If we're not the canonical declaration, we don't need to merge.
2472 if (!DBase->isFirstDecl())
2473 return;
2474
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002475 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002476
Richard Smith5a4737c2015-02-06 02:42:59 +00002477 if (auto *Existing = Redecl.getKnownMergeTarget())
2478 // We already know of an existing declaration we should merge with.
2479 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2480 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002481 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002482 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2483}
2484
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002485/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002486/// We use this to put code in a template that will only be valid for certain
2487/// instantiations.
2488template<typename T> static T assert_cast(T t) { return t; }
2489template<typename T> static T assert_cast(...) {
2490 llvm_unreachable("bad assert_cast");
2491}
2492
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002493/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002494/// declarations.
2495void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2496 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002497 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002498 auto *DPattern = D->getTemplatedDecl();
2499 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002500 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2501 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002502 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002503
Richard Smith547864d2014-07-11 18:22:58 +00002504 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2505 // Merge with any existing definition.
2506 // FIXME: This is duplicated in several places. Refactor.
2507 auto *ExistingClass =
2508 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002509 if (auto *DDD = DClass->DefinitionData) {
2510 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002511 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002512 } else {
2513 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002514 // We may have skipped this before because we thought that DClass
2515 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002516 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002517 }
2518 }
2519 DClass->DefinitionData = ExistingClass->DefinitionData;
2520
Richard Smithf17fdbd2014-04-24 02:25:27 +00002521 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2522 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002523 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002524 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2525 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2526 Result);
2527 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2528 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002529 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2530 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2531 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002532 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002533}
2534
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002535/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002536/// of the same entity.
2537template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002538void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2539 RedeclarableResult &Redecl,
2540 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002541 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002542 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002543 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002544 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002545 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2546 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002547
Richard Smithd55889a2013-09-09 16:55:27 +00002548 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002549 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002550 // appropriate canonical declaration.
2551 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002552 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002553 ExistingCanon->Used |= D->Used;
2554 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002555
2556 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002557 // We cannot have loaded any redeclarations of this declaration yet, so
2558 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002559 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002560 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002561 assert_cast<NamespaceDecl*>(ExistingCanon));
2562
2563 // When we merge a template, merge its pattern.
2564 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2565 mergeTemplatePattern(
2566 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002567 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002568
Richard Smith5fc18a92015-07-12 23:43:21 +00002569 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002570 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002571 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002572 }
2573}
2574
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002575/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2576/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2577/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2578/// that some types are mergeable during deserialization, otherwise name
2579/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002580static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002581 if (!ND)
2582 return false;
2583 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002584 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002585 return true;
2586 return false;
2587}
2588
Tyker9ec6d712019-11-30 16:42:33 +01002589/// Attempts to merge LifetimeExtendedTemporaryDecl with
2590/// identical class definitions from two different modules.
2591void ASTDeclReader::mergeMergeable(LifetimeExtendedTemporaryDecl *D) {
2592 // If modules are not available, there is no reason to perform this merge.
2593 if (!Reader.getContext().getLangOpts().Modules)
2594 return;
2595
2596 LifetimeExtendedTemporaryDecl *LETDecl = D;
2597
2598 LifetimeExtendedTemporaryDecl *&LookupResult =
2599 Reader.LETemporaryForMerging[std::make_pair(
2600 LETDecl->getExtendingDecl(), LETDecl->getManglingNumber())];
2601 if (LookupResult)
2602 Reader.getContext().setPrimaryMergedDecl(LETDecl,
2603 LookupResult->getCanonicalDecl());
2604 else
2605 LookupResult = LETDecl;
2606}
2607
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002608/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002609/// of the same entity, for the case where the entity is not actually
2610/// redeclarable. This happens, for instance, when merging the fields of
2611/// identical class definitions from two different modules.
2612template<typename T>
2613void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2614 // If modules are not available, there is no reason to perform this merge.
2615 if (!Reader.getContext().getLangOpts().Modules)
2616 return;
2617
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002618 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2619 // Note that C identically-named things in different translation units are
2620 // not redeclarations, but may still have compatible types, where ODR-like
2621 // semantics may apply.
2622 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2623 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002624 return;
2625
Richard Smith0b87e072013-10-07 08:02:11 +00002626 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2627 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002628 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2629 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002630}
2631
Alexey Bataeva769e072013-03-22 06:34:35 +00002632void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2633 VisitDecl(D);
2634 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002635 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002636 Vars.reserve(NumVars);
2637 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002638 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002639 }
2640 D->setVars(Vars);
2641}
2642
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002643void ASTDeclReader::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
2644 VisitDecl(D);
2645 unsigned NumVars = D->varlist_size();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002646 unsigned NumClauses = D->clauselist_size();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002647 SmallVector<Expr *, 16> Vars;
2648 Vars.reserve(NumVars);
2649 for (unsigned i = 0; i != NumVars; ++i) {
2650 Vars.push_back(Record.readExpr());
2651 }
2652 D->setVars(Vars);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002653 SmallVector<OMPClause *, 8> Clauses;
2654 Clauses.reserve(NumClauses);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002655 for (unsigned I = 0; I != NumClauses; ++I)
John McCallc2f18312019-12-14 03:01:28 -05002656 Clauses.push_back(Record.readOMPClause());
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002657 D->setClauses(Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002658}
2659
Kelvin Li1408f912018-09-26 04:28:39 +00002660void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
2661 VisitDecl(D);
2662 unsigned NumClauses = D->clauselist_size();
2663 SmallVector<OMPClause *, 8> Clauses;
2664 Clauses.reserve(NumClauses);
Kelvin Li1408f912018-09-26 04:28:39 +00002665 for (unsigned I = 0; I != NumClauses; ++I)
John McCallc2f18312019-12-14 03:01:28 -05002666 Clauses.push_back(Record.readOMPClause());
Kelvin Li1408f912018-09-26 04:28:39 +00002667 D->setClauses(Clauses);
2668}
2669
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002670void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002671 VisitValueDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05002672 D->setLocation(readSourceLocation());
Alexey Bataeve6aa4692018-09-13 16:54:05 +00002673 Expr *In = Record.readExpr();
2674 Expr *Out = Record.readExpr();
2675 D->setCombinerData(In, Out);
2676 Expr *Combiner = Record.readExpr();
2677 D->setCombiner(Combiner);
2678 Expr *Orig = Record.readExpr();
2679 Expr *Priv = Record.readExpr();
2680 D->setInitializerData(Orig, Priv);
2681 Expr *Init = Record.readExpr();
2682 auto IK = static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt());
2683 D->setInitializer(Init, IK);
John McCall3ce3d232019-12-13 03:37:23 -05002684 D->PrevDeclInScope = readDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002685}
2686
Michael Kruse251e1482019-02-01 20:25:04 +00002687void ASTDeclReader::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
2688 VisitValueDecl(D);
John McCall3ce3d232019-12-13 03:37:23 -05002689 D->setLocation(readSourceLocation());
Michael Kruse251e1482019-02-01 20:25:04 +00002690 Expr *MapperVarRefE = Record.readExpr();
2691 D->setMapperVarRef(MapperVarRefE);
2692 D->VarName = Record.readDeclarationName();
John McCall3ce3d232019-12-13 03:37:23 -05002693 D->PrevDeclInScope = readDeclID();
Michael Kruse251e1482019-02-01 20:25:04 +00002694 unsigned NumClauses = D->clauselist_size();
2695 SmallVector<OMPClause *, 8> Clauses;
2696 Clauses.reserve(NumClauses);
Michael Kruse251e1482019-02-01 20:25:04 +00002697 for (unsigned I = 0; I != NumClauses; ++I)
John McCallc2f18312019-12-14 03:01:28 -05002698 Clauses.push_back(Record.readOMPClause());
Michael Kruse251e1482019-02-01 20:25:04 +00002699 D->setClauses(Clauses);
2700}
2701
Alexey Bataev4244be22016-02-11 05:35:55 +00002702void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002703 VisitVarDecl(D);
2704}
2705
Chris Lattner487412d2009-04-27 05:27:42 +00002706//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002707// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002708//===----------------------------------------------------------------------===//
2709
Richard Smithe43e2b32018-08-20 21:47:29 +00002710namespace {
2711class AttrReader {
John McCall3ce3d232019-12-13 03:37:23 -05002712 ASTRecordReader &Reader;
Richard Smithe43e2b32018-08-20 21:47:29 +00002713
2714public:
John McCall3ce3d232019-12-13 03:37:23 -05002715 AttrReader(ASTRecordReader &Reader) : Reader(Reader) {}
Richard Smithe43e2b32018-08-20 21:47:29 +00002716
John McCall3ce3d232019-12-13 03:37:23 -05002717 uint64_t readInt() {
2718 return Reader.readInt();
2719 }
Richard Smithe43e2b32018-08-20 21:47:29 +00002720
2721 SourceRange readSourceRange() {
John McCall3ce3d232019-12-13 03:37:23 -05002722 return Reader.readSourceRange();
Richard Smithe43e2b32018-08-20 21:47:29 +00002723 }
2724
Erich Keane6a24e802019-09-13 17:39:31 +00002725 SourceLocation readSourceLocation() {
John McCall3ce3d232019-12-13 03:37:23 -05002726 return Reader.readSourceLocation();
Erich Keane6a24e802019-09-13 17:39:31 +00002727 }
2728
John McCall3ce3d232019-12-13 03:37:23 -05002729 Expr *readExpr() { return Reader.readExpr(); }
Richard Smithe43e2b32018-08-20 21:47:29 +00002730
2731 std::string readString() {
John McCall3ce3d232019-12-13 03:37:23 -05002732 return Reader.readString();
Richard Smithe43e2b32018-08-20 21:47:29 +00002733 }
2734
John McCall3ce3d232019-12-13 03:37:23 -05002735 TypeSourceInfo *readTypeSourceInfo() {
2736 return Reader.readTypeSourceInfo();
Richard Smithe43e2b32018-08-20 21:47:29 +00002737 }
2738
John McCall3ce3d232019-12-13 03:37:23 -05002739 IdentifierInfo *readIdentifier() {
2740 return Reader.readIdentifier();
Richard Smithe43e2b32018-08-20 21:47:29 +00002741 }
2742
2743 VersionTuple readVersionTuple() {
John McCall3ce3d232019-12-13 03:37:23 -05002744 return Reader.readVersionTuple();
Richard Smithe43e2b32018-08-20 21:47:29 +00002745 }
2746
Johannes Doerfert55eca282020-03-13 23:42:05 -05002747 OMPTraitInfo *readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
Johannes Doerfert1228d422019-12-19 20:42:12 -06002748
Richard Smithe43e2b32018-08-20 21:47:29 +00002749 template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
John McCall3ce3d232019-12-13 03:37:23 -05002750 return Reader.GetLocalDeclAs<T>(LocalID);
Richard Smithe43e2b32018-08-20 21:47:29 +00002751 }
2752};
2753}
2754
John McCall3ce3d232019-12-13 03:37:23 -05002755Attr *ASTRecordReader::readAttr() {
2756 AttrReader Record(*this);
Richard Smithe43e2b32018-08-20 21:47:29 +00002757 auto V = Record.readInt();
2758 if (!V)
2759 return nullptr;
2760
2761 Attr *New = nullptr;
2762 // Kind is stored as a 1-based integer because 0 is used to indicate a null
2763 // Attr pointer.
2764 auto Kind = static_cast<attr::Kind>(V - 1);
Richard Smithe43e2b32018-08-20 21:47:29 +00002765 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002766
John McCall3ce3d232019-12-13 03:37:23 -05002767 IdentifierInfo *AttrName = Record.readIdentifier();
2768 IdentifierInfo *ScopeName = Record.readIdentifier();
Erich Keane6a24e802019-09-13 17:39:31 +00002769 SourceRange AttrRange = Record.readSourceRange();
2770 SourceLocation ScopeLoc = Record.readSourceLocation();
2771 unsigned ParsedKind = Record.readInt();
2772 unsigned Syntax = Record.readInt();
2773 unsigned SpellingIndex = Record.readInt();
2774
2775 AttributeCommonInfo Info(AttrName, ScopeName, AttrRange, ScopeLoc,
2776 AttributeCommonInfo::Kind(ParsedKind),
2777 AttributeCommonInfo::Syntax(Syntax), SpellingIndex);
2778
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.
John McCall3ce3d232019-12-13 03:37:23 -05002786void ASTRecordReader::readAttributes(AttrVec &Attrs) {
2787 for (unsigned I = 0, E = readInt(); I != E; ++I)
2788 Attrs.push_back(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 Bataev2d4f80f2020-02-11 15:15:21 -05002833 isa<OMPDeclareMapperDecl>(D) || isa<OMPAllocateDecl>(D) ||
2834 isa<OMPRequiresDecl>(D))
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002835 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002836 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002837 return Var->isFileVarDecl() &&
Alexey Bataevd01b7492018-08-15 19:45:12 +00002838 (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
2839 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002840 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002841 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002842
2843 if (auto *ES = D->getASTContext().getExternalSource())
2844 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2845 return true;
2846
Douglas Gregor87d81242011-09-10 00:22:34 +00002847 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002848}
2849
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002850/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002851ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002852ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002853 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2854 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002855 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002856 const DeclOffset &DOffs =
2857 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002858 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002859 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002860}
2861
Douglas Gregord32f0352011-07-22 06:10:01 +00002862ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002863 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002864
2865 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2866 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2867}
2868
Douglas Gregorde3ef502011-11-30 23:21:26 +00002869uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002870 return LocalOffset + M.GlobalBitOffset;
2871}
2872
Saar Raz7fb562c2020-03-10 22:05:36 +02002873static bool isSameTemplateParameterList(const ASTContext &C,
2874 const TemplateParameterList *X,
Richard Smithbf78e642013-06-24 22:51:00 +00002875 const TemplateParameterList *Y);
2876
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002877/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002878/// that they may be used in declarations of the same template.
2879static bool isSameTemplateParameter(const NamedDecl *X,
2880 const NamedDecl *Y) {
2881 if (X->getKind() != Y->getKind())
2882 return false;
2883
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002884 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2885 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Saar Raz7fb562c2020-03-10 22:05:36 +02002886 if (TX->isParameterPack() != TY->isParameterPack())
2887 return false;
2888 if (TX->hasTypeConstraint() != TY->hasTypeConstraint())
2889 return false;
2890 if (TX->hasTypeConstraint()) {
2891 const TypeConstraint *TXTC = TX->getTypeConstraint();
2892 const TypeConstraint *TYTC = TY->getTypeConstraint();
2893 if (TXTC->getNamedConcept() != TYTC->getNamedConcept())
2894 return false;
2895 if (TXTC->hasExplicitTemplateArgs() != TYTC->hasExplicitTemplateArgs())
2896 return false;
2897 if (TXTC->hasExplicitTemplateArgs()) {
2898 const auto *TXTCArgs = TXTC->getTemplateArgsAsWritten();
2899 const auto *TYTCArgs = TYTC->getTemplateArgsAsWritten();
2900 if (TXTCArgs->NumTemplateArgs != TYTCArgs->NumTemplateArgs)
2901 return false;
2902 llvm::FoldingSetNodeID XID, YID;
2903 for (const auto &ArgLoc : TXTCArgs->arguments())
2904 ArgLoc.getArgument().Profile(XID, X->getASTContext());
2905 for (const auto &ArgLoc : TYTCArgs->arguments())
2906 ArgLoc.getArgument().Profile(YID, Y->getASTContext());
2907 if (XID != YID)
2908 return false;
2909 }
2910 }
2911 return true;
Richard Smithbf78e642013-06-24 22:51:00 +00002912 }
2913
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002914 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2915 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002916 return TX->isParameterPack() == TY->isParameterPack() &&
2917 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2918 }
2919
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002920 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2921 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002922 return TX->isParameterPack() == TY->isParameterPack() &&
Saar Raz7fb562c2020-03-10 22:05:36 +02002923 isSameTemplateParameterList(TX->getASTContext(),
2924 TX->getTemplateParameters(),
Richard Smithbf78e642013-06-24 22:51:00 +00002925 TY->getTemplateParameters());
2926}
2927
Richard Smith32952e12014-10-14 02:00:47 +00002928static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2929 if (auto *NS = X->getAsNamespace())
2930 return NS;
2931 if (auto *NAS = X->getAsNamespaceAlias())
2932 return NAS->getNamespace();
2933 return nullptr;
2934}
2935
2936static bool isSameQualifier(const NestedNameSpecifier *X,
2937 const NestedNameSpecifier *Y) {
2938 if (auto *NSX = getNamespace(X)) {
2939 auto *NSY = getNamespace(Y);
2940 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2941 return false;
2942 } else if (X->getKind() != Y->getKind())
2943 return false;
2944
2945 // FIXME: For namespaces and types, we're permitted to check that the entity
2946 // is named via the same tokens. We should probably do so.
2947 switch (X->getKind()) {
2948 case NestedNameSpecifier::Identifier:
2949 if (X->getAsIdentifier() != Y->getAsIdentifier())
2950 return false;
2951 break;
2952 case NestedNameSpecifier::Namespace:
2953 case NestedNameSpecifier::NamespaceAlias:
2954 // We've already checked that we named the same namespace.
2955 break;
2956 case NestedNameSpecifier::TypeSpec:
2957 case NestedNameSpecifier::TypeSpecWithTemplate:
2958 if (X->getAsType()->getCanonicalTypeInternal() !=
2959 Y->getAsType()->getCanonicalTypeInternal())
2960 return false;
2961 break;
2962 case NestedNameSpecifier::Global:
2963 case NestedNameSpecifier::Super:
2964 return true;
2965 }
2966
2967 // Recurse into earlier portion of NNS, if any.
2968 auto *PX = X->getPrefix();
2969 auto *PY = Y->getPrefix();
2970 if (PX && PY)
2971 return isSameQualifier(PX, PY);
2972 return !PX && !PY;
2973}
2974
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002975/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002976/// that they may be used in declarations of the same template.
Saar Raz7fb562c2020-03-10 22:05:36 +02002977static bool isSameTemplateParameterList(const ASTContext &C,
2978 const TemplateParameterList *X,
Richard Smithbf78e642013-06-24 22:51:00 +00002979 const TemplateParameterList *Y) {
2980 if (X->size() != Y->size())
2981 return false;
2982
2983 for (unsigned I = 0, N = X->size(); I != N; ++I)
2984 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2985 return false;
2986
Saar Raz7fb562c2020-03-10 22:05:36 +02002987 const Expr *XRC = X->getRequiresClause();
2988 const Expr *YRC = Y->getRequiresClause();
2989 if (!XRC != !YRC)
2990 return false;
2991 if (XRC) {
2992 llvm::FoldingSetNodeID XRCID, YRCID;
2993 XRC->Profile(XRCID, C, /*Canonical=*/true);
2994 YRC->Profile(YRCID, C, /*Canonical=*/true);
2995 if (XRCID != YRCID)
2996 return false;
2997 }
2998
Richard Smithbf78e642013-06-24 22:51:00 +00002999 return true;
3000}
3001
George Burgess IV95845082017-02-15 22:43:27 +00003002/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00003003/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00003004static bool hasSameOverloadableAttrs(const FunctionDecl *A,
3005 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00003006 // Note that pass_object_size attributes are represented in the function's
3007 // ExtParameterInfo, so we don't need to check them here.
3008
George Burgess IV95845082017-02-15 22:43:27 +00003009 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00003010 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
3011 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
Michael Kruse157a3552018-12-10 15:16:37 +00003012
3013 for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) {
3014 Optional<EnableIfAttr *> Cand1A = std::get<0>(Pair);
3015 Optional<EnableIfAttr *> Cand2A = std::get<1>(Pair);
3016
3017 // Return false if the number of enable_if attributes is different.
3018 if (!Cand1A || !Cand2A)
3019 return false;
3020
George Burgess IV95845082017-02-15 22:43:27 +00003021 Cand1ID.clear();
3022 Cand2ID.clear();
3023
Michael Kruse157a3552018-12-10 15:16:37 +00003024 (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true);
3025 (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true);
3026
3027 // Return false if any of the enable_if expressions of A and B are
3028 // different.
George Burgess IV95845082017-02-15 22:43:27 +00003029 if (Cand1ID != Cand2ID)
3030 return false;
3031 }
Michael Kruse157a3552018-12-10 15:16:37 +00003032 return true;
George Burgess IV95845082017-02-15 22:43:27 +00003033}
3034
Saar Raz7fb562c2020-03-10 22:05:36 +02003035/// Determine whether the two declarations refer to the same entity.pr
Douglas Gregor022857e2011-12-22 01:48:48 +00003036static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
3037 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00003038
Douglas Gregor022857e2011-12-22 01:48:48 +00003039 if (X == Y)
3040 return true;
Richard Smithf4634362014-09-03 23:11:22 +00003041
Douglas Gregor022857e2011-12-22 01:48:48 +00003042 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00003043 //
3044 // Note that we can't use DeclContext::Equals here, because the DeclContexts
3045 // could be two different declarations of the same function. (We will fix the
3046 // semantic DC to refer to the primary definition after merging.)
3047 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
3048 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00003049 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003050
3051 // Two typedefs refer to the same entity if they have the same underlying
3052 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003053 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
3054 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003055 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
3056 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00003057
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003058 // Must have the same kind.
3059 if (X->getKind() != Y->getKind())
3060 return false;
Richard Smithf4634362014-09-03 23:11:22 +00003061
Douglas Gregorda389302012-01-01 21:47:52 +00003062 // Objective-C classes and protocols with the same name always match.
3063 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00003064 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00003065
3066 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003067 // No need to handle these here: we merge them when adding them to the
3068 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00003069 return false;
3070 }
Richard Smithd55889a2013-09-09 16:55:27 +00003071
Douglas Gregor2009cee2012-01-03 22:46:00 +00003072 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003073 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
3074 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00003075 return (TagX->getTagKind() == TagY->getTagKind()) ||
3076 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
3077 TagX->getTagKind() == TTK_Interface) &&
3078 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
3079 TagY->getTagKind() == TTK_Interface));
3080 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003081
Joao Matose9a3ed42012-08-31 22:18:20 +00003082 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00003083 // FIXME: This needs to cope with merging of prototyped/non-prototyped
3084 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003085 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
3086 const auto *FuncY = cast<FunctionDecl>(Y);
3087 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
3088 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00003089 if (CtorX->getInheritedConstructor() &&
3090 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
3091 CtorY->getInheritedConstructor().getConstructor()))
3092 return false;
3093 }
Erich Keane281d20b2018-01-08 21:34:17 +00003094
3095 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
3096 return false;
3097
3098 // Multiversioned functions with different feature strings are represented
3099 // as separate declarations.
3100 if (FuncX->isMultiVersion()) {
3101 const auto *TAX = FuncX->getAttr<TargetAttr>();
3102 const auto *TAY = FuncY->getAttr<TargetAttr>();
3103 assert(TAX && TAY && "Multiversion Function without target attribute");
3104
3105 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
3106 return false;
3107 }
3108
Richard Smitha54d3242017-03-08 23:00:26 +00003109 ASTContext &C = FuncX->getASTContext();
Saar Raz7fb562c2020-03-10 22:05:36 +02003110
3111 const Expr *XRC = FuncX->getTrailingRequiresClause();
3112 const Expr *YRC = FuncY->getTrailingRequiresClause();
3113 if (!XRC != !YRC)
3114 return false;
3115 if (XRC) {
3116 llvm::FoldingSetNodeID XRCID, YRCID;
3117 XRC->Profile(XRCID, C, /*Canonical=*/true);
3118 YRC->Profile(YRCID, C, /*Canonical=*/true);
3119 if (XRCID != YRCID)
3120 return false;
3121 }
3122
Richard Smith600adef2018-07-04 02:25:38 +00003123 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
3124 // Map to the first declaration that we've already merged into this one.
3125 // The TSI of redeclarations might not match (due to calling conventions
3126 // being inherited onto the type but not the TSI), but the TSI type of
3127 // the first declaration of the function should match across modules.
3128 FD = FD->getCanonicalDecl();
3129 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
3130 : FD->getType();
3131 };
3132 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
3133 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00003134 // We can get functions with different types on the redecl chain in C++17
3135 // if they have differing exception specifications and at least one of
3136 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00003137 auto *XFPT = XT->getAs<FunctionProtoType>();
3138 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003139 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00003140 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
3141 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00003142 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00003143 return true;
3144 return false;
3145 }
Saar Raz7fb562c2020-03-10 22:05:36 +02003146
George Burgess IV95845082017-02-15 22:43:27 +00003147 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00003148 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00003149 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003150
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003151 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003152 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
3153 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00003154 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
3155 ASTContext &C = VarX->getASTContext();
3156 if (C.hasSameType(VarX->getType(), VarY->getType()))
3157 return true;
3158
3159 // We can get decls with different types on the redecl chain. Eg.
3160 // template <typename T> struct S { static T Var[]; }; // #1
3161 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
3162 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00003163 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00003164 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
3165 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
3166 if (!VarXTy || !VarYTy)
3167 return false;
3168 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
3169 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
3170 }
3171 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003172 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003173
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003174 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003175 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
3176 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003177 return NamespaceX->isInline() == NamespaceY->isInline();
3178 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003179
Richard Smith8f8f05c2013-06-24 04:45:28 +00003180 // Identical template names and kinds match if their template parameter lists
3181 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003182 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
3183 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00003184 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00003185 TemplateY->getTemplatedDecl()) &&
Saar Raz7fb562c2020-03-10 22:05:36 +02003186 isSameTemplateParameterList(TemplateX->getASTContext(),
3187 TemplateX->getTemplateParameters(),
Richard Smithbf78e642013-06-24 22:51:00 +00003188 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003189 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003190
Richard Smith0b87e072013-10-07 08:02:11 +00003191 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003192 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3193 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003194 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003195 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3196 }
3197
Richard Smith8cbd8952015-08-04 02:05:09 +00003198 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003199 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3200 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003201 return IFDX->getAnonField()->getCanonicalDecl() ==
3202 IFDY->getAnonField()->getCanonicalDecl();
3203 }
3204
Richard Smith01a73372013-10-15 22:02:41 +00003205 // Enumerators with the same name match.
3206 if (isa<EnumConstantDecl>(X))
3207 // FIXME: Also check the value is odr-equivalent.
3208 return true;
3209
Richard Smithfd8634a2013-10-23 02:17:46 +00003210 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003211 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3212 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003213 return USX->getTargetDecl() == USY->getTargetDecl();
3214 }
3215
Richard Smith32952e12014-10-14 02:00:47 +00003216 // Using declarations with the same qualifier match. (We already know that
3217 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003218 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3219 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003220 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3221 UX->hasTypename() == UY->hasTypename() &&
3222 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3223 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003224 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3225 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003226 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3227 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3228 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003229 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003230 return isSameQualifier(
3231 UX->getQualifier(),
3232 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3233
Richard Smithf4634362014-09-03 23:11:22 +00003234 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003235 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3236 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003237 return NAX->getNamespace()->Equals(NAY->getNamespace());
3238 }
3239
Douglas Gregor022857e2011-12-22 01:48:48 +00003240 return false;
3241}
3242
Richard Smithd55889a2013-09-09 16:55:27 +00003243/// Find the context in which we should search for previous declarations when
3244/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003245DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3246 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003247 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003248 return ND->getOriginalNamespace();
3249
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003250 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003251 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003252 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003253 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003254 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003255
3256 // If there's no definition yet, then DC's definition is added by an update
3257 // record, but we've not yet loaded that update record. In this case, we
3258 // commit to DC being the canonical definition now, and will fix this when
3259 // we load the update record.
3260 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003261 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003262 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003263 RD->DefinitionData = DD;
3264 RD->getCanonicalDecl()->DefinitionData = DD;
3265
3266 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003267 Reader.PendingFakeDefinitionData.insert(
3268 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003269 }
3270
3271 return DD->Definition;
3272 }
Richard Smithd55889a2013-09-09 16:55:27 +00003273
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003274 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003275 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3276 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003277
Richard Smith4eca9b92015-02-04 23:37:59 +00003278 // We can see the TU here only if we have no Sema object. In that case,
3279 // there's no TU scope to look in, so using the DC alone is sufficient.
3280 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3281 return TU;
3282
Craig Toppera13603a2014-05-22 05:54:18 +00003283 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003284}
3285
Douglas Gregor022857e2011-12-22 01:48:48 +00003286ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003287 // Record that we had a typedef name for linkage whether or not we merge
3288 // with that declaration.
3289 if (TypedefNameForLinkage) {
3290 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3291 Reader.ImportedTypedefNamesForLinkage.insert(
3292 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3293 return;
3294 }
3295
Douglas Gregor9b47f942012-01-09 17:38:47 +00003296 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003297 return;
Richard Smith95d99302013-07-13 02:00:19 +00003298
Richard Smith70d58502014-08-30 00:04:23 +00003299 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003300 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003301 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003302 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3303 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003304 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003305 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003306 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003307 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3308 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003309 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003310 // Add the declaration to its redeclaration context so later merging
3311 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003312 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003313 }
3314}
3315
Richard Smith88ebade2014-08-23 01:45:27 +00003316/// Find the declaration that should be merged into, given the declaration found
3317/// by name lookup. If we're merging an anonymous declaration within a typedef,
3318/// we need a matching typedef, and we merge with the type inside it.
3319static NamedDecl *getDeclForMerging(NamedDecl *Found,
3320 bool IsTypedefNameForLinkage) {
3321 if (!IsTypedefNameForLinkage)
3322 return Found;
3323
3324 // If we found a typedef declaration that gives a name to some other
3325 // declaration, then we want that inner declaration. Declarations from
3326 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003327 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003328 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003329
3330 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003331 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003332
Hans Wennborgdcfba332015-10-06 23:40:43 +00003333 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003334}
3335
Richard Smith600adef2018-07-04 02:25:38 +00003336/// Find the declaration to use to populate the anonymous declaration table
3337/// for the given lexical DeclContext. We only care about finding local
3338/// definitions of the context; we'll merge imported ones as we go.
3339DeclContext *
3340ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3341 // For classes, we track the definition as we merge.
3342 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3343 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3344 return DD ? DD->Definition : nullptr;
3345 }
3346
3347 // For anything else, walk its merged redeclarations looking for a definition.
3348 // Note that we can't just call getDefinition here because the redeclaration
3349 // chain isn't wired up.
3350 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3351 if (auto *FD = dyn_cast<FunctionDecl>(D))
3352 if (FD->isThisDeclarationADefinition())
3353 return FD;
3354 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3355 if (MD->isThisDeclarationADefinition())
3356 return MD;
3357 }
3358
3359 // No merged definition yet.
3360 return nullptr;
3361}
3362
Richard Smithd08aeb62014-08-28 01:33:39 +00003363NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3364 DeclContext *DC,
3365 unsigned Index) {
3366 // If the lexical context has been merged, look into the now-canonical
3367 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003368 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003369
3370 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003371 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003372 if (Index < Previous.size() && Previous[Index])
3373 return Previous[Index];
3374
3375 // If this is the first time, but we have parsed a declaration of the context,
3376 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003377 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3378 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3379 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003380 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003381 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3382 else
Richard Smith2b560572015-02-07 03:11:11 +00003383 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3384 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003385 }
3386
3387 return Index < Previous.size() ? Previous[Index] : nullptr;
3388}
3389
3390void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3391 DeclContext *DC, unsigned Index,
3392 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003393 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003394
Richard Smith600adef2018-07-04 02:25:38 +00003395 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003396 if (Index >= Previous.size())
3397 Previous.resize(Index + 1);
3398 if (!Previous[Index])
3399 Previous[Index] = D;
3400}
3401
Douglas Gregor022857e2011-12-22 01:48:48 +00003402ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003403 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3404 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003405
Richard Smithd08aeb62014-08-28 01:33:39 +00003406 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3407 // Don't bother trying to find unnamed declarations that are in
3408 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003409 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3410 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003411 Result.suppress();
3412 return Result;
3413 }
Richard Smith95d99302013-07-13 02:00:19 +00003414
Douglas Gregor022857e2011-12-22 01:48:48 +00003415 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003416 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003417 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003418 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003419 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3420 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003421 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3422 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003423 // Go on to check in other places in case an existing typedef name
3424 // was not imported.
3425 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003426
Richard Smith2b560572015-02-07 03:11:11 +00003427 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003428 // This is an anonymous declaration that we may need to merge. Look it up
3429 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003430 if (auto *Existing = getAnonymousDeclForMerging(
3431 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3432 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003433 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3434 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003435 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003436 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003437 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003438
3439 // Temporarily consider the identifier to be up-to-date. We don't want to
3440 // cause additional lookups here.
3441 class UpToDateIdentifierRAII {
3442 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003443 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003444
3445 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003446 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003447 if (II) {
3448 WasOutToDate = II->isOutOfDate();
3449 if (WasOutToDate)
3450 II->setOutOfDate(false);
3451 }
3452 }
3453
3454 ~UpToDateIdentifierRAII() {
3455 if (WasOutToDate)
3456 II->setOutOfDate(true);
3457 }
3458 } UpToDate(Name.getAsIdentifierInfo());
3459
Fangrui Song6907ce22018-07-30 19:24:48 +00003460 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003461 IEnd = IdResolver.end();
3462 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003463 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003464 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003465 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3466 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003467 }
Richard Smith8a639892015-01-24 01:07:20 +00003468 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003469 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003470 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003471 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003472 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003473 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3474 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003475 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003476 } else {
3477 // Not in a mergeable context.
3478 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003479 }
Richard Smithd55889a2013-09-09 16:55:27 +00003480
Richard Smith2b9e3e32013-10-18 06:05:18 +00003481 // If this declaration is from a merged context, make a note that we need to
3482 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003483 //
3484 // FIXME: We should do something similar if we merge two definitions of the
3485 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003486 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3487 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3488 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003489 Reader.PendingOdrMergeChecks.push_back(D);
3490
Richard Smithd08aeb62014-08-28 01:33:39 +00003491 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003492 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003493}
3494
Richard Smithb321ecb2014-05-13 01:15:00 +00003495template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003496Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3497 return D->RedeclLink.getLatestNotUpdated();
3498}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003499
Richard Smithc3a53252015-02-28 05:57:02 +00003500Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3501 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3502}
3503
3504Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3505 assert(D);
3506
3507 switch (D->getKind()) {
3508#define ABSTRACT_DECL(TYPE)
3509#define DECL(TYPE, BASE) \
3510 case Decl::TYPE: \
3511 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3512#include "clang/AST/DeclNodes.inc"
3513 }
3514 llvm_unreachable("unknown decl kind");
3515}
3516
Richard Smith8ce51082015-03-11 01:44:51 +00003517Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3518 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3519}
3520
Richard Smithc3a53252015-02-28 05:57:02 +00003521template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003522void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3523 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003524 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003525 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003526 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003527}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003528
Richard Smith24d166c2014-07-31 23:52:38 +00003529namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003530
Richard Smith6de7a242014-07-31 23:46:44 +00003531template<>
3532void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003533 Redeclarable<VarDecl> *D,
3534 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003535 auto *VD = static_cast<VarDecl *>(D);
3536 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003537 D->RedeclLink.setPrevious(PrevVD);
3538 D->First = PrevVD->First;
3539
3540 // We should keep at most one definition on the chain.
3541 // FIXME: Cache the definition once we've found it. Building a chain with
3542 // N definitions currently takes O(N^2) time here.
3543 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3544 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3545 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3546 Reader.mergeDefinitionVisibility(CurD, VD);
3547 VD->demoteThisDefinitionToDeclaration();
3548 break;
3549 }
3550 }
3551 }
3552}
3553
Richard Smitha62d1982018-08-03 01:00:01 +00003554static bool isUndeducedReturnType(QualType T) {
3555 auto *DT = T->getContainedDeducedType();
3556 return DT && !DT->isDeduced();
3557}
3558
Richard Smithedbc6e92016-10-14 21:41:24 +00003559template<>
3560void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003561 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003562 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003563 auto *FD = static_cast<FunctionDecl *>(D);
3564 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003565
3566 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003567 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003568
3569 // If the previous declaration is an inline function declaration, then this
3570 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003571 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003572 // FIXME: [dcl.fct.spec]p4:
3573 // If a function with external linkage is declared inline in one
3574 // translation unit, it shall be declared inline in all translation
3575 // units in which it appears.
3576 //
3577 // Be careful of this case:
3578 //
3579 // module A:
3580 // template<typename T> struct X { void f(); };
3581 // template<typename T> inline void X<T>::f() {}
3582 //
3583 // module B instantiates the declaration of X<int>::f
3584 // module C instantiates the definition of X<int>::f
3585 //
3586 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003587 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003588 }
3589
Richard Smith6de7a242014-07-31 23:46:44 +00003590 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3591 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003592 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003593 // If we need to propagate an exception specification along the redecl
3594 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003595 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3596 bool WasUnresolved =
3597 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3598 if (IsUnresolved != WasUnresolved)
3599 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003600 {Canon, IsUnresolved ? PrevFD : FD});
3601
3602 // If we need to propagate a deduced return type along the redecl chain,
3603 // make a note of that so that we can do it later.
3604 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3605 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3606 if (IsUndeduced != WasUndeduced)
3607 Reader.PendingDeducedTypeUpdates.insert(
3608 {cast<FunctionDecl>(Canon),
3609 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003610 }
3611}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003612
3613} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003614
Richard Smith6de7a242014-07-31 23:46:44 +00003615void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003616 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3617}
3618
Richard Smith8346e522015-06-10 01:47:58 +00003619/// Inherit the default template argument from \p From to \p To. Returns
3620/// \c false if there is no default template for \p From.
3621template <typename ParmDecl>
3622static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3623 Decl *ToD) {
3624 auto *To = cast<ParmDecl>(ToD);
3625 if (!From->hasDefaultArgument())
3626 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003627 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003628 return true;
3629}
3630
3631static void inheritDefaultTemplateArguments(ASTContext &Context,
3632 TemplateDecl *From,
3633 TemplateDecl *To) {
3634 auto *FromTP = From->getTemplateParameters();
3635 auto *ToTP = To->getTemplateParameters();
3636 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3637
3638 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003639 NamedDecl *FromParam = FromTP->getParam(I);
3640 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003641
Richard Smith559cc692018-07-31 21:01:53 +00003642 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3643 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3644 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3645 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3646 else
3647 inheritDefaultTemplateArgument(
3648 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003649 }
3650}
3651
Richard Smith6de7a242014-07-31 23:46:44 +00003652void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003653 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003654 assert(D && Previous);
3655
3656 switch (D->getKind()) {
3657#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003658#define DECL(TYPE, BASE) \
3659 case Decl::TYPE: \
3660 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003661 break;
3662#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003663 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003664
3665 // If the declaration was visible in one module, a redeclaration of it in
3666 // another module remains visible even if it wouldn't be visible by itself.
3667 //
3668 // FIXME: In this case, the declaration should only be visible if a module
3669 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003670 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003671 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003672 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003673
Richard Smith8346e522015-06-10 01:47:58 +00003674 // If the declaration declares a template, it may inherit default arguments
3675 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003676 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003677 inheritDefaultTemplateArguments(Reader.getContext(),
3678 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003679}
3680
Richard Smithb321ecb2014-05-13 01:15:00 +00003681template<typename DeclT>
3682void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003683 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003684}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003685
Richard Smithb321ecb2014-05-13 01:15:00 +00003686void ASTDeclReader::attachLatestDeclImpl(...) {
3687 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3688}
3689
Douglas Gregor05f10352011-12-17 23:38:30 +00003690void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3691 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003692
3693 switch (D->getKind()) {
3694#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003695#define DECL(TYPE, BASE) \
3696 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003697 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3698 break;
3699#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003700 }
3701}
3702
Richard Smith851072e2014-05-19 20:59:20 +00003703template<typename DeclT>
3704void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3705 D->RedeclLink.markIncomplete();
3706}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003707
Richard Smith851072e2014-05-19 20:59:20 +00003708void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3709 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3710}
3711
3712void ASTReader::markIncompleteDeclChain(Decl *D) {
3713 switch (D->getKind()) {
3714#define ABSTRACT_DECL(TYPE)
3715#define DECL(TYPE, BASE) \
3716 case Decl::TYPE: \
3717 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3718 break;
3719#include "clang/AST/DeclNodes.inc"
3720 }
3721}
3722
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003723/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003724Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003725 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003726 SourceLocation DeclLoc;
3727 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003728 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003729 // Keep track of where we are in the stream, then jump back there
3730 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003731 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003732
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003733 ReadingKindTracker ReadingKind(Read_Decl, *this);
3734
Douglas Gregor1342e842009-07-06 18:54:52 +00003735 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003736 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003737
JF Bastien0e828952019-06-26 19:50:12 +00003738 auto Fail = [](const char *what, llvm::Error &&Err) {
John McCall3ce3d232019-12-13 03:37:23 -05003739 llvm::report_fatal_error(Twine("ASTReader::readDeclRecord failed ") + what +
JF Bastien0e828952019-06-26 19:50:12 +00003740 ": " + toString(std::move(Err)));
3741 };
3742
3743 if (llvm::Error JumpFailed = DeclsCursor.JumpToBit(Loc.Offset))
3744 Fail("jumping", std::move(JumpFailed));
David L. Jonesbe1557a2016-12-21 00:17:49 +00003745 ASTRecordReader Record(*this, *Loc.F);
3746 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
JF Bastien0e828952019-06-26 19:50:12 +00003747 Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
3748 if (!MaybeCode)
3749 Fail("reading code", MaybeCode.takeError());
3750 unsigned Code = MaybeCode.get();
Chris Lattner487412d2009-04-27 05:27:42 +00003751
Richard Smithdbafb6c2017-06-29 23:23:46 +00003752 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003753 Decl *D = nullptr;
JF Bastien0e828952019-06-26 19:50:12 +00003754 Expected<unsigned> MaybeDeclCode = Record.readRecord(DeclsCursor, Code);
3755 if (!MaybeDeclCode)
3756 llvm::report_fatal_error(
John McCall3ce3d232019-12-13 03:37:23 -05003757 "ASTReader::readDeclRecord failed reading decl code: " +
JF Bastien0e828952019-06-26 19:50:12 +00003758 toString(MaybeDeclCode.takeError()));
3759 switch ((DeclCode)MaybeDeclCode.get()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003760 case DECL_CONTEXT_LEXICAL:
3761 case DECL_CONTEXT_VISIBLE:
John McCall3ce3d232019-12-13 03:37:23 -05003762 llvm_unreachable("Record cannot be de-serialized with readDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003763 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003764 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003765 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003766 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003767 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003768 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003769 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003770 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003771 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003772 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003773 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003774 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003775 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003776 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003777 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003778 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003779 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003780 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003781 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003782 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003783 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003784 case DECL_EXPORT:
3785 D = ExportDecl::CreateDeserialized(Context, ID);
3786 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003787 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003788 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003789 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003790 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003791 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003792 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003793 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003794 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003795 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003796 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003797 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003798 break;
Richard Smith151c4562016-12-20 21:35:28 +00003799 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003800 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003801 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003802 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003803 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003804 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003805 case DECL_CONSTRUCTOR_USING_SHADOW:
3806 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3807 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003808 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003809 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003810 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003811 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003812 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003813 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003814 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003815 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003816 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003817 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003818 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003819 break;
Richard Smithbc491202017-02-17 20:05:37 +00003820 case DECL_CXX_DEDUCTION_GUIDE:
3821 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3822 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003823 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003824 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003825 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003826 case DECL_CXX_CONSTRUCTOR:
Richard Smith76b90272019-05-09 03:59:21 +00003827 D = CXXConstructorDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003828 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003829 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003830 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003831 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003832 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003833 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003834 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003835 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003836 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003837 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003838 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003839 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003840 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003841 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003842 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003843 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003844 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003845 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003846 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003847 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003848 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003849 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003850 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003851 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003852 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003853 case DECL_VAR_TEMPLATE:
3854 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3855 break;
3856 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3857 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3858 break;
3859 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3860 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3861 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003862 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003863 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003864 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003865 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003866 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003867 break;
Saar Razff1e0fc2020-01-15 02:48:42 +02003868 case DECL_TEMPLATE_TYPE_PARM: {
3869 bool HasTypeConstraint = Record.readInt();
3870 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID,
3871 HasTypeConstraint);
Chris Lattnerca025db2010-05-07 21:43:38 +00003872 break;
Saar Razff1e0fc2020-01-15 02:48:42 +02003873 }
Saar Razb481f022020-01-22 02:03:05 +02003874 case DECL_NON_TYPE_TEMPLATE_PARM: {
3875 bool HasTypeConstraint = Record.readInt();
Saar Raze57a9ab2020-01-22 02:03:05 +02003876 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
Saar Razb481f022020-01-22 02:03:05 +02003877 HasTypeConstraint);
Saar Raze57a9ab2020-01-22 02:03:05 +02003878 break;
Saar Razb481f022020-01-22 02:03:05 +02003879 }
3880 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK: {
3881 bool HasTypeConstraint = Record.readInt();
3882 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3883 Record.readInt(),
3884 HasTypeConstraint);
3885 break;
3886 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003887 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003888 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003889 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003890 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3891 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003892 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003893 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003894 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003895 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003896 break;
Saar Razd7aae332019-07-10 21:25:49 +00003897 case DECL_CONCEPT:
3898 D = ConceptDecl::CreateDeserialized(Context, ID);
3899 break;
Saar Raza0f50d72020-01-18 09:11:43 +02003900 case DECL_REQUIRES_EXPR_BODY:
3901 D = RequiresExprBodyDecl::CreateDeserialized(Context, ID);
3902 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003903 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003904 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003905 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003906 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003907 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003908 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003909 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003910 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003911 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003912 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003913 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003914 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003915 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003916 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003917 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003918 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003919 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003920 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003921 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003922 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003923 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003924 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003925 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003926 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003927 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003928 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003929 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003930 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003931 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003932 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003933 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003934 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003935 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003936 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003937 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003938 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003939 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003940 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003941 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003942 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003943 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003944 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003945 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003946 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003947 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003948 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003949 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003950 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003951 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003952 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003953 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003954 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003955 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003956 break;
3957 case DECL_BINDING:
3958 D = BindingDecl::CreateDeserialized(Context, ID);
3959 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003960 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003961 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003962 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003963 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003964 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003965 break;
John McCall5e77d762013-04-16 07:28:30 +00003966 case DECL_MS_PROPERTY:
3967 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3968 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003969 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003970 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003971 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003972 case DECL_CXX_BASE_SPECIFIERS:
3973 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003974 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003975 case DECL_CXX_CTOR_INITIALIZERS:
3976 Error("attempt to read a C++ ctor initializer record as a declaration");
3977 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003978 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003979 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003980 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003981 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003982 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003983 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003984 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003985 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00003986 case DECL_OMP_ALLOCATE: {
3987 unsigned NumVars = Record.readInt();
3988 unsigned NumClauses = Record.readInt();
3989 D = OMPAllocateDecl::CreateDeserialized(Context, ID, NumVars, NumClauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00003990 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00003991 }
Kelvin Li1408f912018-09-26 04:28:39 +00003992 case DECL_OMP_REQUIRES:
3993 D = OMPRequiresDecl::CreateDeserialized(Context, ID, Record.readInt());
3994 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003995 case DECL_OMP_DECLARE_REDUCTION:
3996 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3997 break;
Michael Kruse251e1482019-02-01 20:25:04 +00003998 case DECL_OMP_DECLARE_MAPPER:
3999 D = OMPDeclareMapperDecl::CreateDeserialized(Context, ID, Record.readInt());
4000 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00004001 case DECL_OMP_CAPTUREDEXPR:
4002 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00004003 break;
Nico Weber66220292016-03-02 17:28:48 +00004004 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00004005 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00004006 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00004007 case DECL_PRAGMA_DETECT_MISMATCH:
4008 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00004009 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00004010 break;
Michael Han84324352013-02-22 17:15:32 +00004011 case DECL_EMPTY:
4012 D = EmptyDecl::CreateDeserialized(Context, ID);
4013 break;
Tykerb0561b32019-11-17 11:41:55 +01004014 case DECL_LIFETIME_EXTENDED_TEMPORARY:
4015 D = LifetimeExtendedTemporaryDecl::CreateDeserialized(Context, ID);
4016 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00004017 case DECL_OBJC_TYPE_PARAM:
4018 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
4019 break;
Chris Lattner487412d2009-04-27 05:27:42 +00004020 }
Chris Lattner487412d2009-04-27 05:27:42 +00004021
Sebastian Redlb3298c32010-08-18 23:56:48 +00004022 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00004023 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00004024 // Set the DeclContext before doing any deserialization, to make sure internal
4025 // calls to Decl::getASTContext() by Decl's methods will find the
4026 // TranslationUnitDecl without crashing.
4027 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00004028 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00004029
4030 // If this declaration is also a declaration context, get the
4031 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004032 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00004033 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00004034 if (Offsets.first &&
4035 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
4036 return nullptr;
4037 if (Offsets.second &&
4038 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
4039 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00004040 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00004041 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00004042
Douglas Gregordab42432011-08-12 00:15:20 +00004043 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004044 PendingUpdateRecords.push_back(
4045 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00004046
Douglas Gregor404cdde2012-01-27 01:47:08 +00004047 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004048 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00004049 // If we already have a definition when deserializing the ObjCInterfaceDecl,
4050 // we put the Decl in PendingDefinitions so we can pull the categories here.
4051 if (Class->isThisDeclarationADefinition() ||
4052 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004053 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00004054
Richard Smith13fb8602016-07-15 21:33:46 +00004055 // If we have deserialized a declaration that has a definition the
4056 // AST consumer might need to know about, queue it.
4057 // We don't pass it to the consumer immediately because we may be in recursive
4058 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004059 PotentiallyInterestingDecls.push_back(
4060 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00004061
Douglas Gregordab42432011-08-12 00:15:20 +00004062 return D;
4063}
4064
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00004065void ASTReader::PassInterestingDeclsToConsumer() {
4066 assert(Consumer);
4067
4068 if (PassingDeclsToConsumer)
4069 return;
4070
4071 // Guard variable to avoid recursively redoing the process of passing
4072 // decls to consumer.
4073 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
4074 true);
4075
4076 // Ensure that we've loaded all potentially-interesting declarations
4077 // that need to be eagerly loaded.
4078 for (auto ID : EagerlyDeserializedDecls)
4079 GetDecl(ID);
4080 EagerlyDeserializedDecls.clear();
4081
4082 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004083 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00004084 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004085 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
4086 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00004087 }
4088}
4089
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004090void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004091 // The declaration may have been modified by files later in the chain.
4092 // If this is the case, read the record containing the updates from each file
4093 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004094 serialization::GlobalDeclID ID = Record.ID;
4095 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004096 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004097 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00004098
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004099 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00004100
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004101 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00004102 auto UpdateOffsets = std::move(UpdI->second);
4103 DeclUpdateOffsets.erase(UpdI);
4104
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004105 // Check if this decl was interesting to the consumer. If we just loaded
4106 // the declaration, then we know it was interesting and we skip the call
4107 // to isConsumerInterestedIn because it is unsafe to call in the
4108 // current ASTReader state.
4109 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004110 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00004111 for (auto &FileAndOffset : UpdateOffsets) {
4112 ModuleFile *F = FileAndOffset.first;
4113 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004114 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
4115 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00004116 if (llvm::Error JumpFailed = Cursor.JumpToBit(Offset))
4117 // FIXME don't do a fatal error.
4118 llvm::report_fatal_error(
4119 "ASTReader::loadDeclUpdateRecords failed jumping: " +
4120 toString(std::move(JumpFailed)));
4121 Expected<unsigned> MaybeCode = Cursor.ReadCode();
4122 if (!MaybeCode)
4123 llvm::report_fatal_error(
4124 "ASTReader::loadDeclUpdateRecords failed reading code: " +
4125 toString(MaybeCode.takeError()));
4126 unsigned Code = MaybeCode.get();
David L. Jonesbe1557a2016-12-21 00:17:49 +00004127 ASTRecordReader Record(*this, *F);
JF Bastien0e828952019-06-26 19:50:12 +00004128 if (Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code))
4129 assert(MaybeRecCode.get() == DECL_UPDATES &&
4130 "Expected DECL_UPDATES record!");
4131 else
4132 llvm::report_fatal_error(
4133 "ASTReader::loadDeclUpdateRecords failed reading rec code: " +
4134 toString(MaybeCode.takeError()));
Richard Smith04d05b52014-03-23 00:27:18 +00004135
David L. Jonesbe1557a2016-12-21 00:17:49 +00004136 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
4137 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00004138 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00004139
4140 // We might have made this declaration interesting. If so, remember that
4141 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004142 if (!WasInteresting &&
4143 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
4144 PotentiallyInterestingDecls.push_back(
4145 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00004146 WasInteresting = true;
4147 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004148 }
4149 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00004150 // Add the lazy specializations to the template.
4151 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
4152 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
4153 "Must not have pending specializations");
4154 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
4155 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
4156 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
4157 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
4158 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
4159 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
4160 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00004161
4162 // Load the pending visible updates for this decl context, if it has any.
4163 auto I = PendingVisibleUpdates.find(ID);
4164 if (I != PendingVisibleUpdates.end()) {
4165 auto VisibleUpdates = std::move(I->second);
4166 PendingVisibleUpdates.erase(I);
4167
4168 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004169 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00004170 Lookups[DC].Table.add(
4171 Update.Mod, Update.Data,
4172 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
4173 DC->setHasExternalVisibleStorage(true);
4174 }
Chris Lattner487412d2009-04-27 05:27:42 +00004175}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004176
Richard Smithd61d4ac2015-08-22 20:13:39 +00004177void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
4178 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00004179 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00004180 if (FirstLocal != CanonDecl) {
4181 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
4182 ASTDeclReader::attachPreviousDecl(
4183 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
4184 CanonDecl);
4185 }
4186
4187 if (!LocalOffset) {
4188 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
4189 return;
4190 }
4191
4192 // Load the list of other redeclarations from this module file.
4193 ModuleFile *M = getOwningModuleFile(FirstLocal);
4194 assert(M && "imported decl from no module file");
4195
4196 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
4197 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00004198 if (llvm::Error JumpFailed = Cursor.JumpToBit(LocalOffset))
4199 llvm::report_fatal_error(
4200 "ASTReader::loadPendingDeclChain failed jumping: " +
4201 toString(std::move(JumpFailed)));
Richard Smithd61d4ac2015-08-22 20:13:39 +00004202
4203 RecordData Record;
JF Bastien0e828952019-06-26 19:50:12 +00004204 Expected<unsigned> MaybeCode = Cursor.ReadCode();
4205 if (!MaybeCode)
4206 llvm::report_fatal_error(
4207 "ASTReader::loadPendingDeclChain failed reading code: " +
4208 toString(MaybeCode.takeError()));
4209 unsigned Code = MaybeCode.get();
4210 if (Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record))
4211 assert(MaybeRecCode.get() == LOCAL_REDECLARATIONS &&
4212 "expected LOCAL_REDECLARATIONS record!");
4213 else
4214 llvm::report_fatal_error(
4215 "ASTReader::loadPendingDeclChain failed reading rec code: " +
4216 toString(MaybeCode.takeError()));
Richard Smithd61d4ac2015-08-22 20:13:39 +00004217
4218 // FIXME: We have several different dispatches on decl kind here; maybe
4219 // we should instead generate one loop per kind and dispatch up-front?
4220 Decl *MostRecent = FirstLocal;
4221 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4222 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00004223 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
4224 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00004225 }
Richard Smithc3a53252015-02-28 05:57:02 +00004226 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00004227}
4228
4229namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004230
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004231 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004232 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004233 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004234 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004235 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00004236 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004237 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004238 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004239 serialization::GlobalDeclID InterfaceID;
4240 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00004241
Douglas Gregor404cdde2012-01-27 01:47:08 +00004242 void add(ObjCCategoryDecl *Cat) {
4243 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00004244 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004245 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00004246
Douglas Gregor404cdde2012-01-27 01:47:08 +00004247 // Check for duplicate categories.
4248 if (Cat->getDeclName()) {
4249 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00004250 if (Existing &&
4251 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00004252 != Reader.getOwningModuleFile(Cat)) {
4253 // FIXME: We should not warn for duplicates in diamond:
4254 //
4255 // MT //
4256 // / \ //
4257 // ML MR //
4258 // \ / //
4259 // MB //
4260 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004261 // If there are duplicates in ML/MR, there will be warning when
4262 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004263 // importing.
4264 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4265 << Interface->getDeclName() << Cat->getDeclName();
4266 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4267 } else if (!Existing) {
4268 // Record this category.
4269 Existing = Cat;
4270 }
4271 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004272
Douglas Gregor404cdde2012-01-27 01:47:08 +00004273 // Add this category to the end of the chain.
4274 if (Tail)
4275 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4276 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004277 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004278 Tail = Cat;
4279 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004280
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004281 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004282 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004283 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004284 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4285 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004286 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004287 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4288 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004289 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004290 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004291 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004292 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004293
Douglas Gregor404cdde2012-01-27 01:47:08 +00004294 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004295 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004296 }
4297 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004298
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004299 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004300 // If we've loaded all of the category information we care about from
4301 // this module file, we're done.
4302 if (M.Generation <= PreviousGeneration)
4303 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004304
4305 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004306 // module file. If there is no such mapping, we'll find nothing here
4307 // (or in any module it imports).
4308 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4309 if (!LocalID)
4310 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004311
Douglas Gregor404cdde2012-01-27 01:47:08 +00004312 // Perform a binary search to find the local redeclarations for this
4313 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004314 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004315 const ObjCCategoriesInfo *Result
4316 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004317 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004318 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004319 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4320 Result->DefinitionID != LocalID) {
4321 // We didn't find anything. If the class definition is in this module
4322 // file, then the module files it depends on cannot have any categories,
4323 // so suppress further lookup.
4324 return Reader.isDeclIDFromModule(InterfaceID, M);
4325 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004326
Douglas Gregor404cdde2012-01-27 01:47:08 +00004327 // We found something. Dig out all of the categories.
4328 unsigned Offset = Result->Offset;
4329 unsigned N = M.ObjCCategories[Offset];
4330 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4331 for (unsigned I = 0; I != N; ++I)
4332 add(cast_or_null<ObjCCategoryDecl>(
4333 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4334 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004335 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004336 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004337
4338} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004339
Douglas Gregor404cdde2012-01-27 01:47:08 +00004340void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4341 ObjCInterfaceDecl *D,
4342 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004343 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004344 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004345 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004346}
Douglas Gregordab42432011-08-12 00:15:20 +00004347
Richard Smithd6db68c2014-08-07 20:58:41 +00004348template<typename DeclT, typename Fn>
4349static void forAllLaterRedecls(DeclT *D, Fn F) {
4350 F(D);
4351
4352 // Check whether we've already merged D into its redeclaration chain.
4353 // MostRecent may or may not be nullptr if D has not been merged. If
4354 // not, walk the merged redecl chain and see if it's there.
4355 auto *MostRecent = D->getMostRecentDecl();
4356 bool Found = false;
4357 for (auto *Redecl = MostRecent; Redecl && !Found;
4358 Redecl = Redecl->getPreviousDecl())
4359 Found = (Redecl == D);
4360
4361 // If this declaration is merged, apply the functor to all later decls.
4362 if (Found) {
4363 for (auto *Redecl = MostRecent; Redecl != D;
4364 Redecl = Redecl->getPreviousDecl())
4365 F(Redecl);
4366 }
4367}
4368
Vassil Vassilev7d264622017-06-30 22:40:17 +00004369void ASTDeclReader::UpdateDecl(Decl *D,
4370 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004371 while (Record.getIdx() < Record.size()) {
4372 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004373 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004374 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004375 // FIXME: If we also have an update record for instantiating the
4376 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004377 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004378 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004379 // FIXME: We should call addHiddenDecl instead, to add the member
4380 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004381 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004382 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004383 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004384
4385 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004386 // It will be added to the template's lazy specialization set.
John McCall3ce3d232019-12-13 03:37:23 -05004387 PendingLazySpecializationIDs.push_back(readDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004388 break;
4389
4390 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
John McCall3ce3d232019-12-13 03:37:23 -05004391 auto *Anon = readDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004392
Douglas Gregor540fd812012-01-09 18:07:24 +00004393 // Each module has its own anonymous namespace, which is disjoint from
4394 // any other module's anonymous namespaces, so don't attach the anonymous
4395 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004396 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004397 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004398 TU->setAnonymousNamespace(Anon);
4399 else
4400 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4401 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004402 break;
4403 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004404
Richard Smith891fc7f2017-12-05 01:31:47 +00004405 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004406 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004407 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4408 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004409 uint64_t Val = Record.readInt();
4410 if (Val && !VD->getInit()) {
4411 VD->setInit(Record.readExpr());
4412 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4413 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4414 Eval->CheckedICE = true;
4415 Eval->IsICE = Val == 3;
4416 }
4417 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004418 break;
Richard Smith05a21352017-06-22 22:18:46 +00004419 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004420
Richard Smith891fc7f2017-12-05 01:31:47 +00004421 case UPD_CXX_POINT_OF_INSTANTIATION: {
4422 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004423 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004424 VTSD->setPointOfInstantiation(POI);
4425 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4426 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4427 } else {
4428 auto *FD = cast<FunctionDecl>(D);
4429 if (auto *FTSInfo = FD->TemplateOrSpecialization
4430 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4431 FTSInfo->setPointOfInstantiation(POI);
4432 else
4433 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4434 ->setPointOfInstantiation(POI);
4435 }
4436 break;
4437 }
4438
John McCall32791cc2016-01-06 22:34:54 +00004439 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004440 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004441
4442 // We have to read the default argument regardless of whether we use it
4443 // so that hypothetical further update records aren't messed up.
4444 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004445 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004446
4447 // Only apply the update if the parameter still has an uninstantiated
4448 // default argument.
4449 if (Param->hasUninstantiatedDefaultArg())
4450 Param->setDefaultArg(DefaultArg);
4451 break;
4452 }
4453
Richard Smith4b054b22016-08-24 21:25:37 +00004454 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004455 auto *FD = cast<FieldDecl>(D);
4456 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004457
4458 // Only apply the update if the field still has an uninstantiated
4459 // default member initializer.
4460 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4461 if (DefaultInit)
4462 FD->setInClassInitializer(DefaultInit);
4463 else
4464 // Instantiation failed. We can get here if we serialized an AST for
4465 // an invalid program.
4466 FD->removeInClassInitializer();
4467 }
4468 break;
4469 }
4470
Richard Smith4d235792014-08-07 18:53:08 +00004471 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004472 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004473 if (Reader.PendingBodies[FD]) {
4474 // FIXME: Maybe check for ODR violations.
4475 // It's safe to stop now because this update record is always last.
4476 return;
4477 }
4478
David L. Jonesbe1557a2016-12-21 00:17:49 +00004479 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004480 // Maintain AST consistency: any later redeclarations of this function
4481 // are inline if this one is. (We might have merged another declaration
4482 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004483 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4484 FD->setImplicitlyInline();
4485 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004486 }
John McCall3ce3d232019-12-13 03:37:23 -05004487 FD->setInnerLocStart(readSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004488 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004489 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004490 break;
4491 }
4492
Richard Smithcd45dbc2014-04-19 03:48:30 +00004493 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4494 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004495 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004496 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004497 OldDD && (OldDD->Definition != RD ||
4498 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004499 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004500 RD->setArgPassingRestrictions(
4501 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004502 ReadCXXRecordDefinition(RD, /*Update*/true);
4503
Richard Smithcd45dbc2014-04-19 03:48:30 +00004504 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004505 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004506 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004507 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004508 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004509 }
4510
David L. Jonesbe1557a2016-12-21 00:17:49 +00004511 auto TSK = (TemplateSpecializationKind)Record.readInt();
John McCall3ce3d232019-12-13 03:37:23 -05004512 SourceLocation POI = readSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004513 if (MemberSpecializationInfo *MSInfo =
4514 RD->getMemberSpecializationInfo()) {
4515 MSInfo->setTemplateSpecializationKind(TSK);
4516 MSInfo->setPointOfInstantiation(POI);
4517 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004518 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004519 Spec->setTemplateSpecializationKind(TSK);
4520 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004521
David L. Jonesbe1557a2016-12-21 00:17:49 +00004522 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004523 auto *PartialSpec =
John McCall3ce3d232019-12-13 03:37:23 -05004524 readDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004525 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004526 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004527 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004528 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004529
4530 // FIXME: If we already have a partial specialization set,
4531 // check that it matches.
4532 if (!Spec->getSpecializedTemplateOrPartial()
4533 .is<ClassTemplatePartialSpecializationDecl *>())
4534 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004535 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004536 }
4537
David L. Jonesbe1557a2016-12-21 00:17:49 +00004538 RD->setTagKind((TagTypeKind)Record.readInt());
John McCall3ce3d232019-12-13 03:37:23 -05004539 RD->setLocation(readSourceLocation());
4540 RD->setLocStart(readSourceLocation());
4541 RD->setBraceRange(readSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004542
David L. Jonesbe1557a2016-12-21 00:17:49 +00004543 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004544 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004545 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004546 // If the declaration already has attributes, we assume that some other
4547 // AST file already loaded them.
4548 if (!D->hasAttrs())
4549 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004550 }
4551 break;
4552 }
4553
Richard Smithf8134002015-03-10 01:41:22 +00004554 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4555 // Set the 'operator delete' directly to avoid emitting another update
4556 // record.
John McCall3ce3d232019-12-13 03:37:23 -05004557 auto *Del = readDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004558 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004559 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004560 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004561 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004562 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004563 First->OperatorDeleteThisArg = ThisArg;
4564 }
Richard Smithf8134002015-03-10 01:41:22 +00004565 break;
4566 }
4567
Richard Smith564417a2014-03-20 21:47:22 +00004568 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith6de7a242014-07-31 23:46:44 +00004569 SmallVector<QualType, 8> ExceptionStorage;
John McCall3ce3d232019-12-13 03:37:23 -05004570 auto ESI = Record.readExceptionSpecInfo(ExceptionStorage);
Richard Smith9e2341d2015-03-23 03:25:59 +00004571
4572 // Update this declaration's exception specification, if needed.
4573 auto *FD = cast<FunctionDecl>(D);
4574 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4575 // FIXME: If the exception specification is already present, check that it
4576 // matches.
4577 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004578 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004579 FPT->getReturnType(), FPT->getParamTypes(),
4580 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004581
4582 // When we get to the end of deserializing, see if there are other decls
4583 // that we need to propagate this exception specification onto.
4584 Reader.PendingExceptionSpecUpdates.insert(
4585 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004586 }
Richard Smith564417a2014-03-20 21:47:22 +00004587 break;
4588 }
4589
Richard Smith1fa5d642013-05-11 05:45:24 +00004590 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004591 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004592 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004593 Reader.PendingDeducedTypeUpdates.insert(
4594 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004595 break;
4596 }
Eli Friedman276dd182013-09-05 00:02:25 +00004597
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004598 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004599 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004600 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004601 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004602
4603 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004604 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4605 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004606 break;
4607
4608 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004609 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4610 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004611 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004612
Alexey Bataev97720002014-11-11 04:05:39 +00004613 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Erich Keane6a24e802019-09-13 17:39:31 +00004614 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
John McCall3ce3d232019-12-13 03:37:23 -05004615 Reader.getContext(), readSourceRange(),
Erich Keane6a24e802019-09-13 17:39:31 +00004616 AttributeCommonInfo::AS_Pragma));
Alexey Bataev97720002014-11-11 04:05:39 +00004617 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004618
Alexey Bataev27ef9512019-03-20 20:14:22 +00004619 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
4620 auto AllocatorKind =
4621 static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(Record.readInt());
4622 Expr *Allocator = Record.readExpr();
John McCall3ce3d232019-12-13 03:37:23 -05004623 SourceRange SR = readSourceRange();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00004624 D->addAttr(OMPAllocateDeclAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00004625 Reader.getContext(), AllocatorKind, Allocator, SR,
4626 AttributeCommonInfo::AS_Pragma));
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004627 break;
Alexey Bataev27ef9512019-03-20 20:14:22 +00004628 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004629
Alex Denisovfde64952015-06-26 05:28:36 +00004630 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004631 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004632 auto *Exported = cast<NamedDecl>(D);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004633 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith13897eb2018-09-12 23:37:00 +00004634 Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
4635 Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004636 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004637 }
Alex Denisovfde64952015-06-26 05:28:36 +00004638
Alexey Bataev729e2422019-08-23 16:11:14 +00004639 case UPD_DECL_MARKED_OPENMP_DECLARETARGET: {
4640 OMPDeclareTargetDeclAttr::MapTypeTy MapType =
4641 static_cast<OMPDeclareTargetDeclAttr::MapTypeTy>(Record.readInt());
4642 OMPDeclareTargetDeclAttr::DevTypeTy DevType =
4643 static_cast<OMPDeclareTargetDeclAttr::DevTypeTy>(Record.readInt());
Alexey Bataevd01b7492018-08-15 19:45:12 +00004644 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
John McCall3ce3d232019-12-13 03:37:23 -05004645 Reader.getContext(), MapType, DevType, readSourceRange(),
Erich Keane6a24e802019-09-13 17:39:31 +00004646 AttributeCommonInfo::AS_Pragma));
Alexey Bataevd01b7492018-08-15 19:45:12 +00004647 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00004648 }
Alexey Bataevd01b7492018-08-15 19:45:12 +00004649
Alex Denisovfde64952015-06-26 05:28:36 +00004650 case UPD_ADDED_ATTR_TO_RECORD:
4651 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004652 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004653 assert(Attrs.size() == 1);
4654 D->addAttr(Attrs[0]);
4655 break;
4656 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004657 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004658}