blob: 27fcc9e40b51734d6daa12cc236c1b982c4171fd [file] [log] [blame]
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001//===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner487412d2009-04-27 05:27:42 +00006//
7//===----------------------------------------------------------------------===//
8//
Sebastian Redl2c499f62010-08-18 23:56:43 +00009// This file implements the ASTReader::ReadDeclRecord method, which is the
Chris Lattner487412d2009-04-27 05:27:42 +000010// entrypoint for loading a decl.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000014#include "ASTCommon.h"
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +000015#include "ASTReaderInternals.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/AttrIterator.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclCXX.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000022#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclOpenMP.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/DeclVisitor.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000027#include "clang/AST/DeclarationName.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "clang/AST/Expr.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000029#include "clang/AST/ExternalASTSource.h"
30#include "clang/AST/LambdaCapture.h"
31#include "clang/AST/NestedNameSpecifier.h"
Kelvin Li1408f912018-09-26 04:28:39 +000032#include "clang/AST/OpenMPClause.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000033#include "clang/AST/Redeclarable.h"
34#include "clang/AST/Stmt.h"
35#include "clang/AST/TemplateBase.h"
36#include "clang/AST/Type.h"
37#include "clang/AST/UnresolvedSet.h"
38#include "clang/Basic/AttrKinds.h"
39#include "clang/Basic/ExceptionSpecificationType.h"
40#include "clang/Basic/IdentifierTable.h"
41#include "clang/Basic/LLVM.h"
42#include "clang/Basic/Lambda.h"
43#include "clang/Basic/LangOptions.h"
44#include "clang/Basic/Linkage.h"
45#include "clang/Basic/Module.h"
46#include "clang/Basic/PragmaKinds.h"
47#include "clang/Basic/SourceLocation.h"
48#include "clang/Basic/Specifiers.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000049#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000050#include "clang/Serialization/ASTBitCodes.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000051#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000052#include "clang/Serialization/ContinuousRangeMap.h"
53#include "clang/Serialization/Module.h"
54#include "llvm/ADT/DenseMap.h"
55#include "llvm/ADT/FoldingSet.h"
56#include "llvm/ADT/STLExtras.h"
57#include "llvm/ADT/SmallPtrSet.h"
58#include "llvm/ADT/SmallVector.h"
59#include "llvm/ADT/iterator_range.h"
60#include "llvm/Bitcode/BitstreamReader.h"
61#include "llvm/Support/Casting.h"
62#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000063#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000064#include <algorithm>
65#include <cassert>
66#include <cstdint>
67#include <cstring>
68#include <string>
69#include <utility>
Hans Wennborgdcfba332015-10-06 23:40:43 +000070
Chris Lattner487412d2009-04-27 05:27:42 +000071using namespace clang;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000072using namespace serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000073
74//===----------------------------------------------------------------------===//
75// Declaration deserialization
76//===----------------------------------------------------------------------===//
77
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000078namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000079
Sebastian Redlb3298c32010-08-18 23:56:48 +000080 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000081 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000082 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000083 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000084 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000085 const SourceLocation ThisDeclLoc;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000086
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000087 using RecordData = ASTReader::RecordData;
88
Richard Smith600adef2018-07-04 02:25:38 +000089 TypeID DeferredTypeID = 0;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000090 unsigned AnonymousDeclNumber;
91 GlobalDeclID NamedDeclForTagDecl = 0;
92 IdentifierInfo *TypedefNameForLinkage = nullptr;
93
94 bool HasPendingBody = false;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +000095
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000096 ///A flag to carry the information for a decl from the entity is
Vassil Vassilev928c8252016-04-28 14:13:28 +000097 /// used. We use it to delay the marking of the canonical decl as used until
98 /// the entire declaration is deserialized and merged.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000099 bool IsDeclMarkedUsed = false;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000100
Sebastian Redlc67764e2010-07-22 22:43:28 +0000101 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000102
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000103 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000104 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000105 assert(LocalOffset < Loc.Offset && "offset point after current record");
106 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +0000107 }
108
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000109 uint64_t ReadGlobalOffset() {
110 uint64_t Local = ReadLocalOffset();
111 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +0000112 }
113
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000114 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000115 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000116 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000117
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000118 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000119 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000120 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000121
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000122 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000123 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000124 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000125
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000126 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000127 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000128 }
Richard Smith0b884372015-02-27 00:25:58 +0000129
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000130 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000131 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +0000132 }
133
Richard Smith0b884372015-02-27 00:25:58 +0000134 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000135 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000136 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +0000137 }
138
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000139 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000140 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000141 }
142
143 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000144 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000145 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000146 }
147
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000148 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000149 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000150 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000151
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000152 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000153 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000154 }
155
156 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000157 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000158 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000159
David L. Jonesbe1557a2016-12-21 00:17:49 +0000160 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000161 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000162
163 Module *readModule() {
164 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000165 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000166
Richard Smith2a9e5c52015-02-03 03:32:14 +0000167 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000168 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
169 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000170 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000171 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000172 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000173 void MergeDefinitionData(ObjCInterfaceDecl *D,
174 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000175 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
176 void MergeDefinitionData(ObjCProtocolDecl *D,
177 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000178
Richard Smith600adef2018-07-04 02:25:38 +0000179 static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
180
Richard Smithd08aeb62014-08-28 01:33:39 +0000181 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
182 DeclContext *DC,
183 unsigned Index);
184 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
185 unsigned Index, NamedDecl *D);
186
Richard Smith0144f512015-08-22 02:09:38 +0000187 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000188 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000189 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000190 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000191 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000192
Douglas Gregor022857e2011-12-22 01:48:48 +0000193 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000194 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000195 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000197 /// Retrieve the first ID.
Douglas Gregor022857e2011-12-22 01:48:48 +0000198 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000200 /// Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000201 bool isKeyDecl() const { return IsKeyDecl; }
202
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000203 /// Get a known declaration that this should be merged with, if
Richard Smith5a4737c2015-02-06 02:42:59 +0000204 /// any.
205 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000206 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000207
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000208 /// Class used to capture the result of searching for an existing
Douglas Gregor022857e2011-12-22 01:48:48 +0000209 /// declaration of a specific kind and name, along with the ability
210 /// to update the place where this result was found (the declaration
211 /// chain hanging off an identifier or the DeclContext we searched in)
212 /// if requested.
213 class FindExistingResult {
214 ASTReader &Reader;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000215 NamedDecl *New = nullptr;
216 NamedDecl *Existing = nullptr;
217 bool AddResult = false;
218 unsigned AnonymousDeclNumber = 0;
219 IdentifierInfo *TypedefNameForLinkage = nullptr;
Richard Smithd08aeb62014-08-28 01:33:39 +0000220
Douglas Gregor022857e2011-12-22 01:48:48 +0000221 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000222 FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000223
Richard Smithd08aeb62014-08-28 01:33:39 +0000224 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000225 unsigned AnonymousDeclNumber,
226 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000227 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000228 AnonymousDeclNumber(AnonymousDeclNumber),
229 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000230
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000231 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000232 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
233 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000234 AnonymousDeclNumber(Other.AnonymousDeclNumber),
235 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000236 Other.AddResult = false;
237 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000238
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000239 FindExistingResult &operator=(FindExistingResult &&) = delete;
Douglas Gregor022857e2011-12-22 01:48:48 +0000240 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000241
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000242 /// Suppress the addition of this result into the known set of
Douglas Gregor2009cee2012-01-03 22:46:00 +0000243 /// names.
244 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000245
Douglas Gregor022857e2011-12-22 01:48:48 +0000246 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000247
Douglas Gregor022857e2011-12-22 01:48:48 +0000248 template<typename T>
249 operator T*() const { return dyn_cast_or_null<T>(Existing); }
250 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000251
Richard Smith8a639892015-01-24 01:07:20 +0000252 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
253 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000254 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000255
Chris Lattner487412d2009-04-27 05:27:42 +0000256 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000257 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
258 ASTReader::RecordLocation Loc,
259 DeclID thisDeclID, SourceLocation ThisDeclLoc)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000260 : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
261 ThisDeclLoc(ThisDeclLoc) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000262
Vassil Vassilev7d264622017-06-30 22:40:17 +0000263 template <typename T> static
264 void AddLazySpecializations(T *D,
265 SmallVectorImpl<serialization::DeclID>& IDs) {
266 if (IDs.empty())
267 return;
268
269 // FIXME: We should avoid this pattern of getting the ASTContext.
270 ASTContext &C = D->getASTContext();
271
272 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
273
274 if (auto &Old = LazySpecializations) {
275 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Fangrui Song55fab262018-09-26 22:16:28 +0000276 llvm::sort(IDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +0000277 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
278 }
279
280 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
281 *Result = IDs.size();
282 std::copy(IDs.begin(), IDs.end(), Result + 1);
283
284 LazySpecializations = Result;
285 }
286
Richard Smithb321ecb2014-05-13 01:15:00 +0000287 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000288 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
289 static Decl *getMostRecentDeclImpl(...);
290 static Decl *getMostRecentDecl(Decl *D);
291
292 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000293 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000294 Redeclarable<DeclT> *D, Decl *Previous,
295 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000296 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000297 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
298 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000299
300 template <typename DeclT>
301 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
302 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000303 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000304
Richard Smith851072e2014-05-19 20:59:20 +0000305 template <typename DeclT>
306 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
307 static void markIncompleteDeclChainImpl(...);
308
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000309 /// Determine whether this declaration has a pending body.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000310 bool hasPendingBody() const { return HasPendingBody; }
311
David Blaikieac4345c2017-02-12 18:45:31 +0000312 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000313 void Visit(Decl *D);
314
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000315 void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000316
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000317 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
318 ObjCCategoryDecl *Next) {
319 Cat->NextClassCategory = Next;
320 }
321
Chris Lattner487412d2009-04-27 05:27:42 +0000322 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000323 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000324 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000325 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
326 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000327 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000328 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000329 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
330 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000331 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000332 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000333 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000334 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000335 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000336 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000337 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000338 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
339 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
340 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
341 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
342 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000343 ClassTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000344
Richard Smith1d209d02013-05-23 01:49:11 +0000345 void VisitClassTemplateSpecializationDecl(
346 ClassTemplateSpecializationDecl *D) {
347 VisitClassTemplateSpecializationDeclImpl(D);
348 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000349
Chris Lattnerca025db2010-05-07 21:43:38 +0000350 void VisitClassTemplatePartialSpecializationDecl(
351 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000352 void VisitClassScopeFunctionSpecializationDecl(
353 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000354 RedeclarableResult
355 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000356
Larisse Voufo39a1e502013-08-06 01:03:05 +0000357 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
358 VisitVarTemplateSpecializationDeclImpl(D);
359 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000360
Larisse Voufo39a1e502013-08-06 01:03:05 +0000361 void VisitVarTemplatePartialSpecializationDecl(
362 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000363 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000364 void VisitValueDecl(ValueDecl *VD);
365 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000366 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000367 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000368 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000369 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000370 void VisitCXXMethodDecl(CXXMethodDecl *D);
371 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
372 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
373 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000374 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000375 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000376 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000377 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
378 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000379 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
380 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000381 void VisitDecompositionDecl(DecompositionDecl *DD);
382 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000383 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000384 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000385 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000386 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000387 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000388 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000389 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000390 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000391 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000392 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000393 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000394 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000395 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000396 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000397 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000398 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000399 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000400 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000401 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000402 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
403 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000404 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000405 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000406 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000407
Chris Lattner487412d2009-04-27 05:27:42 +0000408 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000409
410 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000411 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000412
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000413 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000414 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
415 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000416
417 template<typename T>
418 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000419 RedeclarableResult &Redecl,
420 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000421
Richard Smith0b87e072013-10-07 08:02:11 +0000422 template<typename T>
423 void mergeMergeable(Mergeable<T> *D);
424
Richard Smithf17fdbd2014-04-24 02:25:27 +0000425 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
426 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000427 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000428
Douglas Gregor85f3f952015-07-07 03:57:15 +0000429 ObjCTypeParamList *ReadObjCTypeParamList();
430
Alexis Hunted053252010-05-30 07:21:58 +0000431 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000432 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000433 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000434 void VisitObjCContainerDecl(ObjCContainerDecl *D);
435 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
436 void VisitObjCIvarDecl(ObjCIvarDecl *D);
437 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
438 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000439 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
440 void VisitObjCImplDecl(ObjCImplDecl *D);
441 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
442 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
443 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
444 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
445 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000446 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev25ed0c02019-03-07 17:54:44 +0000447 void VisitOMPAllocateDecl(OMPAllocateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000448 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Michael Kruse251e1482019-02-01 20:25:04 +0000449 void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
Kelvin Li1408f912018-09-26 04:28:39 +0000450 void VisitOMPRequiresDecl(OMPRequiresDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000451 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000452 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000453
454} // namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000455
Richard Smith86dfc1e2015-06-18 22:07:00 +0000456namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000457
Richard Smith86dfc1e2015-06-18 22:07:00 +0000458/// Iterator over the redeclarations of a declaration that have already
459/// been merged into the same redeclaration chain.
460template<typename DeclT>
461class MergedRedeclIterator {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000462 DeclT *Start;
463 DeclT *Canonical = nullptr;
464 DeclT *Current = nullptr;
465
Richard Smith86dfc1e2015-06-18 22:07:00 +0000466public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000467 MergedRedeclIterator() = default;
468 MergedRedeclIterator(DeclT *Start) : Start(Start), Current(Start) {}
Richard Smith86dfc1e2015-06-18 22:07:00 +0000469
470 DeclT *operator*() { return Current; }
471
472 MergedRedeclIterator &operator++() {
473 if (Current->isFirstDecl()) {
474 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000475 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000476 } else
477 Current = Current->getPreviousDecl();
478
479 // If we started in the merged portion, we'll reach our start position
480 // eventually. Otherwise, we'll never reach it, but the second declaration
481 // we reached was the canonical declaration, so stop when we see that one
482 // again.
483 if (Current == Start || Current == Canonical)
484 Current = nullptr;
485 return *this;
486 }
487
488 friend bool operator!=(const MergedRedeclIterator &A,
489 const MergedRedeclIterator &B) {
490 return A.Current != B.Current;
491 }
492};
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000493
494} // namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000495
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000496template <typename DeclT>
497static llvm::iterator_range<MergedRedeclIterator<DeclT>>
498merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000499 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
500 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000501}
502
Sebastian Redlb3298c32010-08-18 23:56:48 +0000503uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000504 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000505}
506
David Blaikieac4345c2017-02-12 18:45:31 +0000507void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000508 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000509 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000510 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
Erich Keane9c665062018-08-01 21:02:40 +0000511 CD->setNumCtorInitializers(Record.readInt());
512 if (CD->getNumCtorInitializers())
David Blaikieac4345c2017-02-12 18:45:31 +0000513 CD->CtorInitializers = ReadGlobalOffset();
514 }
515 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000516 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
517 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000518}
519
Sebastian Redlb3298c32010-08-18 23:56:48 +0000520void ASTDeclReader::Visit(Decl *D) {
521 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000522
Vassil Vassilev928c8252016-04-28 14:13:28 +0000523 // At this point we have deserialized and merged the decl and it is safe to
524 // update its canonical decl to signal that the entire entity is used.
525 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
526 IsDeclMarkedUsed = false;
527
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000528 if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
Richard Smithc23d7342018-06-29 20:46:25 +0000529 if (auto *TInfo = DD->getTypeSourceInfo())
530 Record.readTypeLoc(TInfo->getTypeLoc());
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000531 }
532
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000533 if (auto *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000534 // We have a fully initialized TypeDecl. Read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000535 TD->setTypeForDecl(Reader.GetType(DeferredTypeID).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000536
537 // If this is a tag declaration with a typedef name for linkage, it's safe
538 // to load that typedef now.
539 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000540 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
541 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000542 } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000543 // if we have a fully initialized TypeDecl, we can safely read its type now.
Richard Smith600adef2018-07-04 02:25:38 +0000544 ID->TypeForDecl = Reader.GetType(DeferredTypeID).getTypePtrOrNull();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000545 } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000546 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000547 // We only read it if FD doesn't already have a body (e.g., from another
548 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000549 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000550 if (Record.readInt())
551 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000552 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000553}
554
Sebastian Redlb3298c32010-08-18 23:56:48 +0000555void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000556 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
557 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000558 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000559 // parameter or of a parameter of a function template immediately. These
560 // entities might be used in the formulation of its DeclContext (for
561 // example, a function parameter can be used in decltype() in trailing
562 // return type of the function). Use the translation unit DeclContext as a
563 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000564 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
565 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000566 if (!LexicalDCIDForTemplateParmDecl)
567 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000568 Reader.addPendingDeclContextInfo(D,
569 SemaDCIDForTemplateParmDecl,
570 LexicalDCIDForTemplateParmDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000571 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000572 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000573 auto *SemaDC = ReadDeclAs<DeclContext>();
574 auto *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000575 if (!LexicalDC)
576 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000577 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000578 // Avoid calling setLexicalDeclContext() directly because it uses
579 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000580 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
581 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000582 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000583 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000584 D->setInvalidDecl(Record.readInt());
585 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000586 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000587 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000588 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
589 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000590 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000591 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000592 D->setImplicit(Record.readInt());
593 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000594 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000595 D->setReferenced(Record.readInt());
596 D->setTopLevelDeclInObjCContainer(Record.readInt());
597 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000598 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000599 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000600
Douglas Gregorcf68c582011-12-01 22:20:10 +0000601 // Determine whether this declaration is part of a (sub)module. If so, it
602 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000603 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000604 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000605 D->setModuleOwnershipKind(
606 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
607 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000608 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000609
Richard Smith90dc5252017-06-23 01:04:34 +0000610 if (ModulePrivate) {
611 // Module-private declarations are never visible, so there is no work to
612 // do.
Richard Smith42413142015-05-15 20:05:43 +0000613 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
614 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000615 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000616 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000617 // Mark the declaration as visible when its owning module becomes visible.
618 if (Owner->NameVisibility == Module::AllVisible)
619 D->setVisibleDespiteOwningModule();
620 else
Richard Smith42413142015-05-15 20:05:43 +0000621 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000622 }
Richard Smithd19389a2017-07-05 07:47:11 +0000623 } else if (ModulePrivate) {
624 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000625 }
Chris Lattner487412d2009-04-27 05:27:42 +0000626}
627
Nico Weber66220292016-03-02 17:28:48 +0000628void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
629 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000630 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000631 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000632 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000633 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
634 D->getTrailingObjects<char>()[Arg.size()] = '\0';
635}
636
Nico Webercbbaeb12016-03-02 19:28:54 +0000637void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
638 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000639 D->setLocation(ReadSourceLocation());
640 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000641 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
642 D->getTrailingObjects<char>()[Name.size()] = '\0';
643
644 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000645 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000646 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
647 Value.size());
648 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
649}
650
Sebastian Redlb3298c32010-08-18 23:56:48 +0000651void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000652 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000653}
654
Sebastian Redlb3298c32010-08-18 23:56:48 +0000655void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000656 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000657 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000658 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000659}
660
Sebastian Redlb3298c32010-08-18 23:56:48 +0000661void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000662 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000663 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000664 // Delay type reading until after we have fully initialized the decl.
Richard Smith600adef2018-07-04 02:25:38 +0000665 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000666}
667
Richard Smith43ccec8e2014-08-26 03:52:16 +0000668ASTDeclReader::RedeclarableResult
669ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000670 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000671 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000672 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000673 if (Record.readInt()) { // isModed
674 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000675 TD->setModedTypeSourceInfo(TInfo, modedT);
676 } else
677 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000678 // Read and discard the declaration for which this is a typedef name for
679 // linkage, if it exists. We cannot rely on our type to pull in this decl,
680 // because it might have been merged with a type from another module and
681 // thus might not refer to our version of the declaration.
682 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000683 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000684}
685
686void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000687 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
688 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000689}
690
Richard Smithdda56e42011-04-15 14:24:37 +0000691void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000692 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000693 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000694 // Merged when we merge the template.
695 TD->setDescribedAliasTemplate(Template);
696 else
697 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000698}
699
Richard Smith1d209d02013-05-23 01:49:11 +0000700ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000701 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000702 VisitTypeDecl(TD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000703
David L. Jonesbe1557a2016-12-21 00:17:49 +0000704 TD->IdentifierNamespace = Record.readInt();
705 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000706 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000707 TD->setCompleteDefinition(Record.readInt());
708 TD->setEmbeddedInDeclarator(Record.readInt());
709 TD->setFreeStanding(Record.readInt());
710 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000711 TD->setBraceRange(ReadSourceRange());
Fangrui Song6907ce22018-07-30 19:24:48 +0000712
David L. Jonesbe1557a2016-12-21 00:17:49 +0000713 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000714 case 0:
715 break;
716 case 1: { // ExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000717 auto *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000718 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000719 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000720 break;
721 }
722 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000723 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000724 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000725 break;
Richard Smith70d58502014-08-30 00:04:23 +0000726 default:
727 llvm_unreachable("unexpected tag info kind");
728 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000729
Richard Smithcd45dbc2014-04-19 03:48:30 +0000730 if (!isa<CXXRecordDecl>(TD))
731 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000732 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000733}
734
Sebastian Redlb3298c32010-08-18 23:56:48 +0000735void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000736 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000737 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000738 ED->setIntegerTypeSourceInfo(TI);
739 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000740 ED->setIntegerType(Record.readType());
741 ED->setPromotionType(Record.readType());
742 ED->setNumPositiveBits(Record.readInt());
743 ED->setNumNegativeBits(Record.readInt());
Erich Keanef92f31c2018-08-01 20:48:16 +0000744 ED->setScoped(Record.readInt());
745 ED->setScopedUsingClassTag(Record.readInt());
746 ED->setFixed(Record.readInt());
Richard Smith4b38ded2012-03-14 23:13:10 +0000747
Erich Keanef92f31c2018-08-01 20:48:16 +0000748 ED->setHasODRHash(true);
Richard Trieuab4d7302018-07-25 22:52:05 +0000749 ED->ODRHash = Record.readInt();
750
Richard Smith01a73372013-10-15 22:02:41 +0000751 // If this is a definition subject to the ODR, and we already have a
752 // definition, merge this one into it.
Erich Keanef92f31c2018-08-01 20:48:16 +0000753 if (ED->isCompleteDefinition() &&
Richard Smith01a73372013-10-15 22:02:41 +0000754 Reader.getContext().getLangOpts().Modules &&
755 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000756 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
757 if (!OldDef) {
758 // This is the first time we've seen an imported definition. Look for a
759 // local definition before deciding that we are the first definition.
760 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
761 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
762 OldDef = D;
763 break;
764 }
765 }
766 }
767 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000768 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
Erich Keanef92f31c2018-08-01 20:48:16 +0000769 ED->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +0000770 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Trieuab4d7302018-07-25 22:52:05 +0000771 if (OldDef->getODRHash() != ED->getODRHash())
772 Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Richard Smith01a73372013-10-15 22:02:41 +0000773 } else {
774 OldDef = ED;
775 }
776 }
777
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000778 if (auto *InstED = ReadDeclAs<EnumDecl>()) {
779 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000780 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000781 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
782 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
783 }
Chris Lattner487412d2009-04-27 05:27:42 +0000784}
785
Richard Smith1d209d02013-05-23 01:49:11 +0000786ASTDeclReader::RedeclarableResult
787ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
788 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000789 RD->setHasFlexibleArrayMember(Record.readInt());
790 RD->setAnonymousStructOrUnion(Record.readInt());
791 RD->setHasObjectMember(Record.readInt());
792 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000793 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
794 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
795 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000796 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000797 RD->setArgPassingRestrictions((RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000798 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000799}
800
Sebastian Redlb3298c32010-08-18 23:56:48 +0000801void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000802 VisitNamedDecl(VD);
Richard Smith600adef2018-07-04 02:25:38 +0000803 // For function declarations, defer reading the type in case the function has
804 // a deduced return type that references an entity declared within the
805 // function.
806 if (isa<FunctionDecl>(VD))
807 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
808 else
809 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000810}
811
Sebastian Redlb3298c32010-08-18 23:56:48 +0000812void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000813 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000814 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000815 ECD->setInitExpr(Record.readExpr());
816 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000817 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000818}
819
Sebastian Redlb3298c32010-08-18 23:56:48 +0000820void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000821 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000822 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000823 if (Record.readInt()) { // hasExtInfo
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000824 auto *Info = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000825 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000826 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000827 }
Richard Smithc23d7342018-06-29 20:46:25 +0000828 QualType TSIType = Record.readType();
829 DD->setTypeSourceInfo(
830 TSIType.isNull() ? nullptr
831 : Reader.getContext().CreateTypeSourceInfo(TSIType));
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000832}
833
Sebastian Redlb3298c32010-08-18 23:56:48 +0000834void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000835 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000836 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000837
Richard Smith600adef2018-07-04 02:25:38 +0000838 // Attach a type to this function. Use the real type if possible, but fall
839 // back to the type as written if it involves a deduced return type.
840 if (FD->getTypeSourceInfo() &&
841 FD->getTypeSourceInfo()->getType()->castAs<FunctionType>()
842 ->getReturnType()->getContainedAutoType()) {
843 // We'll set up the real type in Visit, once we've finished loading the
844 // function.
845 FD->setType(FD->getTypeSourceInfo()->getType());
Richard Smitha62d1982018-08-03 01:00:01 +0000846 Reader.PendingFunctionTypes.push_back({FD, DeferredTypeID});
Richard Smith600adef2018-07-04 02:25:38 +0000847 } else {
848 FD->setType(Reader.GetType(DeferredTypeID));
Richard Smith600adef2018-07-04 02:25:38 +0000849 }
Richard Smitha62d1982018-08-03 01:00:01 +0000850 DeferredTypeID = 0;
Richard Smith600adef2018-07-04 02:25:38 +0000851
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000852 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000853 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000854
Douglas Gregorb2585692012-01-04 17:13:46 +0000855 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
856 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000857
Erich Keane9c665062018-08-01 21:02:40 +0000858 FD->setStorageClass(static_cast<StorageClass>(Record.readInt()));
859 FD->setInlineSpecified(Record.readInt());
860 FD->setImplicitlyInline(Record.readInt());
Erich Keane9c665062018-08-01 21:02:40 +0000861 FD->setVirtualAsWritten(Record.readInt());
862 FD->setPure(Record.readInt());
863 FD->setHasInheritedPrototype(Record.readInt());
864 FD->setHasWrittenPrototype(Record.readInt());
865 FD->setDeletedAsWritten(Record.readInt());
866 FD->setTrivial(Record.readInt());
867 FD->setTrivialForCall(Record.readInt());
868 FD->setDefaulted(Record.readInt());
869 FD->setExplicitlyDefaulted(Record.readInt());
870 FD->setHasImplicitReturnZero(Record.readInt());
871 FD->setConstexpr(Record.readInt());
872 FD->setUsesSEHTry(Record.readInt());
873 FD->setHasSkippedBody(Record.readInt());
874 FD->setIsMultiVersion(Record.readInt());
875 FD->setLateTemplateParsed(Record.readInt());
876
877 FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000878 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000879
Richard Trieue6caa262017-12-23 00:41:01 +0000880 FD->ODRHash = Record.readInt();
Erich Keane9c665062018-08-01 21:02:40 +0000881 FD->setHasODRHash(true);
Richard Trieue6caa262017-12-23 00:41:01 +0000882
David L. Jonesbe1557a2016-12-21 00:17:49 +0000883 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000884 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000885 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000886 break;
887 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000888 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000889 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000890 break;
891 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000892 auto *InstFD = ReadDeclAs<FunctionDecl>();
893 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000894 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000895 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000896 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000897 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000898 break;
899 }
900 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000901 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
902 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000903
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000904 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000905 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000906 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000907
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000908 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000909 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000910 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000911 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000912 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000913 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000914 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000915 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000916 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000917
918 LAngleLoc = ReadSourceLocation();
919 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000920 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000921
922 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000923
Douglas Gregor4163aca2011-09-09 21:34:22 +0000924 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000925 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000926 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000927 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000928 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000929 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Richard Smithf19a8b02019-05-02 00:49:14 +0000930
931 MemberSpecializationInfo *MSInfo = nullptr;
932 if (Record.readInt()) {
933 auto *FD = ReadDeclAs<FunctionDecl>();
934 auto TSK = (TemplateSpecializationKind)Record.readInt();
935 SourceLocation POI = ReadSourceLocation();
936
937 MSInfo = new (C) MemberSpecializationInfo(FD, TSK);
938 MSInfo->setPointOfInstantiation(POI);
939 }
940
941 FunctionTemplateSpecializationInfo *FTInfo =
942 FunctionTemplateSpecializationInfo::Create(
943 C, FD, Template, TSK, TemplArgList,
944 HasTemplateArgumentsAsWritten ? &TemplArgsInfo : nullptr, POI,
945 MSInfo);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000946 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000947
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000948 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000949 // The template that contains the specializations set. It's not safe to
950 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000951 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000952 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
953 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
954 // FunctionTemplateSpecializationInfo's Profile().
955 // We avoid getASTContext because a decl in the parent hierarchy may
956 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000957 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000958 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000959 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000960 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000961 FunctionTemplateSpecializationInfo *ExistingInfo =
962 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000963 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000964 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
965 else {
966 assert(Reader.getContext().getLangOpts().Modules &&
967 "already deserialized this template specialization");
Richard Smithf19a8b02019-05-02 00:49:14 +0000968 mergeRedeclarable(FD, ExistingInfo->getFunction(), Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000969 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000970 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000971 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000972 }
973 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
974 // Templates.
975 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000976 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000977 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000978 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
979
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000980 // Templates args.
981 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000982 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000983 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000984 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000985 TemplArgs.setLAngleLoc(ReadSourceLocation());
986 TemplArgs.setRAngleLoc(ReadSourceLocation());
987
Douglas Gregor4163aca2011-09-09 21:34:22 +0000988 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000989 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000990 // These are not merged; we don't need to merge redeclarations of dependent
991 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000992 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000993 }
994 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000995
Chris Lattnerca025db2010-05-07 21:43:38 +0000996 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000997 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000998 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000999 Params.reserve(NumParams);
1000 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001001 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001002 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +00001003}
1004
Sebastian Redlb3298c32010-08-18 23:56:48 +00001005void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001006 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001007 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00001008 // Load the body on-demand. Most clients won't care, because method
1009 // definitions rarely show up in headers.
1010 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1011 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001012 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1013 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001014 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001015 MD->setInstanceMethod(Record.readInt());
1016 MD->setVariadic(Record.readInt());
1017 MD->setPropertyAccessor(Record.readInt());
1018 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001019 MD->setOverriding(Record.readInt());
1020 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001021
Erich Keane9b18eca2018-08-01 21:31:08 +00001022 MD->setIsRedeclaration(Record.readInt());
1023 MD->setHasRedeclaration(Record.readInt());
1024 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001025 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001026 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001027
David L. Jonesbe1557a2016-12-21 00:17:49 +00001028 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1029 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001030 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001031 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001032 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1033 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001034 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001035 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001036 Params.reserve(NumParams);
1037 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001038 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001039
Erich Keane9b18eca2018-08-01 21:31:08 +00001040 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001041 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001042 SmallVector<SourceLocation, 16> SelLocs;
1043 SelLocs.reserve(NumStoredSelLocs);
1044 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001045 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001046
1047 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001048}
1049
Douglas Gregor85f3f952015-07-07 03:57:15 +00001050void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001051 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001052
David L. Jonesbe1557a2016-12-21 00:17:49 +00001053 D->Variance = Record.readInt();
1054 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001055 D->VarianceLoc = ReadSourceLocation();
1056 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001057}
1058
Sebastian Redlb3298c32010-08-18 23:56:48 +00001059void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001060 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001061 CD->setAtStartLoc(ReadSourceLocation());
1062 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001063}
1064
Douglas Gregor85f3f952015-07-07 03:57:15 +00001065ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001066 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001067 if (numParams == 0)
1068 return nullptr;
1069
1070 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1071 typeParams.reserve(numParams);
1072 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001073 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001074 if (!typeParam)
1075 return nullptr;
1076
1077 typeParams.push_back(typeParam);
1078 }
1079
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001080 SourceLocation lAngleLoc = ReadSourceLocation();
1081 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001082
1083 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1084 typeParams, rAngleLoc);
1085}
1086
Manman Renec315f12016-09-09 23:48:27 +00001087void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001088 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001089 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001090 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001091
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001092 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001093 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001094
Manman Renec315f12016-09-09 23:48:27 +00001095 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001096 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001097 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1098 Protocols.reserve(NumProtocols);
1099 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001100 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001101 SmallVector<SourceLocation, 16> ProtoLocs;
1102 ProtoLocs.reserve(NumProtocols);
1103 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001104 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001105 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1106 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001107
Manman Renec315f12016-09-09 23:48:27 +00001108 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001109 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001110 Protocols.clear();
1111 Protocols.reserve(NumProtocols);
1112 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001113 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001114 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1115 Reader.getContext());
1116}
1117
1118void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1119 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1120 // FIXME: odr checking?
1121}
1122
Sebastian Redlb3298c32010-08-18 23:56:48 +00001123void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001124 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001125 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001126 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001127 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001128
1129 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001130 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001131 // Read the definition.
1132 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001133
David L. Jonesbe1557a2016-12-21 00:17:49 +00001134 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001135 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1136 if (Canon->Data.getPointer()) {
1137 // If we already have a definition, keep the definition invariant and
1138 // merge the data.
1139 MergeDefinitionData(Canon, std::move(ID->data()));
1140 ID->Data = Canon->Data;
1141 } else {
1142 // Set the definition data of the canonical declaration, so other
1143 // redeclarations will see it.
1144 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001145
Manman Renec315f12016-09-09 23:48:27 +00001146 // We will rebuild this list lazily.
1147 ID->setIvarList(nullptr);
1148 }
Craig Toppera13603a2014-05-22 05:54:18 +00001149
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001150 // Note that we have deserialized a definition.
1151 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001152
Douglas Gregor404cdde2012-01-27 01:47:08 +00001153 // Note that we've loaded this Objective-C class.
1154 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001155 } else {
1156 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001157 }
Chris Lattner487412d2009-04-27 05:27:42 +00001158}
1159
Sebastian Redlb3298c32010-08-18 23:56:48 +00001160void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001161 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001162 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001163 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001164 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001165 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001166 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001167}
1168
Graydon Hoaree0a68352017-06-28 18:36:27 +00001169void ASTDeclReader::ReadObjCDefinitionData(
1170 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001171 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001172 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1173 ProtoRefs.reserve(NumProtoRefs);
1174 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001175 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001176 SmallVector<SourceLocation, 16> ProtoLocs;
1177 ProtoLocs.reserve(NumProtoRefs);
1178 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001179 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001180 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1181 ProtoLocs.data(), Reader.getContext());
1182}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001183
Graydon Hoaree0a68352017-06-28 18:36:27 +00001184void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1185 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1186 // FIXME: odr checking?
1187}
1188
1189void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1190 RedeclarableResult Redecl = VisitRedeclarable(PD);
1191 VisitObjCContainerDecl(PD);
1192 mergeRedeclarable(PD, Redecl);
1193
1194 if (Record.readInt()) {
1195 // Read the definition.
1196 PD->allocateDefinitionData();
1197
1198 ReadObjCDefinitionData(PD->data());
1199
1200 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1201 if (Canon->Data.getPointer()) {
1202 // If we already have a definition, keep the definition invariant and
1203 // merge the data.
1204 MergeDefinitionData(Canon, std::move(PD->data()));
1205 PD->Data = Canon->Data;
1206 } else {
1207 // Set the definition data of the canonical declaration, so other
1208 // redeclarations will see it.
1209 PD->getCanonicalDecl()->Data = PD->Data;
1210 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001211 // Note that we have deserialized a definition.
1212 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001213 } else {
1214 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001215 }
Chris Lattner487412d2009-04-27 05:27:42 +00001216}
1217
Sebastian Redlb3298c32010-08-18 23:56:48 +00001218void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001219 VisitFieldDecl(FD);
1220}
1221
Sebastian Redlb3298c32010-08-18 23:56:48 +00001222void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001223 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001224 CD->setCategoryNameLoc(ReadSourceLocation());
1225 CD->setIvarLBraceLoc(ReadSourceLocation());
1226 CD->setIvarRBraceLoc(ReadSourceLocation());
1227
Douglas Gregor404cdde2012-01-27 01:47:08 +00001228 // Note that this category has been deserialized. We do this before
1229 // deserializing the interface declaration, so that it will consider this
1230 /// category.
1231 Reader.CategoriesDeserialized.insert(CD);
1232
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001233 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001234 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001235 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001236 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001237 ProtoRefs.reserve(NumProtoRefs);
1238 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001239 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001240 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001241 ProtoLocs.reserve(NumProtoRefs);
1242 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001243 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001244 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001245 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001246
1247 // Protocols in the class extension belong to the class.
1248 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1249 CD->ClassInterface->mergeClassExtensionProtocolList(
1250 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1251 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001252}
1253
Sebastian Redlb3298c32010-08-18 23:56:48 +00001254void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001255 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001256 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001257}
1258
Sebastian Redlb3298c32010-08-18 23:56:48 +00001259void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001260 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001261 D->setAtLoc(ReadSourceLocation());
1262 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001263 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001264 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001265 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001266 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001267 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001268 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001269 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001270 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001271 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001272 DeclarationName GetterName = Record.readDeclarationName();
1273 SourceLocation GetterLoc = ReadSourceLocation();
1274 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1275 DeclarationName SetterName = Record.readDeclarationName();
1276 SourceLocation SetterLoc = ReadSourceLocation();
1277 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001278 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1279 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1280 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001281}
1282
Sebastian Redlb3298c32010-08-18 23:56:48 +00001283void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001284 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001285 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001286}
1287
Sebastian Redlb3298c32010-08-18 23:56:48 +00001288void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001289 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001290 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001291}
1292
Sebastian Redlb3298c32010-08-18 23:56:48 +00001293void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001294 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001295 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1296 D->SuperLoc = ReadSourceLocation();
1297 D->setIvarLBraceLoc(ReadSourceLocation());
1298 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001299 D->setHasNonZeroConstructors(Record.readInt());
1300 D->setHasDestructors(Record.readInt());
1301 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001302 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001303 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001304}
1305
Sebastian Redlb3298c32010-08-18 23:56:48 +00001306void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001307 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001308 D->setAtLoc(ReadSourceLocation());
1309 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1310 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1311 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001312 D->setGetterCXXConstructor(Record.readExpr());
1313 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001314}
1315
Sebastian Redlb3298c32010-08-18 23:56:48 +00001316void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001317 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001318 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001319
1320 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1321 FD->InitStorage.setInt(ISK);
1322 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1323 ? Record.readType().getAsOpaquePtr()
1324 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001325 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001326
1327 if (auto *BW = Record.readExpr())
1328 FD->setBitWidth(BW);
1329
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001330 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001331 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001332 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001333 }
Richard Smith0b87e072013-10-07 08:02:11 +00001334 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001335}
1336
John McCall5e77d762013-04-16 07:28:30 +00001337void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1338 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001339 PD->GetterId = Record.getIdentifierInfo();
1340 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001341}
1342
Francois Pichet783dd6e2010-11-21 06:08:52 +00001343void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1344 VisitValueDecl(FD);
1345
David L. Jonesbe1557a2016-12-21 00:17:49 +00001346 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001347 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001348 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001349
1350 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001351 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001352
1353 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001354}
1355
Larisse Voufo39a1e502013-08-06 01:03:05 +00001356ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001357 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001358 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001359
David L. Jonesbe1557a2016-12-21 00:17:49 +00001360 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1361 VD->VarDeclBits.TSCSpec = Record.readInt();
1362 VD->VarDeclBits.InitStyle = Record.readInt();
Erik Pilkington1e368822019-01-04 18:33:06 +00001363 VD->VarDeclBits.ARCPseudoStrong = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001364 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001365 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1366 Record.readInt();
1367 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001368 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001369 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001370 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001371 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1372 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1373 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1374 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1375 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001376 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
Akira Hatanaka8e57b072018-10-01 21:51:28 +00001377 VD->NonParmVarDeclBits.EscapingByref = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001378 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001379 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001380 VD->setCachedLinkage(VarLinkage);
1381
1382 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001383 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001384 VD->getLexicalDeclContext()->isFunctionOrMethod())
1385 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001386
David L. Jonesbe1557a2016-12-21 00:17:49 +00001387 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001388 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001389 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001390 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1391 Eval->CheckedICE = true;
1392 Eval->IsICE = Val == 3;
1393 }
1394 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001395
Akira Hatanaka9978da32018-08-10 15:09:24 +00001396 if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1397 Expr *CopyExpr = Record.readExpr();
1398 if (CopyExpr)
1399 Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1400 }
1401
Richard Smitha4653622017-09-06 20:01:14 +00001402 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1403 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1404
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001405 enum VarKind {
1406 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1407 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001408 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001409 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001410 // Only true variables (not parameters or implicit parameters) can be
1411 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001412 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1413 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001414 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001415 break;
1416 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001417 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001418 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001419 break;
1420 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001421 auto *Tmpl = ReadDeclAs<VarDecl>();
1422 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001423 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001424 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001425 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001426 break;
1427 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001428 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001429
1430 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001431}
1432
Sebastian Redlb3298c32010-08-18 23:56:48 +00001433void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001434 VisitVarDecl(PD);
1435}
1436
Sebastian Redlb3298c32010-08-18 23:56:48 +00001437void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001438 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001439 unsigned isObjCMethodParam = Record.readInt();
1440 unsigned scopeDepth = Record.readInt();
1441 unsigned scopeIndex = Record.readInt();
1442 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001443 if (isObjCMethodParam) {
1444 assert(scopeDepth == 0);
1445 PD->setObjCMethodScopeInfo(scopeIndex);
1446 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1447 } else {
1448 PD->setScopeInfo(scopeDepth, scopeIndex);
1449 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001450 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1451 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1452 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001453 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001454
1455 // FIXME: If this is a redeclaration of a function from another module, handle
1456 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001457}
1458
Richard Smith7b76d812016-08-12 02:21:25 +00001459void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1460 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001461 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith7b76d812016-08-12 02:21:25 +00001462 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001463 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001464}
1465
1466void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1467 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001468 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001469}
1470
Sebastian Redlb3298c32010-08-18 23:56:48 +00001471void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001472 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001473 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001474 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001475}
1476
Sebastian Redlb3298c32010-08-18 23:56:48 +00001477void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001478 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001479 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001480 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001481 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001482 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001483 Params.reserve(NumParams);
1484 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001485 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001486 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001487
David L. Jonesbe1557a2016-12-21 00:17:49 +00001488 BD->setIsVariadic(Record.readInt());
1489 BD->setBlockMissingReturnType(Record.readInt());
1490 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001491 BD->setDoesNotEscape(Record.readInt());
Akira Hatanakac5792aa2019-02-27 18:17:16 +00001492 BD->setCanAvoidCopyToHeap(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001493
David L. Jonesbe1557a2016-12-21 00:17:49 +00001494 bool capturesCXXThis = Record.readInt();
1495 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001496 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001497 captures.reserve(numCaptures);
1498 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001499 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001500 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001501 bool byRef = (flags & 1);
1502 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001503 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001504
1505 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1506 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001507 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001508}
1509
Ben Langmuirce914fc2013-05-03 19:20:19 +00001510void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1511 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001512 unsigned ContextParamPos = Record.readInt();
1513 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001514 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001515 for (unsigned I = 0; I < CD->NumParams; ++I) {
1516 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001517 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001518 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001519 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001520 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001521}
1522
Sebastian Redlb3298c32010-08-18 23:56:48 +00001523void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001524 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001525 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001526 D->setExternLoc(ReadSourceLocation());
1527 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001528}
1529
Richard Smith8df390f2016-09-08 23:14:54 +00001530void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1531 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001532 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001533}
1534
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001535void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1536 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001537 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001538}
1539
Sebastian Redlb3298c32010-08-18 23:56:48 +00001540void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001541 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001542 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001543 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001544 D->LocStart = ReadSourceLocation();
1545 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001546
Richard Smith15e32fd2015-03-17 02:23:11 +00001547 // Defer loading the anonymous namespace until we've finished merging
1548 // this namespace; loading it might load a later declaration of the
1549 // same namespace, and we have an invariant that older declarations
1550 // get merged before newer ones try to merge.
1551 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001552 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001553 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001554 } else {
1555 // Link this namespace back to the first declaration, which has already
1556 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001557 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001558 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001559
1560 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001561
1562 if (AnonNamespace) {
1563 // Each module has its own anonymous namespace, which is disjoint from
1564 // any other module's anonymous namespaces, so don't attach the anonymous
1565 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001566 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001567 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001568 D->setAnonymousNamespace(Anon);
1569 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001570}
1571
Sebastian Redlb3298c32010-08-18 23:56:48 +00001572void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001573 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001574 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001575 D->NamespaceLoc = ReadSourceLocation();
1576 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001577 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001578 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001579 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001580}
1581
Sebastian Redlb3298c32010-08-18 23:56:48 +00001582void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001583 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001584 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001585 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001586 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1587 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001588 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001589 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001590 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001591 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001592}
1593
Richard Smith151c4562016-12-20 21:35:28 +00001594void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1595 VisitNamedDecl(D);
1596 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001597 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001598 for (unsigned I = 0; I != D->NumExpansions; ++I)
1599 Expansions[I] = ReadDeclAs<NamedDecl>();
1600 mergeMergeable(D);
1601}
1602
Sebastian Redlb3298c32010-08-18 23:56:48 +00001603void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001604 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001605 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001606 D->Underlying = ReadDeclAs<NamedDecl>();
1607 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001608 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001609 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001610 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001611 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001612 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001613}
1614
Richard Smith5179eb72016-06-28 19:03:57 +00001615void ASTDeclReader::VisitConstructorUsingShadowDecl(
1616 ConstructorUsingShadowDecl *D) {
1617 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001618 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1619 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001620 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001621}
1622
Sebastian Redlb3298c32010-08-18 23:56:48 +00001623void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001624 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001625 D->UsingLoc = ReadSourceLocation();
1626 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001627 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001628 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1629 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001630}
1631
Sebastian Redlb3298c32010-08-18 23:56:48 +00001632void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001633 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001634 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001635 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001636 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001637 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001638 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001639}
1640
Sebastian Redlb3298c32010-08-18 23:56:48 +00001641void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001642 UnresolvedUsingTypenameDecl *D) {
1643 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001644 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001645 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001646 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001647 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001648}
1649
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001650void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001651 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001652 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001653 Data.UserDeclaredConstructor = Record.readInt();
1654 Data.UserDeclaredSpecialMembers = Record.readInt();
1655 Data.Aggregate = Record.readInt();
1656 Data.PlainOldData = Record.readInt();
1657 Data.Empty = Record.readInt();
1658 Data.Polymorphic = Record.readInt();
1659 Data.Abstract = Record.readInt();
1660 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001661 Data.IsCXX11StandardLayout = Record.readInt();
1662 Data.HasBasesWithFields = Record.readInt();
1663 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001664 Data.HasPrivateFields = Record.readInt();
1665 Data.HasProtectedFields = Record.readInt();
1666 Data.HasPublicFields = Record.readInt();
1667 Data.HasMutableFields = Record.readInt();
1668 Data.HasVariantMembers = Record.readInt();
1669 Data.HasOnlyCMembers = Record.readInt();
1670 Data.HasInClassInitializer = Record.readInt();
1671 Data.HasUninitializedReferenceMember = Record.readInt();
1672 Data.HasUninitializedFields = Record.readInt();
1673 Data.HasInheritedConstructor = Record.readInt();
1674 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001675 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001676 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1677 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1678 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001679 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001680 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1681 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1682 Data.DefaultedDestructorIsDeleted = Record.readInt();
1683 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001684 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001685 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001686 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001687 Data.HasIrrelevantDestructor = Record.readInt();
1688 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1689 Data.HasDefaultedDefaultConstructor = Record.readInt();
1690 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1691 Data.HasConstexprDefaultConstructor = Record.readInt();
1692 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1693 Data.ComputedVisibleConversions = Record.readInt();
1694 Data.UserProvidedDefaultConstructor = Record.readInt();
1695 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001696 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1697 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001698 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1699 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1700 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001701 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001702 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001703
Richard Smithcd4a7a42017-09-07 00:55:55 +00001704 if (Record.readInt())
1705 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001706
David L. Jonesbe1557a2016-12-21 00:17:49 +00001707 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001708 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001709 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001710 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001711 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001712 Data.VBases = ReadGlobalOffset();
1713
David L. Jonesb6a8f022016-12-21 04:34:52 +00001714 Record.readUnresolvedSet(Data.Conversions);
1715 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001716 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001717 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001718
Douglas Gregor99ae8062012-02-14 17:54:36 +00001719 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001720 using Capture = LambdaCapture;
1721
1722 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001723 Lambda.Dependent = Record.readInt();
1724 Lambda.IsGenericLambda = Record.readInt();
1725 Lambda.CaptureDefault = Record.readInt();
1726 Lambda.NumCaptures = Record.readInt();
1727 Lambda.NumExplicitCaptures = Record.readInt();
1728 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001729 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001730 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1731 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001732 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001733 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001734 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001735 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001736 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001737 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001738 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001739 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001740 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001741 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001742 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001743 break;
1744 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001745 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001746 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001747 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001748 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1749 break;
1750 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001751 }
1752 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001753}
1754
Richard Smithcd45dbc2014-04-19 03:48:30 +00001755void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001756 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001757 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001758 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001759 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001760
Richard Smith02793752015-03-27 21:16:39 +00001761 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001762 // Track that we merged the definitions.
1763 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1764 DD.Definition));
1765 Reader.PendingDefinitions.erase(MergeDD.Definition);
Erich Keanef92f31c2018-08-01 20:48:16 +00001766 MergeDD.Definition->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +00001767 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001768 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1769 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001770 }
1771
Richard Smith2a9e5c52015-02-03 03:32:14 +00001772 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1773 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1774 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001775 // We faked up this definition data because we found a class for which we'd
1776 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001777 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001778 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001779
1780 // Don't change which declaration is the definition; that is required
1781 // to be invariant once we select it.
1782 auto *Def = DD.Definition;
1783 DD = std::move(MergeDD);
1784 DD.Definition = Def;
1785 return;
1786 }
1787
Richard Smithcd45dbc2014-04-19 03:48:30 +00001788 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001789 bool DetectedOdrViolation = false;
1790#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1791#define MATCH_FIELD(Field) \
1792 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1793 OR_FIELD(Field)
1794 MATCH_FIELD(UserDeclaredConstructor)
1795 MATCH_FIELD(UserDeclaredSpecialMembers)
1796 MATCH_FIELD(Aggregate)
1797 MATCH_FIELD(PlainOldData)
1798 MATCH_FIELD(Empty)
1799 MATCH_FIELD(Polymorphic)
1800 MATCH_FIELD(Abstract)
1801 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001802 MATCH_FIELD(IsCXX11StandardLayout)
1803 MATCH_FIELD(HasBasesWithFields)
1804 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001805 MATCH_FIELD(HasPrivateFields)
1806 MATCH_FIELD(HasProtectedFields)
1807 MATCH_FIELD(HasPublicFields)
1808 MATCH_FIELD(HasMutableFields)
1809 MATCH_FIELD(HasVariantMembers)
1810 MATCH_FIELD(HasOnlyCMembers)
1811 MATCH_FIELD(HasInClassInitializer)
1812 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001813 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001814 MATCH_FIELD(HasInheritedConstructor)
1815 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001816 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001817 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1818 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1819 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001820 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001821 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1822 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1823 MATCH_FIELD(DefaultedDestructorIsDeleted)
1824 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001825 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001826 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001827 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001828 MATCH_FIELD(HasIrrelevantDestructor)
1829 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001830 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001831 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1832 OR_FIELD(HasConstexprDefaultConstructor)
1833 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1834 // ComputedVisibleConversions is handled below.
1835 MATCH_FIELD(UserProvidedDefaultConstructor)
1836 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001837 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1838 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001839 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1840 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1841 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1842 MATCH_FIELD(IsLambda)
1843#undef OR_FIELD
1844#undef MATCH_FIELD
1845
1846 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1847 DetectedOdrViolation = true;
1848 // FIXME: Issue a diagnostic if the base classes don't match when we come
1849 // to lazily load them.
1850
1851 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1852 // match when we come to lazily load them.
1853 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1854 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1855 DD.ComputedVisibleConversions = true;
1856 }
1857
1858 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1859 // lazily load it.
1860
1861 if (DD.IsLambda) {
1862 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1863 // when they occur within the body of a function template specialization).
1864 }
1865
Richard Trieufd1acbb2017-04-11 21:31:00 +00001866 if (D->getODRHash() != MergeDD.ODRHash) {
1867 DetectedOdrViolation = true;
1868 }
1869
Richard Smithcd45dbc2014-04-19 03:48:30 +00001870 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001871 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1872 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001873}
1874
Richard Smith2a9e5c52015-02-03 03:32:14 +00001875void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001876 struct CXXRecordDecl::DefinitionData *DD;
1877 ASTContext &C = Reader.getContext();
1878
1879 // Determine whether this is a lambda closure type, so that we can
1880 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001881 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001882 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001883 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001884 LCD_None);
1885 else
1886 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1887
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001888 CXXRecordDecl *Canon = D->getCanonicalDecl();
1889 // Set decl definition data before reading it, so that during deserialization
1890 // when we read CXXRecordDecl, it already has definition data and we don't
1891 // set fake one.
1892 if (!Canon->DefinitionData)
1893 Canon->DefinitionData = DD;
1894 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001895 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001896
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001897 // We might already have a different definition for this record. This can
1898 // happen either because we're reading an update record, or because we've
1899 // already done some merging. Either way, just merge into it.
1900 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001901 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001902 return;
1903 }
1904
Richard Smith97100e32015-06-20 00:22:34 +00001905 // Mark this declaration as being a definition.
Erich Keanef92f31c2018-08-01 20:48:16 +00001906 D->setCompleteDefinition(true);
Richard Smith2a9e5c52015-02-03 03:32:14 +00001907
Richard Smith97100e32015-06-20 00:22:34 +00001908 // If this is not the first declaration or is an update record, we can have
1909 // other redeclarations already. Make a note that we need to propagate the
1910 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001911 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001912 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001913}
1914
Richard Smith1d209d02013-05-23 01:49:11 +00001915ASTDeclReader::RedeclarableResult
1916ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1917 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001918
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001919 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001920
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001921 enum CXXRecKind {
1922 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1923 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001924 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001925 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001926 // Merged when we merge the folding set entry in the primary template.
1927 if (!isa<ClassTemplateSpecializationDecl>(D))
1928 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001929 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001930 case CXXRecTemplate: {
1931 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001932 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001933 D->TemplateOrInstantiation = Template;
1934 if (!Template->getTemplatedDecl()) {
1935 // We've not actually loaded the ClassTemplateDecl yet, because we're
1936 // currently being loaded as its pattern. Rely on it to set up our
1937 // TypeForDecl (see VisitClassTemplateDecl).
1938 //
1939 // Beware: we do not yet know our canonical declaration, and may still
1940 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001941 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001942 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001943 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001944 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001945 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001946 auto *RD = ReadDeclAs<CXXRecordDecl>();
1947 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001948 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001949 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1950 MSI->setPointOfInstantiation(POI);
1951 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001952 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001953 break;
1954 }
1955 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001956
David L. Jonesbe1557a2016-12-21 00:17:49 +00001957 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001958 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001959 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001960 else
1961 // Propagate DefinitionData pointer from the canonical declaration.
1962 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1963
Richard Smith676c4042013-08-29 23:59:27 +00001964 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001965 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001966 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001967 DeclID KeyFn = ReadDeclID();
Erich Keanef92f31c2018-08-01 20:48:16 +00001968 if (KeyFn && D->isCompleteDefinition())
Richard Smitha9a1c682014-07-07 06:38:20 +00001969 // FIXME: This is wrong for the ARM ABI, where some other module may have
1970 // made this function no longer be a key function. We need an update
1971 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001972 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001973 }
Richard Smith1d209d02013-05-23 01:49:11 +00001974
1975 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001976}
1977
Richard Smithbc491202017-02-17 20:05:37 +00001978void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
Richard Smith76b90272019-05-09 03:59:21 +00001979 D->setExplicitSpecifier(Record.readExplicitSpec());
Richard Smithbc491202017-02-17 20:05:37 +00001980 VisitFunctionDecl(D);
Erich Keane9c665062018-08-01 21:02:40 +00001981 D->setIsCopyDeductionCandidate(Record.readInt());
Richard Smithbc491202017-02-17 20:05:37 +00001982}
1983
Sebastian Redlb3298c32010-08-18 23:56:48 +00001984void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001985 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001986
David L. Jonesbe1557a2016-12-21 00:17:49 +00001987 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001988 if (D->isCanonicalDecl()) {
1989 while (NumOverridenMethods--) {
1990 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1991 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001992 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001993 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1994 }
1995 } else {
1996 // We don't care about which declarations this used to override; we get
1997 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001998 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001999 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002000}
2001
Sebastian Redlb3298c32010-08-18 23:56:48 +00002002void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00002003 // We need the inherited constructor information to merge the declaration,
2004 // so we have to read it before we call VisitCXXMethodDecl.
Richard Smith76b90272019-05-09 03:59:21 +00002005 D->setExplicitSpecifier(Record.readExplicitSpec());
Richard Smith5179eb72016-06-28 19:03:57 +00002006 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002007 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
2008 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00002009 *D->getTrailingObjects<InheritedConstructor>() =
2010 InheritedConstructor(Shadow, Ctor);
2011 }
2012
Chris Lattnerca025db2010-05-07 21:43:38 +00002013 VisitCXXMethodDecl(D);
2014}
2015
Sebastian Redlb3298c32010-08-18 23:56:48 +00002016void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002017 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00002018
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002019 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002020 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00002021 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00002022 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00002023 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00002024 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00002025 Canon->OperatorDeleteThisArg = ThisArg;
2026 }
Richard Smithf8134002015-03-10 01:41:22 +00002027 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002028}
2029
Sebastian Redlb3298c32010-08-18 23:56:48 +00002030void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Richard Smith76b90272019-05-09 03:59:21 +00002031 D->setExplicitSpecifier(Record.readExplicitSpec());
Chris Lattnerca025db2010-05-07 21:43:38 +00002032 VisitCXXMethodDecl(D);
2033}
2034
Douglas Gregorba345522011-12-02 23:23:56 +00002035void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2036 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002037 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002038 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002039 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002040 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002041 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002042 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002043}
2044
Sebastian Redlb3298c32010-08-18 23:56:48 +00002045void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002046 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002047 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002048}
2049
Sebastian Redlb3298c32010-08-18 23:56:48 +00002050void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002051 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002052 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002053 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002054 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002055 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002056 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002057 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002058 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002059 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002060 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002061 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002062}
2063
Sebastian Redlb3298c32010-08-18 23:56:48 +00002064void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002065 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002066 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002067 D->NumParams = NumParams;
2068 D->Params = new TemplateParameterList*[NumParams];
2069 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002070 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002071 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002072 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002073 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002074 D->Friend = GetTypeSourceInfo();
2075 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002076}
2077
Richard Smithf17fdbd2014-04-24 02:25:27 +00002078DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002079 VisitNamedDecl(D);
2080
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002081 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002082 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002083 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002084 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002085 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002086
Richard Smithf17fdbd2014-04-24 02:25:27 +00002087 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002088}
2089
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002090ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002091ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002092 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002093
Douglas Gregor68444de2012-01-14 15:13:49 +00002094 // Make sure we've allocated the Common pointer first. We do this before
2095 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2096 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2097 if (!CanonD->Common) {
2098 CanonD->Common = CanonD->newCommon(Reader.getContext());
2099 Reader.PendingDefinitions.insert(CanonD);
2100 }
2101 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002102
Douglas Gregor68444de2012-01-14 15:13:49 +00002103 // If this is the first declaration of the template, fill in the information
2104 // for the 'common' pointer.
2105 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002106 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002107 assert(RTD->getKind() == D->getKind() &&
2108 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002109 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002110 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002111 D->setMemberSpecialization();
2112 }
2113 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002114
Richard Smithf17fdbd2014-04-24 02:25:27 +00002115 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002116 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002117
Richard Smithf17fdbd2014-04-24 02:25:27 +00002118 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002119
Richard Smithd46d6de2013-10-13 23:50:45 +00002120 // If we merged the template with a prior declaration chain, merge the common
2121 // pointer.
2122 // FIXME: Actually merge here, don't just overwrite.
2123 D->Common = D->getCanonicalDecl()->Common;
2124
Douglas Gregor68444de2012-01-14 15:13:49 +00002125 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002126}
2127
Sebastian Redlb3298c32010-08-18 23:56:48 +00002128void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002129 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002130
Douglas Gregor68444de2012-01-14 15:13:49 +00002131 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002132 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2133 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002134 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002135 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002136 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002137 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002138
2139 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2140 // We were loaded before our templated declaration was. We've not set up
2141 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2142 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002143 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002144 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002145 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002146}
2147
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002148void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2149 llvm_unreachable("BuiltinTemplates are not serialized");
2150}
2151
Larisse Voufo30616382013-08-23 22:21:36 +00002152/// TODO: Unify with ClassTemplateDecl version?
2153/// May require unifying ClassTemplateDecl and
2154/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002155void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2156 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2157
2158 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002159 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002160 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002161 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002162 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002163 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002164 }
2165}
2166
Richard Smith1d209d02013-05-23 01:49:11 +00002167ASTDeclReader::RedeclarableResult
2168ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2169 ClassTemplateSpecializationDecl *D) {
2170 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002171
Douglas Gregor4163aca2011-09-09 21:34:22 +00002172 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002173 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002174 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002175 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002176 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002177 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002178 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002179 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002180 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002181 auto *PS =
2182 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002183 SpecializedPartialSpecialization();
2184 PS->PartialSpecialization
2185 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2186 PS->TemplateArgs = ArgList;
2187 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002188 }
2189 }
2190
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002191 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002192 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002193 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002194 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002195 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002196
David L. Jonesbe1557a2016-12-21 00:17:49 +00002197 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002198 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002199 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002200 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002201 // Set this as, or find, the canonical declaration for this specialization
2202 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002203 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002204 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002205 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002206 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002207 CanonSpec =
2208 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2209 }
2210 // If there was already a canonical specialization, merge into it.
2211 if (CanonSpec != D) {
2212 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2213
2214 // This declaration might be a definition. Merge with any existing
2215 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002216 if (auto *DDD = D->DefinitionData) {
2217 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002218 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002219 else
Richard Smith053f6c62014-05-16 23:01:30 +00002220 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002221 }
Richard Smith547864d2014-07-11 18:22:58 +00002222 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002223 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002224 }
2225 }
Richard Smith1d209d02013-05-23 01:49:11 +00002226
Richard Smithd55889a2013-09-09 16:55:27 +00002227 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002228 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002229 auto *ExplicitInfo =
2230 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002231 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002232 ExplicitInfo->ExternLoc = ReadSourceLocation();
2233 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002234 D->ExplicitInfo = ExplicitInfo;
2235 }
2236
Richard Smith1d209d02013-05-23 01:49:11 +00002237 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002238}
2239
Sebastian Redlb3298c32010-08-18 23:56:48 +00002240void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002241 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002242 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002243
David L. Jonesb6a8f022016-12-21 04:34:52 +00002244 D->TemplateParams = Record.readTemplateParameterList();
2245 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002246
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002247 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002248 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002249 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002250 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002251 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002252 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002253}
2254
Sebastian Redlb7448632011-08-31 13:59:56 +00002255void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2256 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002257 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002258 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Richard Smithf19a8b02019-05-02 00:49:14 +00002259 if (Record.readInt())
2260 D->TemplateArgs = Record.readASTTemplateArgumentListInfo();
Francois Pichet09af8c32011-08-17 01:06:54 +00002261}
2262
Sebastian Redlb3298c32010-08-18 23:56:48 +00002263void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002264 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002265
Douglas Gregor68444de2012-01-14 15:13:49 +00002266 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002267 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002268 SmallVector<serialization::DeclID, 32> SpecIDs;
2269 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002270 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002271 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002272}
2273
Larisse Voufo30616382013-08-23 22:21:36 +00002274/// TODO: Unify with ClassTemplateSpecializationDecl version?
2275/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2276/// VarTemplate(Partial)SpecializationDecl with a new data
2277/// structure Template(Partial)SpecializationDecl, and
2278/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002279ASTDeclReader::RedeclarableResult
2280ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2281 VarTemplateSpecializationDecl *D) {
2282 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2283
2284 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002285 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002286 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002287 D->SpecializedTemplate = VTD;
2288 } else {
2289 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002290 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002291 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002292 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002293 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002294 new (C)
2295 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2296 PS->PartialSpecialization =
2297 cast<VarTemplatePartialSpecializationDecl>(InstD);
2298 PS->TemplateArgs = ArgList;
2299 D->SpecializedTemplate = PS;
2300 }
2301 }
2302
2303 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002304 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002305 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002306 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2307 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002308 ExplicitInfo->ExternLoc = ReadSourceLocation();
2309 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002310 D->ExplicitInfo = ExplicitInfo;
2311 }
2312
2313 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002314 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002315 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002316 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002317 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002318 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002319
David L. Jonesbe1557a2016-12-21 00:17:49 +00002320 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002321 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002322 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002323 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002324 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002325 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002326 CanonPattern->getCommonPtr()->PartialSpecializations
2327 .GetOrInsertNode(Partial);
2328 } else {
2329 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2330 }
2331 }
2332 }
2333
2334 return Redecl;
2335}
2336
Larisse Voufo30616382013-08-23 22:21:36 +00002337/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2338/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2339/// VarTemplate(Partial)SpecializationDecl with a new data
2340/// structure Template(Partial)SpecializationDecl, and
2341/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002342void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2343 VarTemplatePartialSpecializationDecl *D) {
2344 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2345
David L. Jonesb6a8f022016-12-21 04:34:52 +00002346 D->TemplateParams = Record.readTemplateParameterList();
2347 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002348
2349 // These are read/set from/to the first declaration.
2350 if (ThisDeclID == Redecl.getFirstID()) {
2351 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002352 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002353 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002354 }
2355}
2356
Sebastian Redlb3298c32010-08-18 23:56:48 +00002357void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002358 VisitTypeDecl(D);
2359
David L. Jonesbe1557a2016-12-21 00:17:49 +00002360 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002361
David L. Jonesbe1557a2016-12-21 00:17:49 +00002362 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002363 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002364}
2365
Sebastian Redlb3298c32010-08-18 23:56:48 +00002366void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002367 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002368 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002369 D->setDepth(Record.readInt());
2370 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002371 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002372 auto TypesAndInfos =
2373 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002374 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002375 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002376 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002377 }
2378 } else {
2379 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002380 D->ParameterPack = Record.readInt();
2381 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002382 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002383 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002384}
2385
Sebastian Redlb3298c32010-08-18 23:56:48 +00002386void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002387 VisitTemplateDecl(D);
2388 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002389 D->setDepth(Record.readInt());
2390 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002391 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002392 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002393 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2394 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002395 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002396 } else {
2397 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002398 D->ParameterPack = Record.readInt();
2399 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002400 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002401 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002402 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002403}
2404
Richard Smith3f1b5d02011-05-05 21:57:07 +00002405void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2406 VisitRedeclarableTemplateDecl(D);
2407}
2408
Sebastian Redlb3298c32010-08-18 23:56:48 +00002409void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002410 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002411 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002412 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002413 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002414 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002415}
2416
Michael Han84324352013-02-22 17:15:32 +00002417void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2418 VisitDecl(D);
2419}
2420
Mike Stump11289f42009-09-09 15:08:12 +00002421std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002422ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002423 uint64_t LexicalOffset = ReadLocalOffset();
2424 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002425 return std::make_pair(LexicalOffset, VisibleOffset);
2426}
2427
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002428template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002429ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002430ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002431 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002432 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002433
Richard Smith5fc18a92015-07-12 23:43:21 +00002434 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002435 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002436
Richard Smithd61d4ac2015-08-22 20:13:39 +00002437 uint64_t RedeclOffset = 0;
2438
Douglas Gregor358cd442012-01-15 16:58:34 +00002439 // 0 indicates that this declaration was the only declaration of its entity,
2440 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002441 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002442 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002443 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002444 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002445 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002446 // This declaration was the first local declaration, but may have imported
2447 // other declarations.
2448 IsKeyDecl = N == 1;
2449 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002450
Richard Smithfe620d22015-03-05 23:24:12 +00002451 // We have some declarations that must be before us in our redeclaration
2452 // chain. Read them now, and remember that we ought to merge with one of
2453 // them.
2454 // FIXME: Provide a known merge target to the second and subsequent such
2455 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002456 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002457 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002458
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002459 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002460 } else {
2461 // This declaration was not the first local declaration. Read the first
2462 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002463 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002464 }
2465
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002466 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002467 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002468 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2469 // We temporarily set the first (canonical) declaration as the previous one
2470 // which is the one that matters and mark the real previous DeclID to be
2471 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002472 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002473 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002474 }
Richard Smithd8a83712015-08-22 01:47:18 +00002475
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002476 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002477
2478 // Note that we need to load local redeclarations of this decl and build a
2479 // decl chain for them. This must happen *after* we perform the preloading
2480 // above; this ensures that the redeclaration chain is built in the correct
2481 // order.
2482 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002483 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002484
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002485 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002486}
2487
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002488/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002489/// of the same entity.
2490template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002491void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002492 RedeclarableResult &Redecl,
2493 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002494 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002495 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002496 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002497
Richard Smith202850a2015-03-10 02:57:50 +00002498 // If we're not the canonical declaration, we don't need to merge.
2499 if (!DBase->isFirstDecl())
2500 return;
2501
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002502 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002503
Richard Smith5a4737c2015-02-06 02:42:59 +00002504 if (auto *Existing = Redecl.getKnownMergeTarget())
2505 // We already know of an existing declaration we should merge with.
2506 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2507 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002508 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002509 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2510}
2511
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002512/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002513/// We use this to put code in a template that will only be valid for certain
2514/// instantiations.
2515template<typename T> static T assert_cast(T t) { return t; }
2516template<typename T> static T assert_cast(...) {
2517 llvm_unreachable("bad assert_cast");
2518}
2519
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002520/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002521/// declarations.
2522void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2523 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002524 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002525 auto *DPattern = D->getTemplatedDecl();
2526 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002527 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2528 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002529 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002530
Richard Smith547864d2014-07-11 18:22:58 +00002531 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2532 // Merge with any existing definition.
2533 // FIXME: This is duplicated in several places. Refactor.
2534 auto *ExistingClass =
2535 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002536 if (auto *DDD = DClass->DefinitionData) {
2537 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002538 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002539 } else {
2540 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002541 // We may have skipped this before because we thought that DClass
2542 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002543 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002544 }
2545 }
2546 DClass->DefinitionData = ExistingClass->DefinitionData;
2547
Richard Smithf17fdbd2014-04-24 02:25:27 +00002548 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2549 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002550 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002551 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2552 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2553 Result);
2554 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2555 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002556 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2557 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2558 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002559 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002560}
2561
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002562/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002563/// of the same entity.
2564template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002565void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2566 RedeclarableResult &Redecl,
2567 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002568 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002569 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002570 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002571 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002572 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2573 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002574
Richard Smithd55889a2013-09-09 16:55:27 +00002575 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002576 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002577 // appropriate canonical declaration.
2578 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002579 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002580 ExistingCanon->Used |= D->Used;
2581 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002582
2583 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002584 // We cannot have loaded any redeclarations of this declaration yet, so
2585 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002586 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002587 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002588 assert_cast<NamespaceDecl*>(ExistingCanon));
2589
2590 // When we merge a template, merge its pattern.
2591 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2592 mergeTemplatePattern(
2593 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002594 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002595
Richard Smith5fc18a92015-07-12 23:43:21 +00002596 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002597 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002598 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002599 }
2600}
2601
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002602/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2603/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2604/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2605/// that some types are mergeable during deserialization, otherwise name
2606/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002607static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002608 if (!ND)
2609 return false;
2610 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002611 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002612 return true;
2613 return false;
2614}
2615
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002616/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002617/// of the same entity, for the case where the entity is not actually
2618/// redeclarable. This happens, for instance, when merging the fields of
2619/// identical class definitions from two different modules.
2620template<typename T>
2621void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2622 // If modules are not available, there is no reason to perform this merge.
2623 if (!Reader.getContext().getLangOpts().Modules)
2624 return;
2625
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002626 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2627 // Note that C identically-named things in different translation units are
2628 // not redeclarations, but may still have compatible types, where ODR-like
2629 // semantics may apply.
2630 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2631 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002632 return;
2633
Richard Smith0b87e072013-10-07 08:02:11 +00002634 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2635 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002636 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2637 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002638}
2639
Alexey Bataeva769e072013-03-22 06:34:35 +00002640void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2641 VisitDecl(D);
2642 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002643 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002644 Vars.reserve(NumVars);
2645 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002646 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002647 }
2648 D->setVars(Vars);
2649}
2650
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002651void ASTDeclReader::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
2652 VisitDecl(D);
2653 unsigned NumVars = D->varlist_size();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002654 unsigned NumClauses = D->clauselist_size();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002655 SmallVector<Expr *, 16> Vars;
2656 Vars.reserve(NumVars);
2657 for (unsigned i = 0; i != NumVars; ++i) {
2658 Vars.push_back(Record.readExpr());
2659 }
2660 D->setVars(Vars);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002661 SmallVector<OMPClause *, 8> Clauses;
2662 Clauses.reserve(NumClauses);
2663 OMPClauseReader ClauseReader(Record);
2664 for (unsigned I = 0; I != NumClauses; ++I)
2665 Clauses.push_back(ClauseReader.readClause());
2666 D->setClauses(Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002667}
2668
Kelvin Li1408f912018-09-26 04:28:39 +00002669void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
2670 VisitDecl(D);
2671 unsigned NumClauses = D->clauselist_size();
2672 SmallVector<OMPClause *, 8> Clauses;
2673 Clauses.reserve(NumClauses);
2674 OMPClauseReader ClauseReader(Record);
2675 for (unsigned I = 0; I != NumClauses; ++I)
2676 Clauses.push_back(ClauseReader.readClause());
2677 D->setClauses(Clauses);
2678}
2679
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002680void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002681 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002682 D->setLocation(ReadSourceLocation());
Alexey Bataeve6aa4692018-09-13 16:54:05 +00002683 Expr *In = Record.readExpr();
2684 Expr *Out = Record.readExpr();
2685 D->setCombinerData(In, Out);
2686 Expr *Combiner = Record.readExpr();
2687 D->setCombiner(Combiner);
2688 Expr *Orig = Record.readExpr();
2689 Expr *Priv = Record.readExpr();
2690 D->setInitializerData(Orig, Priv);
2691 Expr *Init = Record.readExpr();
2692 auto IK = static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt());
2693 D->setInitializer(Init, IK);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002694 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002695}
2696
Michael Kruse251e1482019-02-01 20:25:04 +00002697void ASTDeclReader::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
2698 VisitValueDecl(D);
2699 D->setLocation(ReadSourceLocation());
2700 Expr *MapperVarRefE = Record.readExpr();
2701 D->setMapperVarRef(MapperVarRefE);
2702 D->VarName = Record.readDeclarationName();
2703 D->PrevDeclInScope = ReadDeclID();
2704 unsigned NumClauses = D->clauselist_size();
2705 SmallVector<OMPClause *, 8> Clauses;
2706 Clauses.reserve(NumClauses);
2707 OMPClauseReader ClauseReader(Record);
2708 for (unsigned I = 0; I != NumClauses; ++I)
2709 Clauses.push_back(ClauseReader.readClause());
2710 D->setClauses(Clauses);
2711}
2712
Alexey Bataev4244be22016-02-11 05:35:55 +00002713void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002714 VisitVarDecl(D);
2715}
2716
Chris Lattner487412d2009-04-27 05:27:42 +00002717//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002718// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002719//===----------------------------------------------------------------------===//
2720
Richard Smithe43e2b32018-08-20 21:47:29 +00002721namespace {
2722class AttrReader {
2723 ModuleFile *F;
2724 ASTReader *Reader;
2725 const ASTReader::RecordData &Record;
2726 unsigned &Idx;
2727
2728public:
2729 AttrReader(ModuleFile &F, ASTReader &Reader,
2730 const ASTReader::RecordData &Record, unsigned &Idx)
2731 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
2732
2733 const uint64_t &readInt() { return Record[Idx++]; }
2734
2735 SourceRange readSourceRange() {
2736 return Reader->ReadSourceRange(*F, Record, Idx);
2737 }
2738
2739 Expr *readExpr() { return Reader->ReadExpr(*F); }
2740
2741 std::string readString() {
2742 return Reader->ReadString(Record, Idx);
2743 }
2744
2745 TypeSourceInfo *getTypeSourceInfo() {
2746 return Reader->GetTypeSourceInfo(*F, Record, Idx);
2747 }
2748
2749 IdentifierInfo *getIdentifierInfo() {
2750 return Reader->GetIdentifierInfo(*F, Record, Idx);
2751 }
2752
2753 VersionTuple readVersionTuple() {
2754 return ASTReader::ReadVersionTuple(Record, Idx);
2755 }
2756
2757 template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
2758 return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
2759 }
2760};
2761}
2762
2763Attr *ASTReader::ReadAttr(ModuleFile &M, const RecordData &Rec,
2764 unsigned &Idx) {
2765 AttrReader Record(M, *this, Rec, Idx);
2766 auto V = Record.readInt();
2767 if (!V)
2768 return nullptr;
2769
2770 Attr *New = nullptr;
2771 // Kind is stored as a 1-based integer because 0 is used to indicate a null
2772 // Attr pointer.
2773 auto Kind = static_cast<attr::Kind>(V - 1);
2774 SourceRange Range = Record.readSourceRange();
2775 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002776
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002777#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002778
Richard Smithe43e2b32018-08-20 21:47:29 +00002779 assert(New && "Unable to decode attribute?");
2780 return New;
2781}
2782
2783/// Reads attributes from the current stream position.
2784void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2785 for (unsigned I = 0, E = Record.readInt(); I != E; ++I)
2786 Attrs.push_back(Record.readAttr());
Chris Lattner8f63ab52009-04-27 06:01:06 +00002787}
2788
2789//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002790// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002791//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002792
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002793/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002794/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002795///
Chris Lattner487412d2009-04-27 05:27:42 +00002796/// This routine notes that this declaration has already been loaded,
2797/// so that future GetDecl calls will return this declaration rather
2798/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002799inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002800 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2801 DeclsLoaded[Index] = D;
2802}
2803
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002804/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002805/// this declaration (via HandleTopLevelDecl).
2806///
2807/// This routine should return true for anything that might affect
2808/// code generation, e.g., inline function definitions, Objective-C
2809/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002810static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002811 // An ObjCMethodDecl is never considered as "interesting" because its
2812 // implementation container always is.
2813
Richard Smithcd4a7a42017-09-07 00:55:55 +00002814 // An ImportDecl or VarDecl imported from a module map module will get
2815 // emitted when we import the relevant module.
Richard Smith520a37f2019-02-05 23:37:13 +00002816 if (isPartOfPerModuleInitializer(D)) {
Richard Smithcd4a7a42017-09-07 00:55:55 +00002817 auto *M = D->getImportedOwningModule();
2818 if (M && M->Kind == Module::ModuleMapModule &&
2819 Ctx.DeclMustBeEmitted(D))
2820 return false;
2821 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002822
Fangrui Song6907ce22018-07-30 19:24:48 +00002823 if (isa<FileScopeAsmDecl>(D) ||
2824 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002825 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002826 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002827 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002828 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002829 return true;
Michael Kruse251e1482019-02-01 20:25:04 +00002830 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D) ||
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002831 isa<OMPDeclareMapperDecl>(D) || isa<OMPAllocateDecl>(D))
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002832 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002833 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002834 return Var->isFileVarDecl() &&
Alexey Bataevd01b7492018-08-15 19:45:12 +00002835 (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
2836 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002837 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002838 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002839
2840 if (auto *ES = D->getASTContext().getExternalSource())
2841 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2842 return true;
2843
Douglas Gregor87d81242011-09-10 00:22:34 +00002844 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002845}
2846
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002847/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002848ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002849ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002850 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2851 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002852 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002853 const DeclOffset &DOffs =
2854 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002855 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002856 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002857}
2858
Douglas Gregord32f0352011-07-22 06:10:01 +00002859ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002860 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002861
2862 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2863 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2864}
2865
Douglas Gregorde3ef502011-11-30 23:21:26 +00002866uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002867 return LocalOffset + M.GlobalBitOffset;
2868}
2869
Richard Smithbf78e642013-06-24 22:51:00 +00002870static bool isSameTemplateParameterList(const TemplateParameterList *X,
2871 const TemplateParameterList *Y);
2872
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002873/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002874/// that they may be used in declarations of the same template.
2875static bool isSameTemplateParameter(const NamedDecl *X,
2876 const NamedDecl *Y) {
2877 if (X->getKind() != Y->getKind())
2878 return false;
2879
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002880 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2881 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002882 return TX->isParameterPack() == TY->isParameterPack();
2883 }
2884
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002885 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2886 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002887 return TX->isParameterPack() == TY->isParameterPack() &&
2888 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2889 }
2890
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002891 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2892 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002893 return TX->isParameterPack() == TY->isParameterPack() &&
2894 isSameTemplateParameterList(TX->getTemplateParameters(),
2895 TY->getTemplateParameters());
2896}
2897
Richard Smith32952e12014-10-14 02:00:47 +00002898static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2899 if (auto *NS = X->getAsNamespace())
2900 return NS;
2901 if (auto *NAS = X->getAsNamespaceAlias())
2902 return NAS->getNamespace();
2903 return nullptr;
2904}
2905
2906static bool isSameQualifier(const NestedNameSpecifier *X,
2907 const NestedNameSpecifier *Y) {
2908 if (auto *NSX = getNamespace(X)) {
2909 auto *NSY = getNamespace(Y);
2910 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2911 return false;
2912 } else if (X->getKind() != Y->getKind())
2913 return false;
2914
2915 // FIXME: For namespaces and types, we're permitted to check that the entity
2916 // is named via the same tokens. We should probably do so.
2917 switch (X->getKind()) {
2918 case NestedNameSpecifier::Identifier:
2919 if (X->getAsIdentifier() != Y->getAsIdentifier())
2920 return false;
2921 break;
2922 case NestedNameSpecifier::Namespace:
2923 case NestedNameSpecifier::NamespaceAlias:
2924 // We've already checked that we named the same namespace.
2925 break;
2926 case NestedNameSpecifier::TypeSpec:
2927 case NestedNameSpecifier::TypeSpecWithTemplate:
2928 if (X->getAsType()->getCanonicalTypeInternal() !=
2929 Y->getAsType()->getCanonicalTypeInternal())
2930 return false;
2931 break;
2932 case NestedNameSpecifier::Global:
2933 case NestedNameSpecifier::Super:
2934 return true;
2935 }
2936
2937 // Recurse into earlier portion of NNS, if any.
2938 auto *PX = X->getPrefix();
2939 auto *PY = Y->getPrefix();
2940 if (PX && PY)
2941 return isSameQualifier(PX, PY);
2942 return !PX && !PY;
2943}
2944
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002945/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002946/// that they may be used in declarations of the same template.
2947static bool isSameTemplateParameterList(const TemplateParameterList *X,
2948 const TemplateParameterList *Y) {
2949 if (X->size() != Y->size())
2950 return false;
2951
2952 for (unsigned I = 0, N = X->size(); I != N; ++I)
2953 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2954 return false;
2955
2956 return true;
2957}
2958
George Burgess IV95845082017-02-15 22:43:27 +00002959/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002960/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002961static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2962 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002963 // Note that pass_object_size attributes are represented in the function's
2964 // ExtParameterInfo, so we don't need to check them here.
2965
George Burgess IV95845082017-02-15 22:43:27 +00002966 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00002967 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
2968 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
Michael Kruse157a3552018-12-10 15:16:37 +00002969
2970 for (auto Pair : zip_longest(AEnableIfAttrs, BEnableIfAttrs)) {
2971 Optional<EnableIfAttr *> Cand1A = std::get<0>(Pair);
2972 Optional<EnableIfAttr *> Cand2A = std::get<1>(Pair);
2973
2974 // Return false if the number of enable_if attributes is different.
2975 if (!Cand1A || !Cand2A)
2976 return false;
2977
George Burgess IV95845082017-02-15 22:43:27 +00002978 Cand1ID.clear();
2979 Cand2ID.clear();
2980
Michael Kruse157a3552018-12-10 15:16:37 +00002981 (*Cand1A)->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2982 (*Cand2A)->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2983
2984 // Return false if any of the enable_if expressions of A and B are
2985 // different.
George Burgess IV95845082017-02-15 22:43:27 +00002986 if (Cand1ID != Cand2ID)
2987 return false;
2988 }
Michael Kruse157a3552018-12-10 15:16:37 +00002989 return true;
George Burgess IV95845082017-02-15 22:43:27 +00002990}
2991
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002992/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002993static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2994 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002995
Douglas Gregor022857e2011-12-22 01:48:48 +00002996 if (X == Y)
2997 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002998
Douglas Gregor022857e2011-12-22 01:48:48 +00002999 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00003000 //
3001 // Note that we can't use DeclContext::Equals here, because the DeclContexts
3002 // could be two different declarations of the same function. (We will fix the
3003 // semantic DC to refer to the primary definition after merging.)
3004 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
3005 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00003006 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003007
3008 // Two typedefs refer to the same entity if they have the same underlying
3009 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003010 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
3011 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003012 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
3013 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00003014
Douglas Gregor9b7b3912012-01-04 16:44:10 +00003015 // Must have the same kind.
3016 if (X->getKind() != Y->getKind())
3017 return false;
Richard Smithf4634362014-09-03 23:11:22 +00003018
Douglas Gregorda389302012-01-01 21:47:52 +00003019 // Objective-C classes and protocols with the same name always match.
3020 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00003021 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00003022
3023 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003024 // No need to handle these here: we merge them when adding them to the
3025 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00003026 return false;
3027 }
Richard Smithd55889a2013-09-09 16:55:27 +00003028
Douglas Gregor2009cee2012-01-03 22:46:00 +00003029 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003030 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
3031 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00003032 return (TagX->getTagKind() == TagY->getTagKind()) ||
3033 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
3034 TagX->getTagKind() == TTK_Interface) &&
3035 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
3036 TagY->getTagKind() == TTK_Interface));
3037 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003038
Joao Matose9a3ed42012-08-31 22:18:20 +00003039 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00003040 // FIXME: This needs to cope with merging of prototyped/non-prototyped
3041 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003042 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
3043 const auto *FuncY = cast<FunctionDecl>(Y);
3044 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
3045 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00003046 if (CtorX->getInheritedConstructor() &&
3047 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
3048 CtorY->getInheritedConstructor().getConstructor()))
3049 return false;
3050 }
Erich Keane281d20b2018-01-08 21:34:17 +00003051
3052 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
3053 return false;
3054
3055 // Multiversioned functions with different feature strings are represented
3056 // as separate declarations.
3057 if (FuncX->isMultiVersion()) {
3058 const auto *TAX = FuncX->getAttr<TargetAttr>();
3059 const auto *TAY = FuncY->getAttr<TargetAttr>();
3060 assert(TAX && TAY && "Multiversion Function without target attribute");
3061
3062 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
3063 return false;
3064 }
3065
Richard Smitha54d3242017-03-08 23:00:26 +00003066 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00003067 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
3068 // Map to the first declaration that we've already merged into this one.
3069 // The TSI of redeclarations might not match (due to calling conventions
3070 // being inherited onto the type but not the TSI), but the TSI type of
3071 // the first declaration of the function should match across modules.
3072 FD = FD->getCanonicalDecl();
3073 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
3074 : FD->getType();
3075 };
3076 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
3077 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00003078 // We can get functions with different types on the redecl chain in C++17
3079 // if they have differing exception specifications and at least one of
3080 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00003081 auto *XFPT = XT->getAs<FunctionProtoType>();
3082 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003083 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00003084 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
3085 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00003086 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00003087 return true;
3088 return false;
3089 }
George Burgess IV95845082017-02-15 22:43:27 +00003090 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00003091 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00003092 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003093
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003094 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003095 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
3096 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00003097 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
3098 ASTContext &C = VarX->getASTContext();
3099 if (C.hasSameType(VarX->getType(), VarY->getType()))
3100 return true;
3101
3102 // We can get decls with different types on the redecl chain. Eg.
3103 // template <typename T> struct S { static T Var[]; }; // #1
3104 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
3105 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00003106 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00003107 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
3108 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
3109 if (!VarXTy || !VarYTy)
3110 return false;
3111 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
3112 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
3113 }
3114 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003115 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003116
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003117 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003118 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
3119 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003120 return NamespaceX->isInline() == NamespaceY->isInline();
3121 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003122
Richard Smith8f8f05c2013-06-24 04:45:28 +00003123 // Identical template names and kinds match if their template parameter lists
3124 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003125 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
3126 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00003127 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00003128 TemplateY->getTemplatedDecl()) &&
3129 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3130 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003131 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003132
Richard Smith0b87e072013-10-07 08:02:11 +00003133 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003134 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3135 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003136 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003137 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3138 }
3139
Richard Smith8cbd8952015-08-04 02:05:09 +00003140 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003141 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3142 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003143 return IFDX->getAnonField()->getCanonicalDecl() ==
3144 IFDY->getAnonField()->getCanonicalDecl();
3145 }
3146
Richard Smith01a73372013-10-15 22:02:41 +00003147 // Enumerators with the same name match.
3148 if (isa<EnumConstantDecl>(X))
3149 // FIXME: Also check the value is odr-equivalent.
3150 return true;
3151
Richard Smithfd8634a2013-10-23 02:17:46 +00003152 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003153 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3154 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003155 return USX->getTargetDecl() == USY->getTargetDecl();
3156 }
3157
Richard Smith32952e12014-10-14 02:00:47 +00003158 // Using declarations with the same qualifier match. (We already know that
3159 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003160 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3161 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003162 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3163 UX->hasTypename() == UY->hasTypename() &&
3164 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3165 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003166 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3167 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003168 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3169 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3170 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003171 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003172 return isSameQualifier(
3173 UX->getQualifier(),
3174 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3175
Richard Smithf4634362014-09-03 23:11:22 +00003176 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003177 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3178 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003179 return NAX->getNamespace()->Equals(NAY->getNamespace());
3180 }
3181
Douglas Gregor022857e2011-12-22 01:48:48 +00003182 return false;
3183}
3184
Richard Smithd55889a2013-09-09 16:55:27 +00003185/// Find the context in which we should search for previous declarations when
3186/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003187DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3188 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003189 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003190 return ND->getOriginalNamespace();
3191
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003192 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003193 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003194 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003195 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003196 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003197
3198 // If there's no definition yet, then DC's definition is added by an update
3199 // record, but we've not yet loaded that update record. In this case, we
3200 // commit to DC being the canonical definition now, and will fix this when
3201 // we load the update record.
3202 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003203 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003204 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003205 RD->DefinitionData = DD;
3206 RD->getCanonicalDecl()->DefinitionData = DD;
3207
3208 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003209 Reader.PendingFakeDefinitionData.insert(
3210 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003211 }
3212
3213 return DD->Definition;
3214 }
Richard Smithd55889a2013-09-09 16:55:27 +00003215
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003216 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003217 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3218 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003219
Richard Smith4eca9b92015-02-04 23:37:59 +00003220 // We can see the TU here only if we have no Sema object. In that case,
3221 // there's no TU scope to look in, so using the DC alone is sufficient.
3222 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3223 return TU;
3224
Craig Toppera13603a2014-05-22 05:54:18 +00003225 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003226}
3227
Douglas Gregor022857e2011-12-22 01:48:48 +00003228ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003229 // Record that we had a typedef name for linkage whether or not we merge
3230 // with that declaration.
3231 if (TypedefNameForLinkage) {
3232 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3233 Reader.ImportedTypedefNamesForLinkage.insert(
3234 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3235 return;
3236 }
3237
Douglas Gregor9b47f942012-01-09 17:38:47 +00003238 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003239 return;
Richard Smith95d99302013-07-13 02:00:19 +00003240
Richard Smith70d58502014-08-30 00:04:23 +00003241 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003242 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003243 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003244 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3245 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003246 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003247 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003248 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003249 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3250 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003251 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003252 // Add the declaration to its redeclaration context so later merging
3253 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003254 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003255 }
3256}
3257
Richard Smith88ebade2014-08-23 01:45:27 +00003258/// Find the declaration that should be merged into, given the declaration found
3259/// by name lookup. If we're merging an anonymous declaration within a typedef,
3260/// we need a matching typedef, and we merge with the type inside it.
3261static NamedDecl *getDeclForMerging(NamedDecl *Found,
3262 bool IsTypedefNameForLinkage) {
3263 if (!IsTypedefNameForLinkage)
3264 return Found;
3265
3266 // If we found a typedef declaration that gives a name to some other
3267 // declaration, then we want that inner declaration. Declarations from
3268 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003269 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003270 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003271
3272 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003273 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003274
Hans Wennborgdcfba332015-10-06 23:40:43 +00003275 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003276}
3277
Richard Smith600adef2018-07-04 02:25:38 +00003278/// Find the declaration to use to populate the anonymous declaration table
3279/// for the given lexical DeclContext. We only care about finding local
3280/// definitions of the context; we'll merge imported ones as we go.
3281DeclContext *
3282ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3283 // For classes, we track the definition as we merge.
3284 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3285 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3286 return DD ? DD->Definition : nullptr;
3287 }
3288
3289 // For anything else, walk its merged redeclarations looking for a definition.
3290 // Note that we can't just call getDefinition here because the redeclaration
3291 // chain isn't wired up.
3292 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3293 if (auto *FD = dyn_cast<FunctionDecl>(D))
3294 if (FD->isThisDeclarationADefinition())
3295 return FD;
3296 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3297 if (MD->isThisDeclarationADefinition())
3298 return MD;
3299 }
3300
3301 // No merged definition yet.
3302 return nullptr;
3303}
3304
Richard Smithd08aeb62014-08-28 01:33:39 +00003305NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3306 DeclContext *DC,
3307 unsigned Index) {
3308 // If the lexical context has been merged, look into the now-canonical
3309 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003310 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003311
3312 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003313 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003314 if (Index < Previous.size() && Previous[Index])
3315 return Previous[Index];
3316
3317 // If this is the first time, but we have parsed a declaration of the context,
3318 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003319 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3320 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3321 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003322 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003323 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3324 else
Richard Smith2b560572015-02-07 03:11:11 +00003325 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3326 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003327 }
3328
3329 return Index < Previous.size() ? Previous[Index] : nullptr;
3330}
3331
3332void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3333 DeclContext *DC, unsigned Index,
3334 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003335 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003336
Richard Smith600adef2018-07-04 02:25:38 +00003337 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003338 if (Index >= Previous.size())
3339 Previous.resize(Index + 1);
3340 if (!Previous[Index])
3341 Previous[Index] = D;
3342}
3343
Douglas Gregor022857e2011-12-22 01:48:48 +00003344ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003345 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3346 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003347
Richard Smithd08aeb62014-08-28 01:33:39 +00003348 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3349 // Don't bother trying to find unnamed declarations that are in
3350 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003351 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3352 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003353 Result.suppress();
3354 return Result;
3355 }
Richard Smith95d99302013-07-13 02:00:19 +00003356
Douglas Gregor022857e2011-12-22 01:48:48 +00003357 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003358 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003359 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003360 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003361 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3362 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003363 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3364 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003365 // Go on to check in other places in case an existing typedef name
3366 // was not imported.
3367 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003368
Richard Smith2b560572015-02-07 03:11:11 +00003369 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003370 // This is an anonymous declaration that we may need to merge. Look it up
3371 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003372 if (auto *Existing = getAnonymousDeclForMerging(
3373 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3374 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003375 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3376 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003377 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003378 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003379 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003380
3381 // Temporarily consider the identifier to be up-to-date. We don't want to
3382 // cause additional lookups here.
3383 class UpToDateIdentifierRAII {
3384 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003385 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003386
3387 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003388 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003389 if (II) {
3390 WasOutToDate = II->isOutOfDate();
3391 if (WasOutToDate)
3392 II->setOutOfDate(false);
3393 }
3394 }
3395
3396 ~UpToDateIdentifierRAII() {
3397 if (WasOutToDate)
3398 II->setOutOfDate(true);
3399 }
3400 } UpToDate(Name.getAsIdentifierInfo());
3401
Fangrui Song6907ce22018-07-30 19:24:48 +00003402 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003403 IEnd = IdResolver.end();
3404 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003405 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003406 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003407 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3408 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003409 }
Richard Smith8a639892015-01-24 01:07:20 +00003410 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003411 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003412 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003413 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003414 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003415 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3416 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003417 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003418 } else {
3419 // Not in a mergeable context.
3420 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003421 }
Richard Smithd55889a2013-09-09 16:55:27 +00003422
Richard Smith2b9e3e32013-10-18 06:05:18 +00003423 // If this declaration is from a merged context, make a note that we need to
3424 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003425 //
3426 // FIXME: We should do something similar if we merge two definitions of the
3427 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003428 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3429 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3430 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003431 Reader.PendingOdrMergeChecks.push_back(D);
3432
Richard Smithd08aeb62014-08-28 01:33:39 +00003433 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003434 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003435}
3436
Richard Smithb321ecb2014-05-13 01:15:00 +00003437template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003438Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3439 return D->RedeclLink.getLatestNotUpdated();
3440}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003441
Richard Smithc3a53252015-02-28 05:57:02 +00003442Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3443 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3444}
3445
3446Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3447 assert(D);
3448
3449 switch (D->getKind()) {
3450#define ABSTRACT_DECL(TYPE)
3451#define DECL(TYPE, BASE) \
3452 case Decl::TYPE: \
3453 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3454#include "clang/AST/DeclNodes.inc"
3455 }
3456 llvm_unreachable("unknown decl kind");
3457}
3458
Richard Smith8ce51082015-03-11 01:44:51 +00003459Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3460 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3461}
3462
Richard Smithc3a53252015-02-28 05:57:02 +00003463template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003464void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3465 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003466 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003467 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003468 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003469}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003470
Richard Smith24d166c2014-07-31 23:52:38 +00003471namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003472
Richard Smith6de7a242014-07-31 23:46:44 +00003473template<>
3474void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003475 Redeclarable<VarDecl> *D,
3476 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003477 auto *VD = static_cast<VarDecl *>(D);
3478 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003479 D->RedeclLink.setPrevious(PrevVD);
3480 D->First = PrevVD->First;
3481
3482 // We should keep at most one definition on the chain.
3483 // FIXME: Cache the definition once we've found it. Building a chain with
3484 // N definitions currently takes O(N^2) time here.
3485 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3486 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3487 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3488 Reader.mergeDefinitionVisibility(CurD, VD);
3489 VD->demoteThisDefinitionToDeclaration();
3490 break;
3491 }
3492 }
3493 }
3494}
3495
Richard Smitha62d1982018-08-03 01:00:01 +00003496static bool isUndeducedReturnType(QualType T) {
3497 auto *DT = T->getContainedDeducedType();
3498 return DT && !DT->isDeduced();
3499}
3500
Richard Smithedbc6e92016-10-14 21:41:24 +00003501template<>
3502void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003503 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003504 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003505 auto *FD = static_cast<FunctionDecl *>(D);
3506 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003507
3508 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003509 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003510
3511 // If the previous declaration is an inline function declaration, then this
3512 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003513 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003514 // FIXME: [dcl.fct.spec]p4:
3515 // If a function with external linkage is declared inline in one
3516 // translation unit, it shall be declared inline in all translation
3517 // units in which it appears.
3518 //
3519 // Be careful of this case:
3520 //
3521 // module A:
3522 // template<typename T> struct X { void f(); };
3523 // template<typename T> inline void X<T>::f() {}
3524 //
3525 // module B instantiates the declaration of X<int>::f
3526 // module C instantiates the definition of X<int>::f
3527 //
3528 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003529 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003530 }
3531
Richard Smith6de7a242014-07-31 23:46:44 +00003532 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3533 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003534 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003535 // If we need to propagate an exception specification along the redecl
3536 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003537 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3538 bool WasUnresolved =
3539 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3540 if (IsUnresolved != WasUnresolved)
3541 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003542 {Canon, IsUnresolved ? PrevFD : FD});
3543
3544 // If we need to propagate a deduced return type along the redecl chain,
3545 // make a note of that so that we can do it later.
3546 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3547 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3548 if (IsUndeduced != WasUndeduced)
3549 Reader.PendingDeducedTypeUpdates.insert(
3550 {cast<FunctionDecl>(Canon),
3551 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003552 }
3553}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003554
3555} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003556
Richard Smith6de7a242014-07-31 23:46:44 +00003557void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003558 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3559}
3560
Richard Smith8346e522015-06-10 01:47:58 +00003561/// Inherit the default template argument from \p From to \p To. Returns
3562/// \c false if there is no default template for \p From.
3563template <typename ParmDecl>
3564static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3565 Decl *ToD) {
3566 auto *To = cast<ParmDecl>(ToD);
3567 if (!From->hasDefaultArgument())
3568 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003569 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003570 return true;
3571}
3572
3573static void inheritDefaultTemplateArguments(ASTContext &Context,
3574 TemplateDecl *From,
3575 TemplateDecl *To) {
3576 auto *FromTP = From->getTemplateParameters();
3577 auto *ToTP = To->getTemplateParameters();
3578 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3579
3580 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003581 NamedDecl *FromParam = FromTP->getParam(I);
3582 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003583
Richard Smith559cc692018-07-31 21:01:53 +00003584 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3585 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3586 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3587 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3588 else
3589 inheritDefaultTemplateArgument(
3590 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003591 }
3592}
3593
Richard Smith6de7a242014-07-31 23:46:44 +00003594void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003595 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003596 assert(D && Previous);
3597
3598 switch (D->getKind()) {
3599#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003600#define DECL(TYPE, BASE) \
3601 case Decl::TYPE: \
3602 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003603 break;
3604#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003605 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003606
3607 // If the declaration was visible in one module, a redeclaration of it in
3608 // another module remains visible even if it wouldn't be visible by itself.
3609 //
3610 // FIXME: In this case, the declaration should only be visible if a module
3611 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003612 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003613 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003614 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003615
Richard Smith8346e522015-06-10 01:47:58 +00003616 // If the declaration declares a template, it may inherit default arguments
3617 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003618 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003619 inheritDefaultTemplateArguments(Reader.getContext(),
3620 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003621}
3622
Richard Smithb321ecb2014-05-13 01:15:00 +00003623template<typename DeclT>
3624void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003625 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003626}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003627
Richard Smithb321ecb2014-05-13 01:15:00 +00003628void ASTDeclReader::attachLatestDeclImpl(...) {
3629 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3630}
3631
Douglas Gregor05f10352011-12-17 23:38:30 +00003632void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3633 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003634
3635 switch (D->getKind()) {
3636#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003637#define DECL(TYPE, BASE) \
3638 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003639 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3640 break;
3641#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003642 }
3643}
3644
Richard Smith851072e2014-05-19 20:59:20 +00003645template<typename DeclT>
3646void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3647 D->RedeclLink.markIncomplete();
3648}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003649
Richard Smith851072e2014-05-19 20:59:20 +00003650void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3651 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3652}
3653
3654void ASTReader::markIncompleteDeclChain(Decl *D) {
3655 switch (D->getKind()) {
3656#define ABSTRACT_DECL(TYPE)
3657#define DECL(TYPE, BASE) \
3658 case Decl::TYPE: \
3659 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3660 break;
3661#include "clang/AST/DeclNodes.inc"
3662 }
3663}
3664
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003665/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003666Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003667 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003668 SourceLocation DeclLoc;
3669 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003670 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003671 // Keep track of where we are in the stream, then jump back there
3672 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003673 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003674
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003675 ReadingKindTracker ReadingKind(Read_Decl, *this);
3676
Douglas Gregor1342e842009-07-06 18:54:52 +00003677 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003678 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003679
Sebastian Redl2c373b92010-10-05 15:59:54 +00003680 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003681 ASTRecordReader Record(*this, *Loc.F);
3682 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003683 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003684
Richard Smithdbafb6c2017-06-29 23:23:46 +00003685 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003686 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003687 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003688 case DECL_CONTEXT_LEXICAL:
3689 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003690 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003691 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003692 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003693 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003694 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003695 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003696 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003697 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003698 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003699 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003700 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003701 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003702 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003703 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003704 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003705 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003706 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003707 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003708 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003709 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003710 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003711 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003712 case DECL_EXPORT:
3713 D = ExportDecl::CreateDeserialized(Context, ID);
3714 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003715 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003716 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003717 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003718 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003719 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003720 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003721 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003722 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003723 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003724 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003725 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003726 break;
Richard Smith151c4562016-12-20 21:35:28 +00003727 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003728 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003729 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003730 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003731 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003732 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003733 case DECL_CONSTRUCTOR_USING_SHADOW:
3734 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3735 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003736 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003737 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003738 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003739 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003740 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003741 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003742 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003743 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003744 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003745 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003746 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003747 break;
Richard Smithbc491202017-02-17 20:05:37 +00003748 case DECL_CXX_DEDUCTION_GUIDE:
3749 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3750 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003751 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003752 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003753 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003754 case DECL_CXX_CONSTRUCTOR:
Richard Smith76b90272019-05-09 03:59:21 +00003755 D = CXXConstructorDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003756 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003757 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003758 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003759 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003760 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003761 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003762 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003763 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003764 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003765 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003766 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003767 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003768 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003769 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003770 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003771 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003772 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003773 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003774 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003775 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003776 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003777 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003778 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003779 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003780 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003781 case DECL_VAR_TEMPLATE:
3782 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3783 break;
3784 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3785 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3786 break;
3787 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3788 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3789 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003790 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003791 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003792 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003793 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003794 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003795 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003796 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003797 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003798 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003799 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003800 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003801 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003802 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003803 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3804 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003805 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003806 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003807 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003808 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003809 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3810 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003811 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003812 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003813 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003814 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003815 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003816 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003817 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003818 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003819 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003820 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003821 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003822 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003823 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003824 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003825 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003826 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003827 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003828 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003829 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003830 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003831 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003832 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003833 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003834 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003835 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003836 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003837 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003838 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003839 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003840 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003841 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003842 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003843 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003844 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003845 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003846 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003847 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003848 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003849 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003850 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003851 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003852 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003853 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003854 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003855 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003856 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003857 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003858 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003859 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003860 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003861 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003862 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003863 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003864 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003865 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003866 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003867 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003868 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003869 break;
3870 case DECL_BINDING:
3871 D = BindingDecl::CreateDeserialized(Context, ID);
3872 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003873 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003874 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003875 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003876 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003877 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003878 break;
John McCall5e77d762013-04-16 07:28:30 +00003879 case DECL_MS_PROPERTY:
3880 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3881 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003882 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003883 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003884 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003885 case DECL_CXX_BASE_SPECIFIERS:
3886 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003887 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003888 case DECL_CXX_CTOR_INITIALIZERS:
3889 Error("attempt to read a C++ ctor initializer record as a declaration");
3890 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003891 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003892 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003893 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003894 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003895 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003896 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003897 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003898 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00003899 case DECL_OMP_ALLOCATE: {
3900 unsigned NumVars = Record.readInt();
3901 unsigned NumClauses = Record.readInt();
3902 D = OMPAllocateDecl::CreateDeserialized(Context, ID, NumVars, NumClauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00003903 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00003904 }
Kelvin Li1408f912018-09-26 04:28:39 +00003905 case DECL_OMP_REQUIRES:
3906 D = OMPRequiresDecl::CreateDeserialized(Context, ID, Record.readInt());
3907 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003908 case DECL_OMP_DECLARE_REDUCTION:
3909 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3910 break;
Michael Kruse251e1482019-02-01 20:25:04 +00003911 case DECL_OMP_DECLARE_MAPPER:
3912 D = OMPDeclareMapperDecl::CreateDeserialized(Context, ID, Record.readInt());
3913 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003914 case DECL_OMP_CAPTUREDEXPR:
3915 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003916 break;
Nico Weber66220292016-03-02 17:28:48 +00003917 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003918 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003919 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003920 case DECL_PRAGMA_DETECT_MISMATCH:
3921 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003922 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003923 break;
Michael Han84324352013-02-22 17:15:32 +00003924 case DECL_EMPTY:
3925 D = EmptyDecl::CreateDeserialized(Context, ID);
3926 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003927 case DECL_OBJC_TYPE_PARAM:
3928 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3929 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003930 }
Chris Lattner487412d2009-04-27 05:27:42 +00003931
Sebastian Redlb3298c32010-08-18 23:56:48 +00003932 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003933 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003934 // Set the DeclContext before doing any deserialization, to make sure internal
3935 // calls to Decl::getASTContext() by Decl's methods will find the
3936 // TranslationUnitDecl without crashing.
3937 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003938 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003939
3940 // If this declaration is also a declaration context, get the
3941 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003942 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003943 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003944 if (Offsets.first &&
3945 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3946 return nullptr;
3947 if (Offsets.second &&
3948 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3949 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003950 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003951 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003952
Douglas Gregordab42432011-08-12 00:15:20 +00003953 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003954 PendingUpdateRecords.push_back(
3955 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003956
Douglas Gregor404cdde2012-01-27 01:47:08 +00003957 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003958 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003959 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3960 // we put the Decl in PendingDefinitions so we can pull the categories here.
3961 if (Class->isThisDeclarationADefinition() ||
3962 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003963 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00003964
Richard Smith13fb8602016-07-15 21:33:46 +00003965 // If we have deserialized a declaration that has a definition the
3966 // AST consumer might need to know about, queue it.
3967 // We don't pass it to the consumer immediately because we may be in recursive
3968 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003969 PotentiallyInterestingDecls.push_back(
3970 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003971
Douglas Gregordab42432011-08-12 00:15:20 +00003972 return D;
3973}
3974
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003975void ASTReader::PassInterestingDeclsToConsumer() {
3976 assert(Consumer);
3977
3978 if (PassingDeclsToConsumer)
3979 return;
3980
3981 // Guard variable to avoid recursively redoing the process of passing
3982 // decls to consumer.
3983 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3984 true);
3985
3986 // Ensure that we've loaded all potentially-interesting declarations
3987 // that need to be eagerly loaded.
3988 for (auto ID : EagerlyDeserializedDecls)
3989 GetDecl(ID);
3990 EagerlyDeserializedDecls.clear();
3991
3992 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003993 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003994 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003995 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3996 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003997 }
3998}
3999
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004000void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004001 // The declaration may have been modified by files later in the chain.
4002 // If this is the case, read the record containing the updates from each file
4003 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004004 serialization::GlobalDeclID ID = Record.ID;
4005 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004006 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004007 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00004008
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004009 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00004010
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004011 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00004012 auto UpdateOffsets = std::move(UpdI->second);
4013 DeclUpdateOffsets.erase(UpdI);
4014
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00004015 // Check if this decl was interesting to the consumer. If we just loaded
4016 // the declaration, then we know it was interesting and we skip the call
4017 // to isConsumerInterestedIn because it is unsafe to call in the
4018 // current ASTReader state.
4019 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004020 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00004021 for (auto &FileAndOffset : UpdateOffsets) {
4022 ModuleFile *F = FileAndOffset.first;
4023 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004024 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
4025 SavedStreamPosition SavedPosition(Cursor);
4026 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004027 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00004028 ASTRecordReader Record(*this, *F);
4029 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004030 (void)RecCode;
4031 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00004032
David L. Jonesbe1557a2016-12-21 00:17:49 +00004033 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
4034 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00004035 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00004036
4037 // We might have made this declaration interesting. If so, remember that
4038 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004039 if (!WasInteresting &&
4040 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
4041 PotentiallyInterestingDecls.push_back(
4042 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00004043 WasInteresting = true;
4044 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004045 }
4046 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00004047 // Add the lazy specializations to the template.
4048 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
4049 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
4050 "Must not have pending specializations");
4051 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
4052 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
4053 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
4054 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
4055 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
4056 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
4057 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00004058
4059 // Load the pending visible updates for this decl context, if it has any.
4060 auto I = PendingVisibleUpdates.find(ID);
4061 if (I != PendingVisibleUpdates.end()) {
4062 auto VisibleUpdates = std::move(I->second);
4063 PendingVisibleUpdates.erase(I);
4064
4065 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004066 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00004067 Lookups[DC].Table.add(
4068 Update.Mod, Update.Data,
4069 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
4070 DC->setHasExternalVisibleStorage(true);
4071 }
Chris Lattner487412d2009-04-27 05:27:42 +00004072}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004073
Richard Smithd61d4ac2015-08-22 20:13:39 +00004074void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
4075 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00004076 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00004077 if (FirstLocal != CanonDecl) {
4078 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
4079 ASTDeclReader::attachPreviousDecl(
4080 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
4081 CanonDecl);
4082 }
4083
4084 if (!LocalOffset) {
4085 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
4086 return;
4087 }
4088
4089 // Load the list of other redeclarations from this module file.
4090 ModuleFile *M = getOwningModuleFile(FirstLocal);
4091 assert(M && "imported decl from no module file");
4092
4093 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
4094 SavedStreamPosition SavedPosition(Cursor);
4095 Cursor.JumpToBit(LocalOffset);
4096
4097 RecordData Record;
4098 unsigned Code = Cursor.ReadCode();
4099 unsigned RecCode = Cursor.readRecord(Code, Record);
4100 (void)RecCode;
4101 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
4102
4103 // FIXME: We have several different dispatches on decl kind here; maybe
4104 // we should instead generate one loop per kind and dispatch up-front?
4105 Decl *MostRecent = FirstLocal;
4106 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4107 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00004108 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
4109 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00004110 }
Richard Smithc3a53252015-02-28 05:57:02 +00004111 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00004112}
4113
4114namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004115
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004116 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004117 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004118 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004119 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004120 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00004121 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004122 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004123 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004124 serialization::GlobalDeclID InterfaceID;
4125 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00004126
Douglas Gregor404cdde2012-01-27 01:47:08 +00004127 void add(ObjCCategoryDecl *Cat) {
4128 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00004129 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004130 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00004131
Douglas Gregor404cdde2012-01-27 01:47:08 +00004132 // Check for duplicate categories.
4133 if (Cat->getDeclName()) {
4134 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00004135 if (Existing &&
4136 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00004137 != Reader.getOwningModuleFile(Cat)) {
4138 // FIXME: We should not warn for duplicates in diamond:
4139 //
4140 // MT //
4141 // / \ //
4142 // ML MR //
4143 // \ / //
4144 // MB //
4145 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004146 // If there are duplicates in ML/MR, there will be warning when
4147 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004148 // importing.
4149 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4150 << Interface->getDeclName() << Cat->getDeclName();
4151 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4152 } else if (!Existing) {
4153 // Record this category.
4154 Existing = Cat;
4155 }
4156 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004157
Douglas Gregor404cdde2012-01-27 01:47:08 +00004158 // Add this category to the end of the chain.
4159 if (Tail)
4160 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4161 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004162 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004163 Tail = Cat;
4164 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004165
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004166 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004167 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004168 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004169 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4170 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004171 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004172 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4173 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004174 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004175 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004176 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004177 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004178
Douglas Gregor404cdde2012-01-27 01:47:08 +00004179 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004180 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004181 }
4182 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004183
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004184 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004185 // If we've loaded all of the category information we care about from
4186 // this module file, we're done.
4187 if (M.Generation <= PreviousGeneration)
4188 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004189
4190 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004191 // module file. If there is no such mapping, we'll find nothing here
4192 // (or in any module it imports).
4193 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4194 if (!LocalID)
4195 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004196
Douglas Gregor404cdde2012-01-27 01:47:08 +00004197 // Perform a binary search to find the local redeclarations for this
4198 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004199 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004200 const ObjCCategoriesInfo *Result
4201 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004202 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004203 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004204 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4205 Result->DefinitionID != LocalID) {
4206 // We didn't find anything. If the class definition is in this module
4207 // file, then the module files it depends on cannot have any categories,
4208 // so suppress further lookup.
4209 return Reader.isDeclIDFromModule(InterfaceID, M);
4210 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004211
Douglas Gregor404cdde2012-01-27 01:47:08 +00004212 // We found something. Dig out all of the categories.
4213 unsigned Offset = Result->Offset;
4214 unsigned N = M.ObjCCategories[Offset];
4215 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4216 for (unsigned I = 0; I != N; ++I)
4217 add(cast_or_null<ObjCCategoryDecl>(
4218 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4219 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004220 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004221 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004222
4223} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004224
Douglas Gregor404cdde2012-01-27 01:47:08 +00004225void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4226 ObjCInterfaceDecl *D,
4227 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004228 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004229 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004230 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004231}
Douglas Gregordab42432011-08-12 00:15:20 +00004232
Richard Smithd6db68c2014-08-07 20:58:41 +00004233template<typename DeclT, typename Fn>
4234static void forAllLaterRedecls(DeclT *D, Fn F) {
4235 F(D);
4236
4237 // Check whether we've already merged D into its redeclaration chain.
4238 // MostRecent may or may not be nullptr if D has not been merged. If
4239 // not, walk the merged redecl chain and see if it's there.
4240 auto *MostRecent = D->getMostRecentDecl();
4241 bool Found = false;
4242 for (auto *Redecl = MostRecent; Redecl && !Found;
4243 Redecl = Redecl->getPreviousDecl())
4244 Found = (Redecl == D);
4245
4246 // If this declaration is merged, apply the functor to all later decls.
4247 if (Found) {
4248 for (auto *Redecl = MostRecent; Redecl != D;
4249 Redecl = Redecl->getPreviousDecl())
4250 F(Redecl);
4251 }
4252}
4253
Vassil Vassilev7d264622017-06-30 22:40:17 +00004254void ASTDeclReader::UpdateDecl(Decl *D,
4255 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004256 while (Record.getIdx() < Record.size()) {
4257 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004258 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004259 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004260 // FIXME: If we also have an update record for instantiating the
4261 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004262 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004263 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004264 // FIXME: We should call addHiddenDecl instead, to add the member
4265 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004266 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004267 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004268 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004269
4270 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004271 // It will be added to the template's lazy specialization set.
4272 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004273 break;
4274
4275 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004276 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004277
Douglas Gregor540fd812012-01-09 18:07:24 +00004278 // Each module has its own anonymous namespace, which is disjoint from
4279 // any other module's anonymous namespaces, so don't attach the anonymous
4280 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004281 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004282 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004283 TU->setAnonymousNamespace(Anon);
4284 else
4285 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4286 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004287 break;
4288 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004289
Richard Smith891fc7f2017-12-05 01:31:47 +00004290 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004291 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004292 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4293 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004294 uint64_t Val = Record.readInt();
4295 if (Val && !VD->getInit()) {
4296 VD->setInit(Record.readExpr());
4297 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4298 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4299 Eval->CheckedICE = true;
4300 Eval->IsICE = Val == 3;
4301 }
4302 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004303 break;
Richard Smith05a21352017-06-22 22:18:46 +00004304 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004305
Richard Smith891fc7f2017-12-05 01:31:47 +00004306 case UPD_CXX_POINT_OF_INSTANTIATION: {
4307 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004308 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004309 VTSD->setPointOfInstantiation(POI);
4310 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4311 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4312 } else {
4313 auto *FD = cast<FunctionDecl>(D);
4314 if (auto *FTSInfo = FD->TemplateOrSpecialization
4315 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4316 FTSInfo->setPointOfInstantiation(POI);
4317 else
4318 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4319 ->setPointOfInstantiation(POI);
4320 }
4321 break;
4322 }
4323
John McCall32791cc2016-01-06 22:34:54 +00004324 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004325 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004326
4327 // We have to read the default argument regardless of whether we use it
4328 // so that hypothetical further update records aren't messed up.
4329 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004330 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004331
4332 // Only apply the update if the parameter still has an uninstantiated
4333 // default argument.
4334 if (Param->hasUninstantiatedDefaultArg())
4335 Param->setDefaultArg(DefaultArg);
4336 break;
4337 }
4338
Richard Smith4b054b22016-08-24 21:25:37 +00004339 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004340 auto *FD = cast<FieldDecl>(D);
4341 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004342
4343 // Only apply the update if the field still has an uninstantiated
4344 // default member initializer.
4345 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4346 if (DefaultInit)
4347 FD->setInClassInitializer(DefaultInit);
4348 else
4349 // Instantiation failed. We can get here if we serialized an AST for
4350 // an invalid program.
4351 FD->removeInClassInitializer();
4352 }
4353 break;
4354 }
4355
Richard Smith4d235792014-08-07 18:53:08 +00004356 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004357 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004358 if (Reader.PendingBodies[FD]) {
4359 // FIXME: Maybe check for ODR violations.
4360 // It's safe to stop now because this update record is always last.
4361 return;
4362 }
4363
David L. Jonesbe1557a2016-12-21 00:17:49 +00004364 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004365 // Maintain AST consistency: any later redeclarations of this function
4366 // are inline if this one is. (We might have merged another declaration
4367 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004368 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4369 FD->setImplicitlyInline();
4370 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004371 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004372 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004373 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004374 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004375 break;
4376 }
4377
Richard Smithcd45dbc2014-04-19 03:48:30 +00004378 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4379 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004380 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004381 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004382 OldDD && (OldDD->Definition != RD ||
4383 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004384 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004385 RD->setArgPassingRestrictions(
4386 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004387 ReadCXXRecordDefinition(RD, /*Update*/true);
4388
Richard Smithcd45dbc2014-04-19 03:48:30 +00004389 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004390 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004391 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004392 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004393 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004394 }
4395
David L. Jonesbe1557a2016-12-21 00:17:49 +00004396 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004397 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004398 if (MemberSpecializationInfo *MSInfo =
4399 RD->getMemberSpecializationInfo()) {
4400 MSInfo->setTemplateSpecializationKind(TSK);
4401 MSInfo->setPointOfInstantiation(POI);
4402 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004403 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004404 Spec->setTemplateSpecializationKind(TSK);
4405 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004406
David L. Jonesbe1557a2016-12-21 00:17:49 +00004407 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004408 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004409 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004410 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004411 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004412 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004413 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004414
4415 // FIXME: If we already have a partial specialization set,
4416 // check that it matches.
4417 if (!Spec->getSpecializedTemplateOrPartial()
4418 .is<ClassTemplatePartialSpecializationDecl *>())
4419 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004420 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004421 }
4422
David L. Jonesbe1557a2016-12-21 00:17:49 +00004423 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004424 RD->setLocation(ReadSourceLocation());
4425 RD->setLocStart(ReadSourceLocation());
4426 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004427
David L. Jonesbe1557a2016-12-21 00:17:49 +00004428 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004429 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004430 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004431 // If the declaration already has attributes, we assume that some other
4432 // AST file already loaded them.
4433 if (!D->hasAttrs())
4434 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004435 }
4436 break;
4437 }
4438
Richard Smithf8134002015-03-10 01:41:22 +00004439 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4440 // Set the 'operator delete' directly to avoid emitting another update
4441 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004442 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004443 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004444 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004445 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004446 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004447 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004448 First->OperatorDeleteThisArg = ThisArg;
4449 }
Richard Smithf8134002015-03-10 01:41:22 +00004450 break;
4451 }
4452
Richard Smith564417a2014-03-20 21:47:22 +00004453 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004454 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004455 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004456 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004457
4458 // Update this declaration's exception specification, if needed.
4459 auto *FD = cast<FunctionDecl>(D);
4460 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4461 // FIXME: If the exception specification is already present, check that it
4462 // matches.
4463 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004464 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004465 FPT->getReturnType(), FPT->getParamTypes(),
4466 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004467
4468 // When we get to the end of deserializing, see if there are other decls
4469 // that we need to propagate this exception specification onto.
4470 Reader.PendingExceptionSpecUpdates.insert(
4471 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004472 }
Richard Smith564417a2014-03-20 21:47:22 +00004473 break;
4474 }
4475
Richard Smith1fa5d642013-05-11 05:45:24 +00004476 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004477 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004478 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004479 Reader.PendingDeducedTypeUpdates.insert(
4480 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004481 break;
4482 }
Eli Friedman276dd182013-09-05 00:02:25 +00004483
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004484 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004485 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004486 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004487 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004488
4489 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004490 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4491 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004492 break;
4493
4494 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004495 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4496 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004497 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004498
Alexey Bataev97720002014-11-11 04:05:39 +00004499 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004500 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4501 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004502 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004503
Alexey Bataev27ef9512019-03-20 20:14:22 +00004504 case UPD_DECL_MARKED_OPENMP_ALLOCATE: {
4505 auto AllocatorKind =
4506 static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(Record.readInt());
4507 Expr *Allocator = Record.readExpr();
4508 SourceRange SR = ReadSourceRange();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00004509 D->addAttr(OMPAllocateDeclAttr::CreateImplicit(
Alexey Bataev27ef9512019-03-20 20:14:22 +00004510 Reader.getContext(), AllocatorKind, Allocator, SR));
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004511 break;
Alexey Bataev27ef9512019-03-20 20:14:22 +00004512 }
Alexey Bataev25ed0c02019-03-07 17:54:44 +00004513
Alex Denisovfde64952015-06-26 05:28:36 +00004514 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004515 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004516 auto *Exported = cast<NamedDecl>(D);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004517 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith13897eb2018-09-12 23:37:00 +00004518 Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
4519 Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004520 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004521 }
Alex Denisovfde64952015-06-26 05:28:36 +00004522
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004523 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00004524 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
4525 Reader.getContext(),
4526 static_cast<OMPDeclareTargetDeclAttr::MapTypeTy>(Record.readInt()),
4527 ReadSourceRange()));
4528 break;
4529
Alex Denisovfde64952015-06-26 05:28:36 +00004530 case UPD_ADDED_ATTR_TO_RECORD:
4531 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004532 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004533 assert(Attrs.size() == 1);
4534 D->addAttr(Attrs[0]);
4535 break;
4536 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004537 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004538}