blob: d22a98c13c2735269d8e0ce4070c8ad51e9c49c5 [file] [log] [blame]
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001//===- ASTReaderDecl.cpp - Decl Deserialization ---------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file implements the ASTReader::ReadDeclRecord method, which is the
Chris Lattner487412d2009-04-27 05:27:42 +000011// entrypoint for loading a decl.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000015#include "ASTCommon.h"
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +000016#include "ASTReaderInternals.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/ASTContext.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000018#include "clang/AST/Attr.h"
19#include "clang/AST/AttrIterator.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/AST/DeclCXX.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000023#include "clang/AST/DeclFriend.h"
24#include "clang/AST/DeclObjC.h"
25#include "clang/AST/DeclOpenMP.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/AST/DeclTemplate.h"
27#include "clang/AST/DeclVisitor.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000028#include "clang/AST/DeclarationName.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "clang/AST/Expr.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000030#include "clang/AST/ExternalASTSource.h"
31#include "clang/AST/LambdaCapture.h"
32#include "clang/AST/NestedNameSpecifier.h"
Kelvin Li1408f912018-09-26 04:28:39 +000033#include "clang/AST/OpenMPClause.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000034#include "clang/AST/Redeclarable.h"
35#include "clang/AST/Stmt.h"
36#include "clang/AST/TemplateBase.h"
37#include "clang/AST/Type.h"
38#include "clang/AST/UnresolvedSet.h"
39#include "clang/Basic/AttrKinds.h"
40#include "clang/Basic/ExceptionSpecificationType.h"
41#include "clang/Basic/IdentifierTable.h"
42#include "clang/Basic/LLVM.h"
43#include "clang/Basic/Lambda.h"
44#include "clang/Basic/LangOptions.h"
45#include "clang/Basic/Linkage.h"
46#include "clang/Basic/Module.h"
47#include "clang/Basic/PragmaKinds.h"
48#include "clang/Basic/SourceLocation.h"
49#include "clang/Basic/Specifiers.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000050#include "clang/Sema/IdentifierResolver.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000051#include "clang/Sema/SemaDiagnostic.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000052#include "clang/Serialization/ASTBitCodes.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000053#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000054#include "clang/Serialization/ContinuousRangeMap.h"
55#include "clang/Serialization/Module.h"
56#include "llvm/ADT/DenseMap.h"
57#include "llvm/ADT/FoldingSet.h"
58#include "llvm/ADT/STLExtras.h"
59#include "llvm/ADT/SmallPtrSet.h"
60#include "llvm/ADT/SmallVector.h"
61#include "llvm/ADT/iterator_range.h"
62#include "llvm/Bitcode/BitstreamReader.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000065#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000066#include <algorithm>
67#include <cassert>
68#include <cstdint>
69#include <cstring>
70#include <string>
71#include <utility>
Hans Wennborgdcfba332015-10-06 23:40:43 +000072
Chris Lattner487412d2009-04-27 05:27:42 +000073using namespace clang;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000074using namespace serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000075
76//===----------------------------------------------------------------------===//
77// Declaration deserialization
78//===----------------------------------------------------------------------===//
79
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000080namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000081
Sebastian Redlb3298c32010-08-18 23:56:48 +000082 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000083 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000084 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000085 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000086 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000087 const SourceLocation ThisDeclLoc;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000088
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000089 using RecordData = ASTReader::RecordData;
90
Richard Smith600adef2018-07-04 02:25:38 +000091 TypeID DeferredTypeID = 0;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +000092 unsigned AnonymousDeclNumber;
93 GlobalDeclID NamedDeclForTagDecl = 0;
94 IdentifierInfo *TypedefNameForLinkage = nullptr;
95
96 bool HasPendingBody = false;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +000097
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000098 ///A flag to carry the information for a decl from the entity is
Vassil Vassilev928c8252016-04-28 14:13:28 +000099 /// used. We use it to delay the marking of the canonical decl as used until
100 /// the entire declaration is deserialized and merged.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000101 bool IsDeclMarkedUsed = false;
Vassil Vassilev928c8252016-04-28 14:13:28 +0000102
Sebastian Redlc67764e2010-07-22 22:43:28 +0000103 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000104
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000105 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000106 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000107 assert(LocalOffset < Loc.Offset && "offset point after current record");
108 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +0000109 }
110
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000111 uint64_t ReadGlobalOffset() {
112 uint64_t Local = ReadLocalOffset();
113 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +0000114 }
115
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000116 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000117 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000118 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000119
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000120 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000121 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000122 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000123
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000124 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000125 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +0000126 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +0000127
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000128 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000129 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000130 }
Richard Smith0b884372015-02-27 00:25:58 +0000131
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000132 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000133 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +0000134 }
135
Richard Smith0b884372015-02-27 00:25:58 +0000136 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000137 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000138 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +0000139 }
140
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000141 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000142 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000143 }
144
145 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000146 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000147 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000148 }
149
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000150 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000151 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000152 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000153
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000154 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000155 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000156 }
157
158 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000159 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000160 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000161
David L. Jonesbe1557a2016-12-21 00:17:49 +0000162 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000163 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000164
165 Module *readModule() {
166 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000167 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000168
Richard Smith2a9e5c52015-02-03 03:32:14 +0000169 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000170 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
171 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000172 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000173 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000174 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000175 void MergeDefinitionData(ObjCInterfaceDecl *D,
176 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000177 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
178 void MergeDefinitionData(ObjCProtocolDecl *D,
179 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000180
Richard Smith600adef2018-07-04 02:25:38 +0000181 static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
182
Richard Smithd08aeb62014-08-28 01:33:39 +0000183 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
184 DeclContext *DC,
185 unsigned Index);
186 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
187 unsigned Index, NamedDecl *D);
188
Richard Smith0144f512015-08-22 02:09:38 +0000189 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000190 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000191 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000192 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000193 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000194
Douglas Gregor022857e2011-12-22 01:48:48 +0000195 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000196 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000197 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000198
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000199 /// Retrieve the first ID.
Douglas Gregor022857e2011-12-22 01:48:48 +0000200 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000201
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000202 /// Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000203 bool isKeyDecl() const { return IsKeyDecl; }
204
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000205 /// Get a known declaration that this should be merged with, if
Richard Smith5a4737c2015-02-06 02:42:59 +0000206 /// any.
207 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000208 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000209
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000210 /// Class used to capture the result of searching for an existing
Douglas Gregor022857e2011-12-22 01:48:48 +0000211 /// declaration of a specific kind and name, along with the ability
212 /// to update the place where this result was found (the declaration
213 /// chain hanging off an identifier or the DeclContext we searched in)
214 /// if requested.
215 class FindExistingResult {
216 ASTReader &Reader;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000217 NamedDecl *New = nullptr;
218 NamedDecl *Existing = nullptr;
219 bool AddResult = false;
220 unsigned AnonymousDeclNumber = 0;
221 IdentifierInfo *TypedefNameForLinkage = nullptr;
Richard Smithd08aeb62014-08-28 01:33:39 +0000222
Douglas Gregor022857e2011-12-22 01:48:48 +0000223 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000224 FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000225
Richard Smithd08aeb62014-08-28 01:33:39 +0000226 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000227 unsigned AnonymousDeclNumber,
228 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000229 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000230 AnonymousDeclNumber(AnonymousDeclNumber),
231 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000232
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000233 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000234 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
235 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000236 AnonymousDeclNumber(Other.AnonymousDeclNumber),
237 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000238 Other.AddResult = false;
239 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000240
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000241 FindExistingResult &operator=(FindExistingResult &&) = delete;
Douglas Gregor022857e2011-12-22 01:48:48 +0000242 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000244 /// Suppress the addition of this result into the known set of
Douglas Gregor2009cee2012-01-03 22:46:00 +0000245 /// names.
246 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000247
Douglas Gregor022857e2011-12-22 01:48:48 +0000248 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000249
Douglas Gregor022857e2011-12-22 01:48:48 +0000250 template<typename T>
251 operator T*() const { return dyn_cast_or_null<T>(Existing); }
252 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000253
Richard Smith8a639892015-01-24 01:07:20 +0000254 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
255 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000256 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000257
Chris Lattner487412d2009-04-27 05:27:42 +0000258 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000259 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
260 ASTReader::RecordLocation Loc,
261 DeclID thisDeclID, SourceLocation ThisDeclLoc)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000262 : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
263 ThisDeclLoc(ThisDeclLoc) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000264
Vassil Vassilev7d264622017-06-30 22:40:17 +0000265 template <typename T> static
266 void AddLazySpecializations(T *D,
267 SmallVectorImpl<serialization::DeclID>& IDs) {
268 if (IDs.empty())
269 return;
270
271 // FIXME: We should avoid this pattern of getting the ASTContext.
272 ASTContext &C = D->getASTContext();
273
274 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
275
276 if (auto &Old = LazySpecializations) {
277 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Fangrui Song55fab262018-09-26 22:16:28 +0000278 llvm::sort(IDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +0000279 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
280 }
281
282 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
283 *Result = IDs.size();
284 std::copy(IDs.begin(), IDs.end(), Result + 1);
285
286 LazySpecializations = Result;
287 }
288
Richard Smithb321ecb2014-05-13 01:15:00 +0000289 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000290 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
291 static Decl *getMostRecentDeclImpl(...);
292 static Decl *getMostRecentDecl(Decl *D);
293
294 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000295 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000296 Redeclarable<DeclT> *D, Decl *Previous,
297 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000298 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000299 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
300 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000301
302 template <typename DeclT>
303 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
304 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000305 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000306
Richard Smith851072e2014-05-19 20:59:20 +0000307 template <typename DeclT>
308 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
309 static void markIncompleteDeclChainImpl(...);
310
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000311 /// Determine whether this declaration has a pending body.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000312 bool hasPendingBody() const { return HasPendingBody; }
313
David Blaikieac4345c2017-02-12 18:45:31 +0000314 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000315 void Visit(Decl *D);
316
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000317 void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000318
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000319 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
320 ObjCCategoryDecl *Next) {
321 Cat->NextClassCategory = Next;
322 }
323
Chris Lattner487412d2009-04-27 05:27:42 +0000324 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000325 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000326 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000327 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
328 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000329 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000330 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000331 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
332 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000333 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000334 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000335 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000336 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000337 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000338 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000339 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000340 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
341 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
342 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
343 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
344 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000345 ClassTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000346
Richard Smith1d209d02013-05-23 01:49:11 +0000347 void VisitClassTemplateSpecializationDecl(
348 ClassTemplateSpecializationDecl *D) {
349 VisitClassTemplateSpecializationDeclImpl(D);
350 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000351
Chris Lattnerca025db2010-05-07 21:43:38 +0000352 void VisitClassTemplatePartialSpecializationDecl(
353 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000354 void VisitClassScopeFunctionSpecializationDecl(
355 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000356 RedeclarableResult
357 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000358
Larisse Voufo39a1e502013-08-06 01:03:05 +0000359 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
360 VisitVarTemplateSpecializationDeclImpl(D);
361 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000362
Larisse Voufo39a1e502013-08-06 01:03:05 +0000363 void VisitVarTemplatePartialSpecializationDecl(
364 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000365 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000366 void VisitValueDecl(ValueDecl *VD);
367 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000368 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000369 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000370 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000371 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000372 void VisitCXXMethodDecl(CXXMethodDecl *D);
373 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
374 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
375 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000376 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000377 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000378 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000379 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
380 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000381 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
382 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000383 void VisitDecompositionDecl(DecompositionDecl *DD);
384 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000385 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000386 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000387 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000388 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000389 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000390 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000391 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000392 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000393 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000394 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000395 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000396 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000397 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000398 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000399 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000400 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000401 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000402 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000403 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000404 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
405 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000406 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000407 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000408 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000409
Chris Lattner487412d2009-04-27 05:27:42 +0000410 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000411
412 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000413 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000414
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000415 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000416 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
417 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000418
419 template<typename T>
420 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000421 RedeclarableResult &Redecl,
422 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000423
Richard Smith0b87e072013-10-07 08:02:11 +0000424 template<typename T>
425 void mergeMergeable(Mergeable<T> *D);
426
Richard Smithf17fdbd2014-04-24 02:25:27 +0000427 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
428 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000429 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000430
Douglas Gregor85f3f952015-07-07 03:57:15 +0000431 ObjCTypeParamList *ReadObjCTypeParamList();
432
Alexis Hunted053252010-05-30 07:21:58 +0000433 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000434 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000435 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000436 void VisitObjCContainerDecl(ObjCContainerDecl *D);
437 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
438 void VisitObjCIvarDecl(ObjCIvarDecl *D);
439 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
440 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000441 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
442 void VisitObjCImplDecl(ObjCImplDecl *D);
443 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
444 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
445 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
446 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
447 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000448 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000449 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *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());
861 FD->setExplicitSpecified(Record.readInt());
862 FD->setVirtualAsWritten(Record.readInt());
863 FD->setPure(Record.readInt());
864 FD->setHasInheritedPrototype(Record.readInt());
865 FD->setHasWrittenPrototype(Record.readInt());
866 FD->setDeletedAsWritten(Record.readInt());
867 FD->setTrivial(Record.readInt());
868 FD->setTrivialForCall(Record.readInt());
869 FD->setDefaulted(Record.readInt());
870 FD->setExplicitlyDefaulted(Record.readInt());
871 FD->setHasImplicitReturnZero(Record.readInt());
872 FD->setConstexpr(Record.readInt());
873 FD->setUsesSEHTry(Record.readInt());
874 FD->setHasSkippedBody(Record.readInt());
875 FD->setIsMultiVersion(Record.readInt());
876 FD->setLateTemplateParsed(Record.readInt());
877
878 FD->setCachedLinkage(static_cast<Linkage>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000879 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000880
Richard Trieue6caa262017-12-23 00:41:01 +0000881 FD->ODRHash = Record.readInt();
Erich Keane9c665062018-08-01 21:02:40 +0000882 FD->setHasODRHash(true);
Richard Trieue6caa262017-12-23 00:41:01 +0000883
David L. Jonesbe1557a2016-12-21 00:17:49 +0000884 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000885 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000886 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000887 break;
888 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000889 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000890 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000891 break;
892 case FunctionDecl::TK_MemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000893 auto *InstFD = ReadDeclAs<FunctionDecl>();
894 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000895 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000896 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000897 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000898 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000899 break;
900 }
901 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000902 auto *Template = ReadDeclAs<FunctionTemplateDecl>();
903 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000904
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000905 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000906 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000907 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000908
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000909 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000910 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000911 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000912 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000913 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000914 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000915 TemplArgLocs.reserve(NumTemplateArgLocs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000916 for (unsigned i = 0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000917 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000918
919 LAngleLoc = ReadSourceLocation();
920 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000921 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000922
923 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000924
Douglas Gregor4163aca2011-09-09 21:34:22 +0000925 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000926 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000927 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000928 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000929 for (unsigned i = 0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000930 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000931 FunctionTemplateSpecializationInfo *FTInfo
932 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
933 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000934 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
935 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000936 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000937 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000938
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000939 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000940 // The template that contains the specializations set. It's not safe to
941 // use getCanonicalDecl on Template since it may still be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +0000942 auto *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000943 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
944 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
945 // FunctionTemplateSpecializationInfo's Profile().
946 // We avoid getASTContext because a decl in the parent hierarchy may
947 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000948 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000949 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000950 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000951 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000952 FunctionTemplateSpecializationInfo *ExistingInfo =
953 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000954 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000955 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
956 else {
957 assert(Reader.getContext().getLangOpts().Modules &&
958 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000959 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000960 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000961 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000962 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000963 }
964 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
965 // Templates.
966 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000967 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000968 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000969 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
970
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000971 // Templates args.
972 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000973 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000974 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000975 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000976 TemplArgs.setLAngleLoc(ReadSourceLocation());
977 TemplArgs.setRAngleLoc(ReadSourceLocation());
978
Douglas Gregor4163aca2011-09-09 21:34:22 +0000979 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000980 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000981 // These are not merged; we don't need to merge redeclarations of dependent
982 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000983 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000984 }
985 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000986
Chris Lattnerca025db2010-05-07 21:43:38 +0000987 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000988 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000989 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000990 Params.reserve(NumParams);
991 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000992 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000993 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000994}
995
Sebastian Redlb3298c32010-08-18 23:56:48 +0000996void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000997 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000998 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000999 // Load the body on-demand. Most clients won't care, because method
1000 // definitions rarely show up in headers.
1001 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
1002 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001003 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
1004 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001005 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001006 MD->setInstanceMethod(Record.readInt());
1007 MD->setVariadic(Record.readInt());
1008 MD->setPropertyAccessor(Record.readInt());
1009 MD->setDefined(Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001010 MD->setOverriding(Record.readInt());
1011 MD->setHasSkippedBody(Record.readInt());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001012
Erich Keane9b18eca2018-08-01 21:31:08 +00001013 MD->setIsRedeclaration(Record.readInt());
1014 MD->setHasRedeclaration(Record.readInt());
1015 if (MD->hasRedeclaration())
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001016 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001017 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +00001018
David L. Jonesbe1557a2016-12-21 00:17:49 +00001019 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
1020 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
Erich Keane9b18eca2018-08-01 21:31:08 +00001021 MD->setRelatedResultType(Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001022 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001023 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
1024 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001025 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001026 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001027 Params.reserve(NumParams);
1028 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001029 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001030
Erich Keane9b18eca2018-08-01 21:31:08 +00001031 MD->setSelLocsKind((SelectorLocationsKind)Record.readInt());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001032 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001033 SmallVector<SourceLocation, 16> SelLocs;
1034 SelLocs.reserve(NumStoredSelLocs);
1035 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001036 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001037
1038 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +00001039}
1040
Douglas Gregor85f3f952015-07-07 03:57:15 +00001041void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +00001042 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +00001043
David L. Jonesbe1557a2016-12-21 00:17:49 +00001044 D->Variance = Record.readInt();
1045 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001046 D->VarianceLoc = ReadSourceLocation();
1047 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001048}
1049
Sebastian Redlb3298c32010-08-18 23:56:48 +00001050void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001051 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001052 CD->setAtStartLoc(ReadSourceLocation());
1053 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +00001054}
1055
Douglas Gregor85f3f952015-07-07 03:57:15 +00001056ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001057 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001058 if (numParams == 0)
1059 return nullptr;
1060
1061 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
1062 typeParams.reserve(numParams);
1063 for (unsigned i = 0; i != numParams; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001064 auto *typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001065 if (!typeParam)
1066 return nullptr;
1067
1068 typeParams.push_back(typeParam);
1069 }
1070
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001071 SourceLocation lAngleLoc = ReadSourceLocation();
1072 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001073
1074 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1075 typeParams, rAngleLoc);
1076}
1077
Manman Renec315f12016-09-09 23:48:27 +00001078void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001079 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001080 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001081 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001082
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001083 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001084 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001085
Manman Renec315f12016-09-09 23:48:27 +00001086 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001087 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001088 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1089 Protocols.reserve(NumProtocols);
1090 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001091 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001092 SmallVector<SourceLocation, 16> ProtoLocs;
1093 ProtoLocs.reserve(NumProtocols);
1094 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001095 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001096 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1097 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001098
Manman Renec315f12016-09-09 23:48:27 +00001099 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001100 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001101 Protocols.clear();
1102 Protocols.reserve(NumProtocols);
1103 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001104 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001105 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1106 Reader.getContext());
1107}
1108
1109void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1110 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1111 // FIXME: odr checking?
1112}
1113
Sebastian Redlb3298c32010-08-18 23:56:48 +00001114void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001115 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001116 VisitObjCContainerDecl(ID);
Richard Smith600adef2018-07-04 02:25:38 +00001117 DeferredTypeID = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001118 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001119
1120 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001121 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001122 // Read the definition.
1123 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001124
David L. Jonesbe1557a2016-12-21 00:17:49 +00001125 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001126 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1127 if (Canon->Data.getPointer()) {
1128 // If we already have a definition, keep the definition invariant and
1129 // merge the data.
1130 MergeDefinitionData(Canon, std::move(ID->data()));
1131 ID->Data = Canon->Data;
1132 } else {
1133 // Set the definition data of the canonical declaration, so other
1134 // redeclarations will see it.
1135 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001136
Manman Renec315f12016-09-09 23:48:27 +00001137 // We will rebuild this list lazily.
1138 ID->setIvarList(nullptr);
1139 }
Craig Toppera13603a2014-05-22 05:54:18 +00001140
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001141 // Note that we have deserialized a definition.
1142 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001143
Douglas Gregor404cdde2012-01-27 01:47:08 +00001144 // Note that we've loaded this Objective-C class.
1145 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001146 } else {
1147 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001148 }
Chris Lattner487412d2009-04-27 05:27:42 +00001149}
1150
Sebastian Redlb3298c32010-08-18 23:56:48 +00001151void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001152 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001153 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001154 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001155 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001156 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001157 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001158}
1159
Graydon Hoaree0a68352017-06-28 18:36:27 +00001160void ASTDeclReader::ReadObjCDefinitionData(
1161 struct ObjCProtocolDecl::DefinitionData &Data) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001162 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001163 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1164 ProtoRefs.reserve(NumProtoRefs);
1165 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001166 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001167 SmallVector<SourceLocation, 16> ProtoLocs;
1168 ProtoLocs.reserve(NumProtoRefs);
1169 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001170 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001171 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1172 ProtoLocs.data(), Reader.getContext());
1173}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001174
Graydon Hoaree0a68352017-06-28 18:36:27 +00001175void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1176 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1177 // FIXME: odr checking?
1178}
1179
1180void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1181 RedeclarableResult Redecl = VisitRedeclarable(PD);
1182 VisitObjCContainerDecl(PD);
1183 mergeRedeclarable(PD, Redecl);
1184
1185 if (Record.readInt()) {
1186 // Read the definition.
1187 PD->allocateDefinitionData();
1188
1189 ReadObjCDefinitionData(PD->data());
1190
1191 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1192 if (Canon->Data.getPointer()) {
1193 // If we already have a definition, keep the definition invariant and
1194 // merge the data.
1195 MergeDefinitionData(Canon, std::move(PD->data()));
1196 PD->Data = Canon->Data;
1197 } else {
1198 // Set the definition data of the canonical declaration, so other
1199 // redeclarations will see it.
1200 PD->getCanonicalDecl()->Data = PD->Data;
1201 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001202 // Note that we have deserialized a definition.
1203 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001204 } else {
1205 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001206 }
Chris Lattner487412d2009-04-27 05:27:42 +00001207}
1208
Sebastian Redlb3298c32010-08-18 23:56:48 +00001209void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001210 VisitFieldDecl(FD);
1211}
1212
Sebastian Redlb3298c32010-08-18 23:56:48 +00001213void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001214 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001215 CD->setCategoryNameLoc(ReadSourceLocation());
1216 CD->setIvarLBraceLoc(ReadSourceLocation());
1217 CD->setIvarRBraceLoc(ReadSourceLocation());
1218
Douglas Gregor404cdde2012-01-27 01:47:08 +00001219 // Note that this category has been deserialized. We do this before
1220 // deserializing the interface declaration, so that it will consider this
1221 /// category.
1222 Reader.CategoriesDeserialized.insert(CD);
1223
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001224 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001225 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001226 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001227 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001228 ProtoRefs.reserve(NumProtoRefs);
1229 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001230 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001231 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001232 ProtoLocs.reserve(NumProtoRefs);
1233 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001234 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001235 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001236 Reader.getContext());
Bruno Cardoso Lopesfbff2fa2018-04-27 18:01:23 +00001237
1238 // Protocols in the class extension belong to the class.
1239 if (NumProtoRefs > 0 && CD->ClassInterface && CD->IsClassExtension())
1240 CD->ClassInterface->mergeClassExtensionProtocolList(
1241 (ObjCProtocolDecl *const *)ProtoRefs.data(), NumProtoRefs,
1242 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001243}
1244
Sebastian Redlb3298c32010-08-18 23:56:48 +00001245void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001246 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001247 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001248}
1249
Sebastian Redlb3298c32010-08-18 23:56:48 +00001250void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001251 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001252 D->setAtLoc(ReadSourceLocation());
1253 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001254 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001255 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001256 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001257 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001258 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001259 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001260 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001261 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001262 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001263 DeclarationName GetterName = Record.readDeclarationName();
1264 SourceLocation GetterLoc = ReadSourceLocation();
1265 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1266 DeclarationName SetterName = Record.readDeclarationName();
1267 SourceLocation SetterLoc = ReadSourceLocation();
1268 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001269 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1270 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1271 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001272}
1273
Sebastian Redlb3298c32010-08-18 23:56:48 +00001274void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001275 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001276 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001277}
1278
Sebastian Redlb3298c32010-08-18 23:56:48 +00001279void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001280 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001281 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001282}
1283
Sebastian Redlb3298c32010-08-18 23:56:48 +00001284void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001285 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001286 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1287 D->SuperLoc = ReadSourceLocation();
1288 D->setIvarLBraceLoc(ReadSourceLocation());
1289 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001290 D->setHasNonZeroConstructors(Record.readInt());
1291 D->setHasDestructors(Record.readInt());
1292 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001293 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001294 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001295}
1296
Sebastian Redlb3298c32010-08-18 23:56:48 +00001297void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001298 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001299 D->setAtLoc(ReadSourceLocation());
1300 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1301 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1302 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001303 D->setGetterCXXConstructor(Record.readExpr());
1304 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001305}
1306
Sebastian Redlb3298c32010-08-18 23:56:48 +00001307void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001308 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001309 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001310
1311 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1312 FD->InitStorage.setInt(ISK);
1313 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1314 ? Record.readType().getAsOpaquePtr()
1315 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001316 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001317
1318 if (auto *BW = Record.readExpr())
1319 FD->setBitWidth(BW);
1320
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001321 if (!FD->getDeclName()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001322 if (auto *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001323 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001324 }
Richard Smith0b87e072013-10-07 08:02:11 +00001325 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001326}
1327
John McCall5e77d762013-04-16 07:28:30 +00001328void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1329 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001330 PD->GetterId = Record.getIdentifierInfo();
1331 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001332}
1333
Francois Pichet783dd6e2010-11-21 06:08:52 +00001334void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1335 VisitValueDecl(FD);
1336
David L. Jonesbe1557a2016-12-21 00:17:49 +00001337 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001338 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001339 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001340
1341 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001342 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001343
1344 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001345}
1346
Larisse Voufo39a1e502013-08-06 01:03:05 +00001347ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001348 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001349 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001350
David L. Jonesbe1557a2016-12-21 00:17:49 +00001351 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1352 VD->VarDeclBits.TSCSpec = Record.readInt();
1353 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001354 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001355 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1356 Record.readInt();
1357 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
Taiju Tsuiki3be68e12018-06-19 05:35:30 +00001358 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001359 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001360 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001361 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1362 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1363 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1364 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1365 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1366 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001367 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
Akira Hatanaka8e57b072018-10-01 21:51:28 +00001368 VD->NonParmVarDeclBits.EscapingByref = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001369 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001370 auto VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001371 VD->setCachedLinkage(VarLinkage);
1372
1373 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001374 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001375 VD->getLexicalDeclContext()->isFunctionOrMethod())
1376 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001377
David L. Jonesbe1557a2016-12-21 00:17:49 +00001378 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001379 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001380 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001381 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1382 Eval->CheckedICE = true;
1383 Eval->IsICE = Val == 3;
1384 }
1385 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001386
Akira Hatanaka9978da32018-08-10 15:09:24 +00001387 if (VD->hasAttr<BlocksAttr>() && VD->getType()->getAsCXXRecordDecl()) {
1388 Expr *CopyExpr = Record.readExpr();
1389 if (CopyExpr)
1390 Reader.getContext().setBlockVarCopyInit(VD, CopyExpr, Record.readInt());
1391 }
1392
Richard Smitha4653622017-09-06 20:01:14 +00001393 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1394 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1395
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001396 enum VarKind {
1397 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1398 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001399 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001400 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001401 // Only true variables (not parameters or implicit parameters) can be
1402 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001403 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1404 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001405 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001406 break;
1407 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001408 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001409 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001410 break;
1411 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001412 auto *Tmpl = ReadDeclAs<VarDecl>();
1413 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001414 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001415 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001416 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001417 break;
1418 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001419 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001420
1421 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001422}
1423
Sebastian Redlb3298c32010-08-18 23:56:48 +00001424void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001425 VisitVarDecl(PD);
1426}
1427
Sebastian Redlb3298c32010-08-18 23:56:48 +00001428void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001429 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001430 unsigned isObjCMethodParam = Record.readInt();
1431 unsigned scopeDepth = Record.readInt();
1432 unsigned scopeIndex = Record.readInt();
1433 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001434 if (isObjCMethodParam) {
1435 assert(scopeDepth == 0);
1436 PD->setObjCMethodScopeInfo(scopeIndex);
1437 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1438 } else {
1439 PD->setScopeInfo(scopeDepth, scopeIndex);
1440 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001441 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1442 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1443 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001444 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001445
1446 // FIXME: If this is a redeclaration of a function from another module, handle
1447 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001448}
1449
Richard Smith7b76d812016-08-12 02:21:25 +00001450void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1451 VisitVarDecl(DD);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001452 auto **BDs = DD->getTrailingObjects<BindingDecl *>();
Richard Smith7b76d812016-08-12 02:21:25 +00001453 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001454 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001455}
1456
1457void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1458 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001459 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001460}
1461
Sebastian Redlb3298c32010-08-18 23:56:48 +00001462void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001463 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001464 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001465 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001466}
1467
Sebastian Redlb3298c32010-08-18 23:56:48 +00001468void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001469 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001470 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001471 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001472 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001473 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001474 Params.reserve(NumParams);
1475 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001476 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001477 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001478
David L. Jonesbe1557a2016-12-21 00:17:49 +00001479 BD->setIsVariadic(Record.readInt());
1480 BD->setBlockMissingReturnType(Record.readInt());
1481 BD->setIsConversionFromLambda(Record.readInt());
Akira Hatanakadb49a1f2018-08-01 23:51:53 +00001482 BD->setDoesNotEscape(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001483
David L. Jonesbe1557a2016-12-21 00:17:49 +00001484 bool capturesCXXThis = Record.readInt();
1485 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001486 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001487 captures.reserve(numCaptures);
1488 for (unsigned i = 0; i != numCaptures; ++i) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001489 auto *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001490 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001491 bool byRef = (flags & 1);
1492 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001493 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001494
1495 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1496 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001497 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001498}
1499
Ben Langmuirce914fc2013-05-03 19:20:19 +00001500void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1501 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001502 unsigned ContextParamPos = Record.readInt();
1503 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001504 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001505 for (unsigned I = 0; I < CD->NumParams; ++I) {
1506 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001507 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001508 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001509 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001510 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001511}
1512
Sebastian Redlb3298c32010-08-18 23:56:48 +00001513void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001514 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001515 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001516 D->setExternLoc(ReadSourceLocation());
1517 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001518}
1519
Richard Smith8df390f2016-09-08 23:14:54 +00001520void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1521 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001522 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001523}
1524
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001525void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1526 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001527 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001528}
1529
Sebastian Redlb3298c32010-08-18 23:56:48 +00001530void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001531 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001532 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001533 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001534 D->LocStart = ReadSourceLocation();
1535 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001536
Richard Smith15e32fd2015-03-17 02:23:11 +00001537 // Defer loading the anonymous namespace until we've finished merging
1538 // this namespace; loading it might load a later declaration of the
1539 // same namespace, and we have an invariant that older declarations
1540 // get merged before newer ones try to merge.
1541 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001542 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001543 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001544 } else {
1545 // Link this namespace back to the first declaration, which has already
1546 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001547 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001548 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001549
1550 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001551
1552 if (AnonNamespace) {
1553 // Each module has its own anonymous namespace, which is disjoint from
1554 // any other module's anonymous namespaces, so don't attach the anonymous
1555 // namespace at all.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001556 auto *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001557 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001558 D->setAnonymousNamespace(Anon);
1559 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001560}
1561
Sebastian Redlb3298c32010-08-18 23:56:48 +00001562void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001563 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001564 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001565 D->NamespaceLoc = ReadSourceLocation();
1566 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001567 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001568 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001569 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001570}
1571
Sebastian Redlb3298c32010-08-18 23:56:48 +00001572void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001573 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001574 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001575 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001576 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1577 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001578 D->setTypename(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001579 if (auto *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001580 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001581 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001582}
1583
Richard Smith151c4562016-12-20 21:35:28 +00001584void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1585 VisitNamedDecl(D);
1586 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001587 auto **Expansions = D->getTrailingObjects<NamedDecl *>();
Richard Smith151c4562016-12-20 21:35:28 +00001588 for (unsigned I = 0; I != D->NumExpansions; ++I)
1589 Expansions[I] = ReadDeclAs<NamedDecl>();
1590 mergeMergeable(D);
1591}
1592
Sebastian Redlb3298c32010-08-18 23:56:48 +00001593void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001594 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001595 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001596 D->Underlying = ReadDeclAs<NamedDecl>();
1597 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001598 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001599 auto *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001600 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001601 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001602 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001603}
1604
Richard Smith5179eb72016-06-28 19:03:57 +00001605void ASTDeclReader::VisitConstructorUsingShadowDecl(
1606 ConstructorUsingShadowDecl *D) {
1607 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001608 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1609 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001610 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001611}
1612
Sebastian Redlb3298c32010-08-18 23:56:48 +00001613void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001614 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001615 D->UsingLoc = ReadSourceLocation();
1616 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001617 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001618 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1619 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001620}
1621
Sebastian Redlb3298c32010-08-18 23:56:48 +00001622void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001623 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001624 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001625 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001626 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001627 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001628 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001629}
1630
Sebastian Redlb3298c32010-08-18 23:56:48 +00001631void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001632 UnresolvedUsingTypenameDecl *D) {
1633 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001634 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001635 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001636 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001637 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001638}
1639
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001640void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001641 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001642 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001643 Data.UserDeclaredConstructor = Record.readInt();
1644 Data.UserDeclaredSpecialMembers = Record.readInt();
1645 Data.Aggregate = Record.readInt();
1646 Data.PlainOldData = Record.readInt();
1647 Data.Empty = Record.readInt();
1648 Data.Polymorphic = Record.readInt();
1649 Data.Abstract = Record.readInt();
1650 Data.IsStandardLayout = Record.readInt();
Richard Smithb6070db2018-04-05 18:55:37 +00001651 Data.IsCXX11StandardLayout = Record.readInt();
1652 Data.HasBasesWithFields = Record.readInt();
1653 Data.HasBasesWithNonStaticDataMembers = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001654 Data.HasPrivateFields = Record.readInt();
1655 Data.HasProtectedFields = Record.readInt();
1656 Data.HasPublicFields = Record.readInt();
1657 Data.HasMutableFields = Record.readInt();
1658 Data.HasVariantMembers = Record.readInt();
1659 Data.HasOnlyCMembers = Record.readInt();
1660 Data.HasInClassInitializer = Record.readInt();
1661 Data.HasUninitializedReferenceMember = Record.readInt();
1662 Data.HasUninitializedFields = Record.readInt();
1663 Data.HasInheritedConstructor = Record.readInt();
1664 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001665 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001666 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1667 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1668 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001669 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001670 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1671 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1672 Data.DefaultedDestructorIsDeleted = Record.readInt();
1673 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001674 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001675 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001676 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001677 Data.HasIrrelevantDestructor = Record.readInt();
1678 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1679 Data.HasDefaultedDefaultConstructor = Record.readInt();
1680 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1681 Data.HasConstexprDefaultConstructor = Record.readInt();
1682 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1683 Data.ComputedVisibleConversions = Record.readInt();
1684 Data.UserProvidedDefaultConstructor = Record.readInt();
1685 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001686 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1687 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001688 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1689 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1690 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001691 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001692 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001693
Richard Smithcd4a7a42017-09-07 00:55:55 +00001694 if (Record.readInt())
1695 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001696
David L. Jonesbe1557a2016-12-21 00:17:49 +00001697 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001698 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001699 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001700 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001701 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001702 Data.VBases = ReadGlobalOffset();
1703
David L. Jonesb6a8f022016-12-21 04:34:52 +00001704 Record.readUnresolvedSet(Data.Conversions);
1705 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001706 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001707 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001708
Douglas Gregor99ae8062012-02-14 17:54:36 +00001709 if (Data.IsLambda) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001710 using Capture = LambdaCapture;
1711
1712 auto &Lambda = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001713 Lambda.Dependent = Record.readInt();
1714 Lambda.IsGenericLambda = Record.readInt();
1715 Lambda.CaptureDefault = Record.readInt();
1716 Lambda.NumCaptures = Record.readInt();
1717 Lambda.NumExplicitCaptures = Record.readInt();
1718 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001719 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001720 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1721 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001722 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001723 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001724 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001725 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001726 bool IsImplicit = Record.readInt();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001727 auto Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001728 switch (Kind) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001729 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001730 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001731 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001732 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001733 break;
1734 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001735 case LCK_ByRef:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001736 auto *Var = ReadDeclAs<VarDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001737 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001738 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1739 break;
1740 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001741 }
1742 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001743}
1744
Richard Smithcd45dbc2014-04-19 03:48:30 +00001745void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001746 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001747 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001748 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001749 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001750
Richard Smith02793752015-03-27 21:16:39 +00001751 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001752 // Track that we merged the definitions.
1753 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1754 DD.Definition));
1755 Reader.PendingDefinitions.erase(MergeDD.Definition);
Erich Keanef92f31c2018-08-01 20:48:16 +00001756 MergeDD.Definition->setCompleteDefinition(false);
Richard Smith6561f922016-09-12 21:06:40 +00001757 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001758 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1759 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001760 }
1761
Richard Smith2a9e5c52015-02-03 03:32:14 +00001762 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1763 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1764 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001765 // We faked up this definition data because we found a class for which we'd
1766 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001767 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001768 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001769
1770 // Don't change which declaration is the definition; that is required
1771 // to be invariant once we select it.
1772 auto *Def = DD.Definition;
1773 DD = std::move(MergeDD);
1774 DD.Definition = Def;
1775 return;
1776 }
1777
Richard Smithcd45dbc2014-04-19 03:48:30 +00001778 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001779 bool DetectedOdrViolation = false;
1780#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1781#define MATCH_FIELD(Field) \
1782 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1783 OR_FIELD(Field)
1784 MATCH_FIELD(UserDeclaredConstructor)
1785 MATCH_FIELD(UserDeclaredSpecialMembers)
1786 MATCH_FIELD(Aggregate)
1787 MATCH_FIELD(PlainOldData)
1788 MATCH_FIELD(Empty)
1789 MATCH_FIELD(Polymorphic)
1790 MATCH_FIELD(Abstract)
1791 MATCH_FIELD(IsStandardLayout)
Richard Smithb6070db2018-04-05 18:55:37 +00001792 MATCH_FIELD(IsCXX11StandardLayout)
1793 MATCH_FIELD(HasBasesWithFields)
1794 MATCH_FIELD(HasBasesWithNonStaticDataMembers)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001795 MATCH_FIELD(HasPrivateFields)
1796 MATCH_FIELD(HasProtectedFields)
1797 MATCH_FIELD(HasPublicFields)
1798 MATCH_FIELD(HasMutableFields)
1799 MATCH_FIELD(HasVariantMembers)
1800 MATCH_FIELD(HasOnlyCMembers)
1801 MATCH_FIELD(HasInClassInitializer)
1802 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001803 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001804 MATCH_FIELD(HasInheritedConstructor)
1805 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001806 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001807 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1808 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1809 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001810 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001811 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1812 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1813 MATCH_FIELD(DefaultedDestructorIsDeleted)
1814 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001815 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001816 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001817 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001818 MATCH_FIELD(HasIrrelevantDestructor)
1819 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001820 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001821 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1822 OR_FIELD(HasConstexprDefaultConstructor)
1823 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1824 // ComputedVisibleConversions is handled below.
1825 MATCH_FIELD(UserProvidedDefaultConstructor)
1826 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001827 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1828 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001829 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1830 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1831 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1832 MATCH_FIELD(IsLambda)
1833#undef OR_FIELD
1834#undef MATCH_FIELD
1835
1836 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1837 DetectedOdrViolation = true;
1838 // FIXME: Issue a diagnostic if the base classes don't match when we come
1839 // to lazily load them.
1840
1841 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1842 // match when we come to lazily load them.
1843 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1844 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1845 DD.ComputedVisibleConversions = true;
1846 }
1847
1848 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1849 // lazily load it.
1850
1851 if (DD.IsLambda) {
1852 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1853 // when they occur within the body of a function template specialization).
1854 }
1855
Richard Trieufd1acbb2017-04-11 21:31:00 +00001856 if (D->getODRHash() != MergeDD.ODRHash) {
1857 DetectedOdrViolation = true;
1858 }
1859
Richard Smithcd45dbc2014-04-19 03:48:30 +00001860 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001861 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1862 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001863}
1864
Richard Smith2a9e5c52015-02-03 03:32:14 +00001865void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001866 struct CXXRecordDecl::DefinitionData *DD;
1867 ASTContext &C = Reader.getContext();
1868
1869 // Determine whether this is a lambda closure type, so that we can
1870 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001871 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001872 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001873 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001874 LCD_None);
1875 else
1876 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1877
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001878 CXXRecordDecl *Canon = D->getCanonicalDecl();
1879 // Set decl definition data before reading it, so that during deserialization
1880 // when we read CXXRecordDecl, it already has definition data and we don't
1881 // set fake one.
1882 if (!Canon->DefinitionData)
1883 Canon->DefinitionData = DD;
1884 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001885 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001886
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001887 // We might already have a different definition for this record. This can
1888 // happen either because we're reading an update record, or because we've
1889 // already done some merging. Either way, just merge into it.
1890 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001891 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001892 return;
1893 }
1894
Richard Smith97100e32015-06-20 00:22:34 +00001895 // Mark this declaration as being a definition.
Erich Keanef92f31c2018-08-01 20:48:16 +00001896 D->setCompleteDefinition(true);
Richard Smith2a9e5c52015-02-03 03:32:14 +00001897
Richard Smith97100e32015-06-20 00:22:34 +00001898 // If this is not the first declaration or is an update record, we can have
1899 // other redeclarations already. Make a note that we need to propagate the
1900 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001901 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001902 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001903}
1904
Richard Smith1d209d02013-05-23 01:49:11 +00001905ASTDeclReader::RedeclarableResult
1906ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1907 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001908
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001909 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001910
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001911 enum CXXRecKind {
1912 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1913 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001914 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001915 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001916 // Merged when we merge the folding set entry in the primary template.
1917 if (!isa<ClassTemplateSpecializationDecl>(D))
1918 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001919 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001920 case CXXRecTemplate: {
1921 // Merged when we merge the template.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001922 auto *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001923 D->TemplateOrInstantiation = Template;
1924 if (!Template->getTemplatedDecl()) {
1925 // We've not actually loaded the ClassTemplateDecl yet, because we're
1926 // currently being loaded as its pattern. Rely on it to set up our
1927 // TypeForDecl (see VisitClassTemplateDecl).
1928 //
1929 // Beware: we do not yet know our canonical declaration, and may still
1930 // get merged once the surrounding class template has got off the ground.
Richard Smith600adef2018-07-04 02:25:38 +00001931 DeferredTypeID = 0;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001932 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001933 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001934 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001935 case CXXRecMemberSpecialization: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001936 auto *RD = ReadDeclAs<CXXRecordDecl>();
1937 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001938 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001939 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1940 MSI->setPointOfInstantiation(POI);
1941 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001942 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001943 break;
1944 }
1945 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001946
David L. Jonesbe1557a2016-12-21 00:17:49 +00001947 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001948 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001949 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001950 else
1951 // Propagate DefinitionData pointer from the canonical declaration.
1952 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1953
Richard Smith676c4042013-08-29 23:59:27 +00001954 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001955 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001956 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001957 DeclID KeyFn = ReadDeclID();
Erich Keanef92f31c2018-08-01 20:48:16 +00001958 if (KeyFn && D->isCompleteDefinition())
Richard Smitha9a1c682014-07-07 06:38:20 +00001959 // FIXME: This is wrong for the ARM ABI, where some other module may have
1960 // made this function no longer be a key function. We need an update
1961 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001962 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001963 }
Richard Smith1d209d02013-05-23 01:49:11 +00001964
1965 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001966}
1967
Richard Smithbc491202017-02-17 20:05:37 +00001968void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1969 VisitFunctionDecl(D);
Erich Keane9c665062018-08-01 21:02:40 +00001970 D->setIsCopyDeductionCandidate(Record.readInt());
Richard Smithbc491202017-02-17 20:05:37 +00001971}
1972
Sebastian Redlb3298c32010-08-18 23:56:48 +00001973void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001974 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001975
David L. Jonesbe1557a2016-12-21 00:17:49 +00001976 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001977 if (D->isCanonicalDecl()) {
1978 while (NumOverridenMethods--) {
1979 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1980 // MD may be initializing.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00001981 if (auto *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001982 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1983 }
1984 } else {
1985 // We don't care about which declarations this used to override; we get
1986 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001987 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001988 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001989}
1990
Sebastian Redlb3298c32010-08-18 23:56:48 +00001991void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001992 // We need the inherited constructor information to merge the declaration,
1993 // so we have to read it before we call VisitCXXMethodDecl.
1994 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001995 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1996 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001997 *D->getTrailingObjects<InheritedConstructor>() =
1998 InheritedConstructor(Shadow, Ctor);
1999 }
2000
Chris Lattnerca025db2010-05-07 21:43:38 +00002001 VisitCXXMethodDecl(D);
2002}
2003
Sebastian Redlb3298c32010-08-18 23:56:48 +00002004void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002005 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00002006
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002007 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002008 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00002009 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00002010 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00002011 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00002012 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00002013 Canon->OperatorDeleteThisArg = ThisArg;
2014 }
Richard Smithf8134002015-03-10 01:41:22 +00002015 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002016}
2017
Sebastian Redlb3298c32010-08-18 23:56:48 +00002018void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002019 VisitCXXMethodDecl(D);
2020}
2021
Douglas Gregorba345522011-12-02 23:23:56 +00002022void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
2023 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002024 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002025 D->ImportedAndComplete.setInt(Record.readInt());
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002026 auto *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00002027 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002028 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00002029 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00002030}
2031
Sebastian Redlb3298c32010-08-18 23:56:48 +00002032void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00002033 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002034 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00002035}
2036
Sebastian Redlb3298c32010-08-18 23:56:48 +00002037void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002038 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002039 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002040 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002041 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002042 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00002043 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00002044 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00002045 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002046 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002047 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002048 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00002049}
2050
Sebastian Redlb3298c32010-08-18 23:56:48 +00002051void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002052 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002053 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002054 D->NumParams = NumParams;
2055 D->Params = new TemplateParameterList*[NumParams];
2056 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002057 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002058 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002059 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00002060 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002061 D->Friend = GetTypeSourceInfo();
2062 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002063}
2064
Richard Smithf17fdbd2014-04-24 02:25:27 +00002065DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002066 VisitNamedDecl(D);
2067
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002068 DeclID PatternID = ReadDeclID();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002069 auto *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00002070 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00002071 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002072 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00002073
Richard Smithf17fdbd2014-04-24 02:25:27 +00002074 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00002075}
2076
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002077ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00002078ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002079 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002080
Douglas Gregor68444de2012-01-14 15:13:49 +00002081 // Make sure we've allocated the Common pointer first. We do this before
2082 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
2083 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
2084 if (!CanonD->Common) {
2085 CanonD->Common = CanonD->newCommon(Reader.getContext());
2086 Reader.PendingDefinitions.insert(CanonD);
2087 }
2088 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002089
Douglas Gregor68444de2012-01-14 15:13:49 +00002090 // If this is the first declaration of the template, fill in the information
2091 // for the 'common' pointer.
2092 if (ThisDeclID == Redecl.getFirstID()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002093 if (auto *RTD = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002094 assert(RTD->getKind() == D->getKind() &&
2095 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002096 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002097 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002098 D->setMemberSpecialization();
2099 }
2100 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002101
Richard Smithf17fdbd2014-04-24 02:25:27 +00002102 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002103 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002104
Richard Smithf17fdbd2014-04-24 02:25:27 +00002105 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002106
Richard Smithd46d6de2013-10-13 23:50:45 +00002107 // If we merged the template with a prior declaration chain, merge the common
2108 // pointer.
2109 // FIXME: Actually merge here, don't just overwrite.
2110 D->Common = D->getCanonicalDecl()->Common;
2111
Douglas Gregor68444de2012-01-14 15:13:49 +00002112 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002113}
2114
Sebastian Redlb3298c32010-08-18 23:56:48 +00002115void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002116 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002117
Douglas Gregor68444de2012-01-14 15:13:49 +00002118 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002119 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2120 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002121 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002122 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002123 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002124 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002125
2126 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2127 // We were loaded before our templated declaration was. We've not set up
2128 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2129 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002130 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002131 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002132 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002133}
2134
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002135void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2136 llvm_unreachable("BuiltinTemplates are not serialized");
2137}
2138
Larisse Voufo30616382013-08-23 22:21:36 +00002139/// TODO: Unify with ClassTemplateDecl version?
2140/// May require unifying ClassTemplateDecl and
2141/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002142void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2143 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2144
2145 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002146 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002147 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002148 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002149 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002150 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002151 }
2152}
2153
Richard Smith1d209d02013-05-23 01:49:11 +00002154ASTDeclReader::RedeclarableResult
2155ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2156 ClassTemplateSpecializationDecl *D) {
2157 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002158
Douglas Gregor4163aca2011-09-09 21:34:22 +00002159 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002160 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002161 if (auto *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002162 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002163 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002164 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002165 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002166 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002167 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002168 auto *PS =
2169 new (C) ClassTemplateSpecializationDecl::
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002170 SpecializedPartialSpecialization();
2171 PS->PartialSpecialization
2172 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2173 PS->TemplateArgs = ArgList;
2174 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002175 }
2176 }
2177
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002178 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002179 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002180 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002181 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002182 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002183
David L. Jonesbe1557a2016-12-21 00:17:49 +00002184 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002185 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002186 auto *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002187 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002188 // Set this as, or find, the canonical declaration for this specialization
2189 ClassTemplateSpecializationDecl *CanonSpec;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002190 if (auto *Partial = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002191 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002192 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002193 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002194 CanonSpec =
2195 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2196 }
2197 // If there was already a canonical specialization, merge into it.
2198 if (CanonSpec != D) {
2199 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2200
2201 // This declaration might be a definition. Merge with any existing
2202 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002203 if (auto *DDD = D->DefinitionData) {
2204 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002205 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002206 else
Richard Smith053f6c62014-05-16 23:01:30 +00002207 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002208 }
Richard Smith547864d2014-07-11 18:22:58 +00002209 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002210 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002211 }
2212 }
Richard Smith1d209d02013-05-23 01:49:11 +00002213
Richard Smithd55889a2013-09-09 16:55:27 +00002214 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002215 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002216 auto *ExplicitInfo =
2217 new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
Richard Smithd55889a2013-09-09 16:55:27 +00002218 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002219 ExplicitInfo->ExternLoc = ReadSourceLocation();
2220 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002221 D->ExplicitInfo = ExplicitInfo;
2222 }
2223
Richard Smith1d209d02013-05-23 01:49:11 +00002224 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002225}
2226
Sebastian Redlb3298c32010-08-18 23:56:48 +00002227void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002228 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002229 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002230
David L. Jonesb6a8f022016-12-21 04:34:52 +00002231 D->TemplateParams = Record.readTemplateParameterList();
2232 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002233
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002234 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002235 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002236 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002237 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002238 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002239 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002240}
2241
Sebastian Redlb7448632011-08-31 13:59:56 +00002242void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2243 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002244 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002245 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002246}
2247
Sebastian Redlb3298c32010-08-18 23:56:48 +00002248void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002249 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002250
Douglas Gregor68444de2012-01-14 15:13:49 +00002251 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002252 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002253 SmallVector<serialization::DeclID, 32> SpecIDs;
2254 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002255 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002256 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002257}
2258
Larisse Voufo30616382013-08-23 22:21:36 +00002259/// TODO: Unify with ClassTemplateSpecializationDecl version?
2260/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2261/// VarTemplate(Partial)SpecializationDecl with a new data
2262/// structure Template(Partial)SpecializationDecl, and
2263/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002264ASTDeclReader::RedeclarableResult
2265ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2266 VarTemplateSpecializationDecl *D) {
2267 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2268
2269 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002270 if (Decl *InstD = ReadDecl()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002271 if (auto *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002272 D->SpecializedTemplate = VTD;
2273 } else {
2274 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002275 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002276 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002277 C, TemplArgs);
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002278 auto *PS =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002279 new (C)
2280 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2281 PS->PartialSpecialization =
2282 cast<VarTemplatePartialSpecializationDecl>(InstD);
2283 PS->TemplateArgs = ArgList;
2284 D->SpecializedTemplate = PS;
2285 }
2286 }
2287
2288 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002289 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002290 auto *ExplicitInfo =
Larisse Voufo39a1e502013-08-06 01:03:05 +00002291 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2292 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002293 ExplicitInfo->ExternLoc = ReadSourceLocation();
2294 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002295 D->ExplicitInfo = ExplicitInfo;
2296 }
2297
2298 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002299 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002300 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002301 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002302 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002303 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002304
David L. Jonesbe1557a2016-12-21 00:17:49 +00002305 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002306 if (writtenAsCanonicalDecl) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002307 auto *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002308 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002309 // FIXME: If it's already present, merge it.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002310 if (auto *Partial = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002311 CanonPattern->getCommonPtr()->PartialSpecializations
2312 .GetOrInsertNode(Partial);
2313 } else {
2314 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2315 }
2316 }
2317 }
2318
2319 return Redecl;
2320}
2321
Larisse Voufo30616382013-08-23 22:21:36 +00002322/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2323/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2324/// VarTemplate(Partial)SpecializationDecl with a new data
2325/// structure Template(Partial)SpecializationDecl, and
2326/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002327void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2328 VarTemplatePartialSpecializationDecl *D) {
2329 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2330
David L. Jonesb6a8f022016-12-21 04:34:52 +00002331 D->TemplateParams = Record.readTemplateParameterList();
2332 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002333
2334 // These are read/set from/to the first declaration.
2335 if (ThisDeclID == Redecl.getFirstID()) {
2336 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002337 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002338 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002339 }
2340}
2341
Sebastian Redlb3298c32010-08-18 23:56:48 +00002342void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002343 VisitTypeDecl(D);
2344
David L. Jonesbe1557a2016-12-21 00:17:49 +00002345 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002346
David L. Jonesbe1557a2016-12-21 00:17:49 +00002347 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002348 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002349}
2350
Sebastian Redlb3298c32010-08-18 23:56:48 +00002351void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002352 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002353 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002354 D->setDepth(Record.readInt());
2355 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002356 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002357 auto TypesAndInfos =
2358 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002359 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002360 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002361 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002362 }
2363 } else {
2364 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002365 D->ParameterPack = Record.readInt();
2366 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002367 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002368 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002369}
2370
Sebastian Redlb3298c32010-08-18 23:56:48 +00002371void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002372 VisitTemplateDecl(D);
2373 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002374 D->setDepth(Record.readInt());
2375 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002376 if (D->isExpandedParameterPack()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002377 auto **Data = D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002378 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2379 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002380 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002381 } else {
2382 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002383 D->ParameterPack = Record.readInt();
2384 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002385 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002386 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002387 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002388}
2389
Richard Smith3f1b5d02011-05-05 21:57:07 +00002390void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2391 VisitRedeclarableTemplateDecl(D);
2392}
2393
Sebastian Redlb3298c32010-08-18 23:56:48 +00002394void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002395 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002396 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002397 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002398 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002399 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002400}
2401
Michael Han84324352013-02-22 17:15:32 +00002402void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2403 VisitDecl(D);
2404}
2405
Mike Stump11289f42009-09-09 15:08:12 +00002406std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002407ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002408 uint64_t LexicalOffset = ReadLocalOffset();
2409 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002410 return std::make_pair(LexicalOffset, VisibleOffset);
2411}
2412
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002413template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002414ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002415ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002416 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002417 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002418
Richard Smith5fc18a92015-07-12 23:43:21 +00002419 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002420 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002421
Richard Smithd61d4ac2015-08-22 20:13:39 +00002422 uint64_t RedeclOffset = 0;
2423
Douglas Gregor358cd442012-01-15 16:58:34 +00002424 // 0 indicates that this declaration was the only declaration of its entity,
2425 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002426 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002427 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002428 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002429 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002430 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002431 // This declaration was the first local declaration, but may have imported
2432 // other declarations.
2433 IsKeyDecl = N == 1;
2434 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002435
Richard Smithfe620d22015-03-05 23:24:12 +00002436 // We have some declarations that must be before us in our redeclaration
2437 // chain. Read them now, and remember that we ought to merge with one of
2438 // them.
2439 // FIXME: Provide a known merge target to the second and subsequent such
2440 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002441 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002442 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002443
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002444 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002445 } else {
2446 // This declaration was not the first local declaration. Read the first
2447 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002448 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002449 }
2450
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002451 auto *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
Douglas Gregor358cd442012-01-15 16:58:34 +00002452 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002453 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2454 // We temporarily set the first (canonical) declaration as the previous one
2455 // which is the one that matters and mark the real previous DeclID to be
2456 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002457 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002458 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002459 }
Richard Smithd8a83712015-08-22 01:47:18 +00002460
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002461 auto *DAsT = static_cast<T *>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002462
2463 // Note that we need to load local redeclarations of this decl and build a
2464 // decl chain for them. This must happen *after* we perform the preloading
2465 // above; this ensures that the redeclaration chain is built in the correct
2466 // order.
2467 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002468 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002469
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002470 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002471}
2472
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002473/// Attempts to merge the given declaration (D) with another declaration
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002474/// of the same entity.
2475template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002476void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002477 RedeclarableResult &Redecl,
2478 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002479 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002480 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002481 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002482
Richard Smith202850a2015-03-10 02:57:50 +00002483 // If we're not the canonical declaration, we don't need to merge.
2484 if (!DBase->isFirstDecl())
2485 return;
2486
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002487 auto *D = static_cast<T *>(DBase);
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002488
Richard Smith5a4737c2015-02-06 02:42:59 +00002489 if (auto *Existing = Redecl.getKnownMergeTarget())
2490 // We already know of an existing declaration we should merge with.
2491 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2492 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002493 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002494 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2495}
2496
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002497/// "Cast" to type T, asserting if we don't have an implicit conversion.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002498/// We use this to put code in a template that will only be valid for certain
2499/// instantiations.
2500template<typename T> static T assert_cast(T t) { return t; }
2501template<typename T> static T assert_cast(...) {
2502 llvm_unreachable("bad assert_cast");
2503}
2504
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002505/// Merge together the pattern declarations from two template
Richard Smithf17fdbd2014-04-24 02:25:27 +00002506/// declarations.
2507void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2508 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002509 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002510 auto *DPattern = D->getTemplatedDecl();
2511 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002512 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2513 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002514 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002515
Richard Smith547864d2014-07-11 18:22:58 +00002516 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2517 // Merge with any existing definition.
2518 // FIXME: This is duplicated in several places. Refactor.
2519 auto *ExistingClass =
2520 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002521 if (auto *DDD = DClass->DefinitionData) {
2522 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002523 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002524 } else {
2525 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002526 // We may have skipped this before because we thought that DClass
2527 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002528 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002529 }
2530 }
2531 DClass->DefinitionData = ExistingClass->DefinitionData;
2532
Richard Smithf17fdbd2014-04-24 02:25:27 +00002533 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2534 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002535 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002536 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2537 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2538 Result);
2539 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2540 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002541 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2542 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2543 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002544 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002545}
2546
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002547/// Attempts to merge the given declaration (D) with another declaration
Richard Smithd55889a2013-09-09 16:55:27 +00002548/// of the same entity.
2549template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002550void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2551 RedeclarableResult &Redecl,
2552 DeclID TemplatePatternID) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002553 auto *D = static_cast<T *>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002554 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002555 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002556 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002557 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2558 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002559
Richard Smithd55889a2013-09-09 16:55:27 +00002560 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002561 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002562 // appropriate canonical declaration.
2563 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002564 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002565 ExistingCanon->Used |= D->Used;
2566 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002567
2568 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002569 // We cannot have loaded any redeclarations of this declaration yet, so
2570 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002571 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002572 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002573 assert_cast<NamespaceDecl*>(ExistingCanon));
2574
2575 // When we merge a template, merge its pattern.
2576 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2577 mergeTemplatePattern(
2578 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002579 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002580
Richard Smith5fc18a92015-07-12 23:43:21 +00002581 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002582 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002583 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002584 }
2585}
2586
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002587/// ODR-like semantics for C/ObjC allow us to merge tag types and a structural
2588/// check in Sema guarantees the types can be merged (see C11 6.2.7/1 or C89
2589/// 6.1.2.6/1). Although most merging is done in Sema, we need to guarantee
2590/// that some types are mergeable during deserialization, otherwise name
2591/// lookup fails. This is the case for EnumConstantDecl.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002592static bool allowODRLikeMergeInC(NamedDecl *ND) {
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002593 if (!ND)
2594 return false;
2595 // TODO: implement merge for other necessary decls.
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00002596 if (isa<EnumConstantDecl>(ND))
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002597 return true;
2598 return false;
2599}
2600
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002601/// Attempts to merge the given declaration (D) with another declaration
Richard Smith0b87e072013-10-07 08:02:11 +00002602/// of the same entity, for the case where the entity is not actually
2603/// redeclarable. This happens, for instance, when merging the fields of
2604/// identical class definitions from two different modules.
2605template<typename T>
2606void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2607 // If modules are not available, there is no reason to perform this merge.
2608 if (!Reader.getContext().getLangOpts().Modules)
2609 return;
2610
Bruno Cardoso Lopes85f87dd2018-04-30 22:14:29 +00002611 // ODR-based merging is performed in C++ and in some cases (tag types) in C.
2612 // Note that C identically-named things in different translation units are
2613 // not redeclarations, but may still have compatible types, where ODR-like
2614 // semantics may apply.
2615 if (!Reader.getContext().getLangOpts().CPlusPlus &&
2616 !allowODRLikeMergeInC(dyn_cast<NamedDecl>(static_cast<T*>(D))))
Richard Smith01a73372013-10-15 22:02:41 +00002617 return;
2618
Richard Smith0b87e072013-10-07 08:02:11 +00002619 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2620 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002621 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2622 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002623}
2624
Alexey Bataeva769e072013-03-22 06:34:35 +00002625void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2626 VisitDecl(D);
2627 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002628 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002629 Vars.reserve(NumVars);
2630 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002631 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002632 }
2633 D->setVars(Vars);
2634}
2635
Kelvin Li1408f912018-09-26 04:28:39 +00002636void ASTDeclReader::VisitOMPRequiresDecl(OMPRequiresDecl * D) {
2637 VisitDecl(D);
2638 unsigned NumClauses = D->clauselist_size();
2639 SmallVector<OMPClause *, 8> Clauses;
2640 Clauses.reserve(NumClauses);
2641 OMPClauseReader ClauseReader(Record);
2642 for (unsigned I = 0; I != NumClauses; ++I)
2643 Clauses.push_back(ClauseReader.readClause());
2644 D->setClauses(Clauses);
2645}
2646
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002647void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002648 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002649 D->setLocation(ReadSourceLocation());
Alexey Bataeve6aa4692018-09-13 16:54:05 +00002650 Expr *In = Record.readExpr();
2651 Expr *Out = Record.readExpr();
2652 D->setCombinerData(In, Out);
2653 Expr *Combiner = Record.readExpr();
2654 D->setCombiner(Combiner);
2655 Expr *Orig = Record.readExpr();
2656 Expr *Priv = Record.readExpr();
2657 D->setInitializerData(Orig, Priv);
2658 Expr *Init = Record.readExpr();
2659 auto IK = static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt());
2660 D->setInitializer(Init, IK);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002661 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002662}
2663
Alexey Bataev4244be22016-02-11 05:35:55 +00002664void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002665 VisitVarDecl(D);
2666}
2667
Chris Lattner487412d2009-04-27 05:27:42 +00002668//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002669// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002670//===----------------------------------------------------------------------===//
2671
Richard Smithe43e2b32018-08-20 21:47:29 +00002672namespace {
2673class AttrReader {
2674 ModuleFile *F;
2675 ASTReader *Reader;
2676 const ASTReader::RecordData &Record;
2677 unsigned &Idx;
2678
2679public:
2680 AttrReader(ModuleFile &F, ASTReader &Reader,
2681 const ASTReader::RecordData &Record, unsigned &Idx)
2682 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {}
2683
2684 const uint64_t &readInt() { return Record[Idx++]; }
2685
2686 SourceRange readSourceRange() {
2687 return Reader->ReadSourceRange(*F, Record, Idx);
2688 }
2689
2690 Expr *readExpr() { return Reader->ReadExpr(*F); }
2691
2692 std::string readString() {
2693 return Reader->ReadString(Record, Idx);
2694 }
2695
2696 TypeSourceInfo *getTypeSourceInfo() {
2697 return Reader->GetTypeSourceInfo(*F, Record, Idx);
2698 }
2699
2700 IdentifierInfo *getIdentifierInfo() {
2701 return Reader->GetIdentifierInfo(*F, Record, Idx);
2702 }
2703
2704 VersionTuple readVersionTuple() {
2705 return ASTReader::ReadVersionTuple(Record, Idx);
2706 }
2707
2708 template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
2709 return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
2710 }
2711};
2712}
2713
2714Attr *ASTReader::ReadAttr(ModuleFile &M, const RecordData &Rec,
2715 unsigned &Idx) {
2716 AttrReader Record(M, *this, Rec, Idx);
2717 auto V = Record.readInt();
2718 if (!V)
2719 return nullptr;
2720
2721 Attr *New = nullptr;
2722 // Kind is stored as a 1-based integer because 0 is used to indicate a null
2723 // Attr pointer.
2724 auto Kind = static_cast<attr::Kind>(V - 1);
2725 SourceRange Range = Record.readSourceRange();
2726 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002727
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002728#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002729
Richard Smithe43e2b32018-08-20 21:47:29 +00002730 assert(New && "Unable to decode attribute?");
2731 return New;
2732}
2733
2734/// Reads attributes from the current stream position.
2735void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2736 for (unsigned I = 0, E = Record.readInt(); I != E; ++I)
2737 Attrs.push_back(Record.readAttr());
Chris Lattner8f63ab52009-04-27 06:01:06 +00002738}
2739
2740//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002741// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002742//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002743
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002744/// Note that we have loaded the declaration with the given
Chris Lattner487412d2009-04-27 05:27:42 +00002745/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002746///
Chris Lattner487412d2009-04-27 05:27:42 +00002747/// This routine notes that this declaration has already been loaded,
2748/// so that future GetDecl calls will return this declaration rather
2749/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002750inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002751 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2752 DeclsLoaded[Index] = D;
2753}
2754
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002755/// Determine whether the consumer will be interested in seeing
Chris Lattner487412d2009-04-27 05:27:42 +00002756/// this declaration (via HandleTopLevelDecl).
2757///
2758/// This routine should return true for anything that might affect
2759/// code generation, e.g., inline function definitions, Objective-C
2760/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002761static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002762 // An ObjCMethodDecl is never considered as "interesting" because its
2763 // implementation container always is.
2764
Richard Smithcd4a7a42017-09-07 00:55:55 +00002765 // An ImportDecl or VarDecl imported from a module map module will get
2766 // emitted when we import the relevant module.
2767 if (isa<ImportDecl>(D) || isa<VarDecl>(D)) {
2768 auto *M = D->getImportedOwningModule();
2769 if (M && M->Kind == Module::ModuleMapModule &&
2770 Ctx.DeclMustBeEmitted(D))
2771 return false;
2772 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002773
Fangrui Song6907ce22018-07-30 19:24:48 +00002774 if (isa<FileScopeAsmDecl>(D) ||
2775 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002776 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002777 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002778 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002779 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002780 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002781 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2782 return !D->getDeclContext()->isFunctionOrMethod();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002783 if (const auto *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002784 return Var->isFileVarDecl() &&
Alexey Bataevd01b7492018-08-15 19:45:12 +00002785 (Var->isThisDeclarationADefinition() == VarDecl::Definition ||
2786 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002787 if (const auto *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002788 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002789
2790 if (auto *ES = D->getASTContext().getExternalSource())
2791 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2792 return true;
2793
Douglas Gregor87d81242011-09-10 00:22:34 +00002794 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002795}
2796
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002797/// Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002798ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002799ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002800 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2801 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002802 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002803 const DeclOffset &DOffs =
2804 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002805 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002806 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002807}
2808
Douglas Gregord32f0352011-07-22 06:10:01 +00002809ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002810 auto I = GlobalBitOffsetsMap.find(GlobalOffset);
Douglas Gregord32f0352011-07-22 06:10:01 +00002811
2812 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2813 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2814}
2815
Douglas Gregorde3ef502011-11-30 23:21:26 +00002816uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002817 return LocalOffset + M.GlobalBitOffset;
2818}
2819
Richard Smithbf78e642013-06-24 22:51:00 +00002820static bool isSameTemplateParameterList(const TemplateParameterList *X,
2821 const TemplateParameterList *Y);
2822
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002823/// Determine whether two template parameters are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002824/// that they may be used in declarations of the same template.
2825static bool isSameTemplateParameter(const NamedDecl *X,
2826 const NamedDecl *Y) {
2827 if (X->getKind() != Y->getKind())
2828 return false;
2829
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002830 if (const auto *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2831 const auto *TY = cast<TemplateTypeParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002832 return TX->isParameterPack() == TY->isParameterPack();
2833 }
2834
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002835 if (const auto *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2836 const auto *TY = cast<NonTypeTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002837 return TX->isParameterPack() == TY->isParameterPack() &&
2838 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2839 }
2840
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002841 const auto *TX = cast<TemplateTemplateParmDecl>(X);
2842 const auto *TY = cast<TemplateTemplateParmDecl>(Y);
Richard Smithbf78e642013-06-24 22:51:00 +00002843 return TX->isParameterPack() == TY->isParameterPack() &&
2844 isSameTemplateParameterList(TX->getTemplateParameters(),
2845 TY->getTemplateParameters());
2846}
2847
Richard Smith32952e12014-10-14 02:00:47 +00002848static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2849 if (auto *NS = X->getAsNamespace())
2850 return NS;
2851 if (auto *NAS = X->getAsNamespaceAlias())
2852 return NAS->getNamespace();
2853 return nullptr;
2854}
2855
2856static bool isSameQualifier(const NestedNameSpecifier *X,
2857 const NestedNameSpecifier *Y) {
2858 if (auto *NSX = getNamespace(X)) {
2859 auto *NSY = getNamespace(Y);
2860 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2861 return false;
2862 } else if (X->getKind() != Y->getKind())
2863 return false;
2864
2865 // FIXME: For namespaces and types, we're permitted to check that the entity
2866 // is named via the same tokens. We should probably do so.
2867 switch (X->getKind()) {
2868 case NestedNameSpecifier::Identifier:
2869 if (X->getAsIdentifier() != Y->getAsIdentifier())
2870 return false;
2871 break;
2872 case NestedNameSpecifier::Namespace:
2873 case NestedNameSpecifier::NamespaceAlias:
2874 // We've already checked that we named the same namespace.
2875 break;
2876 case NestedNameSpecifier::TypeSpec:
2877 case NestedNameSpecifier::TypeSpecWithTemplate:
2878 if (X->getAsType()->getCanonicalTypeInternal() !=
2879 Y->getAsType()->getCanonicalTypeInternal())
2880 return false;
2881 break;
2882 case NestedNameSpecifier::Global:
2883 case NestedNameSpecifier::Super:
2884 return true;
2885 }
2886
2887 // Recurse into earlier portion of NNS, if any.
2888 auto *PX = X->getPrefix();
2889 auto *PY = Y->getPrefix();
2890 if (PX && PY)
2891 return isSameQualifier(PX, PY);
2892 return !PX && !PY;
2893}
2894
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002895/// Determine whether two template parameter lists are similar enough
Richard Smithbf78e642013-06-24 22:51:00 +00002896/// that they may be used in declarations of the same template.
2897static bool isSameTemplateParameterList(const TemplateParameterList *X,
2898 const TemplateParameterList *Y) {
2899 if (X->size() != Y->size())
2900 return false;
2901
2902 for (unsigned I = 0, N = X->size(); I != N; ++I)
2903 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2904 return false;
2905
2906 return true;
2907}
2908
George Burgess IV95845082017-02-15 22:43:27 +00002909/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002910/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002911static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2912 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002913 // Note that pass_object_size attributes are represented in the function's
2914 // ExtParameterInfo, so we don't need to check them here.
2915
Michael Krusedc5ce722018-08-03 01:21:16 +00002916 // Return false if any of the enable_if expressions of A and B are different.
George Burgess IV95845082017-02-15 22:43:27 +00002917 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
Michael Krusedc5ce722018-08-03 01:21:16 +00002918 auto AEnableIfAttrs = A->specific_attrs<EnableIfAttr>();
2919 auto BEnableIfAttrs = B->specific_attrs<EnableIfAttr>();
2920 auto AEnableIf = AEnableIfAttrs.begin();
2921 auto BEnableIf = BEnableIfAttrs.begin();
2922 for (; AEnableIf != AEnableIfAttrs.end() && BEnableIf != BEnableIfAttrs.end();
2923 ++BEnableIf, ++AEnableIf) {
George Burgess IV95845082017-02-15 22:43:27 +00002924 Cand1ID.clear();
2925 Cand2ID.clear();
2926
Michael Krusedc5ce722018-08-03 01:21:16 +00002927 AEnableIf->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2928 BEnableIf->getCond()->Profile(Cand2ID, B->getASTContext(), true);
George Burgess IV95845082017-02-15 22:43:27 +00002929 if (Cand1ID != Cand2ID)
2930 return false;
2931 }
2932
Michael Krusedc5ce722018-08-03 01:21:16 +00002933 // Return false if the number of enable_if attributes was different.
2934 return AEnableIf == AEnableIfAttrs.end() && BEnableIf == BEnableIfAttrs.end();
George Burgess IV95845082017-02-15 22:43:27 +00002935}
2936
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002937/// Determine whether the two declarations refer to the same entity.
Douglas Gregor022857e2011-12-22 01:48:48 +00002938static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2939 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002940
Douglas Gregor022857e2011-12-22 01:48:48 +00002941 if (X == Y)
2942 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002943
Douglas Gregor022857e2011-12-22 01:48:48 +00002944 // Must be in the same context.
Richard Smith600adef2018-07-04 02:25:38 +00002945 //
2946 // Note that we can't use DeclContext::Equals here, because the DeclContexts
2947 // could be two different declarations of the same function. (We will fix the
2948 // semantic DC to refer to the primary definition after merging.)
2949 if (!declaresSameEntity(cast<Decl>(X->getDeclContext()->getRedeclContext()),
2950 cast<Decl>(Y->getDeclContext()->getRedeclContext())))
Douglas Gregor022857e2011-12-22 01:48:48 +00002951 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002952
2953 // Two typedefs refer to the same entity if they have the same underlying
2954 // type.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002955 if (const auto *TypedefX = dyn_cast<TypedefNameDecl>(X))
2956 if (const auto *TypedefY = dyn_cast<TypedefNameDecl>(Y))
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002957 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2958 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002959
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002960 // Must have the same kind.
2961 if (X->getKind() != Y->getKind())
2962 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002963
Douglas Gregorda389302012-01-01 21:47:52 +00002964 // Objective-C classes and protocols with the same name always match.
2965 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002966 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002967
2968 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002969 // No need to handle these here: we merge them when adding them to the
2970 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002971 return false;
2972 }
Richard Smithd55889a2013-09-09 16:55:27 +00002973
Douglas Gregor2009cee2012-01-03 22:46:00 +00002974 // Compatible tags match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002975 if (const auto *TagX = dyn_cast<TagDecl>(X)) {
2976 const auto *TagY = cast<TagDecl>(Y);
Joao Matose9a3ed42012-08-31 22:18:20 +00002977 return (TagX->getTagKind() == TagY->getTagKind()) ||
2978 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2979 TagX->getTagKind() == TTK_Interface) &&
2980 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2981 TagY->getTagKind() == TTK_Interface));
2982 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002983
Joao Matose9a3ed42012-08-31 22:18:20 +00002984 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002985 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2986 // functions, etc.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00002987 if (const auto *FuncX = dyn_cast<FunctionDecl>(X)) {
2988 const auto *FuncY = cast<FunctionDecl>(Y);
2989 if (const auto *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2990 const auto *CtorY = cast<CXXConstructorDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002991 if (CtorX->getInheritedConstructor() &&
2992 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2993 CtorY->getInheritedConstructor().getConstructor()))
2994 return false;
2995 }
Erich Keane281d20b2018-01-08 21:34:17 +00002996
2997 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
2998 return false;
2999
3000 // Multiversioned functions with different feature strings are represented
3001 // as separate declarations.
3002 if (FuncX->isMultiVersion()) {
3003 const auto *TAX = FuncX->getAttr<TargetAttr>();
3004 const auto *TAY = FuncY->getAttr<TargetAttr>();
3005 assert(TAX && TAY && "Multiversion Function without target attribute");
3006
3007 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
3008 return false;
3009 }
3010
Richard Smitha54d3242017-03-08 23:00:26 +00003011 ASTContext &C = FuncX->getASTContext();
Richard Smith600adef2018-07-04 02:25:38 +00003012 auto GetTypeAsWritten = [](const FunctionDecl *FD) {
3013 // Map to the first declaration that we've already merged into this one.
3014 // The TSI of redeclarations might not match (due to calling conventions
3015 // being inherited onto the type but not the TSI), but the TSI type of
3016 // the first declaration of the function should match across modules.
3017 FD = FD->getCanonicalDecl();
3018 return FD->getTypeSourceInfo() ? FD->getTypeSourceInfo()->getType()
3019 : FD->getType();
3020 };
3021 QualType XT = GetTypeAsWritten(FuncX), YT = GetTypeAsWritten(FuncY);
3022 if (!C.hasSameType(XT, YT)) {
Richard Smitha54d3242017-03-08 23:00:26 +00003023 // We can get functions with different types on the redecl chain in C++17
3024 // if they have differing exception specifications and at least one of
3025 // the excpetion specs is unresolved.
Richard Smith600adef2018-07-04 02:25:38 +00003026 auto *XFPT = XT->getAs<FunctionProtoType>();
3027 auto *YFPT = YT->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003028 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00003029 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
3030 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
Richard Smith600adef2018-07-04 02:25:38 +00003031 C.hasSameFunctionTypeIgnoringExceptionSpec(XT, YT))
Richard Smitha54d3242017-03-08 23:00:26 +00003032 return true;
3033 return false;
3034 }
George Burgess IV95845082017-02-15 22:43:27 +00003035 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00003036 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00003037 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003038
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003039 // Variables with the same type and linkage match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003040 if (const auto *VarX = dyn_cast<VarDecl>(X)) {
3041 const auto *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00003042 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
3043 ASTContext &C = VarX->getASTContext();
3044 if (C.hasSameType(VarX->getType(), VarY->getType()))
3045 return true;
3046
3047 // We can get decls with different types on the redecl chain. Eg.
3048 // template <typename T> struct S { static T Var[]; }; // #1
3049 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
3050 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00003051 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00003052 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
3053 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
3054 if (!VarXTy || !VarYTy)
3055 return false;
3056 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
3057 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
3058 }
3059 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00003060 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00003061
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003062 // Namespaces with the same name and inlinedness match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003063 if (const auto *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
3064 const auto *NamespaceY = cast<NamespaceDecl>(Y);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00003065 return NamespaceX->isInline() == NamespaceY->isInline();
3066 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003067
Richard Smith8f8f05c2013-06-24 04:45:28 +00003068 // Identical template names and kinds match if their template parameter lists
3069 // and patterns match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003070 if (const auto *TemplateX = dyn_cast<TemplateDecl>(X)) {
3071 const auto *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00003072 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00003073 TemplateY->getTemplatedDecl()) &&
3074 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
3075 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00003076 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00003077
Richard Smith0b87e072013-10-07 08:02:11 +00003078 // Fields with the same name and the same type match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003079 if (const auto *FDX = dyn_cast<FieldDecl>(X)) {
3080 const auto *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00003081 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00003082 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
3083 }
3084
Richard Smith8cbd8952015-08-04 02:05:09 +00003085 // Indirect fields with the same target field match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003086 if (const auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
3087 const auto *IFDY = cast<IndirectFieldDecl>(Y);
Richard Smith8cbd8952015-08-04 02:05:09 +00003088 return IFDX->getAnonField()->getCanonicalDecl() ==
3089 IFDY->getAnonField()->getCanonicalDecl();
3090 }
3091
Richard Smith01a73372013-10-15 22:02:41 +00003092 // Enumerators with the same name match.
3093 if (isa<EnumConstantDecl>(X))
3094 // FIXME: Also check the value is odr-equivalent.
3095 return true;
3096
Richard Smithfd8634a2013-10-23 02:17:46 +00003097 // Using shadow declarations with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003098 if (const auto *USX = dyn_cast<UsingShadowDecl>(X)) {
3099 const auto *USY = cast<UsingShadowDecl>(Y);
Richard Smithfd8634a2013-10-23 02:17:46 +00003100 return USX->getTargetDecl() == USY->getTargetDecl();
3101 }
3102
Richard Smith32952e12014-10-14 02:00:47 +00003103 // Using declarations with the same qualifier match. (We already know that
3104 // the name matches.)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003105 if (const auto *UX = dyn_cast<UsingDecl>(X)) {
3106 const auto *UY = cast<UsingDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003107 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3108 UX->hasTypename() == UY->hasTypename() &&
3109 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3110 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003111 if (const auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
3112 const auto *UY = cast<UnresolvedUsingValueDecl>(Y);
Richard Smith32952e12014-10-14 02:00:47 +00003113 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
3114 UX->isAccessDeclaration() == UY->isAccessDeclaration();
3115 }
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003116 if (const auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
Richard Smith32952e12014-10-14 02:00:47 +00003117 return isSameQualifier(
3118 UX->getQualifier(),
3119 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
3120
Richard Smithf4634362014-09-03 23:11:22 +00003121 // Namespace alias definitions with the same target match.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003122 if (const auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
3123 const auto *NAY = cast<NamespaceAliasDecl>(Y);
Richard Smithf4634362014-09-03 23:11:22 +00003124 return NAX->getNamespace()->Equals(NAY->getNamespace());
3125 }
3126
Douglas Gregor022857e2011-12-22 01:48:48 +00003127 return false;
3128}
3129
Richard Smithd55889a2013-09-09 16:55:27 +00003130/// Find the context in which we should search for previous declarations when
3131/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00003132DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
3133 DeclContext *DC) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003134 if (auto *ND = dyn_cast<NamespaceDecl>(DC))
Richard Smithd55889a2013-09-09 16:55:27 +00003135 return ND->getOriginalNamespace();
3136
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003137 if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
Richard Smith8a639892015-01-24 01:07:20 +00003138 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00003139 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003140 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00003141 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00003142
3143 // If there's no definition yet, then DC's definition is added by an update
3144 // record, but we've not yet loaded that update record. In this case, we
3145 // commit to DC being the canonical definition now, and will fix this when
3146 // we load the update record.
3147 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00003148 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Erich Keanef92f31c2018-08-01 20:48:16 +00003149 RD->setCompleteDefinition(true);
Richard Smith8a639892015-01-24 01:07:20 +00003150 RD->DefinitionData = DD;
3151 RD->getCanonicalDecl()->DefinitionData = DD;
3152
3153 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00003154 Reader.PendingFakeDefinitionData.insert(
3155 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00003156 }
3157
3158 return DD->Definition;
3159 }
Richard Smithd55889a2013-09-09 16:55:27 +00003160
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003161 if (auto *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00003162 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
3163 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00003164
Richard Smith4eca9b92015-02-04 23:37:59 +00003165 // We can see the TU here only if we have no Sema object. In that case,
3166 // there's no TU scope to look in, so using the DC alone is sufficient.
3167 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
3168 return TU;
3169
Craig Toppera13603a2014-05-22 05:54:18 +00003170 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003171}
3172
Douglas Gregor022857e2011-12-22 01:48:48 +00003173ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003174 // Record that we had a typedef name for linkage whether or not we merge
3175 // with that declaration.
3176 if (TypedefNameForLinkage) {
3177 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3178 Reader.ImportedTypedefNamesForLinkage.insert(
3179 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3180 return;
3181 }
3182
Douglas Gregor9b47f942012-01-09 17:38:47 +00003183 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003184 return;
Richard Smith95d99302013-07-13 02:00:19 +00003185
Richard Smith70d58502014-08-30 00:04:23 +00003186 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003187 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003188 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003189 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3190 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003191 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003192 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003193 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003194 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3195 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003196 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003197 // Add the declaration to its redeclaration context so later merging
3198 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003199 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003200 }
3201}
3202
Richard Smith88ebade2014-08-23 01:45:27 +00003203/// Find the declaration that should be merged into, given the declaration found
3204/// by name lookup. If we're merging an anonymous declaration within a typedef,
3205/// we need a matching typedef, and we merge with the type inside it.
3206static NamedDecl *getDeclForMerging(NamedDecl *Found,
3207 bool IsTypedefNameForLinkage) {
3208 if (!IsTypedefNameForLinkage)
3209 return Found;
3210
3211 // If we found a typedef declaration that gives a name to some other
3212 // declaration, then we want that inner declaration. Declarations from
3213 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003214 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003215 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003216
3217 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003218 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003219
Hans Wennborgdcfba332015-10-06 23:40:43 +00003220 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003221}
3222
Richard Smith600adef2018-07-04 02:25:38 +00003223/// Find the declaration to use to populate the anonymous declaration table
3224/// for the given lexical DeclContext. We only care about finding local
3225/// definitions of the context; we'll merge imported ones as we go.
3226DeclContext *
3227ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
3228 // For classes, we track the definition as we merge.
3229 if (auto *RD = dyn_cast<CXXRecordDecl>(LexicalDC)) {
3230 auto *DD = RD->getCanonicalDecl()->DefinitionData;
3231 return DD ? DD->Definition : nullptr;
3232 }
3233
3234 // For anything else, walk its merged redeclarations looking for a definition.
3235 // Note that we can't just call getDefinition here because the redeclaration
3236 // chain isn't wired up.
3237 for (auto *D : merged_redecls(cast<Decl>(LexicalDC))) {
3238 if (auto *FD = dyn_cast<FunctionDecl>(D))
3239 if (FD->isThisDeclarationADefinition())
3240 return FD;
3241 if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
3242 if (MD->isThisDeclarationADefinition())
3243 return MD;
3244 }
3245
3246 // No merged definition yet.
3247 return nullptr;
3248}
3249
Richard Smithd08aeb62014-08-28 01:33:39 +00003250NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3251 DeclContext *DC,
3252 unsigned Index) {
3253 // If the lexical context has been merged, look into the now-canonical
3254 // definition.
Richard Smith600adef2018-07-04 02:25:38 +00003255 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003256
3257 // If we've seen this before, return the canonical declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003258 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003259 if (Index < Previous.size() && Previous[Index])
3260 return Previous[Index];
3261
3262 // If this is the first time, but we have parsed a declaration of the context,
3263 // build the anonymous declaration list from the parsed declaration.
Richard Smith600adef2018-07-04 02:25:38 +00003264 auto *PrimaryDC = getPrimaryDCForAnonymousDecl(DC);
3265 if (PrimaryDC && !cast<Decl>(PrimaryDC)->isFromASTFile()) {
3266 numberAnonymousDeclsWithin(PrimaryDC, [&](NamedDecl *ND, unsigned Number) {
Richard Smith2b560572015-02-07 03:11:11 +00003267 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003268 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3269 else
Richard Smith2b560572015-02-07 03:11:11 +00003270 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3271 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003272 }
3273
3274 return Index < Previous.size() ? Previous[Index] : nullptr;
3275}
3276
3277void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3278 DeclContext *DC, unsigned Index,
3279 NamedDecl *D) {
Richard Smith600adef2018-07-04 02:25:38 +00003280 auto *CanonDC = cast<Decl>(DC)->getCanonicalDecl();
Richard Smithd08aeb62014-08-28 01:33:39 +00003281
Richard Smith600adef2018-07-04 02:25:38 +00003282 auto &Previous = Reader.AnonymousDeclarationsForMerging[CanonDC];
Richard Smithd08aeb62014-08-28 01:33:39 +00003283 if (Index >= Previous.size())
3284 Previous.resize(Index + 1);
3285 if (!Previous[Index])
3286 Previous[Index] = D;
3287}
3288
Douglas Gregor022857e2011-12-22 01:48:48 +00003289ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003290 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3291 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003292
Richard Smithd08aeb62014-08-28 01:33:39 +00003293 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3294 // Don't bother trying to find unnamed declarations that are in
3295 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003296 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3297 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003298 Result.suppress();
3299 return Result;
3300 }
Richard Smith95d99302013-07-13 02:00:19 +00003301
Douglas Gregor022857e2011-12-22 01:48:48 +00003302 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003303 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003304 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003305 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003306 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3307 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003308 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3309 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003310 // Go on to check in other places in case an existing typedef name
3311 // was not imported.
3312 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003313
Richard Smith2b560572015-02-07 03:11:11 +00003314 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003315 // This is an anonymous declaration that we may need to merge. Look it up
3316 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003317 if (auto *Existing = getAnonymousDeclForMerging(
3318 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3319 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003320 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3321 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003322 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003323 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003324 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003325
3326 // Temporarily consider the identifier to be up-to-date. We don't want to
3327 // cause additional lookups here.
3328 class UpToDateIdentifierRAII {
3329 IdentifierInfo *II;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003330 bool WasOutToDate = false;
Douglas Gregor6168bd22013-02-18 15:53:43 +00003331
3332 public:
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003333 explicit UpToDateIdentifierRAII(IdentifierInfo *II) : II(II) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00003334 if (II) {
3335 WasOutToDate = II->isOutOfDate();
3336 if (WasOutToDate)
3337 II->setOutOfDate(false);
3338 }
3339 }
3340
3341 ~UpToDateIdentifierRAII() {
3342 if (WasOutToDate)
3343 II->setOutOfDate(true);
3344 }
3345 } UpToDate(Name.getAsIdentifierInfo());
3346
Fangrui Song6907ce22018-07-30 19:24:48 +00003347 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003348 IEnd = IdResolver.end();
3349 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003350 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003351 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003352 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3353 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003354 }
Richard Smith8a639892015-01-24 01:07:20 +00003355 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003356 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003357 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003358 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003359 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003360 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3361 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003362 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003363 } else {
3364 // Not in a mergeable context.
3365 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003366 }
Richard Smithd55889a2013-09-09 16:55:27 +00003367
Richard Smith2b9e3e32013-10-18 06:05:18 +00003368 // If this declaration is from a merged context, make a note that we need to
3369 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003370 //
3371 // FIXME: We should do something similar if we merge two definitions of the
3372 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003373 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3374 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3375 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003376 Reader.PendingOdrMergeChecks.push_back(D);
3377
Richard Smithd08aeb62014-08-28 01:33:39 +00003378 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003379 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003380}
3381
Richard Smithb321ecb2014-05-13 01:15:00 +00003382template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003383Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3384 return D->RedeclLink.getLatestNotUpdated();
3385}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003386
Richard Smithc3a53252015-02-28 05:57:02 +00003387Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3388 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3389}
3390
3391Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3392 assert(D);
3393
3394 switch (D->getKind()) {
3395#define ABSTRACT_DECL(TYPE)
3396#define DECL(TYPE, BASE) \
3397 case Decl::TYPE: \
3398 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3399#include "clang/AST/DeclNodes.inc"
3400 }
3401 llvm_unreachable("unknown decl kind");
3402}
3403
Richard Smith8ce51082015-03-11 01:44:51 +00003404Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3405 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3406}
3407
Richard Smithc3a53252015-02-28 05:57:02 +00003408template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003409void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3410 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003411 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003412 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003413 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003414}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003415
Richard Smith24d166c2014-07-31 23:52:38 +00003416namespace clang {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003417
Richard Smith6de7a242014-07-31 23:46:44 +00003418template<>
3419void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003420 Redeclarable<VarDecl> *D,
3421 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003422 auto *VD = static_cast<VarDecl *>(D);
3423 auto *PrevVD = cast<VarDecl>(Previous);
Richard Smithedbc6e92016-10-14 21:41:24 +00003424 D->RedeclLink.setPrevious(PrevVD);
3425 D->First = PrevVD->First;
3426
3427 // We should keep at most one definition on the chain.
3428 // FIXME: Cache the definition once we've found it. Building a chain with
3429 // N definitions currently takes O(N^2) time here.
3430 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3431 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3432 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3433 Reader.mergeDefinitionVisibility(CurD, VD);
3434 VD->demoteThisDefinitionToDeclaration();
3435 break;
3436 }
3437 }
3438 }
3439}
3440
Richard Smitha62d1982018-08-03 01:00:01 +00003441static bool isUndeducedReturnType(QualType T) {
3442 auto *DT = T->getContainedDeducedType();
3443 return DT && !DT->isDeduced();
3444}
3445
Richard Smithedbc6e92016-10-14 21:41:24 +00003446template<>
3447void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003448 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003449 Decl *Previous, Decl *Canon) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003450 auto *FD = static_cast<FunctionDecl *>(D);
3451 auto *PrevFD = cast<FunctionDecl>(Previous);
Richard Smith6de7a242014-07-31 23:46:44 +00003452
3453 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003454 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003455
3456 // If the previous declaration is an inline function declaration, then this
3457 // declaration is too.
Erich Keane9c665062018-08-01 21:02:40 +00003458 if (PrevFD->isInlined() != FD->isInlined()) {
Richard Smith6de7a242014-07-31 23:46:44 +00003459 // FIXME: [dcl.fct.spec]p4:
3460 // If a function with external linkage is declared inline in one
3461 // translation unit, it shall be declared inline in all translation
3462 // units in which it appears.
3463 //
3464 // Be careful of this case:
3465 //
3466 // module A:
3467 // template<typename T> struct X { void f(); };
3468 // template<typename T> inline void X<T>::f() {}
3469 //
3470 // module B instantiates the declaration of X<int>::f
3471 // module C instantiates the definition of X<int>::f
3472 //
3473 // If module B and C are merged, we do not have a violation of this rule.
Erich Keane9c665062018-08-01 21:02:40 +00003474 FD->setImplicitlyInline(true);
Richard Smith6de7a242014-07-31 23:46:44 +00003475 }
3476
Richard Smith6de7a242014-07-31 23:46:44 +00003477 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3478 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003479 if (FPT && PrevFPT) {
Richard Smitha62d1982018-08-03 01:00:01 +00003480 // If we need to propagate an exception specification along the redecl
3481 // chain, make a note of that so that we can do so later.
Richard Smith9e2341d2015-03-23 03:25:59 +00003482 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3483 bool WasUnresolved =
3484 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3485 if (IsUnresolved != WasUnresolved)
3486 Reader.PendingExceptionSpecUpdates.insert(
Richard Smitha62d1982018-08-03 01:00:01 +00003487 {Canon, IsUnresolved ? PrevFD : FD});
3488
3489 // If we need to propagate a deduced return type along the redecl chain,
3490 // make a note of that so that we can do it later.
3491 bool IsUndeduced = isUndeducedReturnType(FPT->getReturnType());
3492 bool WasUndeduced = isUndeducedReturnType(PrevFPT->getReturnType());
3493 if (IsUndeduced != WasUndeduced)
3494 Reader.PendingDeducedTypeUpdates.insert(
3495 {cast<FunctionDecl>(Canon),
3496 (IsUndeduced ? PrevFPT : FPT)->getReturnType()});
Richard Smith6de7a242014-07-31 23:46:44 +00003497 }
3498}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003499
3500} // namespace clang
Hans Wennborgdcfba332015-10-06 23:40:43 +00003501
Richard Smith6de7a242014-07-31 23:46:44 +00003502void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003503 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3504}
3505
Richard Smith8346e522015-06-10 01:47:58 +00003506/// Inherit the default template argument from \p From to \p To. Returns
3507/// \c false if there is no default template for \p From.
3508template <typename ParmDecl>
3509static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3510 Decl *ToD) {
3511 auto *To = cast<ParmDecl>(ToD);
3512 if (!From->hasDefaultArgument())
3513 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003514 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003515 return true;
3516}
3517
3518static void inheritDefaultTemplateArguments(ASTContext &Context,
3519 TemplateDecl *From,
3520 TemplateDecl *To) {
3521 auto *FromTP = From->getTemplateParameters();
3522 auto *ToTP = To->getTemplateParameters();
3523 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3524
3525 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
Richard Smith559cc692018-07-31 21:01:53 +00003526 NamedDecl *FromParam = FromTP->getParam(I);
3527 NamedDecl *ToParam = ToTP->getParam(I);
Richard Smith8346e522015-06-10 01:47:58 +00003528
Richard Smith559cc692018-07-31 21:01:53 +00003529 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam))
3530 inheritDefaultTemplateArgument(Context, FTTP, ToParam);
3531 else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam))
3532 inheritDefaultTemplateArgument(Context, FNTTP, ToParam);
3533 else
3534 inheritDefaultTemplateArgument(
3535 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam);
Richard Smith8346e522015-06-10 01:47:58 +00003536 }
3537}
3538
Richard Smith6de7a242014-07-31 23:46:44 +00003539void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003540 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003541 assert(D && Previous);
3542
3543 switch (D->getKind()) {
3544#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003545#define DECL(TYPE, BASE) \
3546 case Decl::TYPE: \
3547 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003548 break;
3549#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003550 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003551
3552 // If the declaration was visible in one module, a redeclaration of it in
3553 // another module remains visible even if it wouldn't be visible by itself.
3554 //
3555 // FIXME: In this case, the declaration should only be visible if a module
3556 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003557 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003558 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003559 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003560
Richard Smith8346e522015-06-10 01:47:58 +00003561 // If the declaration declares a template, it may inherit default arguments
3562 // from the previous declaration.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003563 if (auto *TD = dyn_cast<TemplateDecl>(D))
Richard Smith8346e522015-06-10 01:47:58 +00003564 inheritDefaultTemplateArguments(Reader.getContext(),
3565 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003566}
3567
Richard Smithb321ecb2014-05-13 01:15:00 +00003568template<typename DeclT>
3569void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003570 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003571}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003572
Richard Smithb321ecb2014-05-13 01:15:00 +00003573void ASTDeclReader::attachLatestDeclImpl(...) {
3574 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3575}
3576
Douglas Gregor05f10352011-12-17 23:38:30 +00003577void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3578 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003579
3580 switch (D->getKind()) {
3581#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003582#define DECL(TYPE, BASE) \
3583 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003584 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3585 break;
3586#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003587 }
3588}
3589
Richard Smith851072e2014-05-19 20:59:20 +00003590template<typename DeclT>
3591void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3592 D->RedeclLink.markIncomplete();
3593}
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003594
Richard Smith851072e2014-05-19 20:59:20 +00003595void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3596 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3597}
3598
3599void ASTReader::markIncompleteDeclChain(Decl *D) {
3600 switch (D->getKind()) {
3601#define ABSTRACT_DECL(TYPE)
3602#define DECL(TYPE, BASE) \
3603 case Decl::TYPE: \
3604 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3605 break;
3606#include "clang/AST/DeclNodes.inc"
3607 }
3608}
3609
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003610/// Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003611Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003612 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003613 SourceLocation DeclLoc;
3614 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003615 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003616 // Keep track of where we are in the stream, then jump back there
3617 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003618 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003619
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003620 ReadingKindTracker ReadingKind(Read_Decl, *this);
3621
Douglas Gregor1342e842009-07-06 18:54:52 +00003622 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003623 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003624
Sebastian Redl2c373b92010-10-05 15:59:54 +00003625 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003626 ASTRecordReader Record(*this, *Loc.F);
3627 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003628 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003629
Richard Smithdbafb6c2017-06-29 23:23:46 +00003630 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003631 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003632 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003633 case DECL_CONTEXT_LEXICAL:
3634 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003635 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003636 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003637 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003638 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003639 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003640 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003641 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003642 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003643 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003644 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003645 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003646 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003647 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003648 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003649 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003650 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003651 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003652 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003653 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003654 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003655 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003656 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003657 case DECL_EXPORT:
3658 D = ExportDecl::CreateDeserialized(Context, ID);
3659 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003660 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003661 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003662 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003663 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003664 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003665 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003666 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003667 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003668 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003669 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003670 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003671 break;
Richard Smith151c4562016-12-20 21:35:28 +00003672 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003673 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003674 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003675 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003676 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003677 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003678 case DECL_CONSTRUCTOR_USING_SHADOW:
3679 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3680 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003681 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003682 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003683 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003684 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003685 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003686 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003687 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003688 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003689 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003690 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003691 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003692 break;
Richard Smithbc491202017-02-17 20:05:37 +00003693 case DECL_CXX_DEDUCTION_GUIDE:
3694 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3695 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003696 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003697 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003698 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003699 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003700 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3701 break;
3702 case DECL_CXX_INHERITED_CONSTRUCTOR:
3703 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003704 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003705 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003706 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003707 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003708 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003709 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003710 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003711 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003712 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003713 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003714 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003715 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003716 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003717 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003718 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003719 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003720 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003721 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003722 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003723 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003724 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003725 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003726 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003727 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003728 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003729 case DECL_VAR_TEMPLATE:
3730 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3731 break;
3732 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3733 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3734 break;
3735 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3736 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3737 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003738 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003739 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003740 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003741 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003742 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003743 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003744 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003745 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003746 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003747 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003748 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003749 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003750 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003751 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3752 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003753 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003754 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003755 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003756 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003757 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3758 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003759 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003760 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003761 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003762 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003763 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003764 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003765 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003766 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003767 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003768 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003769 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003770 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003771 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003772 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003773 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003774 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003775 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003776 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003777 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003778 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003779 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003780 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003781 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003782 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003783 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003784 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003785 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003786 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003787 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003788 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003789 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003790 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003791 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003792 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003793 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003794 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003795 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003796 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003797 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003798 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003799 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003800 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003801 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003802 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003803 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003804 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003805 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003806 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003807 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003808 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003809 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003810 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003811 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003812 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003813 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003814 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003815 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003816 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003817 break;
3818 case DECL_BINDING:
3819 D = BindingDecl::CreateDeserialized(Context, ID);
3820 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003821 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003822 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003823 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003824 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003825 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003826 break;
John McCall5e77d762013-04-16 07:28:30 +00003827 case DECL_MS_PROPERTY:
3828 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3829 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003830 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003831 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003832 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003833 case DECL_CXX_BASE_SPECIFIERS:
3834 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003835 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003836 case DECL_CXX_CTOR_INITIALIZERS:
3837 Error("attempt to read a C++ ctor initializer record as a declaration");
3838 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003839 case DECL_IMPORT:
Fangrui Song6907ce22018-07-30 19:24:48 +00003840 // Note: last entry of the ImportDecl record is the number of stored source
Douglas Gregorba345522011-12-02 23:23:56 +00003841 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003842 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003843 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003844 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003845 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003846 break;
Kelvin Li1408f912018-09-26 04:28:39 +00003847 case DECL_OMP_REQUIRES:
3848 D = OMPRequiresDecl::CreateDeserialized(Context, ID, Record.readInt());
3849 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003850 case DECL_OMP_DECLARE_REDUCTION:
3851 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3852 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003853 case DECL_OMP_CAPTUREDEXPR:
3854 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003855 break;
Nico Weber66220292016-03-02 17:28:48 +00003856 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003857 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003858 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003859 case DECL_PRAGMA_DETECT_MISMATCH:
3860 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003861 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003862 break;
Michael Han84324352013-02-22 17:15:32 +00003863 case DECL_EMPTY:
3864 D = EmptyDecl::CreateDeserialized(Context, ID);
3865 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003866 case DECL_OBJC_TYPE_PARAM:
3867 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3868 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003869 }
Chris Lattner487412d2009-04-27 05:27:42 +00003870
Sebastian Redlb3298c32010-08-18 23:56:48 +00003871 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003872 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003873 // Set the DeclContext before doing any deserialization, to make sure internal
3874 // calls to Decl::getASTContext() by Decl's methods will find the
3875 // TranslationUnitDecl without crashing.
3876 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003877 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003878
3879 // If this declaration is also a declaration context, get the
3880 // offsets for its tables of lexical and visible declarations.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003881 if (auto *DC = dyn_cast<DeclContext>(D)) {
Chris Lattner487412d2009-04-27 05:27:42 +00003882 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003883 if (Offsets.first &&
3884 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3885 return nullptr;
3886 if (Offsets.second &&
3887 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3888 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003889 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003890 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003891
Douglas Gregordab42432011-08-12 00:15:20 +00003892 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003893 PendingUpdateRecords.push_back(
3894 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003895
Douglas Gregor404cdde2012-01-27 01:47:08 +00003896 // Load the categories after recursive loading is finished.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003897 if (auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003898 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3899 // we put the Decl in PendingDefinitions so we can pull the categories here.
3900 if (Class->isThisDeclarationADefinition() ||
3901 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003902 loadObjCCategories(ID, Class);
Fangrui Song6907ce22018-07-30 19:24:48 +00003903
Richard Smith13fb8602016-07-15 21:33:46 +00003904 // If we have deserialized a declaration that has a definition the
3905 // AST consumer might need to know about, queue it.
3906 // We don't pass it to the consumer immediately because we may be in recursive
3907 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003908 PotentiallyInterestingDecls.push_back(
3909 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003910
Douglas Gregordab42432011-08-12 00:15:20 +00003911 return D;
3912}
3913
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003914void ASTReader::PassInterestingDeclsToConsumer() {
3915 assert(Consumer);
3916
3917 if (PassingDeclsToConsumer)
3918 return;
3919
3920 // Guard variable to avoid recursively redoing the process of passing
3921 // decls to consumer.
3922 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3923 true);
3924
3925 // Ensure that we've loaded all potentially-interesting declarations
3926 // that need to be eagerly loaded.
3927 for (auto ID : EagerlyDeserializedDecls)
3928 GetDecl(ID);
3929 EagerlyDeserializedDecls.clear();
3930
3931 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003932 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003933 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003934 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3935 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003936 }
3937}
3938
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003939void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003940 // The declaration may have been modified by files later in the chain.
3941 // If this is the case, read the record containing the updates from each file
3942 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003943 serialization::GlobalDeclID ID = Record.ID;
3944 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003945 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003946 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003947
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00003948 SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
Vassil Vassilev7d264622017-06-30 22:40:17 +00003949
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003950 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003951 auto UpdateOffsets = std::move(UpdI->second);
3952 DeclUpdateOffsets.erase(UpdI);
3953
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003954 // Check if this decl was interesting to the consumer. If we just loaded
3955 // the declaration, then we know it was interesting and we skip the call
3956 // to isConsumerInterestedIn because it is unsafe to call in the
3957 // current ASTReader state.
3958 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003959 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003960 for (auto &FileAndOffset : UpdateOffsets) {
3961 ModuleFile *F = FileAndOffset.first;
3962 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003963 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3964 SavedStreamPosition SavedPosition(Cursor);
3965 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003966 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003967 ASTRecordReader Record(*this, *F);
3968 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003969 (void)RecCode;
3970 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003971
David L. Jonesbe1557a2016-12-21 00:17:49 +00003972 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3973 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003974 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003975
3976 // We might have made this declaration interesting. If so, remember that
3977 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003978 if (!WasInteresting &&
3979 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
3980 PotentiallyInterestingDecls.push_back(
3981 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003982 WasInteresting = true;
3983 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003984 }
3985 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00003986 // Add the lazy specializations to the template.
3987 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3988 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3989 "Must not have pending specializations");
3990 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3991 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3992 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3993 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3994 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3995 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
3996 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00003997
3998 // Load the pending visible updates for this decl context, if it has any.
3999 auto I = PendingVisibleUpdates.find(ID);
4000 if (I != PendingVisibleUpdates.end()) {
4001 auto VisibleUpdates = std::move(I->second);
4002 PendingVisibleUpdates.erase(I);
4003
4004 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004005 for (const auto &Update : VisibleUpdates)
Richard Smith1e8e91b2016-04-08 20:53:26 +00004006 Lookups[DC].Table.add(
4007 Update.Mod, Update.Data,
4008 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
4009 DC->setHasExternalVisibleStorage(true);
4010 }
Chris Lattner487412d2009-04-27 05:27:42 +00004011}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004012
Richard Smithd61d4ac2015-08-22 20:13:39 +00004013void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
4014 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00004015 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00004016 if (FirstLocal != CanonDecl) {
4017 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
4018 ASTDeclReader::attachPreviousDecl(
4019 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
4020 CanonDecl);
4021 }
4022
4023 if (!LocalOffset) {
4024 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
4025 return;
4026 }
4027
4028 // Load the list of other redeclarations from this module file.
4029 ModuleFile *M = getOwningModuleFile(FirstLocal);
4030 assert(M && "imported decl from no module file");
4031
4032 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
4033 SavedStreamPosition SavedPosition(Cursor);
4034 Cursor.JumpToBit(LocalOffset);
4035
4036 RecordData Record;
4037 unsigned Code = Cursor.ReadCode();
4038 unsigned RecCode = Cursor.readRecord(Code, Record);
4039 (void)RecCode;
4040 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
4041
4042 // FIXME: We have several different dispatches on decl kind here; maybe
4043 // we should instead generate one loop per kind and dispatch up-front?
4044 Decl *MostRecent = FirstLocal;
4045 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4046 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00004047 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
4048 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00004049 }
Richard Smithc3a53252015-02-28 05:57:02 +00004050 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00004051}
4052
4053namespace {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004054
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004055 /// Given an ObjC interface, goes through the modules and links to the
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004056 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004057 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004058 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004059 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00004060 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004061 ObjCCategoryDecl *Tail = nullptr;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004062 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004063 serialization::GlobalDeclID InterfaceID;
4064 unsigned PreviousGeneration;
Fangrui Song6907ce22018-07-30 19:24:48 +00004065
Douglas Gregor404cdde2012-01-27 01:47:08 +00004066 void add(ObjCCategoryDecl *Cat) {
4067 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00004068 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00004069 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00004070
Douglas Gregor404cdde2012-01-27 01:47:08 +00004071 // Check for duplicate categories.
4072 if (Cat->getDeclName()) {
4073 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
Fangrui Song6907ce22018-07-30 19:24:48 +00004074 if (Existing &&
4075 Reader.getOwningModuleFile(Existing)
Douglas Gregor404cdde2012-01-27 01:47:08 +00004076 != Reader.getOwningModuleFile(Cat)) {
4077 // FIXME: We should not warn for duplicates in diamond:
4078 //
4079 // MT //
4080 // / \ //
4081 // ML MR //
4082 // \ / //
4083 // MB //
4084 //
Fangrui Song6907ce22018-07-30 19:24:48 +00004085 // If there are duplicates in ML/MR, there will be warning when
4086 // creating MB *and* when importing MB. We should not warn when
Douglas Gregor404cdde2012-01-27 01:47:08 +00004087 // importing.
4088 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
4089 << Interface->getDeclName() << Cat->getDeclName();
4090 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
4091 } else if (!Existing) {
4092 // Record this category.
4093 Existing = Cat;
4094 }
4095 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004096
Douglas Gregor404cdde2012-01-27 01:47:08 +00004097 // Add this category to the end of the chain.
4098 if (Tail)
4099 ASTDeclReader::setNextObjCCategory(Tail, Cat);
4100 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004101 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004102 Tail = Cat;
4103 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004104
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004105 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00004106 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004107 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004108 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
4109 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004110 unsigned PreviousGeneration)
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004111 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
4112 InterfaceID(InterfaceID), PreviousGeneration(PreviousGeneration) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004113 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00004114 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004115 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00004116 NameCategoryMap[Cat->getDeclName()] = Cat;
Fangrui Song6907ce22018-07-30 19:24:48 +00004117
Douglas Gregor404cdde2012-01-27 01:47:08 +00004118 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00004119 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004120 }
4121 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004122
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004123 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00004124 // If we've loaded all of the category information we care about from
4125 // this module file, we're done.
4126 if (M.Generation <= PreviousGeneration)
4127 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00004128
4129 // Map global ID of the definition down to the local ID used in this
Douglas Gregor404cdde2012-01-27 01:47:08 +00004130 // module file. If there is no such mapping, we'll find nothing here
4131 // (or in any module it imports).
4132 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
4133 if (!LocalID)
4134 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004135
Douglas Gregor404cdde2012-01-27 01:47:08 +00004136 // Perform a binary search to find the local redeclarations for this
4137 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00004138 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00004139 const ObjCCategoriesInfo *Result
4140 = std::lower_bound(M.ObjCCategoriesMap,
Fangrui Song6907ce22018-07-30 19:24:48 +00004141 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00004142 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004143 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
4144 Result->DefinitionID != LocalID) {
4145 // We didn't find anything. If the class definition is in this module
4146 // file, then the module files it depends on cannot have any categories,
4147 // so suppress further lookup.
4148 return Reader.isDeclIDFromModule(InterfaceID, M);
4149 }
Fangrui Song6907ce22018-07-30 19:24:48 +00004150
Douglas Gregor404cdde2012-01-27 01:47:08 +00004151 // We found something. Dig out all of the categories.
4152 unsigned Offset = Result->Offset;
4153 unsigned N = M.ObjCCategories[Offset];
4154 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
4155 for (unsigned I = 0; I != N; ++I)
4156 add(cast_or_null<ObjCCategoryDecl>(
4157 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
4158 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004159 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004160 };
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004161
4162} // namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004163
Douglas Gregor404cdde2012-01-27 01:47:08 +00004164void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
4165 ObjCInterfaceDecl *D,
4166 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00004167 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00004168 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00004169 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004170}
Douglas Gregordab42432011-08-12 00:15:20 +00004171
Richard Smithd6db68c2014-08-07 20:58:41 +00004172template<typename DeclT, typename Fn>
4173static void forAllLaterRedecls(DeclT *D, Fn F) {
4174 F(D);
4175
4176 // Check whether we've already merged D into its redeclaration chain.
4177 // MostRecent may or may not be nullptr if D has not been merged. If
4178 // not, walk the merged redecl chain and see if it's there.
4179 auto *MostRecent = D->getMostRecentDecl();
4180 bool Found = false;
4181 for (auto *Redecl = MostRecent; Redecl && !Found;
4182 Redecl = Redecl->getPreviousDecl())
4183 Found = (Redecl == D);
4184
4185 // If this declaration is merged, apply the functor to all later decls.
4186 if (Found) {
4187 for (auto *Redecl = MostRecent; Redecl != D;
4188 Redecl = Redecl->getPreviousDecl())
4189 F(Redecl);
4190 }
4191}
4192
Vassil Vassilev7d264622017-06-30 22:40:17 +00004193void ASTDeclReader::UpdateDecl(Decl *D,
4194 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00004195 while (Record.getIdx() < Record.size()) {
4196 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004197 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00004198 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00004199 // FIXME: If we also have an update record for instantiating the
4200 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004201 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004202 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00004203 // FIXME: We should call addHiddenDecl instead, to add the member
4204 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00004205 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004206 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00004207 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004208
4209 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00004210 // It will be added to the template's lazy specialization set.
4211 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004212 break;
4213
4214 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004215 auto *Anon = ReadDeclAs<NamespaceDecl>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004216
Douglas Gregor540fd812012-01-09 18:07:24 +00004217 // Each module has its own anonymous namespace, which is disjoint from
4218 // any other module's anonymous namespaces, so don't attach the anonymous
4219 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004220 if (!Record.isModule()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004221 if (auto *TU = dyn_cast<TranslationUnitDecl>(D))
Douglas Gregor540fd812012-01-09 18:07:24 +00004222 TU->setAnonymousNamespace(Anon);
4223 else
4224 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4225 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004226 break;
4227 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004228
Richard Smith891fc7f2017-12-05 01:31:47 +00004229 case UPD_CXX_ADDED_VAR_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004230 auto *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004231 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4232 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004233 uint64_t Val = Record.readInt();
4234 if (Val && !VD->getInit()) {
4235 VD->setInit(Record.readExpr());
4236 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4237 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4238 Eval->CheckedICE = true;
4239 Eval->IsICE = Val == 3;
4240 }
4241 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004242 break;
Richard Smith05a21352017-06-22 22:18:46 +00004243 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004244
Richard Smith891fc7f2017-12-05 01:31:47 +00004245 case UPD_CXX_POINT_OF_INSTANTIATION: {
4246 SourceLocation POI = Record.readSourceLocation();
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004247 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Richard Smith891fc7f2017-12-05 01:31:47 +00004248 VTSD->setPointOfInstantiation(POI);
4249 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4250 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4251 } else {
4252 auto *FD = cast<FunctionDecl>(D);
4253 if (auto *FTSInfo = FD->TemplateOrSpecialization
4254 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4255 FTSInfo->setPointOfInstantiation(POI);
4256 else
4257 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4258 ->setPointOfInstantiation(POI);
4259 }
4260 break;
4261 }
4262
John McCall32791cc2016-01-06 22:34:54 +00004263 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004264 auto *Param = cast<ParmVarDecl>(D);
John McCall32791cc2016-01-06 22:34:54 +00004265
4266 // We have to read the default argument regardless of whether we use it
4267 // so that hypothetical further update records aren't messed up.
4268 // TODO: Add a function to skip over the next expr record.
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004269 auto *DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004270
4271 // Only apply the update if the parameter still has an uninstantiated
4272 // default argument.
4273 if (Param->hasUninstantiatedDefaultArg())
4274 Param->setDefaultArg(DefaultArg);
4275 break;
4276 }
4277
Richard Smith4b054b22016-08-24 21:25:37 +00004278 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004279 auto *FD = cast<FieldDecl>(D);
4280 auto *DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004281
4282 // Only apply the update if the field still has an uninstantiated
4283 // default member initializer.
4284 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4285 if (DefaultInit)
4286 FD->setInClassInitializer(DefaultInit);
4287 else
4288 // Instantiation failed. We can get here if we serialized an AST for
4289 // an invalid program.
4290 FD->removeInClassInitializer();
4291 }
4292 break;
4293 }
4294
Richard Smith4d235792014-08-07 18:53:08 +00004295 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004296 auto *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004297 if (Reader.PendingBodies[FD]) {
4298 // FIXME: Maybe check for ODR violations.
4299 // It's safe to stop now because this update record is always last.
4300 return;
4301 }
4302
David L. Jonesbe1557a2016-12-21 00:17:49 +00004303 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004304 // Maintain AST consistency: any later redeclarations of this function
4305 // are inline if this one is. (We might have merged another declaration
4306 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004307 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4308 FD->setImplicitlyInline();
4309 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004310 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004311 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004312 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004313 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004314 break;
4315 }
4316
Richard Smithcd45dbc2014-04-19 03:48:30 +00004317 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4318 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004319 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004320 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004321 OldDD && (OldDD->Definition != RD ||
4322 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004323 RD->setParamDestroyedInCallee(Record.readInt());
Akira Hatanakae6313ac2018-04-09 22:48:22 +00004324 RD->setArgPassingRestrictions(
4325 (RecordDecl::ArgPassingKind)Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004326 ReadCXXRecordDefinition(RD, /*Update*/true);
4327
Richard Smithcd45dbc2014-04-19 03:48:30 +00004328 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004329 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004330 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004331 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004332 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004333 }
4334
David L. Jonesbe1557a2016-12-21 00:17:49 +00004335 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004336 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004337 if (MemberSpecializationInfo *MSInfo =
4338 RD->getMemberSpecializationInfo()) {
4339 MSInfo->setTemplateSpecializationKind(TSK);
4340 MSInfo->setPointOfInstantiation(POI);
4341 } else {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004342 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004343 Spec->setTemplateSpecializationKind(TSK);
4344 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004345
David L. Jonesbe1557a2016-12-21 00:17:49 +00004346 if (Record.readInt()) {
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004347 auto *PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004348 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004349 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004350 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004351 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004352 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004353
4354 // FIXME: If we already have a partial specialization set,
4355 // check that it matches.
4356 if (!Spec->getSpecializedTemplateOrPartial()
4357 .is<ClassTemplatePartialSpecializationDecl *>())
4358 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004359 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004360 }
4361
David L. Jonesbe1557a2016-12-21 00:17:49 +00004362 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004363 RD->setLocation(ReadSourceLocation());
4364 RD->setLocStart(ReadSourceLocation());
4365 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004366
David L. Jonesbe1557a2016-12-21 00:17:49 +00004367 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004368 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004369 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004370 // If the declaration already has attributes, we assume that some other
4371 // AST file already loaded them.
4372 if (!D->hasAttrs())
4373 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004374 }
4375 break;
4376 }
4377
Richard Smithf8134002015-03-10 01:41:22 +00004378 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4379 // Set the 'operator delete' directly to avoid emitting another update
4380 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004381 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004382 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004383 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004384 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004385 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004386 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004387 First->OperatorDeleteThisArg = ThisArg;
4388 }
Richard Smithf8134002015-03-10 01:41:22 +00004389 break;
4390 }
4391
Richard Smith564417a2014-03-20 21:47:22 +00004392 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004393 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004394 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004395 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004396
4397 // Update this declaration's exception specification, if needed.
4398 auto *FD = cast<FunctionDecl>(D);
4399 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4400 // FIXME: If the exception specification is already present, check that it
4401 // matches.
4402 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004403 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004404 FPT->getReturnType(), FPT->getParamTypes(),
4405 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004406
4407 // When we get to the end of deserializing, see if there are other decls
4408 // that we need to propagate this exception specification onto.
4409 Reader.PendingExceptionSpecUpdates.insert(
4410 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004411 }
Richard Smith564417a2014-03-20 21:47:22 +00004412 break;
4413 }
4414
Richard Smith1fa5d642013-05-11 05:45:24 +00004415 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smitha62d1982018-08-03 01:00:01 +00004416 auto *FD = cast<FunctionDecl>(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004417 QualType DeducedResultType = Record.readType();
Richard Smitha62d1982018-08-03 01:00:01 +00004418 Reader.PendingDeducedTypeUpdates.insert(
4419 {FD->getCanonicalDecl(), DeducedResultType});
Richard Smith1fa5d642013-05-11 05:45:24 +00004420 break;
4421 }
Eli Friedman276dd182013-09-05 00:02:25 +00004422
Eugene Zelenkoe69b33f2018-04-11 20:57:28 +00004423 case UPD_DECL_MARKED_USED:
Richard Smith675d2792014-06-16 20:26:19 +00004424 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004425 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004426 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004427
4428 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004429 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4430 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004431 break;
4432
4433 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004434 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4435 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004436 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004437
Alexey Bataev97720002014-11-11 04:05:39 +00004438 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004439 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4440 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004441 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004442
Alex Denisovfde64952015-06-26 05:28:36 +00004443 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004444 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004445 auto *Exported = cast<NamedDecl>(D);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004446 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith13897eb2018-09-12 23:37:00 +00004447 Reader.getContext().mergeDefinitionIntoModule(Exported, Owner);
4448 Reader.PendingMergedDefinitionsToDeduplicate.insert(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004449 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004450 }
Alex Denisovfde64952015-06-26 05:28:36 +00004451
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004452 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alexey Bataevd01b7492018-08-15 19:45:12 +00004453 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(
4454 Reader.getContext(),
4455 static_cast<OMPDeclareTargetDeclAttr::MapTypeTy>(Record.readInt()),
4456 ReadSourceRange()));
4457 break;
4458
Alex Denisovfde64952015-06-26 05:28:36 +00004459 case UPD_ADDED_ATTR_TO_RECORD:
4460 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004461 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004462 assert(Attrs.size() == 1);
4463 D->addAttr(Attrs[0]);
4464 break;
4465 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004466 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004467}