blob: 76928182a1e8e4c2160a2310f92a5317f46a13d2 [file] [log] [blame]
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001//===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner487412d2009-04-27 05:27:42 +00006//
7//===----------------------------------------------------------------------===//
8//
Sebastian Redl2c499f62010-08-18 23:56:43 +00009// This file implements the ASTReader::ReadDeclRecord method, which is the
Chris Lattner487412d2009-04-27 05:27:42 +000010// entrypoint for loading a decl.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000014#include "ASTCommon.h"
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +000015#include "ASTReaderInternals.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/AttrIterator.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclCXX.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000022#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclOpenMP.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/DeclVisitor.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000027#include "clang/AST/DeclarationName.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "clang/AST/Expr.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000029#include "clang/AST/ExternalASTSource.h"
30#include "clang/AST/LambdaCapture.h"
31#include "clang/AST/NestedNameSpecifier.h"
Kelvin Li1408f912018-09-26 04:28:39 +000032#include "clang/AST/OpenMPClause.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000033#include "clang/AST/Redeclarable.h"
34#include "clang/AST/Stmt.h"
35#include "clang/AST/TemplateBase.h"
36#include "clang/AST/Type.h"
37#include "clang/AST/UnresolvedSet.h"
38#include "clang/Basic/AttrKinds.h"
39#include "clang/Basic/ExceptionSpecificationType.h"
40#include "clang/Basic/IdentifierTable.h"
41#include "clang/Basic/LLVM.h"
42#include "clang/Basic/Lambda.h"
43#include "clang/Basic/LangOptions.h"
44#include "clang/Basic/Linkage.h"
45#include "clang/Basic/Module.h"
46#include "clang/Basic/PragmaKinds.h"
47#include "clang/Basic/SourceLocation.h"
48#include "clang/Basic/Specifiers.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000049#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000050#include "clang/Serialization/ASTBitCodes.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000051#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000052#include "clang/Serialization/ContinuousRangeMap.h"
53#include "clang/Serialization/Module.h"
54#include "llvm/ADT/DenseMap.h"
55#include "llvm/ADT/FoldingSet.h"
56#include "llvm/ADT/STLExtras.h"
57#include "llvm/ADT/SmallPtrSet.h"
58#include "llvm/ADT/SmallVector.h"
59#include "llvm/ADT/iterator_range.h"
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
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000114 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000115 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000116 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000117
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000118 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000119 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000120 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000121
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000122 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000123 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000124 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000125
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000126 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000127 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000128 }
Richard Smith0b884372015-02-27 00:25:58 +0000129
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000130 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000131 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +0000132 }
133
Richard Smith0b884372015-02-27 00:25:58 +0000134 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000135 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000136 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +0000137 }
138
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000139 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000140 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000141 }
142
143 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000144 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000145 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000146 }
147
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000148 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000149 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000150 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000151
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000152 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000153 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000154 }
155
156 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000157 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000158 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000159
David L. Jonesbe1557a2016-12-21 00:17:49 +0000160 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000161 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000162
163 Module *readModule() {
164 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000165 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000166
Richard Smith2a9e5c52015-02-03 03:32:14 +0000167 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000168 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
169 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000170 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000171 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000172 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000173 void MergeDefinitionData(ObjCInterfaceDecl *D,
174 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000175 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
176 void MergeDefinitionData(ObjCProtocolDecl *D,
177 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000178
Richard Smith600adef2018-07-04 02:25:38 +0000179 static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
180
Richard Smithd08aeb62014-08-28 01:33:39 +0000181 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
182 DeclContext *DC,
183 unsigned Index);
184 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
185 unsigned Index, NamedDecl *D);
186
Richard Smith0144f512015-08-22 02:09:38 +0000187 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000188 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000189 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000190 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000191 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000192
Douglas Gregor022857e2011-12-22 01:48:48 +0000193 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000194 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000195 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000197 /// Retrieve the first ID.
Douglas Gregor022857e2011-12-22 01:48:48 +0000198 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000200 /// Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000201 bool isKeyDecl() const { return IsKeyDecl; }
202
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000203 /// Get a known declaration that this should be merged with, if
Richard Smith5a4737c2015-02-06 02:42:59 +0000204 /// any.
205 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000206 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000207
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000208 /// Class used to capture the result of searching for an existing
Douglas Gregor022857e2011-12-22 01:48:48 +0000209 /// declaration of a specific kind and name, along with the ability
210 /// to update the place where this result was found (the declaration
211 /// chain hanging off an identifier or the DeclContext we searched in)
212 /// if requested.
213 class FindExistingResult {
214 ASTReader &Reader;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000215 NamedDecl *New = nullptr;
216 NamedDecl *Existing = nullptr;
217 bool AddResult = false;
218 unsigned AnonymousDeclNumber = 0;
219 IdentifierInfo *TypedefNameForLinkage = nullptr;
Richard Smithd08aeb62014-08-28 01:33:39 +0000220
Douglas Gregor022857e2011-12-22 01:48:48 +0000221 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000222 FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000223
Richard Smithd08aeb62014-08-28 01:33:39 +0000224 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000225 unsigned AnonymousDeclNumber,
226 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000227 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000228 AnonymousDeclNumber(AnonymousDeclNumber),
229 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000230
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000231 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000232 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
233 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000234 AnonymousDeclNumber(Other.AnonymousDeclNumber),
235 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000236 Other.AddResult = false;
237 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000238
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000239 FindExistingResult &operator=(FindExistingResult &&) = delete;
Douglas Gregor022857e2011-12-22 01:48:48 +0000240 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000241
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000242 /// Suppress the addition of this result into the known set of
Douglas Gregor2009cee2012-01-03 22:46:00 +0000243 /// names.
244 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000245
Douglas Gregor022857e2011-12-22 01:48:48 +0000246 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000247
Douglas Gregor022857e2011-12-22 01:48:48 +0000248 template<typename T>
249 operator T*() const { return dyn_cast_or_null<T>(Existing); }
250 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000251
Richard Smith8a639892015-01-24 01:07:20 +0000252 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
253 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000254 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000255
Chris Lattner487412d2009-04-27 05:27:42 +0000256 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000257 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
258 ASTReader::RecordLocation Loc,
259 DeclID thisDeclID, SourceLocation ThisDeclLoc)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000260 : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
261 ThisDeclLoc(ThisDeclLoc) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000262
Vassil Vassilev7d264622017-06-30 22:40:17 +0000263 template <typename T> static
264 void AddLazySpecializations(T *D,
265 SmallVectorImpl<serialization::DeclID>& IDs) {
266 if (IDs.empty())
267 return;
268
269 // FIXME: We should avoid this pattern of getting the ASTContext.
270 ASTContext &C = D->getASTContext();
271
272 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
273
274 if (auto &Old = LazySpecializations) {
275 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Fangrui Song55fab262018-09-26 22:16:28 +0000276 llvm::sort(IDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +0000277 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
278 }
279
280 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
281 *Result = IDs.size();
282 std::copy(IDs.begin(), IDs.end(), Result + 1);
283
284 LazySpecializations = Result;
285 }
286
Richard Smithb321ecb2014-05-13 01:15:00 +0000287 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000288 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
289 static Decl *getMostRecentDeclImpl(...);
290 static Decl *getMostRecentDecl(Decl *D);
291
292 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000293 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000294 Redeclarable<DeclT> *D, Decl *Previous,
295 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000296 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000297 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
298 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000299
300 template <typename DeclT>
301 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
302 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000303 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000304
Richard Smith851072e2014-05-19 20:59:20 +0000305 template <typename DeclT>
306 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
307 static void markIncompleteDeclChainImpl(...);
308
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000309 /// Determine whether this declaration has a pending body.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000310 bool hasPendingBody() const { return HasPendingBody; }
311
David Blaikieac4345c2017-02-12 18:45:31 +0000312 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000313 void Visit(Decl *D);
314
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000315 void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000316
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000317 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
318 ObjCCategoryDecl *Next) {
319 Cat->NextClassCategory = Next;
320 }
321
Chris Lattner487412d2009-04-27 05:27:42 +0000322 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000323 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000324 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000325 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
326 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000327 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000328 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000329 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
330 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000331 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000332 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000333 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000334 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000335 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000336 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000337 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000338 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
339 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
340 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
341 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
342 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000343 ClassTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000344
Richard Smith1d209d02013-05-23 01:49:11 +0000345 void VisitClassTemplateSpecializationDecl(
346 ClassTemplateSpecializationDecl *D) {
347 VisitClassTemplateSpecializationDeclImpl(D);
348 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000349
Chris Lattnerca025db2010-05-07 21:43:38 +0000350 void VisitClassTemplatePartialSpecializationDecl(
351 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000352 void VisitClassScopeFunctionSpecializationDecl(
353 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000354 RedeclarableResult
355 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000356
Larisse Voufo39a1e502013-08-06 01:03:05 +0000357 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
358 VisitVarTemplateSpecializationDeclImpl(D);
359 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000360
Larisse Voufo39a1e502013-08-06 01:03:05 +0000361 void VisitVarTemplatePartialSpecializationDecl(
362 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000363 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000364 void VisitValueDecl(ValueDecl *VD);
365 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000366 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000367 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000368 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000369 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000370 void VisitCXXMethodDecl(CXXMethodDecl *D);
371 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
372 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
373 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000374 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000375 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000376 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000377 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
378 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000379 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
380 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000381 void VisitDecompositionDecl(DecompositionDecl *DD);
382 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000383 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000384 DeclID VisitTemplateDecl(TemplateDecl *D);
Saar Razd7aae332019-07-10 21:25:49 +0000385 void VisitConceptDecl(ConceptDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000386 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000387 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000388 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000389 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000390 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000391 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000392 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000393 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000394 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000395 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000396 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000397 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000398 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000399 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000400 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000401 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000402 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000403 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
404 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000405 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000406 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000407 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000408
Chris Lattner487412d2009-04-27 05:27:42 +0000409 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000410
411 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000412 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000413
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000414 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000415 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
416 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000417
418 template<typename T>
419 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000420 RedeclarableResult &Redecl,
421 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000422
Richard Smith0b87e072013-10-07 08:02:11 +0000423 template<typename T>
424 void mergeMergeable(Mergeable<T> *D);
425
Richard Smithf17fdbd2014-04-24 02:25:27 +0000426 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
427 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000428 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000429
Douglas Gregor85f3f952015-07-07 03:57:15 +0000430 ObjCTypeParamList *ReadObjCTypeParamList();
431
Alexis Hunted053252010-05-30 07:21:58 +0000432 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000433 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000434 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000435 void VisitObjCContainerDecl(ObjCContainerDecl *D);
436 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
437 void VisitObjCIvarDecl(ObjCIvarDecl *D);
438 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
439 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000440 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
441 void VisitObjCImplDecl(ObjCImplDecl *D);
442 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
443 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
444 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
445 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
446 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000447 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000448 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000449 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Michael Kruse251e1482019-02-01 20:25:04 +0000450 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
Kelvin Li1408f912018-09-26 04:28:39 +0000451 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000452 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000453 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000454
455} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000456
Richard Smith86dfc1e2015-06-18 22:07:00 +0000457namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000458
Richard Smith86dfc1e2015-06-18 22:07:00 +0000459/// Iterator over the redeclarations of a declaration that have already
460/// been merged into the same redeclaration chain.
461template<typename DeclT>
462class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000463 DeclT *Start;
464 DeclT *Canonical = nullptr;
465 DeclT *Current = nullptr;
466
Richard Smith86dfc1e2015-06-18 22:07:00 +0000467public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000468 MergedRedeclIterator() = default;
469 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000470
471 DeclT *operator*() { return Current; }
472
473 MergedRedeclIterator &operator++() {
474 if (Current->isFirstDecl()) {
475 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000476 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000477 } else
478 Current = Current->getPreviousDecl();
479
480 // If we started in the merged portion, we'll reach our start position
481 // eventually. Otherwise, we'll never reach it, but the second declaration
482 // we reached was the canonical declaration, so stop when we see that one
483 // again.
484 if (Current == Start || Current == Canonical)
485 Current = nullptr;
486 return *this;
487 }
488
489 friend bool operator!=(const MergedRedeclIterator &A,
490 const MergedRedeclIterator &B) {
491 return A.Current != B.Current;
492 }
493};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000494
495} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000496
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000497template <typename DeclT>
498static llvm::iterator_range<MergedRedeclIterator<DeclT>>
499merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000500 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
501 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000502}
503
Sebastian Redlb3298c32010-08-18 23:56:48 +0000504uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000505 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000506}
507
David Blaikieac4345c2017-02-12 18:45:31 +0000508void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000509 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000510 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000511 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Erich Keane9c665062018-08-01 21:02:40 +0000512 CD->setNumCtorInitializers(Record.readInt());
513 if (CD->getNumCtorInitializers())
David Blaikieac4345c2017-02-12 18:45:31 +0000514 CD->CtorInitializers = ReadGlobalOffset();
515 }
516 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000517 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
518 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000519}
520
Sebastian Redlb3298c32010-08-18 23:56:48 +0000521void ASTDeclReader::Visit(Decl *D) {
522 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000523
Vassil Vassilev928c8252016-04-28 14:13:28 +0000524 // At this point we have deserialized and merged the decl and it is safe to
525 // update its canonical decl to signal that the entire entity is used.
526 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
527 IsDeclMarkedUsed = false;
528
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000529 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000530 if (auto *TInfo = DD->getTypeSourceInfo())
531 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000532 }
533
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000534 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000535 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000536 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000537
538 // If this is a tag declaration with a typedef name for linkage, it's safe
539 // to load that typedef now.
540 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000541 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
542 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000543 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000544 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000545 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000546 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000547 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000548 // We only read it if FD doesn't already have a body (e.g., from another
549 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000550 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000551 if (Record.readInt())
552 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000553 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000554}
555
Sebastian Redlb3298c32010-08-18 23:56:48 +0000556void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000557 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
558 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000559 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000560 // parameter or of a parameter of a function template immediately. These
561 // entities might be used in the formulation of its DeclContext (for
562 // example, a function parameter can be used in decltype() in trailing
563 // return type of the function). Use the translation unit DeclContext as a
564 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000565 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
566 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000567 if (!LexicalDCIDForTemplateParmDecl)
568 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000569 Reader.addPendingDeclContextInfo(D,
570 SemaDCIDForTemplateParmDecl,
571 LexicalDCIDForTemplateParmDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000572 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000573 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000574 auto *SemaDC = ReadDeclAs<DeclContext>();
575 auto *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000576 if (!LexicalDC)
577 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000578 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000579 // Avoid calling setLexicalDeclContext() directly because it uses
580 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000581 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
582 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000583 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000584 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000585 D->setInvalidDecl(Record.readInt());
586 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000587 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000588 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000589 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
590 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000591 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000592 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000593 D->setImplicit(Record.readInt());
594 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000595 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000596 D->setReferenced(Record.readInt());
597 D->setTopLevelDeclInObjCContainer(Record.readInt());
598 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000599 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000600 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000601
Douglas Gregorcf68c582011-12-01 22:20:10 +0000602 // Determine whether this declaration is part of a (sub)module. If so, it
603 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000604 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000605 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000606 D->setModuleOwnershipKind(
607 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
608 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000609 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000610
Richard Smith90dc5252017-06-23 01:04:34 +0000611 if (ModulePrivate) {
612 // Module-private declarations are never visible, so there is no work to
613 // do.
Richard Smith42413142015-05-15 20:05:43 +0000614 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
615 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000616 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000617 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000618 // Mark the declaration as visible when its owning module becomes visible.
619 if (Owner->NameVisibility == Module::AllVisible)
620 D->setVisibleDespiteOwningModule();
621 else
Richard Smith42413142015-05-15 20:05:43 +0000622 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000623 }
Richard Smithd19389a2017-07-05 07:47:11 +0000624 } else if (ModulePrivate) {
625 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000626 }
Chris Lattner487412d2009-04-27 05:27:42 +0000627}
628
Nico Weber66220292016-03-02 17:28:48 +0000629void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
630 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000631 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000632 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000633 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000634 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
635 D->getTrailingObjects<char>()[Arg.size()] = '\0';
636}
637
Nico Webercbbaeb12016-03-02 19:28:54 +0000638void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
639 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000640 D->setLocation(ReadSourceLocation());
641 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000642 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
643 D->getTrailingObjects<char>()[Name.size()] = '\0';
644
645 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000646 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000647 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
648 Value.size());
649 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
650}
651
Sebastian Redlb3298c32010-08-18 23:56:48 +0000652void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000653 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000654}
655
Sebastian Redlb3298c32010-08-18 23:56:48 +0000656void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000657 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000658 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000659 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000660}
661
Sebastian Redlb3298c32010-08-18 23:56:48 +0000662void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000663 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000664 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000665 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000666 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000667}
668
Richard Smith43ccec8e2014-08-26 03:52:16 +0000669ASTDeclReader::RedeclarableResult
670ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000671 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000672 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000673 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000674 if (Record.readInt()) { // isModed
675 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000676 TD->setModedTypeSourceInfo(TInfo, modedT);
677 } else
678 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000679 // Read and discard the declaration for which this is a typedef name for
680 // linkage, if it exists. We cannot rely on our type to pull in this decl,
681 // because it might have been merged with a type from another module and
682 // thus might not refer to our version of the declaration.
683 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000684 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000685}
686
687void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000688 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
689 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000690}
691
Richard Smithdda56e42011-04-15 14:24:37 +0000692void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000693 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000694 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000695 // Merged when we merge the template.
696 TD->setDescribedAliasTemplate(Template);
697 else
698 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000699}
700
Richard Smith1d209d02013-05-23 01:49:11 +0000701ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000702 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000703 VisitTypeDecl(TD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000704
David L. Jonesbe1557a2016-12-21 00:17:49 +0000705 TD->IdentifierNamespace = Record.readInt();
706 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000707 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000708 TD->setCompleteDefinition(Record.readInt());
709 TD->setEmbeddedInDeclarator(Record.readInt());
710 TD->setFreeStanding(Record.readInt());
711 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000712 TD->setBraceRange(ReadSourceRange());
Fangrui Song6907ce22018-07-30 19:24:48 +0000713
David L. Jonesbe1557a2016-12-21 00:17:49 +0000714 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000715 case 0:
716 break;
717 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000718 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000719 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000720 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000721 break;
722 }
723 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000724 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000725 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000726 break;
Richard Smith70d58502014-08-30 00:04:23 +0000727 default:
728 llvm_unreachable("unexpected tag info kind");
729 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000730
Richard Smithcd45dbc2014-04-19 03:48:30 +0000731 if (!isa<CXXRecordDecl>(TD))
732 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000733 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000734}
735
Sebastian Redlb3298c32010-08-18 23:56:48 +0000736void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000737 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000738 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000739 ED->setIntegerTypeSourceInfo(TI);
740 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000741 ED->setIntegerType(Record.readType());
742 ED->setPromotionType(Record.readType());
743 ED->setNumPositiveBits(Record.readInt());
744 ED->setNumNegativeBits(Record.readInt());
Erich Keanef92f31c2018-08-01 20:48:16 +0000745 ED->setScoped(Record.readInt());
746 ED->setScopedUsingClassTag(Record.readInt());
747 ED->setFixed(Record.readInt());
Richard Smith4b38ded2012-03-14 23:13:10 +0000748
Erich Keanef92f31c2018-08-01 20:48:16 +0000749 ED->setHasODRHash(true);
Richard Trieuab4d7302018-07-25 22:52:05 +0000750 ED->ODRHash = Record.readInt();
751
Richard Smith01a73372013-10-15 22:02:41 +0000752 // If this is a definition subject to the ODR, and we already have a
753 // definition, merge this one into it.
Erich Keanef92f31c2018-08-01 20:48:16 +0000754 if (ED->isCompleteDefinition() &&
Richard Smith01a73372013-10-15 22:02:41 +0000755 Reader.getContext().getLangOpts().Modules &&
756 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000757 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
758 if (!OldDef) {
759 // This is the first time we've seen an imported definition. Look for a
760 // local definition before deciding that we are the first definition.
761 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
762 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
763 OldDef = D;
764 break;
765 }
766 }
767 }
768 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000769 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
Erich Keanef92f31c2018-08-01 20:48:16 +0000770 ED->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +0000771 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000772 if (OldDef->getODRHash() != ED->getODRHash())
773 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000774 } else {
775 OldDef = ED;
776 }
777 }
778
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000779 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
780 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000781 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000782 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
783 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
784 }
Chris Lattner487412d2009-04-27 05:27:42 +0000785}
786
Richard Smith1d209d02013-05-23 01:49:11 +0000787ASTDeclReader::RedeclarableResult
788ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
789 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000790 RD->setHasFlexibleArrayMember(Record.readInt());
791 RD->setAnonymousStructOrUnion(Record.readInt());
792 RD->setHasObjectMember(Record.readInt());
793 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000794 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
795 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
796 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanaka09051062019-09-07 00:34:43 +0000797 RD->setHasNonTrivialToPrimitiveDefaultInitializeCUnion(Record.readInt());
798 RD->setHasNonTrivialToPrimitiveDestructCUnion(Record.readInt());
799 RD->setHasNonTrivialToPrimitiveCopyCUnion(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000800 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000801 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000802 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000803}
804
Sebastian Redlb3298c32010-08-18 23:56:48 +0000805void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000806 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000807 // For function declarations, defer reading the type in case the function has
808 // a deduced return type that references an entity declared within the
809 // function.
810 if (isa<FunctionDecl>(VD))
811 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
812 else
813 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000814}
815
Sebastian Redlb3298c32010-08-18 23:56:48 +0000816void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000817 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000818 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000819 ECD->setInitExpr(Record.readExpr());
820 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000821 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000822}
823
Sebastian Redlb3298c32010-08-18 23:56:48 +0000824void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000825 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000826 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000827 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000828 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000829 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000830 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000831 }
Richard Smithc23d7342018-06-29 20:46:25 +0000832 QualType TSIType = Record.readType();
833 DD->setTypeSourceInfo(
834 TSIType.isNull() ? nullptr
835 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000836}
837
Sebastian Redlb3298c32010-08-18 23:56:48 +0000838void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000839 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000840 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000841
Richard Smith600adef2018-07-04 02:25:38 +0000842 // Attach a type to this function. Use the real type if possible, but fall
843 // back to the type as written if it involves a deduced return type.
844 if (FD->getTypeSourceInfo() &&
845 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
846 ->getReturnType()->getContainedAutoType()) {
847 // We'll set up the real type in Visit, once we've finished loading the
848 // function.
849 FD->setType(FD->getTypeSourceInfo()->getType());
Richard Smitha62d1982018-08-03 01:00:01 +0000850 Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
Richard Smith600adef2018-07-04 02:25:38 +0000851 } else {
852 FD->setType(Reader.GetType(DeferredTypeID));
Richard Smith600adef2018-07-04 02:25:38 +0000853 }
Richard Smitha62d1982018-08-03 01:00:01 +0000854 DeferredTypeID = 0;
Richard Smith600adef2018-07-04 02:25:38 +0000855
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000856 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000857 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000858
Douglas Gregorb2585692012-01-04 17:13:46 +0000859 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
860 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000861
Erich Keane9c665062018-08-01 21:02:40 +0000862 FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
863 FD->setInlineSpecified(Record.readInt());
864 FD->setImplicitlyInline(Record.readInt());
Erich Keane9c665062018-08-01 21:02:40 +0000865 FD->setVirtualAsWritten(Record.readInt());
866 FD->setPure(Record.readInt());
867 FD->setHasInheritedPrototype(Record.readInt());
868 FD->setHasWrittenPrototype(Record.readInt());
869 FD->setDeletedAsWritten(Record.readInt());
870 FD->setTrivial(Record.readInt());
871 FD->setTrivialForCall(Record.readInt());
872 FD->setDefaulted(Record.readInt());
873 FD->setExplicitlyDefaulted(Record.readInt());
874 FD->setHasImplicitReturnZero(Record.readInt());
Gauthier Harnisch796ed032019-06-14 08:56:20 +0000875 FD->setConstexprKind(static_cast<ConstexprSpecKind>(Record.readInt()));
Erich Keane9c665062018-08-01 21:02:40 +0000876 FD->setUsesSEHTry(Record.readInt());
877 FD->setHasSkippedBody(Record.readInt());
878 FD->setIsMultiVersion(Record.readInt());
879 FD->setLateTemplateParsed(Record.readInt());
880
881 FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000882 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000883
Richard Trieue6caa262017-12-23 00:41:01 +0000884 FD->ODRHash = Record.readInt();
Erich Keane9c665062018-08-01 21:02:40 +0000885 FD->setHasODRHash(true);
Richard Trieue6caa262017-12-23 00:41:01 +0000886
David L. Jonesbe1557a2016-12-21 00:17:49 +0000887 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000888 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000889 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000890 break;
891 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000892 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000893 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000894 break;
895 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000896 auto *InstFD = ReadDeclAs<FunctionDecl>();
897 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000898 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000899 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000900 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000901 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000902 break;
903 }
904 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000905 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
906 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000907
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000908 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000909 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000910 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000911
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000912 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000913 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000914 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000915 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000916 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000917 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000918 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000919 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000920 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000921
922 LAngleLoc = ReadSourceLocation();
923 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000924 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000925
926 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000927
Douglas Gregor4163aca2011-09-09 21:34:22 +0000928 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000929 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000930 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000931 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000932 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000933 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Richard Smithf19a8b02019-05-02 00:49:14 +0000934
935 MemberSpecializationInfo *MSInfo = nullptr;
936 if (Record.readInt()) {
937 auto *FD = ReadDeclAs<FunctionDecl>();
938 auto TSK = (TemplateSpecializationKind)Record.readInt();
939 SourceLocation POI = ReadSourceLocation();
940
941 MSInfo = new (C) MemberSpecializationInfo(FD, TSK);
942 MSInfo->setPointOfInstantiation(POI);
943 }
944
945 FunctionTemplateSpecializationInfo *FTInfo =
946 FunctionTemplateSpecializationInfo::Create(
947 C, FD, Template, TSK, TemplArgList,
948 HasTemplateArgumentsAsWritten ? &TemplArgsInfo : nullptr, POI,
949 MSInfo);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000950 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000951
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000952 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000953 // The template that contains the specializations set. It's not safe to
954 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000955 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000956 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
957 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
958 // FunctionTemplateSpecializationInfo's Profile().
959 // We avoid getASTContext because a decl in the parent hierarchy may
960 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000961 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000962 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000963 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000964 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000965 FunctionTemplateSpecializationInfo *ExistingInfo =
966 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000967 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000968 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
969 else {
970 assert(Reader.getContext().getLangOpts().Modules &&
971 "already deserialized this template specialization");
Richard Smithf19a8b02019-05-02 00:49:14 +0000972 mergeRedeclarable(FD, ExistingInfo->getFunction(), Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000973 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000974 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000975 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000976 }
977 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
978 // Templates.
979 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000980 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000981 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000982 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
983
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000984 // Templates args.
985 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000986 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000987 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000988 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000989 TemplArgs.setLAngleLoc(ReadSourceLocation());
990 TemplArgs.setRAngleLoc(ReadSourceLocation());
991
Douglas Gregor4163aca2011-09-09 21:34:22 +0000992 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000993 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000994 // These are not merged; we don't need to merge redeclarations of dependent
995 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000996 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000997 }
998 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000999
Chris Lattnerca025db2010-05-07 21:43:38 +00001000 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001001 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001002 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001003 Params.reserve(NumParams);
1004 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001005 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001006 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +00001007}
1008
Sebastian Redlb3298c32010-08-18 23:56:48 +00001009void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001010 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001011 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00001012 // Load the body on-demand. Most clients won't care, because method
1013 // definitions rarely show up in headers.
1014 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1015 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001016 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1017 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001018 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001019 MD->setInstanceMethod(Record.readInt());
1020 MD->setVariadic(Record.readInt());
1021 MD->setPropertyAccessor(Record.readInt());
1022 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001023 MD->setOverriding(Record.readInt());
1024 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001025
Erich Keane9b18eca2018-08-01 21:31:08 +00001026 MD->setIsRedeclaration(Record.readInt());
1027 MD->setHasRedeclaration(Record.readInt());
1028 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001029 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001030 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001031
David L. Jonesbe1557a2016-12-21 00:17:49 +00001032 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1033 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001034 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001035 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001036 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1037 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001038 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001039 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001040 Params.reserve(NumParams);
1041 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001042 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001043
Erich Keane9b18eca2018-08-01 21:31:08 +00001044 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001045 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001046 SmallVector<SourceLocation, 16> SelLocs;
1047 SelLocs.reserve(NumStoredSelLocs);
1048 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001049 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001050
1051 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001052}
1053
Douglas Gregor85f3f952015-07-07 03:57:15 +00001054void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001055 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001056
David L. Jonesbe1557a2016-12-21 00:17:49 +00001057 D->Variance = Record.readInt();
1058 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001059 D->VarianceLoc = ReadSourceLocation();
1060 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001061}
1062
Sebastian Redlb3298c32010-08-18 23:56:48 +00001063void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001064 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001065 CD->setAtStartLoc(ReadSourceLocation());
1066 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001067}
1068
Douglas Gregor85f3f952015-07-07 03:57:15 +00001069ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001070 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001071 if (numParams == 0)
1072 return nullptr;
1073
1074 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1075 typeParams.reserve(numParams);
1076 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001077 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001078 if (!typeParam)
1079 return nullptr;
1080
1081 typeParams.push_back(typeParam);
1082 }
1083
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001084 SourceLocation lAngleLoc = ReadSourceLocation();
1085 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001086
1087 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1088 typeParams, rAngleLoc);
1089}
1090
Manman Renec315f12016-09-09 23:48:27 +00001091void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001092 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001093 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001094 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001095
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001096 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001097 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001098
Manman Renec315f12016-09-09 23:48:27 +00001099 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001100 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001101 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1102 Protocols.reserve(NumProtocols);
1103 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001104 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001105 SmallVector<SourceLocation, 16> ProtoLocs;
1106 ProtoLocs.reserve(NumProtocols);
1107 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001108 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001109 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1110 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001111
Manman Renec315f12016-09-09 23:48:27 +00001112 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001113 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001114 Protocols.clear();
1115 Protocols.reserve(NumProtocols);
1116 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001117 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001118 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1119 Reader.getContext());
1120}
1121
1122void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1123 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1124 // FIXME: odr checking?
1125}
1126
Sebastian Redlb3298c32010-08-18 23:56:48 +00001127void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001128 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001129 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001130 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001131 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001132
1133 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001134 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001135 // Read the definition.
1136 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001137
David L. Jonesbe1557a2016-12-21 00:17:49 +00001138 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001139 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1140 if (Canon->Data.getPointer()) {
1141 // If we already have a definition, keep the definition invariant and
1142 // merge the data.
1143 MergeDefinitionData(Canon, std::move(ID->data()));
1144 ID->Data = Canon->Data;
1145 } else {
1146 // Set the definition data of the canonical declaration, so other
1147 // redeclarations will see it.
1148 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001149
Manman Renec315f12016-09-09 23:48:27 +00001150 // We will rebuild this list lazily.
1151 ID->setIvarList(nullptr);
1152 }
Craig Toppera13603a2014-05-22 05:54:18 +00001153
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001154 // Note that we have deserialized a definition.
1155 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001156
Douglas Gregor404cdde2012-01-27 01:47:08 +00001157 // Note that we've loaded this Objective-C class.
1158 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001159 } else {
1160 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001161 }
Chris Lattner487412d2009-04-27 05:27:42 +00001162}
1163
Sebastian Redlb3298c32010-08-18 23:56:48 +00001164void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001165 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001166 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001167 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001168 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001169 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001170 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001171}
1172
Graydon Hoaree0a68352017-06-28 18:36:27 +00001173void ASTDeclReader::ReadObjCDefinitionData(
1174 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001175 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001176 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1177 ProtoRefs.reserve(NumProtoRefs);
1178 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001179 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001180 SmallVector<SourceLocation, 16> ProtoLocs;
1181 ProtoLocs.reserve(NumProtoRefs);
1182 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001183 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001184 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1185 ProtoLocs.data(), Reader.getContext());
1186}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001187
Graydon Hoaree0a68352017-06-28 18:36:27 +00001188void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1189 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1190 // FIXME: odr checking?
1191}
1192
1193void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1194 RedeclarableResult Redecl = VisitRedeclarable(PD);
1195 VisitObjCContainerDecl(PD);
1196 mergeRedeclarable(PD, Redecl);
1197
1198 if (Record.readInt()) {
1199 // Read the definition.
1200 PD->allocateDefinitionData();
1201
1202 ReadObjCDefinitionData(PD->data());
1203
1204 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1205 if (Canon->Data.getPointer()) {
1206 // If we already have a definition, keep the definition invariant and
1207 // merge the data.
1208 MergeDefinitionData(Canon, std::move(PD->data()));
1209 PD->Data = Canon->Data;
1210 } else {
1211 // Set the definition data of the canonical declaration, so other
1212 // redeclarations will see it.
1213 PD->getCanonicalDecl()->Data = PD->Data;
1214 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001215 // Note that we have deserialized a definition.
1216 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001217 } else {
1218 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001219 }
Chris Lattner487412d2009-04-27 05:27:42 +00001220}
1221
Sebastian Redlb3298c32010-08-18 23:56:48 +00001222void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001223 VisitFieldDecl(FD);
1224}
1225
Sebastian Redlb3298c32010-08-18 23:56:48 +00001226void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001227 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001228 CD->setCategoryNameLoc(ReadSourceLocation());
1229 CD->setIvarLBraceLoc(ReadSourceLocation());
1230 CD->setIvarRBraceLoc(ReadSourceLocation());
1231
Douglas Gregor404cdde2012-01-27 01:47:08 +00001232 // Note that this category has been deserialized. We do this before
1233 // deserializing the interface declaration, so that it will consider this
1234 /// category.
1235 Reader.CategoriesDeserialized.insert(CD);
1236
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001237 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001238 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001239 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001240 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001241 ProtoRefs.reserve(NumProtoRefs);
1242 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001243 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001244 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001245 ProtoLocs.reserve(NumProtoRefs);
1246 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001247 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001248 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001249 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001250
1251 // Protocols in the class extension belong to the class.
1252 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1253 CD->ClassInterface->mergeClassExtensionProtocolList(
1254 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1255 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001256}
1257
Sebastian Redlb3298c32010-08-18 23:56:48 +00001258void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001259 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001260 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001261}
1262
Sebastian Redlb3298c32010-08-18 23:56:48 +00001263void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001264 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001265 D->setAtLoc(ReadSourceLocation());
1266 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001267 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001268 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001269 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001270 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001271 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001272 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001273 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001274 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001275 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001276 DeclarationName GetterName = Record.readDeclarationName();
1277 SourceLocation GetterLoc = ReadSourceLocation();
1278 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1279 DeclarationName SetterName = Record.readDeclarationName();
1280 SourceLocation SetterLoc = ReadSourceLocation();
1281 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001282 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1283 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1284 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001285}
1286
Sebastian Redlb3298c32010-08-18 23:56:48 +00001287void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001288 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001289 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001290}
1291
Sebastian Redlb3298c32010-08-18 23:56:48 +00001292void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001293 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001294 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001295}
1296
Sebastian Redlb3298c32010-08-18 23:56:48 +00001297void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001298 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001299 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1300 D->SuperLoc = ReadSourceLocation();
1301 D->setIvarLBraceLoc(ReadSourceLocation());
1302 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001303 D->setHasNonZeroConstructors(Record.readInt());
1304 D->setHasDestructors(Record.readInt());
1305 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001306 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001307 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001308}
1309
Sebastian Redlb3298c32010-08-18 23:56:48 +00001310void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001311 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001312 D->setAtLoc(ReadSourceLocation());
1313 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1314 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1315 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001316 D->setGetterCXXConstructor(Record.readExpr());
1317 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001318}
1319
Sebastian Redlb3298c32010-08-18 23:56:48 +00001320void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001321 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001322 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001323
1324 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1325 FD->InitStorage.setInt(ISK);
1326 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1327 ? Record.readType().getAsOpaquePtr()
1328 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001329 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001330
1331 if (auto *BW = Record.readExpr())
1332 FD->setBitWidth(BW);
1333
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001334 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001335 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001336 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001337 }
Richard Smith0b87e072013-10-07 08:02:11 +00001338 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001339}
1340
John McCall5e77d762013-04-16 07:28:30 +00001341void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1342 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001343 PD->GetterId = Record.getIdentifierInfo();
1344 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001345}
1346
Francois Pichet783dd6e2010-11-21 06:08:52 +00001347void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1348 VisitValueDecl(FD);
1349
David L. Jonesbe1557a2016-12-21 00:17:49 +00001350 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001351 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001352 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001353
1354 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001355 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001356
1357 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001358}
1359
Larisse Voufo39a1e502013-08-06 01:03:05 +00001360ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001361 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001362 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001363
David L. Jonesbe1557a2016-12-21 00:17:49 +00001364 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1365 VD->VarDeclBits.TSCSpec = Record.readInt();
1366 VD->VarDeclBits.InitStyle = Record.readInt();
Erik Pilkington1e368822019-01-04 18:33:06 +00001367 VD->VarDeclBits.ARCPseudoStrong = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001368 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001369 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1370 Record.readInt();
1371 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001372 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001373 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001374 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001375 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1376 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1377 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1378 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1379 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001380 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
Akira Hatanaka8e57b072018-10-01 21:51:28 +00001381 VD->NonParmVarDeclBits.EscapingByref = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001382 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001383 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001384 VD->setCachedLinkage(VarLinkage);
1385
1386 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001387 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001388 VD->getLexicalDeclContext()->isFunctionOrMethod())
1389 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001390
David L. Jonesbe1557a2016-12-21 00:17:49 +00001391 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001392 VD->setInit(Record.readExpr());
Richard Smith2b4fa532019-09-29 05:08:46 +00001393 if (Val > 1) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001394 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1395 Eval->CheckedICE = true;
Richard Smith2b4fa532019-09-29 05:08:46 +00001396 Eval->IsICE = (Val & 1) != 0;
1397 Eval->HasConstantDestruction = (Val & 4) != 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001398 }
1399 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001400
Akira Hatanaka9978da32018-08-10 15:09:24 +00001401 if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1402 Expr *CopyExpr = Record.readExpr();
1403 if (CopyExpr)
1404 Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1405 }
1406
Richard Smitha4653622017-09-06 20:01:14 +00001407 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1408 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1409
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001410 enum VarKind {
1411 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1412 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001413 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001414 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001415 // Only true variables (not parameters or implicit parameters) can be
1416 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001417 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1418 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001419 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001420 break;
1421 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001422 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001423 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001424 break;
1425 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001426 auto *Tmpl = ReadDeclAs<VarDecl>();
1427 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001428 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001429 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001430 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001431 break;
1432 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001433 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001434
1435 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001436}
1437
Sebastian Redlb3298c32010-08-18 23:56:48 +00001438void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001439 VisitVarDecl(PD);
1440}
1441
Sebastian Redlb3298c32010-08-18 23:56:48 +00001442void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001443 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001444 unsigned isObjCMethodParam = Record.readInt();
1445 unsigned scopeDepth = Record.readInt();
1446 unsigned scopeIndex = Record.readInt();
1447 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001448 if (isObjCMethodParam) {
1449 assert(scopeDepth == 0);
1450 PD->setObjCMethodScopeInfo(scopeIndex);
1451 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1452 } else {
1453 PD->setScopeInfo(scopeDepth, scopeIndex);
1454 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001455 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1456 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1457 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001458 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001459
1460 // FIXME: If this is a redeclaration of a function from another module, handle
1461 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001462}
1463
Richard Smith7b76d812016-08-12 02:21:25 +00001464void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1465 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001466 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith0353e5a2019-05-25 01:04:17 +00001467 for (unsigned I = 0; I != DD->NumBindings; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001468 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith0353e5a2019-05-25 01:04:17 +00001469 BDs[I]->setDecomposedDecl(DD);
1470 }
Richard Smith7b76d812016-08-12 02:21:25 +00001471}
1472
1473void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1474 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001475 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001476}
1477
Sebastian Redlb3298c32010-08-18 23:56:48 +00001478void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001479 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001480 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001481 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001482}
1483
Sebastian Redlb3298c32010-08-18 23:56:48 +00001484void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001485 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001486 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001487 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001488 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001489 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001490 Params.reserve(NumParams);
1491 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001492 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001493 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001494
David L. Jonesbe1557a2016-12-21 00:17:49 +00001495 BD->setIsVariadic(Record.readInt());
1496 BD->setBlockMissingReturnType(Record.readInt());
1497 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001498 BD->setDoesNotEscape(Record.readInt());
Akira Hatanakac5792aa2019-02-27 18:17:16 +00001499 BD->setCanAvoidCopyToHeap(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001500
David L. Jonesbe1557a2016-12-21 00:17:49 +00001501 bool capturesCXXThis = Record.readInt();
1502 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001503 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001504 captures.reserve(numCaptures);
1505 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001506 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001507 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001508 bool byRef = (flags & 1);
1509 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001510 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001511
1512 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1513 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001514 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001515}
1516
Ben Langmuirce914fc2013-05-03 19:20:19 +00001517void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1518 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001519 unsigned ContextParamPos = Record.readInt();
1520 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001521 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001522 for (unsigned I = 0; I < CD->NumParams; ++I) {
1523 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001524 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001525 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001526 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001527 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001528}
1529
Sebastian Redlb3298c32010-08-18 23:56:48 +00001530void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001531 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001532 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001533 D->setExternLoc(ReadSourceLocation());
1534 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001535}
1536
Richard Smith8df390f2016-09-08 23:14:54 +00001537void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1538 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001539 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001540}
1541
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001542void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1543 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001544 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001545}
1546
Sebastian Redlb3298c32010-08-18 23:56:48 +00001547void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001548 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001549 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001550 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001551 D->LocStart = ReadSourceLocation();
1552 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001553
Richard Smith15e32fd2015-03-17 02:23:11 +00001554 // Defer loading the anonymous namespace until we've finished merging
1555 // this namespace; loading it might load a later declaration of the
1556 // same namespace, and we have an invariant that older declarations
1557 // get merged before newer ones try to merge.
1558 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001559 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001560 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001561 } else {
1562 // Link this namespace back to the first declaration, which has already
1563 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001564 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001565 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001566
1567 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001568
1569 if (AnonNamespace) {
1570 // Each module has its own anonymous namespace, which is disjoint from
1571 // any other module's anonymous namespaces, so don't attach the anonymous
1572 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001573 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001574 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001575 D->setAnonymousNamespace(Anon);
1576 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001577}
1578
Sebastian Redlb3298c32010-08-18 23:56:48 +00001579void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001580 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001581 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001582 D->NamespaceLoc = ReadSourceLocation();
1583 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001584 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001585 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001586 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001587}
1588
Sebastian Redlb3298c32010-08-18 23:56:48 +00001589void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001590 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001591 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001592 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001593 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1594 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001595 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001596 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001597 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001598 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001599}
1600
Richard Smith151c4562016-12-20 21:35:28 +00001601void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1602 VisitNamedDecl(D);
1603 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001604 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001605 for (unsigned I = 0; I != D->NumExpansions; ++I)
1606 Expansions[I] = ReadDeclAs<NamedDecl>();
1607 mergeMergeable(D);
1608}
1609
Sebastian Redlb3298c32010-08-18 23:56:48 +00001610void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001611 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001612 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001613 D->Underlying = ReadDeclAs<NamedDecl>();
1614 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001615 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001616 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001617 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001618 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001619 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001620}
1621
Richard Smith5179eb72016-06-28 19:03:57 +00001622void ASTDeclReader::VisitConstructorUsingShadowDecl(
1623 ConstructorUsingShadowDecl *D) {
1624 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001625 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1626 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001627 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001628}
1629
Sebastian Redlb3298c32010-08-18 23:56:48 +00001630void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001631 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001632 D->UsingLoc = ReadSourceLocation();
1633 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001634 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001635 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1636 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001637}
1638
Sebastian Redlb3298c32010-08-18 23:56:48 +00001639void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001640 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001641 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001642 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001643 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001644 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001645 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001646}
1647
Sebastian Redlb3298c32010-08-18 23:56:48 +00001648void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001649 UnresolvedUsingTypenameDecl *D) {
1650 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001651 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001652 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001653 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001654 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001655}
1656
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001657void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001658 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001659 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001660 Data.UserDeclaredConstructor = Record.readInt();
1661 Data.UserDeclaredSpecialMembers = Record.readInt();
1662 Data.Aggregate = Record.readInt();
1663 Data.PlainOldData = Record.readInt();
1664 Data.Empty = Record.readInt();
1665 Data.Polymorphic = Record.readInt();
1666 Data.Abstract = Record.readInt();
1667 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001668 Data.IsCXX11StandardLayout = Record.readInt();
1669 Data.HasBasesWithFields = Record.readInt();
1670 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001671 Data.HasPrivateFields = Record.readInt();
1672 Data.HasProtectedFields = Record.readInt();
1673 Data.HasPublicFields = Record.readInt();
1674 Data.HasMutableFields = Record.readInt();
1675 Data.HasVariantMembers = Record.readInt();
1676 Data.HasOnlyCMembers = Record.readInt();
1677 Data.HasInClassInitializer = Record.readInt();
1678 Data.HasUninitializedReferenceMember = Record.readInt();
1679 Data.HasUninitializedFields = Record.readInt();
1680 Data.HasInheritedConstructor = Record.readInt();
1681 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001682 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001683 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1684 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1685 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001686 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001687 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1688 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1689 Data.DefaultedDestructorIsDeleted = Record.readInt();
1690 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001691 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001692 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001693 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001694 Data.HasIrrelevantDestructor = Record.readInt();
1695 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1696 Data.HasDefaultedDefaultConstructor = Record.readInt();
1697 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1698 Data.HasConstexprDefaultConstructor = Record.readInt();
1699 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1700 Data.ComputedVisibleConversions = Record.readInt();
1701 Data.UserProvidedDefaultConstructor = Record.readInt();
1702 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001703 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1704 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001705 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1706 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1707 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001708 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001709 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001710
Richard Smithcd4a7a42017-09-07 00:55:55 +00001711 if (Record.readInt())
1712 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001713
David L. Jonesbe1557a2016-12-21 00:17:49 +00001714 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001715 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001716 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001717 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001718 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001719 Data.VBases = ReadGlobalOffset();
1720
David L. Jonesb6a8f022016-12-21 04:34:52 +00001721 Record.readUnresolvedSet(Data.Conversions);
1722 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001723 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001724 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001725
Douglas Gregor99ae8062012-02-14 17:54:36 +00001726 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001727 using Capture = LambdaCapture;
1728
1729 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001730 Lambda.Dependent = Record.readInt();
1731 Lambda.IsGenericLambda = Record.readInt();
1732 Lambda.CaptureDefault = Record.readInt();
1733 Lambda.NumCaptures = Record.readInt();
1734 Lambda.NumExplicitCaptures = Record.readInt();
1735 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001736 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001737 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1738 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001739 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001740 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001741 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001742 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001743 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001744 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001745 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001746 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001747 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001748 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001749 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001750 break;
1751 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001752 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001753 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001754 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001755 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1756 break;
1757 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001758 }
1759 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001760}
1761
Richard Smithcd45dbc2014-04-19 03:48:30 +00001762void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001763 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001764 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001765 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001766 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001767
Richard Smith02793752015-03-27 21:16:39 +00001768 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001769 // Track that we merged the definitions.
1770 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1771 DD.Definition));
1772 Reader.PendingDefinitions.erase(MergeDD.Definition);
Erich Keanef92f31c2018-08-01 20:48:16 +00001773 MergeDD.Definition->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +00001774 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001775 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1776 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001777 }
1778
Richard Smith2a9e5c52015-02-03 03:32:14 +00001779 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1780 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1781 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001782 // We faked up this definition data because we found a class for which we'd
1783 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001784 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001785 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001786
1787 // Don't change which declaration is the definition; that is required
1788 // to be invariant once we select it.
1789 auto *Def = DD.Definition;
1790 DD = std::move(MergeDD);
1791 DD.Definition = Def;
1792 return;
1793 }
1794
Richard Smithcd45dbc2014-04-19 03:48:30 +00001795 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001796 bool DetectedOdrViolation = false;
1797#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1798#define MATCH_FIELD(Field) \
1799 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1800 OR_FIELD(Field)
1801 MATCH_FIELD(UserDeclaredConstructor)
1802 MATCH_FIELD(UserDeclaredSpecialMembers)
1803 MATCH_FIELD(Aggregate)
1804 MATCH_FIELD(PlainOldData)
1805 MATCH_FIELD(Empty)
1806 MATCH_FIELD(Polymorphic)
1807 MATCH_FIELD(Abstract)
1808 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001809 MATCH_FIELD(IsCXX11StandardLayout)
1810 MATCH_FIELD(HasBasesWithFields)
1811 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001812 MATCH_FIELD(HasPrivateFields)
1813 MATCH_FIELD(HasProtectedFields)
1814 MATCH_FIELD(HasPublicFields)
1815 MATCH_FIELD(HasMutableFields)
1816 MATCH_FIELD(HasVariantMembers)
1817 MATCH_FIELD(HasOnlyCMembers)
1818 MATCH_FIELD(HasInClassInitializer)
1819 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001820 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001821 MATCH_FIELD(HasInheritedConstructor)
1822 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001823 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001824 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1825 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1826 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001827 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001828 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1829 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1830 MATCH_FIELD(DefaultedDestructorIsDeleted)
1831 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001832 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001833 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001834 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001835 MATCH_FIELD(HasIrrelevantDestructor)
1836 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001837 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001838 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1839 OR_FIELD(HasConstexprDefaultConstructor)
1840 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1841 // ComputedVisibleConversions is handled below.
1842 MATCH_FIELD(UserProvidedDefaultConstructor)
1843 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001844 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1845 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001846 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1847 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1848 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1849 MATCH_FIELD(IsLambda)
1850#undef OR_FIELD
1851#undef MATCH_FIELD
1852
1853 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1854 DetectedOdrViolation = true;
1855 // FIXME: Issue a diagnostic if the base classes don't match when we come
1856 // to lazily load them.
1857
1858 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1859 // match when we come to lazily load them.
1860 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1861 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1862 DD.ComputedVisibleConversions = true;
1863 }
1864
1865 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1866 // lazily load it.
1867
1868 if (DD.IsLambda) {
1869 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1870 // when they occur within the body of a function template specialization).
1871 }
1872
Richard Trieufd1acbb2017-04-11 21:31:00 +00001873 if (D->getODRHash() != MergeDD.ODRHash) {
1874 DetectedOdrViolation = true;
1875 }
1876
Richard Smithcd45dbc2014-04-19 03:48:30 +00001877 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001878 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1879 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001880}
1881
Richard Smith2a9e5c52015-02-03 03:32:14 +00001882void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001883 struct CXXRecordDecl::DefinitionData *DD;
1884 ASTContext &C = Reader.getContext();
1885
1886 // Determine whether this is a lambda closure type, so that we can
1887 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001888 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001889 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001890 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001891 LCD_None);
1892 else
1893 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1894
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001895 CXXRecordDecl *Canon = D->getCanonicalDecl();
1896 // Set decl definition data before reading it, so that during deserialization
1897 // when we read CXXRecordDecl, it already has definition data and we don't
1898 // set fake one.
1899 if (!Canon->DefinitionData)
1900 Canon->DefinitionData = DD;
1901 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001902 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001903
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001904 // We might already have a different definition for this record. This can
1905 // happen either because we're reading an update record, or because we've
1906 // already done some merging. Either way, just merge into it.
1907 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001908 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001909 return;
1910 }
1911
Richard Smith97100e32015-06-20 00:22:34 +00001912 // Mark this declaration as being a definition.
Erich Keanef92f31c2018-08-01 20:48:16 +00001913 D->setCompleteDefinition(true);
Richard Smith2a9e5c52015-02-03 03:32:14 +00001914
Richard Smith97100e32015-06-20 00:22:34 +00001915 // If this is not the first declaration or is an update record, we can have
1916 // other redeclarations already. Make a note that we need to propagate the
1917 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001918 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001919 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001920}
1921
Richard Smith1d209d02013-05-23 01:49:11 +00001922ASTDeclReader::RedeclarableResult
1923ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1924 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001925
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001926 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001927
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001928 enum CXXRecKind {
1929 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1930 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001931 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001932 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001933 // Merged when we merge the folding set entry in the primary template.
1934 if (!isa<ClassTemplateSpecializationDecl>(D))
1935 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001936 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001937 case CXXRecTemplate: {
1938 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001939 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001940 D->TemplateOrInstantiation = Template;
1941 if (!Template->getTemplatedDecl()) {
1942 // We've not actually loaded the ClassTemplateDecl yet, because we're
1943 // currently being loaded as its pattern. Rely on it to set up our
1944 // TypeForDecl (see VisitClassTemplateDecl).
1945 //
1946 // Beware: we do not yet know our canonical declaration, and may still
1947 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001948 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001949 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001950 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001951 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001952 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001953 auto *RD = ReadDeclAs<CXXRecordDecl>();
1954 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001955 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001956 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1957 MSI->setPointOfInstantiation(POI);
1958 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001959 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001960 break;
1961 }
1962 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001963
David L. Jonesbe1557a2016-12-21 00:17:49 +00001964 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001965 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001966 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001967 else
1968 // Propagate DefinitionData pointer from the canonical declaration.
1969 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1970
Richard Smith676c4042013-08-29 23:59:27 +00001971 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001972 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001973 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001974 DeclID KeyFn = ReadDeclID();
Erich Keanef92f31c2018-08-01 20:48:16 +00001975 if (KeyFn && D->isCompleteDefinition())
Richard Smitha9a1c682014-07-07 06:38:20 +00001976 // FIXME: This is wrong for the ARM ABI, where some other module may have
1977 // made this function no longer be a key function. We need an update
1978 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001979 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001980 }
Richard Smith1d209d02013-05-23 01:49:11 +00001981
1982 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001983}
1984
Richard Smithbc491202017-02-17 20:05:37 +00001985void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
Richard Smith76b90272019-05-09 03:59:21 +00001986 D->setExplicitSpecifier(Record.readExplicitSpec());
Richard Smithbc491202017-02-17 20:05:37 +00001987 VisitFunctionDecl(D);
Erich Keane9c665062018-08-01 21:02:40 +00001988 D->setIsCopyDeductionCandidate(Record.readInt());
Richard Smithbc491202017-02-17 20:05:37 +00001989}
1990
Sebastian Redlb3298c32010-08-18 23:56:48 +00001991void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001992 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001993
David L. Jonesbe1557a2016-12-21 00:17:49 +00001994 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001995 if (D->isCanonicalDecl()) {
1996 while (NumOverridenMethods--) {
1997 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1998 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001999 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00002000 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
2001 }
2002 } else {
2003 // We don't care about which declarations this used to override; we get
2004 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002005 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002006 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002007}
2008
Sebastian Redlb3298c32010-08-18 23:56:48 +00002009void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00002010 // We need the inherited constructor information to merge the declaration,
2011 // so we have to read it before we call VisitCXXMethodDecl.
Richard Smith76b90272019-05-09 03:59:21 +00002012 D->setExplicitSpecifier(Record.readExplicitSpec());
Richard Smith5179eb72016-06-28 19:03:57 +00002013 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002014 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
2015 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00002016 *D->getTrailingObjects<InheritedConstructor>() =
2017 InheritedConstructor(Shadow, Ctor);
2018 }
2019
Chris Lattnerca025db2010-05-07 21:43:38 +00002020 VisitCXXMethodDecl(D);
2021}
2022
Sebastian Redlb3298c32010-08-18 23:56:48 +00002023void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002024 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00002025
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002026 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002027 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00002028 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00002029 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00002030 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00002031 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00002032 Canon->OperatorDeleteThisArg = ThisArg;
2033 }
Richard Smithf8134002015-03-10 01:41:22 +00002034 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002035}
2036
Sebastian Redlb3298c32010-08-18 23:56:48 +00002037void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Richard Smith76b90272019-05-09 03:59:21 +00002038 D->setExplicitSpecifier(Record.readExplicitSpec());
Chris Lattnerca025db2010-05-07 21:43:38 +00002039 VisitCXXMethodDecl(D);
2040}
2041
Douglas Gregorba345522011-12-02 23:23:56 +00002042void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2043 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002044 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002045 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002046 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002047 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002048 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002049 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002050}
2051
Sebastian Redlb3298c32010-08-18 23:56:48 +00002052void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002053 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002054 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002055}
2056
Sebastian Redlb3298c32010-08-18 23:56:48 +00002057void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002058 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002059 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002060 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002061 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002062 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002063 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002064 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002065 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002066 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002067 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002068 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002069}
2070
Sebastian Redlb3298c32010-08-18 23:56:48 +00002071void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002072 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002073 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002074 D->NumParams = NumParams;
2075 D->Params = new TemplateParameterList*[NumParams];
2076 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002077 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002078 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002079 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002080 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002081 D->Friend = GetTypeSourceInfo();
2082 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002083}
2084
Richard Smithf17fdbd2014-04-24 02:25:27 +00002085DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002086 VisitNamedDecl(D);
2087
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002088 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002089 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002090 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002091 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002092 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002093
Richard Smithf17fdbd2014-04-24 02:25:27 +00002094 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002095}
2096
Saar Razd7aae332019-07-10 21:25:49 +00002097void ASTDeclReader::VisitConceptDecl(ConceptDecl *D) {
2098 VisitTemplateDecl(D);
2099 D->ConstraintExpr = Record.readExpr();
2100 mergeMergeable(D);
2101}
2102
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002103ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002104ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002105 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002106
Douglas Gregor68444de2012-01-14 15:13:49 +00002107 // Make sure we've allocated the Common pointer first. We do this before
2108 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2109 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2110 if (!CanonD->Common) {
2111 CanonD->Common = CanonD->newCommon(Reader.getContext());
2112 Reader.PendingDefinitions.insert(CanonD);
2113 }
2114 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002115
Douglas Gregor68444de2012-01-14 15:13:49 +00002116 // If this is the first declaration of the template, fill in the information
2117 // for the 'common' pointer.
2118 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002119 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002120 assert(RTD->getKind() == D->getKind() &&
2121 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002122 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002123 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002124 D->setMemberSpecialization();
2125 }
2126 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002127
Richard Smithf17fdbd2014-04-24 02:25:27 +00002128 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002129 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002130
Richard Smithf17fdbd2014-04-24 02:25:27 +00002131 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002132
Richard Smithd46d6de2013-10-13 23:50:45 +00002133 // If we merged the template with a prior declaration chain, merge the common
2134 // pointer.
2135 // FIXME: Actually merge here, don't just overwrite.
2136 D->Common = D->getCanonicalDecl()->Common;
2137
Douglas Gregor68444de2012-01-14 15:13:49 +00002138 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002139}
2140
Sebastian Redlb3298c32010-08-18 23:56:48 +00002141void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002142 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002143
Douglas Gregor68444de2012-01-14 15:13:49 +00002144 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002145 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2146 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002147 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002148 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002149 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002150 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002151
2152 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2153 // We were loaded before our templated declaration was. We've not set up
2154 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2155 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002156 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002157 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002158 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002159}
2160
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002161void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2162 llvm_unreachable("BuiltinTemplates are not serialized");
2163}
2164
Larisse Voufo30616382013-08-23 22:21:36 +00002165/// TODO: Unify with ClassTemplateDecl version?
2166/// May require unifying ClassTemplateDecl and
2167/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002168void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2169 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2170
2171 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002172 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002173 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002174 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002175 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002176 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002177 }
2178}
2179
Richard Smith1d209d02013-05-23 01:49:11 +00002180ASTDeclReader::RedeclarableResult
2181ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2182 ClassTemplateSpecializationDecl *D) {
2183 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002184
Douglas Gregor4163aca2011-09-09 21:34:22 +00002185 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002186 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002187 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002188 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002189 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002190 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002191 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002192 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002193 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002194 auto *PS =
2195 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002196 SpecializedPartialSpecialization();
2197 PS->PartialSpecialization
2198 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2199 PS->TemplateArgs = ArgList;
2200 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002201 }
2202 }
2203
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002204 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002205 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002206 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002207 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002208 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002209
David L. Jonesbe1557a2016-12-21 00:17:49 +00002210 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002211 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002212 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002213 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002214 // Set this as, or find, the canonical declaration for this specialization
2215 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002216 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002217 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002218 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002219 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002220 CanonSpec =
2221 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2222 }
2223 // If there was already a canonical specialization, merge into it.
2224 if (CanonSpec != D) {
2225 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2226
2227 // This declaration might be a definition. Merge with any existing
2228 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002229 if (auto *DDD = D->DefinitionData) {
2230 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002231 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002232 else
Richard Smith053f6c62014-05-16 23:01:30 +00002233 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002234 }
Richard Smith547864d2014-07-11 18:22:58 +00002235 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002236 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002237 }
2238 }
Richard Smith1d209d02013-05-23 01:49:11 +00002239
Richard Smithd55889a2013-09-09 16:55:27 +00002240 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002241 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002242 auto *ExplicitInfo =
2243 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002244 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002245 ExplicitInfo->ExternLoc = ReadSourceLocation();
2246 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002247 D->ExplicitInfo = ExplicitInfo;
2248 }
2249
Richard Smith1d209d02013-05-23 01:49:11 +00002250 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002251}
2252
Sebastian Redlb3298c32010-08-18 23:56:48 +00002253void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002254 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002255 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002256
David L. Jonesb6a8f022016-12-21 04:34:52 +00002257 D->TemplateParams = Record.readTemplateParameterList();
2258 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002259
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002260 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002261 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002262 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002263 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002264 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002265 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002266}
2267
Sebastian Redlb7448632011-08-31 13:59:56 +00002268void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2269 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002270 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002271 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Richard Smithf19a8b02019-05-02 00:49:14 +00002272 if (Record.readInt())
2273 D->TemplateArgs = Record.readASTTemplateArgumentListInfo();
Francois Pichet09af8c32011-08-17 01:06:54 +00002274}
2275
Sebastian Redlb3298c32010-08-18 23:56:48 +00002276void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002277 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002278
Douglas Gregor68444de2012-01-14 15:13:49 +00002279 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002280 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002281 SmallVector<serialization::DeclID, 32> SpecIDs;
2282 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002283 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002284 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002285}
2286
Larisse Voufo30616382013-08-23 22:21:36 +00002287/// TODO: Unify with ClassTemplateSpecializationDecl version?
2288/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2289/// VarTemplate(Partial)SpecializationDecl with a new data
2290/// structure Template(Partial)SpecializationDecl, and
2291/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002292ASTDeclReader::RedeclarableResult
2293ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2294 VarTemplateSpecializationDecl *D) {
2295 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2296
2297 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002298 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002299 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002300 D->SpecializedTemplate = VTD;
2301 } else {
2302 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002303 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002304 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002305 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002306 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002307 new (C)
2308 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2309 PS->PartialSpecialization =
2310 cast<VarTemplatePartialSpecializationDecl>(InstD);
2311 PS->TemplateArgs = ArgList;
2312 D->SpecializedTemplate = PS;
2313 }
2314 }
2315
2316 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002317 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002318 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002319 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2320 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002321 ExplicitInfo->ExternLoc = ReadSourceLocation();
2322 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002323 D->ExplicitInfo = ExplicitInfo;
2324 }
2325
2326 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002327 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002328 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002329 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002330 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002331 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002332
David L. Jonesbe1557a2016-12-21 00:17:49 +00002333 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002334 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002335 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002336 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002337 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002338 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002339 CanonPattern->getCommonPtr()->PartialSpecializations
2340 .GetOrInsertNode(Partial);
2341 } else {
2342 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2343 }
2344 }
2345 }
2346
2347 return Redecl;
2348}
2349
Larisse Voufo30616382013-08-23 22:21:36 +00002350/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2351/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2352/// VarTemplate(Partial)SpecializationDecl with a new data
2353/// structure Template(Partial)SpecializationDecl, and
2354/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002355void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2356 VarTemplatePartialSpecializationDecl *D) {
2357 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2358
David L. Jonesb6a8f022016-12-21 04:34:52 +00002359 D->TemplateParams = Record.readTemplateParameterList();
2360 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002361
2362 // These are read/set from/to the first declaration.
2363 if (ThisDeclID == Redecl.getFirstID()) {
2364 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002365 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002366 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002367 }
2368}
2369
Sebastian Redlb3298c32010-08-18 23:56:48 +00002370void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002371 VisitTypeDecl(D);
2372
David L. Jonesbe1557a2016-12-21 00:17:49 +00002373 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002374
David L. Jonesbe1557a2016-12-21 00:17:49 +00002375 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002376 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002377}
2378
Sebastian Redlb3298c32010-08-18 23:56:48 +00002379void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002380 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002381 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002382 D->setDepth(Record.readInt());
2383 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002384 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002385 auto TypesAndInfos =
2386 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002387 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002388 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002389 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002390 }
2391 } else {
2392 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002393 D->ParameterPack = Record.readInt();
2394 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002395 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002396 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002397}
2398
Sebastian Redlb3298c32010-08-18 23:56:48 +00002399void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002400 VisitTemplateDecl(D);
2401 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002402 D->setDepth(Record.readInt());
2403 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002404 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002405 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002406 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2407 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002408 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002409 } else {
2410 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002411 D->ParameterPack = Record.readInt();
2412 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002413 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002414 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002415 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002416}
2417
Richard Smith3f1b5d02011-05-05 21:57:07 +00002418void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2419 VisitRedeclarableTemplateDecl(D);
2420}
2421
Sebastian Redlb3298c32010-08-18 23:56:48 +00002422void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002423 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002424 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002425 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002426 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002427 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002428}
2429
Michael Han84324352013-02-22 17:15:32 +00002430void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2431 VisitDecl(D);
2432}
2433
Mike Stump11289f42009-09-09 15:08:12 +00002434std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002435ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002436 uint64_t LexicalOffset = ReadLocalOffset();
2437 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002438 return std::make_pair(LexicalOffset, VisibleOffset);
2439}
2440
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002441template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002442ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002443ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002444 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002445 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002446
Richard Smith5fc18a92015-07-12 23:43:21 +00002447 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002448 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002449
Richard Smithd61d4ac2015-08-22 20:13:39 +00002450 uint64_t RedeclOffset = 0;
2451
Douglas Gregor358cd442012-01-15 16:58:34 +00002452 // 0 indicates that this declaration was the only declaration of its entity,
2453 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002454 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002455 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002456 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002457 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002458 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002459 // This declaration was the first local declaration, but may have imported
2460 // other declarations.
2461 IsKeyDecl = N == 1;
2462 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002463
Richard Smithfe620d22015-03-05 23:24:12 +00002464 // We have some declarations that must be before us in our redeclaration
2465 // chain. Read them now, and remember that we ought to merge with one of
2466 // them.
2467 // FIXME: Provide a known merge target to the second and subsequent such
2468 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002469 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002470 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002471
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002472 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002473 } else {
2474 // This declaration was not the first local declaration. Read the first
2475 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002476 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002477 }
2478
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002479 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002480 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002481 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2482 // We temporarily set the first (canonical) declaration as the previous one
2483 // which is the one that matters and mark the real previous DeclID to be
2484 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002485 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002486 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002487 }
Richard Smithd8a83712015-08-22 01:47:18 +00002488
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002489 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002490
2491 // Note that we need to load local redeclarations of this decl and build a
2492 // decl chain for them. This must happen *after* we perform the preloading
2493 // above; this ensures that the redeclaration chain is built in the correct
2494 // order.
2495 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002496 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002497
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002498 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002499}
2500
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002501/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002502/// of the same entity.
2503template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002504void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002505 RedeclarableResult &Redecl,
2506 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002507 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002508 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002509 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002510
Richard Smith202850a2015-03-10 02:57:50 +00002511 // If we're not the canonical declaration, we don't need to merge.
2512 if (!DBase->isFirstDecl())
2513 return;
2514
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002515 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002516
Richard Smith5a4737c2015-02-06 02:42:59 +00002517 if (auto *Existing = Redecl.getKnownMergeTarget())
2518 // We already know of an existing declaration we should merge with.
2519 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2520 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002521 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002522 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2523}
2524
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002525/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002526/// We use this to put code in a template that will only be valid for certain
2527/// instantiations.
2528template<typename T> static T assert_cast(T t) { return t; }
2529template<typename T> static T assert_cast(...) {
2530 llvm_unreachable("bad assert_cast");
2531}
2532
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002533/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002534/// declarations.
2535void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2536 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002537 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002538 auto *DPattern = D->getTemplatedDecl();
2539 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002540 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2541 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002542 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002543
Richard Smith547864d2014-07-11 18:22:58 +00002544 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2545 // Merge with any existing definition.
2546 // FIXME: This is duplicated in several places. Refactor.
2547 auto *ExistingClass =
2548 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002549 if (auto *DDD = DClass->DefinitionData) {
2550 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002551 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002552 } else {
2553 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002554 // We may have skipped this before because we thought that DClass
2555 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002556 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002557 }
2558 }
2559 DClass->DefinitionData = ExistingClass->DefinitionData;
2560
Richard Smithf17fdbd2014-04-24 02:25:27 +00002561 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2562 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002563 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002564 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2565 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2566 Result);
2567 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2568 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002569 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2570 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2571 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002572 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002573}
2574
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002575/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002576/// of the same entity.
2577template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002578void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2579 RedeclarableResult &Redecl,
2580 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002581 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002582 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002583 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002584 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002585 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2586 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002587
Richard Smithd55889a2013-09-09 16:55:27 +00002588 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002589 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002590 // appropriate canonical declaration.
2591 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002592 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002593 ExistingCanon->Used |= D->Used;
2594 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002595
2596 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002597 // We cannot have loaded any redeclarations of this declaration yet, so
2598 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002599 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002600 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002601 assert_cast<NamespaceDecl*>(ExistingCanon));
2602
2603 // When we merge a template, merge its pattern.
2604 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2605 mergeTemplatePattern(
2606 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002607 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002608
Richard Smith5fc18a92015-07-12 23:43:21 +00002609 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002610 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002611 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002612 }
2613}
2614
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002615/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2616/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2617/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2618/// that some types are mergeable during deserialization, otherwise name
2619/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002620static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002621 if (!ND)
2622 return false;
2623 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002624 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002625 return true;
2626 return false;
2627}
2628
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002629/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002630/// of the same entity, for the case where the entity is not actually
2631/// redeclarable. This happens, for instance, when merging the fields of
2632/// identical class definitions from two different modules.
2633template<typename T>
2634void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2635 // If modules are not available, there is no reason to perform this merge.
2636 if (!Reader.getContext().getLangOpts().Modules)
2637 return;
2638
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002639 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2640 // Note that C identically-named things in different translation units are
2641 // not redeclarations, but may still have compatible types, where ODR-like
2642 // semantics may apply.
2643 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2644 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002645 return;
2646
Richard Smith0b87e072013-10-07 08:02:11 +00002647 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2648 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002649 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2650 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002651}
2652
Alexey Bataeva769e072013-03-22 06:34:35 +00002653void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2654 VisitDecl(D);
2655 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002656 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002657 Vars.reserve(NumVars);
2658 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002659 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002660 }
2661 D->setVars(Vars);
2662}
2663
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002664void ASTDeclReader::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
2665 VisitDecl(D);
2666 unsigned NumVars = D->varlist_size();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002667 unsigned NumClauses = D->clauselist_size();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002668 SmallVector<Expr *, 16> Vars;
2669 Vars.reserve(NumVars);
2670 for (unsigned i = 0; i != NumVars; ++i) {
2671 Vars.push_back(Record.readExpr());
2672 }
2673 D->setVars(Vars);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002674 SmallVector<OMPClause *, 8> Clauses;
2675 Clauses.reserve(NumClauses);
2676 OMPClauseReader ClauseReader(Record);
2677 for (unsigned I = 0; I != NumClauses; ++I)
2678 Clauses.push_back(ClauseReader.readClause());
2679 D->setClauses(Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002680}
2681
Kelvin Li1408f912018-09-26 04:28:39 +00002682void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
2683 VisitDecl(D);
2684 unsigned NumClauses = D->clauselist_size();
2685 SmallVector<OMPClause *, 8> Clauses;
2686 Clauses.reserve(NumClauses);
2687 OMPClauseReader ClauseReader(Record);
2688 for (unsigned I = 0; I != NumClauses; ++I)
2689 Clauses.push_back(ClauseReader.readClause());
2690 D->setClauses(Clauses);
2691}
2692
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002693void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002694 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002695 D->setLocation(ReadSourceLocation());
Alexey Bataeve6aa4692018-09-13 16:54:05 +00002696 Expr *In = Record.readExpr();
2697 Expr *Out = Record.readExpr();
2698 D->setCombinerData(In, Out);
2699 Expr *Combiner = Record.readExpr();
2700 D->setCombiner(Combiner);
2701 Expr *Orig = Record.readExpr();
2702 Expr *Priv = Record.readExpr();
2703 D->setInitializerData(Orig, Priv);
2704 Expr *Init = Record.readExpr();
2705 auto IK = static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt());
2706 D->setInitializer(Init, IK);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002707 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002708}
2709
Michael Kruse251e1482019-02-01 20:25:04 +00002710void ASTDeclReader::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
2711 VisitValueDecl(D);
2712 D->setLocation(ReadSourceLocation());
2713 Expr *MapperVarRefE = Record.readExpr();
2714 D->setMapperVarRef(MapperVarRefE);
2715 D->VarName = Record.readDeclarationName();
2716 D->PrevDeclInScope = ReadDeclID();
2717 unsigned NumClauses = D->clauselist_size();
2718 SmallVector<OMPClause *, 8> Clauses;
2719 Clauses.reserve(NumClauses);
2720 OMPClauseReader ClauseReader(Record);
2721 for (unsigned I = 0; I != NumClauses; ++I)
2722 Clauses.push_back(ClauseReader.readClause());
2723 D->setClauses(Clauses);
2724}
2725
Alexey Bataev4244be22016-02-11 05:35:55 +00002726void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002727 VisitVarDecl(D);
2728}
2729
Chris Lattner487412d2009-04-27 05:27:42 +00002730//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002731// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002732//===----------------------------------------------------------------------===//
2733
Richard Smithe43e2b32018-08-20 21:47:29 +00002734namespace {
2735class AttrReader {
2736 ModuleFile *F;
2737 ASTReader *Reader;
2738 const ASTReader::RecordData &Record;
2739 unsigned &Idx;
2740
2741public:
2742 AttrReader(ModuleFile &F, ASTReader &Reader,
2743 const ASTReader::RecordData &Record, unsigned &Idx)
2744 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
2745
2746 const uint64_t &readInt() { return Record[Idx++]; }
2747
2748 SourceRange readSourceRange() {
2749 return Reader->ReadSourceRange(*F, Record, Idx);
2750 }
2751
Erich Keane6a24e802019-09-13 17:39:31 +00002752 SourceLocation readSourceLocation() {
2753 return Reader->ReadSourceLocation(*F, Record, Idx);
2754 }
2755
Richard Smithe43e2b32018-08-20 21:47:29 +00002756 Expr *readExpr() { return Reader->ReadExpr(*F); }
2757
2758 std::string readString() {
2759 return Reader->ReadString(Record, Idx);
2760 }
2761
2762 TypeSourceInfo *getTypeSourceInfo() {
2763 return Reader->GetTypeSourceInfo(*F, Record, Idx);
2764 }
2765
2766 IdentifierInfo *getIdentifierInfo() {
2767 return Reader->GetIdentifierInfo(*F, Record, Idx);
2768 }
2769
2770 VersionTuple readVersionTuple() {
2771 return ASTReader::ReadVersionTuple(Record, Idx);
2772 }
2773
2774 template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
2775 return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
2776 }
2777};
2778}
2779
2780Attr *ASTReader::ReadAttr(ModuleFile &M, const RecordData &Rec,
2781 unsigned &Idx) {
2782 AttrReader Record(M, *this, Rec, Idx);
2783 auto V = Record.readInt();
2784 if (!V)
2785 return nullptr;
2786
2787 Attr *New = nullptr;
2788 // Kind is stored as a 1-based integer because 0 is used to indicate a null
2789 // Attr pointer.
2790 auto Kind = static_cast<attr::Kind>(V - 1);
Richard Smithe43e2b32018-08-20 21:47:29 +00002791 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002792
Erich Keane6a24e802019-09-13 17:39:31 +00002793 IdentifierInfo *AttrName = Record.getIdentifierInfo();
2794 IdentifierInfo *ScopeName = Record.getIdentifierInfo();
2795 SourceRange AttrRange = Record.readSourceRange();
2796 SourceLocation ScopeLoc = Record.readSourceLocation();
2797 unsigned ParsedKind = Record.readInt();
2798 unsigned Syntax = Record.readInt();
2799 unsigned SpellingIndex = Record.readInt();
2800
2801 AttributeCommonInfo Info(AttrName, ScopeName, AttrRange, ScopeLoc,
2802 AttributeCommonInfo::Kind(ParsedKind),
2803 AttributeCommonInfo::Syntax(Syntax), SpellingIndex);
2804
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002805#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002806
Richard Smithe43e2b32018-08-20 21:47:29 +00002807 assert(New && "Unable to decode attribute?");
2808 return New;
2809}
2810
2811/// Reads attributes from the current stream position.
2812void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2813 for (unsigned I = 0, E = Record.readInt(); I != E; ++I)
2814 Attrs.push_back(Record.readAttr());
Chris Lattner8f63ab52009-04-27 06:01:06 +00002815}
2816
2817//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002818// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002819//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002820
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002821/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002822/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002823///
Chris Lattner487412d2009-04-27 05:27:42 +00002824/// This routine notes that this declaration has already been loaded,
2825/// so that future GetDecl calls will return this declaration rather
2826/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002827inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002828 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2829 DeclsLoaded[Index] = D;
2830}
2831
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002832/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002833/// this declaration (via HandleTopLevelDecl).
2834///
2835/// This routine should return true for anything that might affect
2836/// code generation, e.g., inline function definitions, Objective-C
2837/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002838static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002839 // An ObjCMethodDecl is never considered as "interesting" because its
2840 // implementation container always is.
2841
Richard Smithcd4a7a42017-09-07 00:55:55 +00002842 // An ImportDecl or VarDecl imported from a module map module will get
2843 // emitted when we import the relevant module.
Richard Smith520a37f2019-02-05 23:37:13 +00002844 if (isPartOfPerModuleInitializer(D)) {
Richard Smithcd4a7a42017-09-07 00:55:55 +00002845 auto *M = D->getImportedOwningModule();
2846 if (M && M->Kind == Module::ModuleMapModule &&
2847 Ctx.DeclMustBeEmitted(D))
2848 return false;
2849 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002850
Fangrui Song6907ce22018-07-30 19:24:48 +00002851 if (isa<FileScopeAsmDecl>(D) ||
2852 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002853 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002854 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002855 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002856 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002857 return true;
Michael Kruse251e1482019-02-01 20:25:04 +00002858 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D) ||
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002859 isa<OMPDeclareMapperDecl>(D) || isa<OMPAllocateDecl>(D))
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002860 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002861 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002862 return Var->isFileVarDecl() &&
Alexey Bataevd01b7492018-08-15 19:45:12 +00002863 (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
2864 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002865 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002866 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002867
2868 if (auto *ES = D->getASTContext().getExternalSource())
2869 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2870 return true;
2871
Douglas Gregor87d81242011-09-10 00:22:34 +00002872 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002873}
2874
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002875/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002876ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002877ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002878 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2879 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002880 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002881 const DeclOffset &DOffs =
2882 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002883 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002884 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002885}
2886
Douglas Gregord32f0352011-07-22 06:10:01 +00002887ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002888 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002889
2890 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2891 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2892}
2893
Douglas Gregorde3ef502011-11-30 23:21:26 +00002894uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002895 return LocalOffset + M.GlobalBitOffset;
2896}
2897
Richard Smithbf78e642013-06-24 22:51:00 +00002898static bool isSameTemplateParameterList(const TemplateParameterList *X,
2899 const TemplateParameterList *Y);
2900
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002901/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002902/// that they may be used in declarations of the same template.
2903static bool isSameTemplateParameter(const NamedDecl *X,
2904 const NamedDecl *Y) {
2905 if (X->getKind() != Y->getKind())
2906 return false;
2907
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002908 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2909 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002910 return TX->isParameterPack() == TY->isParameterPack();
2911 }
2912
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002913 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2914 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002915 return TX->isParameterPack() == TY->isParameterPack() &&
2916 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2917 }
2918
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002919 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2920 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002921 return TX->isParameterPack() == TY->isParameterPack() &&
2922 isSameTemplateParameterList(TX->getTemplateParameters(),
2923 TY->getTemplateParameters());
2924}
2925
Richard Smith32952e12014-10-14 02:00:47 +00002926static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2927 if (auto *NS = X->getAsNamespace())
2928 return NS;
2929 if (auto *NAS = X->getAsNamespaceAlias())
2930 return NAS->getNamespace();
2931 return nullptr;
2932}
2933
2934static bool isSameQualifier(const NestedNameSpecifier *X,
2935 const NestedNameSpecifier *Y) {
2936 if (auto *NSX = getNamespace(X)) {
2937 auto *NSY = getNamespace(Y);
2938 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2939 return false;
2940 } else if (X->getKind() != Y->getKind())
2941 return false;
2942
2943 // FIXME: For namespaces and types, we're permitted to check that the entity
2944 // is named via the same tokens. We should probably do so.
2945 switch (X->getKind()) {
2946 case NestedNameSpecifier::Identifier:
2947 if (X->getAsIdentifier() != Y->getAsIdentifier())
2948 return false;
2949 break;
2950 case NestedNameSpecifier::Namespace:
2951 case NestedNameSpecifier::NamespaceAlias:
2952 // We've already checked that we named the same namespace.
2953 break;
2954 case NestedNameSpecifier::TypeSpec:
2955 case NestedNameSpecifier::TypeSpecWithTemplate:
2956 if (X->getAsType()->getCanonicalTypeInternal() !=
2957 Y->getAsType()->getCanonicalTypeInternal())
2958 return false;
2959 break;
2960 case NestedNameSpecifier::Global:
2961 case NestedNameSpecifier::Super:
2962 return true;
2963 }
2964
2965 // Recurse into earlier portion of NNS, if any.
2966 auto *PX = X->getPrefix();
2967 auto *PY = Y->getPrefix();
2968 if (PX && PY)
2969 return isSameQualifier(PX, PY);
2970 return !PX && !PY;
2971}
2972
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002973/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002974/// that they may be used in declarations of the same template.
2975static bool isSameTemplateParameterList(const TemplateParameterList *X,
2976 const TemplateParameterList *Y) {
2977 if (X->size() != Y->size())
2978 return false;
2979
2980 for (unsigned I = 0, N = X->size(); I != N; ++I)
2981 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2982 return false;
2983
2984 return true;
2985}
2986
George Burgess IV95845082017-02-15 22:43:27 +00002987/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002988/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002989static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2990 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002991 // Note that pass_object_size attributes are represented in the function's
2992 // ExtParameterInfo, so we don't need to check them here.
2993
George Burgess IV95845082017-02-15 22:43:27 +00002994 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00002995 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
2996 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
Michael Kruse157a3552018-12-10 15:16:37 +00002997
2998 for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) {
2999 Optional<EnableIfAttr *> Cand1A = std::get<0>(Pair);
3000 Optional<EnableIfAttr *> Cand2A = std::get<1>(Pair);
3001
3002 // Return false if the number of enable_if attributes is different.
3003 if (!Cand1A || !Cand2A)
3004 return false;
3005
George Burgess IV95845082017-02-15 22:43:27 +00003006 Cand1ID.clear();
3007 Cand2ID.clear();
3008
Michael Kruse157a3552018-12-10 15:16:37 +00003009 (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true);
3010 (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true);
3011
3012 // Return false if any of the enable_if expressions of A and B are
3013 // different.
George Burgess IV95845082017-02-15 22:43:27 +00003014 if (Cand1ID != Cand2ID)
3015 return false;
3016 }
Michael Kruse157a3552018-12-10 15:16:37 +00003017 return true;
George Burgess IV95845082017-02-15 22:43:27 +00003018}
3019
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003020/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00003021static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
3022 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00003023
Douglas Gregor022857e2011-12-22 01:48:48 +00003024 if (X == Y)
3025 return true;
Richard Smithf4634362014-09-03 23:11:22 +00003026
Douglas Gregor022857e2011-12-22 01:48:48 +00003027 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00003028 //
3029 // Note that we can't use DeclContext::Equals here, because the DeclContexts
3030 // could be two different declarations of the same function. (We will fix the
3031 // semantic DC to refer to the primary definition after merging.)
3032 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
3033 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00003034 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003035
3036 // Two typedefs refer to the same entity if they have the same underlying
3037 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003038 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
3039 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003040 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
3041 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00003042
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003043 // Must have the same kind.
3044 if (X->getKind() != Y->getKind())
3045 return false;
Richard Smithf4634362014-09-03 23:11:22 +00003046
Douglas Gregorda389302012-01-01 21:47:52 +00003047 // Objective-C classes and protocols with the same name always match.
3048 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00003049 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00003050
3051 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003052 // No need to handle these here: we merge them when adding them to the
3053 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00003054 return false;
3055 }
Richard Smithd55889a2013-09-09 16:55:27 +00003056
Douglas Gregor2009cee2012-01-03 22:46:00 +00003057 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003058 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
3059 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00003060 return (TagX->getTagKind() == TagY->getTagKind()) ||
3061 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
3062 TagX->getTagKind() == TTK_Interface) &&
3063 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
3064 TagY->getTagKind() == TTK_Interface));
3065 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003066
Joao Matose9a3ed42012-08-31 22:18:20 +00003067 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00003068 // FIXME: This needs to cope with merging of prototyped/non-prototyped
3069 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003070 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
3071 const auto *FuncY = cast<FunctionDecl>(Y);
3072 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
3073 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00003074 if (CtorX->getInheritedConstructor() &&
3075 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
3076 CtorY->getInheritedConstructor().getConstructor()))
3077 return false;
3078 }
Erich Keane281d20b2018-01-08 21:34:17 +00003079
3080 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
3081 return false;
3082
3083 // Multiversioned functions with different feature strings are represented
3084 // as separate declarations.
3085 if (FuncX->isMultiVersion()) {
3086 const auto *TAX = FuncX->getAttr<TargetAttr>();
3087 const auto *TAY = FuncY->getAttr<TargetAttr>();
3088 assert(TAX && TAY && "Multiversion Function without target attribute");
3089
3090 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
3091 return false;
3092 }
3093
Richard Smitha54d3242017-03-08 23:00:26 +00003094 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00003095 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
3096 // Map to the first declaration that we've already merged into this one.
3097 // The TSI of redeclarations might not match (due to calling conventions
3098 // being inherited onto the type but not the TSI), but the TSI type of
3099 // the first declaration of the function should match across modules.
3100 FD = FD->getCanonicalDecl();
3101 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
3102 : FD->getType();
3103 };
3104 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
3105 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00003106 // We can get functions with different types on the redecl chain in C++17
3107 // if they have differing exception specifications and at least one of
3108 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00003109 auto *XFPT = XT->getAs<FunctionProtoType>();
3110 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003111 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00003112 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
3113 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00003114 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00003115 return true;
3116 return false;
3117 }
George Burgess IV95845082017-02-15 22:43:27 +00003118 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00003119 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00003120 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003121
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003122 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003123 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
3124 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00003125 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
3126 ASTContext &C = VarX->getASTContext();
3127 if (C.hasSameType(VarX->getType(), VarY->getType()))
3128 return true;
3129
3130 // We can get decls with different types on the redecl chain. Eg.
3131 // template <typename T> struct S { static T Var[]; }; // #1
3132 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
3133 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00003134 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00003135 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
3136 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
3137 if (!VarXTy || !VarYTy)
3138 return false;
3139 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
3140 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
3141 }
3142 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003143 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003144
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003145 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003146 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
3147 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003148 return NamespaceX->isInline() == NamespaceY->isInline();
3149 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003150
Richard Smith8f8f05c2013-06-24 04:45:28 +00003151 // Identical template names and kinds match if their template parameter lists
3152 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003153 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
3154 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00003155 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00003156 TemplateY->getTemplatedDecl()) &&
3157 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3158 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003159 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003160
Richard Smith0b87e072013-10-07 08:02:11 +00003161 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003162 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3163 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003164 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003165 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3166 }
3167
Richard Smith8cbd8952015-08-04 02:05:09 +00003168 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003169 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3170 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003171 return IFDX->getAnonField()->getCanonicalDecl() ==
3172 IFDY->getAnonField()->getCanonicalDecl();
3173 }
3174
Richard Smith01a73372013-10-15 22:02:41 +00003175 // Enumerators with the same name match.
3176 if (isa<EnumConstantDecl>(X))
3177 // FIXME: Also check the value is odr-equivalent.
3178 return true;
3179
Richard Smithfd8634a2013-10-23 02:17:46 +00003180 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003181 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3182 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003183 return USX->getTargetDecl() == USY->getTargetDecl();
3184 }
3185
Richard Smith32952e12014-10-14 02:00:47 +00003186 // Using declarations with the same qualifier match. (We already know that
3187 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003188 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3189 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003190 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3191 UX->hasTypename() == UY->hasTypename() &&
3192 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3193 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003194 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3195 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003196 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3197 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3198 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003199 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003200 return isSameQualifier(
3201 UX->getQualifier(),
3202 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3203
Richard Smithf4634362014-09-03 23:11:22 +00003204 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003205 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3206 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003207 return NAX->getNamespace()->Equals(NAY->getNamespace());
3208 }
3209
Douglas Gregor022857e2011-12-22 01:48:48 +00003210 return false;
3211}
3212
Richard Smithd55889a2013-09-09 16:55:27 +00003213/// Find the context in which we should search for previous declarations when
3214/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003215DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3216 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003217 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003218 return ND->getOriginalNamespace();
3219
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003220 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003221 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003222 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003223 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003224 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003225
3226 // If there's no definition yet, then DC's definition is added by an update
3227 // record, but we've not yet loaded that update record. In this case, we
3228 // commit to DC being the canonical definition now, and will fix this when
3229 // we load the update record.
3230 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003231 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003232 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003233 RD->DefinitionData = DD;
3234 RD->getCanonicalDecl()->DefinitionData = DD;
3235
3236 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003237 Reader.PendingFakeDefinitionData.insert(
3238 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003239 }
3240
3241 return DD->Definition;
3242 }
Richard Smithd55889a2013-09-09 16:55:27 +00003243
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003244 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003245 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3246 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003247
Richard Smith4eca9b92015-02-04 23:37:59 +00003248 // We can see the TU here only if we have no Sema object. In that case,
3249 // there's no TU scope to look in, so using the DC alone is sufficient.
3250 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3251 return TU;
3252
Craig Toppera13603a2014-05-22 05:54:18 +00003253 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003254}
3255
Douglas Gregor022857e2011-12-22 01:48:48 +00003256ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003257 // Record that we had a typedef name for linkage whether or not we merge
3258 // with that declaration.
3259 if (TypedefNameForLinkage) {
3260 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3261 Reader.ImportedTypedefNamesForLinkage.insert(
3262 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3263 return;
3264 }
3265
Douglas Gregor9b47f942012-01-09 17:38:47 +00003266 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003267 return;
Richard Smith95d99302013-07-13 02:00:19 +00003268
Richard Smith70d58502014-08-30 00:04:23 +00003269 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003270 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003271 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003272 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3273 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003274 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003275 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003276 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003277 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3278 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003279 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003280 // Add the declaration to its redeclaration context so later merging
3281 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003282 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003283 }
3284}
3285
Richard Smith88ebade2014-08-23 01:45:27 +00003286/// Find the declaration that should be merged into, given the declaration found
3287/// by name lookup. If we're merging an anonymous declaration within a typedef,
3288/// we need a matching typedef, and we merge with the type inside it.
3289static NamedDecl *getDeclForMerging(NamedDecl *Found,
3290 bool IsTypedefNameForLinkage) {
3291 if (!IsTypedefNameForLinkage)
3292 return Found;
3293
3294 // If we found a typedef declaration that gives a name to some other
3295 // declaration, then we want that inner declaration. Declarations from
3296 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003297 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003298 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003299
3300 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003301 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003302
Hans Wennborgdcfba332015-10-06 23:40:43 +00003303 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003304}
3305
Richard Smith600adef2018-07-04 02:25:38 +00003306/// Find the declaration to use to populate the anonymous declaration table
3307/// for the given lexical DeclContext. We only care about finding local
3308/// definitions of the context; we'll merge imported ones as we go.
3309DeclContext *
3310ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3311 // For classes, we track the definition as we merge.
3312 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3313 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3314 return DD ? DD->Definition : nullptr;
3315 }
3316
3317 // For anything else, walk its merged redeclarations looking for a definition.
3318 // Note that we can't just call getDefinition here because the redeclaration
3319 // chain isn't wired up.
3320 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3321 if (auto *FD = dyn_cast<FunctionDecl>(D))
3322 if (FD->isThisDeclarationADefinition())
3323 return FD;
3324 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3325 if (MD->isThisDeclarationADefinition())
3326 return MD;
3327 }
3328
3329 // No merged definition yet.
3330 return nullptr;
3331}
3332
Richard Smithd08aeb62014-08-28 01:33:39 +00003333NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3334 DeclContext *DC,
3335 unsigned Index) {
3336 // If the lexical context has been merged, look into the now-canonical
3337 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003338 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003339
3340 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003341 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003342 if (Index < Previous.size() && Previous[Index])
3343 return Previous[Index];
3344
3345 // If this is the first time, but we have parsed a declaration of the context,
3346 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003347 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3348 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3349 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003350 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003351 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3352 else
Richard Smith2b560572015-02-07 03:11:11 +00003353 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3354 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003355 }
3356
3357 return Index < Previous.size() ? Previous[Index] : nullptr;
3358}
3359
3360void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3361 DeclContext *DC, unsigned Index,
3362 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003363 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003364
Richard Smith600adef2018-07-04 02:25:38 +00003365 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003366 if (Index >= Previous.size())
3367 Previous.resize(Index + 1);
3368 if (!Previous[Index])
3369 Previous[Index] = D;
3370}
3371
Douglas Gregor022857e2011-12-22 01:48:48 +00003372ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003373 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3374 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003375
Richard Smithd08aeb62014-08-28 01:33:39 +00003376 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3377 // Don't bother trying to find unnamed declarations that are in
3378 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003379 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3380 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003381 Result.suppress();
3382 return Result;
3383 }
Richard Smith95d99302013-07-13 02:00:19 +00003384
Douglas Gregor022857e2011-12-22 01:48:48 +00003385 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003386 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003387 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003388 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003389 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3390 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003391 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3392 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003393 // Go on to check in other places in case an existing typedef name
3394 // was not imported.
3395 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003396
Richard Smith2b560572015-02-07 03:11:11 +00003397 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003398 // This is an anonymous declaration that we may need to merge. Look it up
3399 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003400 if (auto *Existing = getAnonymousDeclForMerging(
3401 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3402 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003403 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3404 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003405 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003406 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003407 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003408
3409 // Temporarily consider the identifier to be up-to-date. We don't want to
3410 // cause additional lookups here.
3411 class UpToDateIdentifierRAII {
3412 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003413 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003414
3415 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003416 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003417 if (II) {
3418 WasOutToDate = II->isOutOfDate();
3419 if (WasOutToDate)
3420 II->setOutOfDate(false);
3421 }
3422 }
3423
3424 ~UpToDateIdentifierRAII() {
3425 if (WasOutToDate)
3426 II->setOutOfDate(true);
3427 }
3428 } UpToDate(Name.getAsIdentifierInfo());
3429
Fangrui Song6907ce22018-07-30 19:24:48 +00003430 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003431 IEnd = IdResolver.end();
3432 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003433 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003434 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003435 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3436 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003437 }
Richard Smith8a639892015-01-24 01:07:20 +00003438 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003439 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003440 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003441 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003442 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003443 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3444 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003445 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003446 } else {
3447 // Not in a mergeable context.
3448 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003449 }
Richard Smithd55889a2013-09-09 16:55:27 +00003450
Richard Smith2b9e3e32013-10-18 06:05:18 +00003451 // If this declaration is from a merged context, make a note that we need to
3452 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003453 //
3454 // FIXME: We should do something similar if we merge two definitions of the
3455 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003456 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3457 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3458 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003459 Reader.PendingOdrMergeChecks.push_back(D);
3460
Richard Smithd08aeb62014-08-28 01:33:39 +00003461 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003462 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003463}
3464
Richard Smithb321ecb2014-05-13 01:15:00 +00003465template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003466Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3467 return D->RedeclLink.getLatestNotUpdated();
3468}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003469
Richard Smithc3a53252015-02-28 05:57:02 +00003470Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3471 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3472}
3473
3474Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3475 assert(D);
3476
3477 switch (D->getKind()) {
3478#define ABSTRACT_DECL(TYPE)
3479#define DECL(TYPE, BASE) \
3480 case Decl::TYPE: \
3481 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3482#include "clang/AST/DeclNodes.inc"
3483 }
3484 llvm_unreachable("unknown decl kind");
3485}
3486
Richard Smith8ce51082015-03-11 01:44:51 +00003487Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3488 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3489}
3490
Richard Smithc3a53252015-02-28 05:57:02 +00003491template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003492void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3493 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003494 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003495 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003496 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003497}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003498
Richard Smith24d166c2014-07-31 23:52:38 +00003499namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003500
Richard Smith6de7a242014-07-31 23:46:44 +00003501template<>
3502void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003503 Redeclarable<VarDecl> *D,
3504 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003505 auto *VD = static_cast<VarDecl *>(D);
3506 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003507 D->RedeclLink.setPrevious(PrevVD);
3508 D->First = PrevVD->First;
3509
3510 // We should keep at most one definition on the chain.
3511 // FIXME: Cache the definition once we've found it. Building a chain with
3512 // N definitions currently takes O(N^2) time here.
3513 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3514 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3515 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3516 Reader.mergeDefinitionVisibility(CurD, VD);
3517 VD->demoteThisDefinitionToDeclaration();
3518 break;
3519 }
3520 }
3521 }
3522}
3523
Richard Smitha62d1982018-08-03 01:00:01 +00003524static bool isUndeducedReturnType(QualType T) {
3525 auto *DT = T->getContainedDeducedType();
3526 return DT && !DT->isDeduced();
3527}
3528
Richard Smithedbc6e92016-10-14 21:41:24 +00003529template<>
3530void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003531 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003532 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003533 auto *FD = static_cast<FunctionDecl *>(D);
3534 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003535
3536 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003537 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003538
3539 // If the previous declaration is an inline function declaration, then this
3540 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003541 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003542 // FIXME: [dcl.fct.spec]p4:
3543 // If a function with external linkage is declared inline in one
3544 // translation unit, it shall be declared inline in all translation
3545 // units in which it appears.
3546 //
3547 // Be careful of this case:
3548 //
3549 // module A:
3550 // template<typename T> struct X { void f(); };
3551 // template<typename T> inline void X<T>::f() {}
3552 //
3553 // module B instantiates the declaration of X<int>::f
3554 // module C instantiates the definition of X<int>::f
3555 //
3556 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003557 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003558 }
3559
Richard Smith6de7a242014-07-31 23:46:44 +00003560 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3561 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003562 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003563 // If we need to propagate an exception specification along the redecl
3564 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003565 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3566 bool WasUnresolved =
3567 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3568 if (IsUnresolved != WasUnresolved)
3569 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003570 {Canon, IsUnresolved ? PrevFD : FD});
3571
3572 // If we need to propagate a deduced return type along the redecl chain,
3573 // make a note of that so that we can do it later.
3574 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3575 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3576 if (IsUndeduced != WasUndeduced)
3577 Reader.PendingDeducedTypeUpdates.insert(
3578 {cast<FunctionDecl>(Canon),
3579 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003580 }
3581}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003582
3583} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003584
Richard Smith6de7a242014-07-31 23:46:44 +00003585void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003586 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3587}
3588
Richard Smith8346e522015-06-10 01:47:58 +00003589/// Inherit the default template argument from \p From to \p To. Returns
3590/// \c false if there is no default template for \p From.
3591template <typename ParmDecl>
3592static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3593 Decl *ToD) {
3594 auto *To = cast<ParmDecl>(ToD);
3595 if (!From->hasDefaultArgument())
3596 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003597 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003598 return true;
3599}
3600
3601static void inheritDefaultTemplateArguments(ASTContext &Context,
3602 TemplateDecl *From,
3603 TemplateDecl *To) {
3604 auto *FromTP = From->getTemplateParameters();
3605 auto *ToTP = To->getTemplateParameters();
3606 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3607
3608 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003609 NamedDecl *FromParam = FromTP->getParam(I);
3610 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003611
Richard Smith559cc692018-07-31 21:01:53 +00003612 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3613 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3614 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3615 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3616 else
3617 inheritDefaultTemplateArgument(
3618 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003619 }
3620}
3621
Richard Smith6de7a242014-07-31 23:46:44 +00003622void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003623 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003624 assert(D && Previous);
3625
3626 switch (D->getKind()) {
3627#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003628#define DECL(TYPE, BASE) \
3629 case Decl::TYPE: \
3630 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003631 break;
3632#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003633 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003634
3635 // If the declaration was visible in one module, a redeclaration of it in
3636 // another module remains visible even if it wouldn't be visible by itself.
3637 //
3638 // FIXME: In this case, the declaration should only be visible if a module
3639 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003640 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003641 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003642 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003643
Richard Smith8346e522015-06-10 01:47:58 +00003644 // If the declaration declares a template, it may inherit default arguments
3645 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003646 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003647 inheritDefaultTemplateArguments(Reader.getContext(),
3648 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003649}
3650
Richard Smithb321ecb2014-05-13 01:15:00 +00003651template<typename DeclT>
3652void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003653 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003654}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003655
Richard Smithb321ecb2014-05-13 01:15:00 +00003656void ASTDeclReader::attachLatestDeclImpl(...) {
3657 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3658}
3659
Douglas Gregor05f10352011-12-17 23:38:30 +00003660void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3661 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003662
3663 switch (D->getKind()) {
3664#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003665#define DECL(TYPE, BASE) \
3666 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003667 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3668 break;
3669#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003670 }
3671}
3672
Richard Smith851072e2014-05-19 20:59:20 +00003673template<typename DeclT>
3674void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3675 D->RedeclLink.markIncomplete();
3676}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003677
Richard Smith851072e2014-05-19 20:59:20 +00003678void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3679 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3680}
3681
3682void ASTReader::markIncompleteDeclChain(Decl *D) {
3683 switch (D->getKind()) {
3684#define ABSTRACT_DECL(TYPE)
3685#define DECL(TYPE, BASE) \
3686 case Decl::TYPE: \
3687 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3688 break;
3689#include "clang/AST/DeclNodes.inc"
3690 }
3691}
3692
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003693/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003694Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003695 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003696 SourceLocation DeclLoc;
3697 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003698 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003699 // Keep track of where we are in the stream, then jump back there
3700 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003701 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003702
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003703 ReadingKindTracker ReadingKind(Read_Decl, *this);
3704
Douglas Gregor1342e842009-07-06 18:54:52 +00003705 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003706 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003707
JF Bastien0e828952019-06-26 19:50:12 +00003708 auto Fail = [](const char *what, llvm::Error &&Err) {
3709 llvm::report_fatal_error(Twine("ASTReader::ReadDeclRecord failed ") + what +
3710 ": " + toString(std::move(Err)));
3711 };
3712
3713 if (llvm::Error JumpFailed = DeclsCursor.JumpToBit(Loc.Offset))
3714 Fail("jumping", std::move(JumpFailed));
David L. Jonesbe1557a2016-12-21 00:17:49 +00003715 ASTRecordReader Record(*this, *Loc.F);
3716 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
JF Bastien0e828952019-06-26 19:50:12 +00003717 Expected<unsigned> MaybeCode = DeclsCursor.ReadCode();
3718 if (!MaybeCode)
3719 Fail("reading code", MaybeCode.takeError());
3720 unsigned Code = MaybeCode.get();
Chris Lattner487412d2009-04-27 05:27:42 +00003721
Richard Smithdbafb6c2017-06-29 23:23:46 +00003722 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003723 Decl *D = nullptr;
JF Bastien0e828952019-06-26 19:50:12 +00003724 Expected<unsigned> MaybeDeclCode = Record.readRecord(DeclsCursor, Code);
3725 if (!MaybeDeclCode)
3726 llvm::report_fatal_error(
3727 "ASTReader::ReadDeclRecord failed reading decl code: " +
3728 toString(MaybeDeclCode.takeError()));
3729 switch ((DeclCode)MaybeDeclCode.get()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003730 case DECL_CONTEXT_LEXICAL:
3731 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003732 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003733 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003734 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003735 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003736 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003737 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003738 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003739 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003740 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003741 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003742 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003743 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003744 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003745 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003746 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003747 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003748 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003749 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003750 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003751 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003752 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003753 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003754 case DECL_EXPORT:
3755 D = ExportDecl::CreateDeserialized(Context, ID);
3756 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003757 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003758 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003759 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003760 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003761 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003762 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003763 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003764 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003765 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003766 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003767 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003768 break;
Richard Smith151c4562016-12-20 21:35:28 +00003769 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003770 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003771 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003772 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003773 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003774 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003775 case DECL_CONSTRUCTOR_USING_SHADOW:
3776 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3777 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003778 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003779 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003780 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003781 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003782 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003783 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003784 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003785 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003786 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003787 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003788 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003789 break;
Richard Smithbc491202017-02-17 20:05:37 +00003790 case DECL_CXX_DEDUCTION_GUIDE:
3791 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3792 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003793 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003794 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003795 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003796 case DECL_CXX_CONSTRUCTOR:
Richard Smith76b90272019-05-09 03:59:21 +00003797 D = CXXConstructorDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003798 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003799 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003800 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003801 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003802 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003803 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003804 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003805 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003806 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003807 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003808 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003809 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003810 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003811 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003812 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003813 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003814 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003815 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003816 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003817 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003818 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003819 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003820 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003821 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003822 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003823 case DECL_VAR_TEMPLATE:
3824 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3825 break;
3826 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3827 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3828 break;
3829 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3830 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3831 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003832 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003833 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003834 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003835 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003836 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003837 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003838 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003839 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003840 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003841 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003842 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003843 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003844 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003845 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3846 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003847 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003848 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003849 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003850 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003851 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3852 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003853 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003854 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003855 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003856 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003857 break;
Saar Razd7aae332019-07-10 21:25:49 +00003858 case DECL_CONCEPT:
3859 D = ConceptDecl::CreateDeserialized(Context, ID);
3860 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003861 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003862 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003863 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003864 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003865 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003866 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003867 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003868 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003869 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003870 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003871 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003872 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003873 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003874 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003875 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003876 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003877 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003878 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003879 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003880 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003881 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003882 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003883 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003884 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003885 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003886 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003887 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003888 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003889 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003890 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003891 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003892 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003893 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003894 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003895 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003896 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003897 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003898 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003899 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003900 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003901 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003902 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003903 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003904 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003905 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003906 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003907 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003908 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003909 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003910 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003911 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003912 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003913 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003914 break;
3915 case DECL_BINDING:
3916 D = BindingDecl::CreateDeserialized(Context, ID);
3917 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003918 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003919 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003920 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003921 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003922 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003923 break;
John McCall5e77d762013-04-16 07:28:30 +00003924 case DECL_MS_PROPERTY:
3925 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3926 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003927 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003928 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003929 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003930 case DECL_CXX_BASE_SPECIFIERS:
3931 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003932 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003933 case DECL_CXX_CTOR_INITIALIZERS:
3934 Error("attempt to read a C++ ctor initializer record as a declaration");
3935 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003936 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003937 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003938 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003939 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003940 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003941 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003942 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003943 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00003944 case DECL_OMP_ALLOCATE: {
3945 unsigned NumVars = Record.readInt();
3946 unsigned NumClauses = Record.readInt();
3947 D = OMPAllocateDecl::CreateDeserialized(Context, ID, NumVars, NumClauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00003948 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00003949 }
Kelvin Li1408f912018-09-26 04:28:39 +00003950 case DECL_OMP_REQUIRES:
3951 D = OMPRequiresDecl::CreateDeserialized(Context, ID, Record.readInt());
3952 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003953 case DECL_OMP_DECLARE_REDUCTION:
3954 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3955 break;
Michael Kruse251e1482019-02-01 20:25:04 +00003956 case DECL_OMP_DECLARE_MAPPER:
3957 D = OMPDeclareMapperDecl::CreateDeserialized(Context, ID, Record.readInt());
3958 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003959 case DECL_OMP_CAPTUREDEXPR:
3960 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003961 break;
Nico Weber66220292016-03-02 17:28:48 +00003962 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003963 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003964 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003965 case DECL_PRAGMA_DETECT_MISMATCH:
3966 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003967 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003968 break;
Michael Han84324352013-02-22 17:15:32 +00003969 case DECL_EMPTY:
3970 D = EmptyDecl::CreateDeserialized(Context, ID);
3971 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003972 case DECL_OBJC_TYPE_PARAM:
3973 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3974 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003975 }
Chris Lattner487412d2009-04-27 05:27:42 +00003976
Sebastian Redlb3298c32010-08-18 23:56:48 +00003977 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003978 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003979 // Set the DeclContext before doing any deserialization, to make sure internal
3980 // calls to Decl::getASTContext() by Decl's methods will find the
3981 // TranslationUnitDecl without crashing.
3982 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003983 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003984
3985 // If this declaration is also a declaration context, get the
3986 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003987 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003988 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003989 if (Offsets.first &&
3990 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3991 return nullptr;
3992 if (Offsets.second &&
3993 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3994 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003995 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003996 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003997
Douglas Gregordab42432011-08-12 00:15:20 +00003998 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003999 PendingUpdateRecords.push_back(
4000 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00004001
Douglas Gregor404cdde2012-01-27 01:47:08 +00004002 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004003 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00004004 // If we already have a definition when deserializing the ObjCInterfaceDecl,
4005 // we put the Decl in PendingDefinitions so we can pull the categories here.
4006 if (Class->isThisDeclarationADefinition() ||
4007 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004008 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00004009
Richard Smith13fb8602016-07-15 21:33:46 +00004010 // If we have deserialized a declaration that has a definition the
4011 // AST consumer might need to know about, queue it.
4012 // We don't pass it to the consumer immediately because we may be in recursive
4013 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004014 PotentiallyInterestingDecls.push_back(
4015 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00004016
Douglas Gregordab42432011-08-12 00:15:20 +00004017 return D;
4018}
4019
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00004020void ASTReader::PassInterestingDeclsToConsumer() {
4021 assert(Consumer);
4022
4023 if (PassingDeclsToConsumer)
4024 return;
4025
4026 // Guard variable to avoid recursively redoing the process of passing
4027 // decls to consumer.
4028 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
4029 true);
4030
4031 // Ensure that we've loaded all potentially-interesting declarations
4032 // that need to be eagerly loaded.
4033 for (auto ID : EagerlyDeserializedDecls)
4034 GetDecl(ID);
4035 EagerlyDeserializedDecls.clear();
4036
4037 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004038 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00004039 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004040 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
4041 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00004042 }
4043}
4044
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004045void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004046 // The declaration may have been modified by files later in the chain.
4047 // If this is the case, read the record containing the updates from each file
4048 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004049 serialization::GlobalDeclID ID = Record.ID;
4050 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004051 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004052 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00004053
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004054 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00004055
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004056 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00004057 auto UpdateOffsets = std::move(UpdI->second);
4058 DeclUpdateOffsets.erase(UpdI);
4059
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004060 // Check if this decl was interesting to the consumer. If we just loaded
4061 // the declaration, then we know it was interesting and we skip the call
4062 // to isConsumerInterestedIn because it is unsafe to call in the
4063 // current ASTReader state.
4064 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004065 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00004066 for (auto &FileAndOffset : UpdateOffsets) {
4067 ModuleFile *F = FileAndOffset.first;
4068 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004069 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
4070 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00004071 if (llvm::Error JumpFailed = Cursor.JumpToBit(Offset))
4072 // FIXME don't do a fatal error.
4073 llvm::report_fatal_error(
4074 "ASTReader::loadDeclUpdateRecords failed jumping: " +
4075 toString(std::move(JumpFailed)));
4076 Expected<unsigned> MaybeCode = Cursor.ReadCode();
4077 if (!MaybeCode)
4078 llvm::report_fatal_error(
4079 "ASTReader::loadDeclUpdateRecords failed reading code: " +
4080 toString(MaybeCode.takeError()));
4081 unsigned Code = MaybeCode.get();
David L. Jonesbe1557a2016-12-21 00:17:49 +00004082 ASTRecordReader Record(*this, *F);
JF Bastien0e828952019-06-26 19:50:12 +00004083 if (Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code))
4084 assert(MaybeRecCode.get() == DECL_UPDATES &&
4085 "Expected DECL_UPDATES record!");
4086 else
4087 llvm::report_fatal_error(
4088 "ASTReader::loadDeclUpdateRecords failed reading rec code: " +
4089 toString(MaybeCode.takeError()));
Richard Smith04d05b52014-03-23 00:27:18 +00004090
David L. Jonesbe1557a2016-12-21 00:17:49 +00004091 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
4092 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00004093 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00004094
4095 // We might have made this declaration interesting. If so, remember that
4096 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004097 if (!WasInteresting &&
4098 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
4099 PotentiallyInterestingDecls.push_back(
4100 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00004101 WasInteresting = true;
4102 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004103 }
4104 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00004105 // Add the lazy specializations to the template.
4106 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
4107 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
4108 "Must not have pending specializations");
4109 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
4110 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
4111 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
4112 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
4113 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
4114 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
4115 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00004116
4117 // Load the pending visible updates for this decl context, if it has any.
4118 auto I = PendingVisibleUpdates.find(ID);
4119 if (I != PendingVisibleUpdates.end()) {
4120 auto VisibleUpdates = std::move(I->second);
4121 PendingVisibleUpdates.erase(I);
4122
4123 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004124 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00004125 Lookups[DC].Table.add(
4126 Update.Mod, Update.Data,
4127 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
4128 DC->setHasExternalVisibleStorage(true);
4129 }
Chris Lattner487412d2009-04-27 05:27:42 +00004130}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004131
Richard Smithd61d4ac2015-08-22 20:13:39 +00004132void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
4133 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00004134 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00004135 if (FirstLocal != CanonDecl) {
4136 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
4137 ASTDeclReader::attachPreviousDecl(
4138 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
4139 CanonDecl);
4140 }
4141
4142 if (!LocalOffset) {
4143 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
4144 return;
4145 }
4146
4147 // Load the list of other redeclarations from this module file.
4148 ModuleFile *M = getOwningModuleFile(FirstLocal);
4149 assert(M && "imported decl from no module file");
4150
4151 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
4152 SavedStreamPosition SavedPosition(Cursor);
JF Bastien0e828952019-06-26 19:50:12 +00004153 if (llvm::Error JumpFailed = Cursor.JumpToBit(LocalOffset))
4154 llvm::report_fatal_error(
4155 "ASTReader::loadPendingDeclChain failed jumping: " +
4156 toString(std::move(JumpFailed)));
Richard Smithd61d4ac2015-08-22 20:13:39 +00004157
4158 RecordData Record;
JF Bastien0e828952019-06-26 19:50:12 +00004159 Expected<unsigned> MaybeCode = Cursor.ReadCode();
4160 if (!MaybeCode)
4161 llvm::report_fatal_error(
4162 "ASTReader::loadPendingDeclChain failed reading code: " +
4163 toString(MaybeCode.takeError()));
4164 unsigned Code = MaybeCode.get();
4165 if (Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record))
4166 assert(MaybeRecCode.get() == LOCAL_REDECLARATIONS &&
4167 "expected LOCAL_REDECLARATIONS record!");
4168 else
4169 llvm::report_fatal_error(
4170 "ASTReader::loadPendingDeclChain failed reading rec code: " +
4171 toString(MaybeCode.takeError()));
Richard Smithd61d4ac2015-08-22 20:13:39 +00004172
4173 // FIXME: We have several different dispatches on decl kind here; maybe
4174 // we should instead generate one loop per kind and dispatch up-front?
4175 Decl *MostRecent = FirstLocal;
4176 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4177 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00004178 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
4179 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00004180 }
Richard Smithc3a53252015-02-28 05:57:02 +00004181 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00004182}
4183
4184namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004185
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004186 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004187 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004188 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004189 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004190 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00004191 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004192 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004193 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004194 serialization::GlobalDeclID InterfaceID;
4195 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00004196
Douglas Gregor404cdde2012-01-27 01:47:08 +00004197 void add(ObjCCategoryDecl *Cat) {
4198 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00004199 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004200 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00004201
Douglas Gregor404cdde2012-01-27 01:47:08 +00004202 // Check for duplicate categories.
4203 if (Cat->getDeclName()) {
4204 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00004205 if (Existing &&
4206 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00004207 != Reader.getOwningModuleFile(Cat)) {
4208 // FIXME: We should not warn for duplicates in diamond:
4209 //
4210 // MT //
4211 // / \ //
4212 // ML MR //
4213 // \ / //
4214 // MB //
4215 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004216 // If there are duplicates in ML/MR, there will be warning when
4217 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004218 // importing.
4219 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4220 << Interface->getDeclName() << Cat->getDeclName();
4221 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4222 } else if (!Existing) {
4223 // Record this category.
4224 Existing = Cat;
4225 }
4226 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004227
Douglas Gregor404cdde2012-01-27 01:47:08 +00004228 // Add this category to the end of the chain.
4229 if (Tail)
4230 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4231 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004232 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004233 Tail = Cat;
4234 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004235
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004236 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004237 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004238 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004239 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4240 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004241 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004242 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4243 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004244 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004245 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004246 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004247 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004248
Douglas Gregor404cdde2012-01-27 01:47:08 +00004249 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004250 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004251 }
4252 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004253
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004254 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004255 // If we've loaded all of the category information we care about from
4256 // this module file, we're done.
4257 if (M.Generation <= PreviousGeneration)
4258 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004259
4260 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004261 // module file. If there is no such mapping, we'll find nothing here
4262 // (or in any module it imports).
4263 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4264 if (!LocalID)
4265 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004266
Douglas Gregor404cdde2012-01-27 01:47:08 +00004267 // Perform a binary search to find the local redeclarations for this
4268 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004269 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004270 const ObjCCategoriesInfo *Result
4271 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004272 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004273 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004274 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4275 Result->DefinitionID != LocalID) {
4276 // We didn't find anything. If the class definition is in this module
4277 // file, then the module files it depends on cannot have any categories,
4278 // so suppress further lookup.
4279 return Reader.isDeclIDFromModule(InterfaceID, M);
4280 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004281
Douglas Gregor404cdde2012-01-27 01:47:08 +00004282 // We found something. Dig out all of the categories.
4283 unsigned Offset = Result->Offset;
4284 unsigned N = M.ObjCCategories[Offset];
4285 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4286 for (unsigned I = 0; I != N; ++I)
4287 add(cast_or_null<ObjCCategoryDecl>(
4288 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4289 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004290 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004291 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004292
4293} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004294
Douglas Gregor404cdde2012-01-27 01:47:08 +00004295void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4296 ObjCInterfaceDecl *D,
4297 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004298 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004299 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004300 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004301}
Douglas Gregordab42432011-08-12 00:15:20 +00004302
Richard Smithd6db68c2014-08-07 20:58:41 +00004303template<typename DeclT, typename Fn>
4304static void forAllLaterRedecls(DeclT *D, Fn F) {
4305 F(D);
4306
4307 // Check whether we've already merged D into its redeclaration chain.
4308 // MostRecent may or may not be nullptr if D has not been merged. If
4309 // not, walk the merged redecl chain and see if it's there.
4310 auto *MostRecent = D->getMostRecentDecl();
4311 bool Found = false;
4312 for (auto *Redecl = MostRecent; Redecl && !Found;
4313 Redecl = Redecl->getPreviousDecl())
4314 Found = (Redecl == D);
4315
4316 // If this declaration is merged, apply the functor to all later decls.
4317 if (Found) {
4318 for (auto *Redecl = MostRecent; Redecl != D;
4319 Redecl = Redecl->getPreviousDecl())
4320 F(Redecl);
4321 }
4322}
4323
Vassil Vassilev7d264622017-06-30 22:40:17 +00004324void ASTDeclReader::UpdateDecl(Decl *D,
4325 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004326 while (Record.getIdx() < Record.size()) {
4327 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004328 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004329 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004330 // FIXME: If we also have an update record for instantiating the
4331 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004332 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004333 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004334 // FIXME: We should call addHiddenDecl instead, to add the member
4335 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004336 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004337 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004338 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004339
4340 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004341 // It will be added to the template's lazy specialization set.
4342 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004343 break;
4344
4345 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004346 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004347
Douglas Gregor540fd812012-01-09 18:07:24 +00004348 // Each module has its own anonymous namespace, which is disjoint from
4349 // any other module's anonymous namespaces, so don't attach the anonymous
4350 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004351 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004352 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004353 TU->setAnonymousNamespace(Anon);
4354 else
4355 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4356 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004357 break;
4358 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004359
Richard Smith891fc7f2017-12-05 01:31:47 +00004360 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004361 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004362 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4363 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004364 uint64_t Val = Record.readInt();
4365 if (Val && !VD->getInit()) {
4366 VD->setInit(Record.readExpr());
4367 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4368 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4369 Eval->CheckedICE = true;
4370 Eval->IsICE = Val == 3;
4371 }
4372 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004373 break;
Richard Smith05a21352017-06-22 22:18:46 +00004374 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004375
Richard Smith891fc7f2017-12-05 01:31:47 +00004376 case UPD_CXX_POINT_OF_INSTANTIATION: {
4377 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004378 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004379 VTSD->setPointOfInstantiation(POI);
4380 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4381 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4382 } else {
4383 auto *FD = cast<FunctionDecl>(D);
4384 if (auto *FTSInfo = FD->TemplateOrSpecialization
4385 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4386 FTSInfo->setPointOfInstantiation(POI);
4387 else
4388 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4389 ->setPointOfInstantiation(POI);
4390 }
4391 break;
4392 }
4393
John McCall32791cc2016-01-06 22:34:54 +00004394 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004395 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004396
4397 // We have to read the default argument regardless of whether we use it
4398 // so that hypothetical further update records aren't messed up.
4399 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004400 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004401
4402 // Only apply the update if the parameter still has an uninstantiated
4403 // default argument.
4404 if (Param->hasUninstantiatedDefaultArg())
4405 Param->setDefaultArg(DefaultArg);
4406 break;
4407 }
4408
Richard Smith4b054b22016-08-24 21:25:37 +00004409 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004410 auto *FD = cast<FieldDecl>(D);
4411 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004412
4413 // Only apply the update if the field still has an uninstantiated
4414 // default member initializer.
4415 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4416 if (DefaultInit)
4417 FD->setInClassInitializer(DefaultInit);
4418 else
4419 // Instantiation failed. We can get here if we serialized an AST for
4420 // an invalid program.
4421 FD->removeInClassInitializer();
4422 }
4423 break;
4424 }
4425
Richard Smith4d235792014-08-07 18:53:08 +00004426 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004427 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004428 if (Reader.PendingBodies[FD]) {
4429 // FIXME: Maybe check for ODR violations.
4430 // It's safe to stop now because this update record is always last.
4431 return;
4432 }
4433
David L. Jonesbe1557a2016-12-21 00:17:49 +00004434 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004435 // Maintain AST consistency: any later redeclarations of this function
4436 // are inline if this one is. (We might have merged another declaration
4437 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004438 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4439 FD->setImplicitlyInline();
4440 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004441 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004442 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004443 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004444 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004445 break;
4446 }
4447
Richard Smithcd45dbc2014-04-19 03:48:30 +00004448 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4449 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004450 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004451 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004452 OldDD && (OldDD->Definition != RD ||
4453 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004454 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004455 RD->setArgPassingRestrictions(
4456 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004457 ReadCXXRecordDefinition(RD, /*Update*/true);
4458
Richard Smithcd45dbc2014-04-19 03:48:30 +00004459 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004460 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004461 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004462 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004463 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004464 }
4465
David L. Jonesbe1557a2016-12-21 00:17:49 +00004466 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004467 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004468 if (MemberSpecializationInfo *MSInfo =
4469 RD->getMemberSpecializationInfo()) {
4470 MSInfo->setTemplateSpecializationKind(TSK);
4471 MSInfo->setPointOfInstantiation(POI);
4472 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004473 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004474 Spec->setTemplateSpecializationKind(TSK);
4475 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004476
David L. Jonesbe1557a2016-12-21 00:17:49 +00004477 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004478 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004479 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004480 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004481 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004482 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004483 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004484
4485 // FIXME: If we already have a partial specialization set,
4486 // check that it matches.
4487 if (!Spec->getSpecializedTemplateOrPartial()
4488 .is<ClassTemplatePartialSpecializationDecl *>())
4489 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004490 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004491 }
4492
David L. Jonesbe1557a2016-12-21 00:17:49 +00004493 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004494 RD->setLocation(ReadSourceLocation());
4495 RD->setLocStart(ReadSourceLocation());
4496 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004497
David L. Jonesbe1557a2016-12-21 00:17:49 +00004498 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004499 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004500 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004501 // If the declaration already has attributes, we assume that some other
4502 // AST file already loaded them.
4503 if (!D->hasAttrs())
4504 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004505 }
4506 break;
4507 }
4508
Richard Smithf8134002015-03-10 01:41:22 +00004509 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4510 // Set the 'operator delete' directly to avoid emitting another update
4511 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004512 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004513 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004514 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004515 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004516 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004517 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004518 First->OperatorDeleteThisArg = ThisArg;
4519 }
Richard Smithf8134002015-03-10 01:41:22 +00004520 break;
4521 }
4522
Richard Smith564417a2014-03-20 21:47:22 +00004523 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004524 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004525 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004526 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004527
4528 // Update this declaration's exception specification, if needed.
4529 auto *FD = cast<FunctionDecl>(D);
4530 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4531 // FIXME: If the exception specification is already present, check that it
4532 // matches.
4533 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004534 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004535 FPT->getReturnType(), FPT->getParamTypes(),
4536 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004537
4538 // When we get to the end of deserializing, see if there are other decls
4539 // that we need to propagate this exception specification onto.
4540 Reader.PendingExceptionSpecUpdates.insert(
4541 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004542 }
Richard Smith564417a2014-03-20 21:47:22 +00004543 break;
4544 }
4545
Richard Smith1fa5d642013-05-11 05:45:24 +00004546 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004547 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004548 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004549 Reader.PendingDeducedTypeUpdates.insert(
4550 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004551 break;
4552 }
Eli Friedman276dd182013-09-05 00:02:25 +00004553
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004554 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004555 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004556 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004557 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004558
4559 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004560 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4561 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004562 break;
4563
4564 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004565 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4566 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004567 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004568
Alexey Bataev97720002014-11-11 04:05:39 +00004569 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Erich Keane6a24e802019-09-13 17:39:31 +00004570 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
4571 Reader.getContext(), ReadSourceRange(),
4572 AttributeCommonInfo::AS_Pragma));
Alexey Bataev97720002014-11-11 04:05:39 +00004573 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004574
Alexey Bataev27ef9512019-03-20 20:14:22 +00004575 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
4576 auto AllocatorKind =
4577 static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(Record.readInt());
4578 Expr *Allocator = Record.readExpr();
4579 SourceRange SR = ReadSourceRange();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00004580 D->addAttr(OMPAllocateDeclAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00004581 Reader.getContext(), AllocatorKind, Allocator, SR,
4582 AttributeCommonInfo::AS_Pragma));
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004583 break;
Alexey Bataev27ef9512019-03-20 20:14:22 +00004584 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004585
Alex Denisovfde64952015-06-26 05:28:36 +00004586 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004587 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004588 auto *Exported = cast<NamedDecl>(D);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004589 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith13897eb2018-09-12 23:37:00 +00004590 Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
4591 Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004592 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004593 }
Alex Denisovfde64952015-06-26 05:28:36 +00004594
Alexey Bataev729e2422019-08-23 16:11:14 +00004595 case UPD_DECL_MARKED_OPENMP_DECLARETARGET: {
4596 OMPDeclareTargetDeclAttr::MapTypeTy MapType =
4597 static_cast<OMPDeclareTargetDeclAttr::MapTypeTy>(Record.readInt());
4598 OMPDeclareTargetDeclAttr::DevTypeTy DevType =
4599 static_cast<OMPDeclareTargetDeclAttr::DevTypeTy>(Record.readInt());
Alexey Bataevd01b7492018-08-15 19:45:12 +00004600 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
Erich Keane6a24e802019-09-13 17:39:31 +00004601 Reader.getContext(), MapType, DevType, ReadSourceRange(),
4602 AttributeCommonInfo::AS_Pragma));
Alexey Bataevd01b7492018-08-15 19:45:12 +00004603 break;
Alexey Bataev729e2422019-08-23 16:11:14 +00004604 }
Alexey Bataevd01b7492018-08-15 19:45:12 +00004605
Alex Denisovfde64952015-06-26 05:28:36 +00004606 case UPD_ADDED_ATTR_TO_RECORD:
4607 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004608 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004609 assert(Attrs.size() == 1);
4610 D->addAttr(Attrs[0]);
4611 break;
4612 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004613 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004614}