blob: fa0db51e7f312e0e33c3445e88832505880021b8 [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===//
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"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclGroup.h"
20#include "clang/AST/DeclTemplate.h"
21#include "clang/AST/DeclVisitor.h"
22#include "clang/AST/Expr.h"
Douglas Gregor022857e2011-12-22 01:48:48 +000023#include "clang/Sema/IdentifierResolver.h"
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +000024#include "clang/Sema/SemaDiagnostic.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000025#include "clang/Serialization/ASTReader.h"
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +000026#include "llvm/Support/SaveAndRestore.h"
Hans Wennborgdcfba332015-10-06 23:40:43 +000027
Chris Lattner487412d2009-04-27 05:27:42 +000028using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000029using namespace clang::serialization;
Chris Lattner487412d2009-04-27 05:27:42 +000030
31//===----------------------------------------------------------------------===//
32// Declaration deserialization
33//===----------------------------------------------------------------------===//
34
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000035namespace clang {
Sebastian Redlb3298c32010-08-18 23:56:48 +000036 class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
Sebastian Redl2c499f62010-08-18 23:56:43 +000037 ASTReader &Reader;
David L. Jonesbe1557a2016-12-21 00:17:49 +000038 ASTRecordReader &Record;
David L. Jonesc4808b9e2016-12-15 20:53:26 +000039 ASTReader::RecordLocation Loc;
Sebastian Redl539c5062010-08-18 23:57:32 +000040 const DeclID ThisDeclID;
Richard Smithcb34bd32016-03-27 07:28:06 +000041 const SourceLocation ThisDeclLoc;
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +000042 typedef ASTReader::RecordData RecordData;
Sebastian Redl539c5062010-08-18 23:57:32 +000043 TypeID TypeIDForTypeDecl;
Richard Smithd08aeb62014-08-28 01:33:39 +000044 unsigned AnonymousDeclNumber;
Richard Smith70d58502014-08-30 00:04:23 +000045 GlobalDeclID NamedDeclForTagDecl;
46 IdentifierInfo *TypedefNameForLinkage;
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000047
Daniel Jasper4a6d5b72017-10-11 07:47:54 +000048 bool HasPendingBody;
49
Vassil Vassilev928c8252016-04-28 14:13:28 +000050 ///\brief A flag to carry the information for a decl from the entity is
51 /// used. We use it to delay the marking of the canonical decl as used until
52 /// the entire declaration is deserialized and merged.
53 bool IsDeclMarkedUsed;
54
Sebastian Redlc67764e2010-07-22 22:43:28 +000055 uint64_t GetCurrentCursorOffset();
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000056
David L. Jonesc4808b9e2016-12-15 20:53:26 +000057 uint64_t ReadLocalOffset() {
David L. Jonesbe1557a2016-12-21 00:17:49 +000058 uint64_t LocalOffset = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +000059 assert(LocalOffset < Loc.Offset && "offset point after current record");
60 return LocalOffset ? Loc.Offset - LocalOffset : 0;
Richard Smithe37e9f42016-03-25 01:17:43 +000061 }
62
David L. Jonesc4808b9e2016-12-15 20:53:26 +000063 uint64_t ReadGlobalOffset() {
64 uint64_t Local = ReadLocalOffset();
65 return Local ? Record.getGlobalBitOffset(Local) : 0;
Richard Smithaa165cf2016-04-13 21:57:08 +000066 }
67
David L. Jonesc4808b9e2016-12-15 20:53:26 +000068 SourceLocation ReadSourceLocation() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000069 return Record.readSourceLocation();
Sebastian Redl2c373b92010-10-05 15:59:54 +000070 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000071
David L. Jonesc4808b9e2016-12-15 20:53:26 +000072 SourceRange ReadSourceRange() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000073 return Record.readSourceRange();
Sebastian Redl2c373b92010-10-05 15:59:54 +000074 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000075
David L. Jonesc4808b9e2016-12-15 20:53:26 +000076 TypeSourceInfo *GetTypeSourceInfo() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000077 return Record.getTypeSourceInfo();
Sebastian Redl2c373b92010-10-05 15:59:54 +000078 }
Vassil Vassilev6565dfc2016-02-26 10:43:34 +000079
David L. Jonesc4808b9e2016-12-15 20:53:26 +000080 serialization::DeclID ReadDeclID() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000081 return Record.readDeclID();
Douglas Gregor7fb09192011-07-21 22:35:25 +000082 }
Richard Smith0b884372015-02-27 00:25:58 +000083
David L. Jonesc4808b9e2016-12-15 20:53:26 +000084 std::string ReadString() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000085 return Record.readString();
Nico Weber66220292016-03-02 17:28:48 +000086 }
87
Richard Smith0b884372015-02-27 00:25:58 +000088 void ReadDeclIDList(SmallVectorImpl<DeclID> &IDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +000089 for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +000090 IDs.push_back(ReadDeclID());
Richard Smith0b884372015-02-27 00:25:58 +000091 }
92
David L. Jonesc4808b9e2016-12-15 20:53:26 +000093 Decl *ReadDecl() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000094 return Record.readDecl();
Douglas Gregor7fb09192011-07-21 22:35:25 +000095 }
96
97 template<typename T>
David L. Jonesc4808b9e2016-12-15 20:53:26 +000098 T *ReadDeclAs() {
David L. Jonesb6a8f022016-12-21 04:34:52 +000099 return Record.readDeclAs<T>();
Douglas Gregor7fb09192011-07-21 22:35:25 +0000100 }
101
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000102 void ReadQualifierInfo(QualifierInfo &Info) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000103 Record.readQualifierInfo(Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000104 }
Sebastian Redlc67764e2010-07-22 22:43:28 +0000105
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000106 void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name) {
David L. Jonesb6a8f022016-12-21 04:34:52 +0000107 Record.readDeclarationNameLoc(DNLoc, Name);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000108 }
109
110 serialization::SubmoduleID readSubmoduleID() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000111 if (Record.getIdx() == Record.size())
Douglas Gregorcf68c582011-12-01 22:20:10 +0000112 return 0;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000113
David L. Jonesbe1557a2016-12-21 00:17:49 +0000114 return Record.getGlobalSubmoduleID(Record.readInt());
Douglas Gregorcf68c582011-12-01 22:20:10 +0000115 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000116
117 Module *readModule() {
118 return Record.getSubmodule(readSubmoduleID());
Douglas Gregorba345522011-12-02 23:23:56 +0000119 }
Richard Smithcd45dbc2014-04-19 03:48:30 +0000120
Richard Smith2a9e5c52015-02-03 03:32:14 +0000121 void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update);
David Blaikie1ac9c982017-04-11 21:13:37 +0000122 void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
123 const CXXRecordDecl *D);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000124 void MergeDefinitionData(CXXRecordDecl *D,
Richard Smith8a639892015-01-24 01:07:20 +0000125 struct CXXRecordDecl::DefinitionData &&NewDD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000126 void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
Manman Renec315f12016-09-09 23:48:27 +0000127 void MergeDefinitionData(ObjCInterfaceDecl *D,
128 struct ObjCInterfaceDecl::DefinitionData &&NewDD);
Graydon Hoaree0a68352017-06-28 18:36:27 +0000129 void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
130 void MergeDefinitionData(ObjCProtocolDecl *D,
131 struct ObjCProtocolDecl::DefinitionData &&NewDD);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000132
Richard Smithd08aeb62014-08-28 01:33:39 +0000133 static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
134 DeclContext *DC,
135 unsigned Index);
136 static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
137 unsigned Index, NamedDecl *D);
138
Richard Smith0144f512015-08-22 02:09:38 +0000139 /// Results from loading a RedeclarableDecl.
Douglas Gregor022857e2011-12-22 01:48:48 +0000140 class RedeclarableResult {
Richard Smith5a4737c2015-02-06 02:42:59 +0000141 Decl *MergeWith;
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000142 GlobalDeclID FirstID;
Richard Smith5fc18a92015-07-12 23:43:21 +0000143 bool IsKeyDecl;
Richard Smithc89bb9d2015-02-25 01:11:29 +0000144
Douglas Gregor022857e2011-12-22 01:48:48 +0000145 public:
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +0000146 RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
147 : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
Richard Smith9b88a4c2015-07-27 05:40:23 +0000148
Douglas Gregor022857e2011-12-22 01:48:48 +0000149 /// \brief Retrieve the first ID.
150 GlobalDeclID getFirstID() const { return FirstID; }
Richard Smith5a4737c2015-02-06 02:42:59 +0000151
Richard Smith0144f512015-08-22 02:09:38 +0000152 /// \brief Is this declaration a key declaration?
Richard Smith5fc18a92015-07-12 23:43:21 +0000153 bool isKeyDecl() const { return IsKeyDecl; }
154
Richard Smith5a4737c2015-02-06 02:42:59 +0000155 /// \brief Get a known declaration that this should be merged with, if
156 /// any.
157 Decl *getKnownMergeTarget() const { return MergeWith; }
Douglas Gregor022857e2011-12-22 01:48:48 +0000158 };
Douglas Gregorfe732d52013-01-21 16:16:40 +0000159
Douglas Gregor022857e2011-12-22 01:48:48 +0000160 /// \brief Class used to capture the result of searching for an existing
161 /// declaration of a specific kind and name, along with the ability
162 /// to update the place where this result was found (the declaration
163 /// chain hanging off an identifier or the DeclContext we searched in)
164 /// if requested.
165 class FindExistingResult {
166 ASTReader &Reader;
167 NamedDecl *New;
168 NamedDecl *Existing;
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000169 bool AddResult;
Richard Smith70d58502014-08-30 00:04:23 +0000170
Richard Smithd08aeb62014-08-28 01:33:39 +0000171 unsigned AnonymousDeclNumber;
Richard Smith70d58502014-08-30 00:04:23 +0000172 IdentifierInfo *TypedefNameForLinkage;
Richard Smithd08aeb62014-08-28 01:33:39 +0000173
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000174 void operator=(FindExistingResult &&) = delete;
Richard Smithd08aeb62014-08-28 01:33:39 +0000175
Douglas Gregor022857e2011-12-22 01:48:48 +0000176 public:
177 FindExistingResult(ASTReader &Reader)
Richard Smithd08aeb62014-08-28 01:33:39 +0000178 : Reader(Reader), New(nullptr), Existing(nullptr), AddResult(false),
Hans Wennborgdcfba332015-10-06 23:40:43 +0000179 AnonymousDeclNumber(0), TypedefNameForLinkage(nullptr) {}
Craig Toppera13603a2014-05-22 05:54:18 +0000180
Richard Smithd08aeb62014-08-28 01:33:39 +0000181 FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
Richard Smith70d58502014-08-30 00:04:23 +0000182 unsigned AnonymousDeclNumber,
183 IdentifierInfo *TypedefNameForLinkage)
Richard Smithd08aeb62014-08-28 01:33:39 +0000184 : Reader(Reader), New(New), Existing(Existing), AddResult(true),
Richard Smith70d58502014-08-30 00:04:23 +0000185 AnonymousDeclNumber(AnonymousDeclNumber),
186 TypedefNameForLinkage(TypedefNameForLinkage) {}
Richard Smithd08aeb62014-08-28 01:33:39 +0000187
Benjamin Kramerb6aadc12016-08-06 12:45:16 +0000188 FindExistingResult(FindExistingResult &&Other)
Richard Smithd08aeb62014-08-28 01:33:39 +0000189 : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
190 AddResult(Other.AddResult),
Richard Smith70d58502014-08-30 00:04:23 +0000191 AnonymousDeclNumber(Other.AnonymousDeclNumber),
192 TypedefNameForLinkage(Other.TypedefNameForLinkage) {
Douglas Gregor022857e2011-12-22 01:48:48 +0000193 Other.AddResult = false;
194 }
Richard Smithd08aeb62014-08-28 01:33:39 +0000195
Douglas Gregor022857e2011-12-22 01:48:48 +0000196 ~FindExistingResult();
Richard Smithd08aeb62014-08-28 01:33:39 +0000197
Douglas Gregor2009cee2012-01-03 22:46:00 +0000198 /// \brief Suppress the addition of this result into the known set of
199 /// names.
200 void suppress() { AddResult = false; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000201
Douglas Gregor022857e2011-12-22 01:48:48 +0000202 operator NamedDecl*() const { return Existing; }
Richard Smithd08aeb62014-08-28 01:33:39 +0000203
Douglas Gregor022857e2011-12-22 01:48:48 +0000204 template<typename T>
205 operator T*() const { return dyn_cast_or_null<T>(Existing); }
206 };
Richard Smithd08aeb62014-08-28 01:33:39 +0000207
Richard Smith8a639892015-01-24 01:07:20 +0000208 static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
209 DeclContext *DC);
Douglas Gregor022857e2011-12-22 01:48:48 +0000210 FindExistingResult findExisting(NamedDecl *D);
Richard Smithd08aeb62014-08-28 01:33:39 +0000211
Chris Lattner487412d2009-04-27 05:27:42 +0000212 public:
David L. Jonesbe1557a2016-12-21 00:17:49 +0000213 ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
214 ASTReader::RecordLocation Loc,
215 DeclID thisDeclID, SourceLocation ThisDeclLoc)
216 : Reader(Reader), Record(Record), Loc(Loc),
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000217 ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc),
David L. Jonesbe1557a2016-12-21 00:17:49 +0000218 TypeIDForTypeDecl(0), NamedDeclForTagDecl(0),
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000219 TypedefNameForLinkage(nullptr), HasPendingBody(false),
220 IsDeclMarkedUsed(false) {}
Chris Lattner487412d2009-04-27 05:27:42 +0000221
Vassil Vassilev7d264622017-06-30 22:40:17 +0000222 template <typename T> static
223 void AddLazySpecializations(T *D,
224 SmallVectorImpl<serialization::DeclID>& IDs) {
225 if (IDs.empty())
226 return;
227
228 // FIXME: We should avoid this pattern of getting the ASTContext.
229 ASTContext &C = D->getASTContext();
230
231 auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
232
233 if (auto &Old = LazySpecializations) {
234 IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]);
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +0000235 llvm::sort(IDs.begin(), IDs.end());
Vassil Vassilev7d264622017-06-30 22:40:17 +0000236 IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
237 }
238
239 auto *Result = new (C) serialization::DeclID[1 + IDs.size()];
240 *Result = IDs.size();
241 std::copy(IDs.begin(), IDs.end(), Result + 1);
242
243 LazySpecializations = Result;
244 }
245
Richard Smithb321ecb2014-05-13 01:15:00 +0000246 template <typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +0000247 static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
248 static Decl *getMostRecentDeclImpl(...);
249 static Decl *getMostRecentDecl(Decl *D);
250
251 template <typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +0000252 static void attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith9e2341d2015-03-23 03:25:59 +0000253 Redeclarable<DeclT> *D, Decl *Previous,
254 Decl *Canon);
Richard Smith6de7a242014-07-31 23:46:44 +0000255 static void attachPreviousDeclImpl(ASTReader &Reader, ...);
Richard Smith9e2341d2015-03-23 03:25:59 +0000256 static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
257 Decl *Canon);
Richard Smithb321ecb2014-05-13 01:15:00 +0000258
259 template <typename DeclT>
260 static void attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest);
261 static void attachLatestDeclImpl(...);
Douglas Gregor05f10352011-12-17 23:38:30 +0000262 static void attachLatestDecl(Decl *D, Decl *latest);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +0000263
Richard Smith851072e2014-05-19 20:59:20 +0000264 template <typename DeclT>
265 static void markIncompleteDeclChainImpl(Redeclarable<DeclT> *D);
266 static void markIncompleteDeclChainImpl(...);
267
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000268 /// \brief Determine whether this declaration has a pending body.
269 bool hasPendingBody() const { return HasPendingBody; }
270
David Blaikieac4345c2017-02-12 18:45:31 +0000271 void ReadFunctionDefinition(FunctionDecl *FD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000272 void Visit(Decl *D);
273
Vassil Vassilev7d264622017-06-30 22:40:17 +0000274 void UpdateDecl(Decl *D, llvm::SmallVectorImpl<serialization::DeclID>&);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000275
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +0000276 static void setNextObjCCategory(ObjCCategoryDecl *Cat,
277 ObjCCategoryDecl *Next) {
278 Cat->NextClassCategory = Next;
279 }
280
Chris Lattner487412d2009-04-27 05:27:42 +0000281 void VisitDecl(Decl *D);
Nico Weber66220292016-03-02 17:28:48 +0000282 void VisitPragmaCommentDecl(PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000283 void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000284 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
285 void VisitNamedDecl(NamedDecl *ND);
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000286 void VisitLabelDecl(LabelDecl *LD);
Douglas Gregore31bbd92010-02-21 18:22:14 +0000287 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000288 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
289 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000290 void VisitTypeDecl(TypeDecl *TD);
Richard Smith43ccec8e2014-08-26 03:52:16 +0000291 RedeclarableResult VisitTypedefNameDecl(TypedefNameDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000292 void VisitTypedefDecl(TypedefDecl *TD);
Richard Smithdda56e42011-04-15 14:24:37 +0000293 void VisitTypeAliasDecl(TypeAliasDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000294 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000295 RedeclarableResult VisitTagDecl(TagDecl *TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000296 void VisitEnumDecl(EnumDecl *ED);
Richard Smith1d209d02013-05-23 01:49:11 +0000297 RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
298 void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
299 RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
300 void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
301 RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000302 ClassTemplateSpecializationDecl *D);
Richard Smith1d209d02013-05-23 01:49:11 +0000303 void VisitClassTemplateSpecializationDecl(
304 ClassTemplateSpecializationDecl *D) {
305 VisitClassTemplateSpecializationDeclImpl(D);
306 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000307 void VisitClassTemplatePartialSpecializationDecl(
308 ClassTemplatePartialSpecializationDecl *D);
Sebastian Redlb7448632011-08-31 13:59:56 +0000309 void VisitClassScopeFunctionSpecializationDecl(
310 ClassScopeFunctionSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000311 RedeclarableResult
312 VisitVarTemplateSpecializationDeclImpl(VarTemplateSpecializationDecl *D);
313 void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
314 VisitVarTemplateSpecializationDeclImpl(D);
315 }
316 void VisitVarTemplatePartialSpecializationDecl(
317 VarTemplatePartialSpecializationDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000318 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000319 void VisitValueDecl(ValueDecl *VD);
320 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000321 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000322 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +0000323 void VisitFunctionDecl(FunctionDecl *FD);
Richard Smithbc491202017-02-17 20:05:37 +0000324 void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *GD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000325 void VisitCXXMethodDecl(CXXMethodDecl *D);
326 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
327 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
328 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000329 void VisitFieldDecl(FieldDecl *FD);
John McCall5e77d762013-04-16 07:28:30 +0000330 void VisitMSPropertyDecl(MSPropertyDecl *FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000331 void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000332 RedeclarableResult VisitVarDeclImpl(VarDecl *D);
333 void VisitVarDecl(VarDecl *VD) { VisitVarDeclImpl(VD); }
Chris Lattner487412d2009-04-27 05:27:42 +0000334 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
335 void VisitParmVarDecl(ParmVarDecl *PD);
Richard Smith7b76d812016-08-12 02:21:25 +0000336 void VisitDecompositionDecl(DecompositionDecl *DD);
337 void VisitBindingDecl(BindingDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000338 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000339 DeclID VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor54079202012-01-06 22:05:37 +0000340 RedeclarableResult VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000341 void VisitClassTemplateDecl(ClassTemplateDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000342 void VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000343 void VisitVarTemplateDecl(VarTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000344 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000345 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000346 void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000347 void VisitUsingDecl(UsingDecl *D);
Richard Smith151c4562016-12-20 21:35:28 +0000348 void VisitUsingPackDecl(UsingPackDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000349 void VisitUsingShadowDecl(UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000350 void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000351 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith8df390f2016-09-08 23:14:54 +0000352 void VisitExportDecl(ExportDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000353 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Douglas Gregorba345522011-12-02 23:23:56 +0000354 void VisitImportDecl(ImportDecl *D);
Abramo Bagnarad7340582010-06-05 05:09:32 +0000355 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000356 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000357 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
358 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000359 void VisitBlockDecl(BlockDecl *BD);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000360 void VisitCapturedDecl(CapturedDecl *CD);
Michael Han84324352013-02-22 17:15:32 +0000361 void VisitEmptyDecl(EmptyDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000362
Chris Lattner487412d2009-04-27 05:27:42 +0000363 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000364
365 template<typename T>
Douglas Gregor022857e2011-12-22 01:48:48 +0000366 RedeclarableResult VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +0000367
Douglas Gregor2c46b5b2012-01-03 17:27:13 +0000368 template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +0000369 void mergeRedeclarable(Redeclarable<T> *D, RedeclarableResult &Redecl,
370 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000371
372 template<typename T>
373 void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
Richard Smithf17fdbd2014-04-24 02:25:27 +0000374 RedeclarableResult &Redecl,
375 DeclID TemplatePatternID = 0);
Richard Smithd55889a2013-09-09 16:55:27 +0000376
Richard Smith0b87e072013-10-07 08:02:11 +0000377 template<typename T>
378 void mergeMergeable(Mergeable<T> *D);
379
Richard Smithf17fdbd2014-04-24 02:25:27 +0000380 void mergeTemplatePattern(RedeclarableTemplateDecl *D,
381 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +0000382 DeclID DsID, bool IsKeyDecl);
Richard Smithf17fdbd2014-04-24 02:25:27 +0000383
Douglas Gregor85f3f952015-07-07 03:57:15 +0000384 ObjCTypeParamList *ReadObjCTypeParamList();
385
Alexis Hunted053252010-05-30 07:21:58 +0000386 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000387 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000388 void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000389 void VisitObjCContainerDecl(ObjCContainerDecl *D);
390 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
391 void VisitObjCIvarDecl(ObjCIvarDecl *D);
392 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
393 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000394 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
395 void VisitObjCImplDecl(ObjCImplDecl *D);
396 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
397 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
398 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
399 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
400 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Alexey Bataeva769e072013-03-22 06:34:35 +0000401 void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000402 void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
Alexey Bataev4244be22016-02-11 05:35:55 +0000403 void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +0000404 };
Hans Wennborgdcfba332015-10-06 23:40:43 +0000405} // end namespace clang
Chris Lattner487412d2009-04-27 05:27:42 +0000406
Richard Smith86dfc1e2015-06-18 22:07:00 +0000407namespace {
408/// Iterator over the redeclarations of a declaration that have already
409/// been merged into the same redeclaration chain.
410template<typename DeclT>
411class MergedRedeclIterator {
412 DeclT *Start, *Canonical, *Current;
413public:
414 MergedRedeclIterator() : Current(nullptr) {}
415 MergedRedeclIterator(DeclT *Start)
416 : Start(Start), Canonical(nullptr), Current(Start) {}
417
418 DeclT *operator*() { return Current; }
419
420 MergedRedeclIterator &operator++() {
421 if (Current->isFirstDecl()) {
422 Canonical = Current;
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000423 Current = Current->getMostRecentDecl();
Richard Smith86dfc1e2015-06-18 22:07:00 +0000424 } else
425 Current = Current->getPreviousDecl();
426
427 // If we started in the merged portion, we'll reach our start position
428 // eventually. Otherwise, we'll never reach it, but the second declaration
429 // we reached was the canonical declaration, so stop when we see that one
430 // again.
431 if (Current == Start || Current == Canonical)
432 Current = nullptr;
433 return *this;
434 }
435
436 friend bool operator!=(const MergedRedeclIterator &A,
437 const MergedRedeclIterator &B) {
438 return A.Current != B.Current;
439 }
440};
Hans Wennborgdcfba332015-10-06 23:40:43 +0000441} // end anonymous namespace
442
Benjamin Kramera0a13c32016-08-06 11:21:04 +0000443template <typename DeclT>
444static llvm::iterator_range<MergedRedeclIterator<DeclT>>
445merged_redecls(DeclT *D) {
Craig Topper59c2ada2015-12-06 05:07:12 +0000446 return llvm::make_range(MergedRedeclIterator<DeclT>(D),
447 MergedRedeclIterator<DeclT>());
Richard Smith86dfc1e2015-06-18 22:07:00 +0000448}
449
Sebastian Redlb3298c32010-08-18 23:56:48 +0000450uint64_t ASTDeclReader::GetCurrentCursorOffset() {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000451 return Loc.F->DeclsCursor.GetCurrentBitNo() + Loc.F->GlobalBitOffset;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000452}
453
David Blaikieac4345c2017-02-12 18:45:31 +0000454void ASTDeclReader::ReadFunctionDefinition(FunctionDecl *FD) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000455 if (Record.readInt())
Richard Smitha4653622017-09-06 20:01:14 +0000456 Reader.DefinitionSource[FD] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikieac4345c2017-02-12 18:45:31 +0000457 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
458 CD->NumCtorInitializers = Record.readInt();
459 if (CD->NumCtorInitializers)
460 CD->CtorInitializers = ReadGlobalOffset();
461 }
462 // Store the offset of the body so we can lazily load it later.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000463 Reader.PendingBodies[FD] = GetCurrentCursorOffset();
464 HasPendingBody = true;
David Blaikieac4345c2017-02-12 18:45:31 +0000465}
466
Sebastian Redlb3298c32010-08-18 23:56:48 +0000467void ASTDeclReader::Visit(Decl *D) {
468 DeclVisitor<ASTDeclReader, void>::Visit(D);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000469
Vassil Vassilev928c8252016-04-28 14:13:28 +0000470 // At this point we have deserialized and merged the decl and it is safe to
471 // update its canonical decl to signal that the entire entity is used.
472 D->getCanonicalDecl()->Used |= IsDeclMarkedUsed;
473 IsDeclMarkedUsed = false;
474
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000475 if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
476 if (DD->DeclInfo) {
477 DeclaratorDecl::ExtInfo *Info =
478 DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000479 Info->TInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000480 }
481 else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000482 DD->DeclInfo = GetTypeSourceInfo();
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000483 }
484 }
485
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000486 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
Richard Smithf17fdbd2014-04-24 02:25:27 +0000487 // We have a fully initialized TypeDecl. Read its type now.
Douglas Gregor0cdc8322010-12-10 17:03:06 +0000488 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
Richard Smith70d58502014-08-30 00:04:23 +0000489
490 // If this is a tag declaration with a typedef name for linkage, it's safe
491 // to load that typedef now.
492 if (NamedDeclForTagDecl)
David Majnemer00350522015-08-31 18:48:39 +0000493 cast<TagDecl>(D)->TypedefNameDeclOrQualifier =
494 cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl));
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000495 } else if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
496 // if we have a fully initialized TypeDecl, we can safely read its type now.
497 ID->TypeForDecl = Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull();
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000498 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
499 // FunctionDecl's body was written last after all other Stmts/Exprs.
Douglas Gregor559458c2012-10-03 18:34:48 +0000500 // We only read it if FD doesn't already have a body (e.g., from another
501 // module).
Douglas Gregore4a838a2012-10-03 18:36:10 +0000502 // FIXME: Can we diagnose ODR violations somehow?
David Blaikieac4345c2017-02-12 18:45:31 +0000503 if (Record.readInt())
504 ReadFunctionDefinition(FD);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000505 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000506}
507
Sebastian Redlb3298c32010-08-18 23:56:48 +0000508void ASTDeclReader::VisitDecl(Decl *D) {
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000509 if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
510 isa<ParmVarDecl>(D)) {
Douglas Gregor250ffb12011-03-05 01:35:54 +0000511 // We don't want to deserialize the DeclContext of a template
Dmitri Gribenkob353ee12013-12-19 02:05:20 +0000512 // parameter or of a parameter of a function template immediately. These
513 // entities might be used in the formulation of its DeclContext (for
514 // example, a function parameter can be used in decltype() in trailing
515 // return type of the function). Use the translation unit DeclContext as a
516 // placeholder.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000517 GlobalDeclID SemaDCIDForTemplateParmDecl = ReadDeclID();
518 GlobalDeclID LexicalDCIDForTemplateParmDecl = ReadDeclID();
Richard Smith8aed4222015-12-11 22:41:00 +0000519 if (!LexicalDCIDForTemplateParmDecl)
520 LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl;
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +0000521 Reader.addPendingDeclContextInfo(D,
522 SemaDCIDForTemplateParmDecl,
523 LexicalDCIDForTemplateParmDecl);
Douglas Gregor4163aca2011-09-09 21:34:22 +0000524 D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000525 } else {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000526 DeclContext *SemaDC = ReadDeclAs<DeclContext>();
527 DeclContext *LexicalDC = ReadDeclAs<DeclContext>();
Richard Smith8aed4222015-12-11 22:41:00 +0000528 if (!LexicalDC)
529 LexicalDC = SemaDC;
Richard Smithd55889a2013-09-09 16:55:27 +0000530 DeclContext *MergedSemaDC = Reader.MergedDeclContexts.lookup(SemaDC);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000531 // Avoid calling setLexicalDeclContext() directly because it uses
532 // Decl::getASTContext() internally which is unsafe during derialization.
Richard Smithd55889a2013-09-09 16:55:27 +0000533 D->setDeclContextsImpl(MergedSemaDC ? MergedSemaDC : SemaDC, LexicalDC,
534 Reader.getContext());
Douglas Gregor250ffb12011-03-05 01:35:54 +0000535 }
Richard Smithcb34bd32016-03-27 07:28:06 +0000536 D->setLocation(ThisDeclLoc);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000537 D->setInvalidDecl(Record.readInt());
538 if (Record.readInt()) { // hasAttrs
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000539 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000540 Record.readAttributes(Attrs);
Argyrios Kyrtzidis7456ac42012-02-09 07:46:54 +0000541 // Avoid calling setAttrs() directly because it uses Decl::getASTContext()
542 // internally which is unsafe during derialization.
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000543 D->setAttrsImpl(Attrs, Reader.getContext());
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000544 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000545 D->setImplicit(Record.readInt());
546 D->Used = Record.readInt();
Vassil Vassilev928c8252016-04-28 14:13:28 +0000547 IsDeclMarkedUsed |= D->Used;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000548 D->setReferenced(Record.readInt());
549 D->setTopLevelDeclInObjCContainer(Record.readInt());
550 D->setAccess((AccessSpecifier)Record.readInt());
Douglas Gregor98c05b22011-09-10 00:09:20 +0000551 D->FromASTFile = true;
Richard Smith90dc5252017-06-23 01:04:34 +0000552 bool ModulePrivate = Record.readInt();
Richard Smith42413142015-05-15 20:05:43 +0000553
Douglas Gregorcf68c582011-12-01 22:20:10 +0000554 // Determine whether this declaration is part of a (sub)module. If so, it
555 // may not yet be visible.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000556 if (unsigned SubmoduleID = readSubmoduleID()) {
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000557 // Store the owning submodule ID in the declaration.
Richard Smith90dc5252017-06-23 01:04:34 +0000558 D->setModuleOwnershipKind(
559 ModulePrivate ? Decl::ModuleOwnershipKind::ModulePrivate
560 : Decl::ModuleOwnershipKind::VisibleWhenImported);
Douglas Gregorcfe7dc62012-01-09 17:30:44 +0000561 D->setOwningModuleID(SubmoduleID);
Richard Smith42413142015-05-15 20:05:43 +0000562
Richard Smith90dc5252017-06-23 01:04:34 +0000563 if (ModulePrivate) {
564 // Module-private declarations are never visible, so there is no work to
565 // do.
Richard Smith42413142015-05-15 20:05:43 +0000566 } else if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
567 // If local visibility is being tracked, this declaration will become
Richard Smith90dc5252017-06-23 01:04:34 +0000568 // hidden and visible as the owning module does.
Richard Smith42413142015-05-15 20:05:43 +0000569 } else if (Module *Owner = Reader.getSubmodule(SubmoduleID)) {
Richard Smith90dc5252017-06-23 01:04:34 +0000570 // Mark the declaration as visible when its owning module becomes visible.
571 if (Owner->NameVisibility == Module::AllVisible)
572 D->setVisibleDespiteOwningModule();
573 else
Richard Smith42413142015-05-15 20:05:43 +0000574 Reader.HiddenNamesMap[Owner].push_back(D);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000575 }
Richard Smithd19389a2017-07-05 07:47:11 +0000576 } else if (ModulePrivate) {
577 D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::ModulePrivate);
Douglas Gregorcf68c582011-12-01 22:20:10 +0000578 }
Chris Lattner487412d2009-04-27 05:27:42 +0000579}
580
Nico Weber66220292016-03-02 17:28:48 +0000581void ASTDeclReader::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
582 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000583 D->setLocation(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000584 D->CommentKind = (PragmaMSCommentKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000585 std::string Arg = ReadString();
Nico Weber66220292016-03-02 17:28:48 +0000586 memcpy(D->getTrailingObjects<char>(), Arg.data(), Arg.size());
587 D->getTrailingObjects<char>()[Arg.size()] = '\0';
588}
589
Nico Webercbbaeb12016-03-02 19:28:54 +0000590void ASTDeclReader::VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D) {
591 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000592 D->setLocation(ReadSourceLocation());
593 std::string Name = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000594 memcpy(D->getTrailingObjects<char>(), Name.data(), Name.size());
595 D->getTrailingObjects<char>()[Name.size()] = '\0';
596
597 D->ValueStart = Name.size() + 1;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000598 std::string Value = ReadString();
Nico Webercbbaeb12016-03-02 19:28:54 +0000599 memcpy(D->getTrailingObjects<char>() + D->ValueStart, Value.data(),
600 Value.size());
601 D->getTrailingObjects<char>()[D->ValueStart + Value.size()] = '\0';
602}
603
Sebastian Redlb3298c32010-08-18 23:56:48 +0000604void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
Douglas Gregordab42432011-08-12 00:15:20 +0000605 llvm_unreachable("Translation units are not serialized");
Chris Lattner487412d2009-04-27 05:27:42 +0000606}
607
Sebastian Redlb3298c32010-08-18 23:56:48 +0000608void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
Chris Lattner487412d2009-04-27 05:27:42 +0000609 VisitDecl(ND);
David L. Jonesb6a8f022016-12-21 04:34:52 +0000610 ND->setDeclName(Record.readDeclarationName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000611 AnonymousDeclNumber = Record.readInt();
Chris Lattner487412d2009-04-27 05:27:42 +0000612}
613
Sebastian Redlb3298c32010-08-18 23:56:48 +0000614void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000615 VisitNamedDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000616 TD->setLocStart(ReadSourceLocation());
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000617 // Delay type reading until after we have fully initialized the decl.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000618 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +0000619}
620
Richard Smith43ccec8e2014-08-26 03:52:16 +0000621ASTDeclReader::RedeclarableResult
622ASTDeclReader::VisitTypedefNameDecl(TypedefNameDecl *TD) {
Douglas Gregor9b7b3912012-01-04 16:44:10 +0000623 RedeclarableResult Redecl = VisitRedeclarable(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000624 VisitTypeDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000625 TypeSourceInfo *TInfo = GetTypeSourceInfo();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000626 if (Record.readInt()) { // isModed
627 QualType modedT = Record.readType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +0000628 TD->setModedTypeSourceInfo(TInfo, modedT);
629 } else
630 TD->setTypeSourceInfo(TInfo);
Richard Smithc0ca4c22017-01-26 22:39:55 +0000631 // Read and discard the declaration for which this is a typedef name for
632 // linkage, if it exists. We cannot rely on our type to pull in this decl,
633 // because it might have been merged with a type from another module and
634 // thus might not refer to our version of the declaration.
635 ReadDecl();
Richard Smith43ccec8e2014-08-26 03:52:16 +0000636 return Redecl;
Douglas Gregor1f179062011-12-19 14:40:25 +0000637}
638
639void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000640 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
641 mergeRedeclarable(TD, Redecl);
Chris Lattner487412d2009-04-27 05:27:42 +0000642}
643
Richard Smithdda56e42011-04-15 14:24:37 +0000644void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
Richard Smith43ccec8e2014-08-26 03:52:16 +0000645 RedeclarableResult Redecl = VisitTypedefNameDecl(TD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000646 if (auto *Template = ReadDeclAs<TypeAliasTemplateDecl>())
Richard Smith43ccec8e2014-08-26 03:52:16 +0000647 // Merged when we merge the template.
648 TD->setDescribedAliasTemplate(Template);
649 else
650 mergeRedeclarable(TD, Redecl);
Richard Smithdda56e42011-04-15 14:24:37 +0000651}
652
Richard Smith1d209d02013-05-23 01:49:11 +0000653ASTDeclReader::RedeclarableResult ASTDeclReader::VisitTagDecl(TagDecl *TD) {
Douglas Gregor2009cee2012-01-03 22:46:00 +0000654 RedeclarableResult Redecl = VisitRedeclarable(TD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000655 VisitTypeDecl(TD);
Douglas Gregor2009cee2012-01-03 22:46:00 +0000656
David L. Jonesbe1557a2016-12-21 00:17:49 +0000657 TD->IdentifierNamespace = Record.readInt();
658 TD->setTagKind((TagDecl::TagKind)Record.readInt());
Richard Smith2c381642014-08-27 23:11:59 +0000659 if (!isa<CXXRecordDecl>(TD))
David L. Jonesbe1557a2016-12-21 00:17:49 +0000660 TD->setCompleteDefinition(Record.readInt());
661 TD->setEmbeddedInDeclarator(Record.readInt());
662 TD->setFreeStanding(Record.readInt());
663 TD->setCompleteDefinitionRequired(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000664 TD->setBraceRange(ReadSourceRange());
Douglas Gregor2009cee2012-01-03 22:46:00 +0000665
David L. Jonesbe1557a2016-12-21 00:17:49 +0000666 switch (Record.readInt()) {
Richard Smith70d58502014-08-30 00:04:23 +0000667 case 0:
668 break;
669 case 1: { // ExtInfo
Douglas Gregor4163aca2011-09-09 21:34:22 +0000670 TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000671 ReadQualifierInfo(*Info);
David Majnemer00350522015-08-31 18:48:39 +0000672 TD->TypedefNameDeclOrQualifier = Info;
Richard Smith70d58502014-08-30 00:04:23 +0000673 break;
674 }
675 case 2: // TypedefNameForAnonDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000676 NamedDeclForTagDecl = ReadDeclID();
David L. Jonesb6a8f022016-12-21 04:34:52 +0000677 TypedefNameForLinkage = Record.getIdentifierInfo();
Richard Smith70d58502014-08-30 00:04:23 +0000678 break;
Richard Smith70d58502014-08-30 00:04:23 +0000679 default:
680 llvm_unreachable("unexpected tag info kind");
681 }
Douglas Gregor2009cee2012-01-03 22:46:00 +0000682
Richard Smithcd45dbc2014-04-19 03:48:30 +0000683 if (!isa<CXXRecordDecl>(TD))
684 mergeRedeclarable(TD, Redecl);
Richard Smith1d209d02013-05-23 01:49:11 +0000685 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000686}
687
Sebastian Redlb3298c32010-08-18 23:56:48 +0000688void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
Chris Lattner487412d2009-04-27 05:27:42 +0000689 VisitTagDecl(ED);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000690 if (TypeSourceInfo *TI = GetTypeSourceInfo())
Douglas Gregor0bf31402010-10-08 23:50:27 +0000691 ED->setIntegerTypeSourceInfo(TI);
692 else
David L. Jonesbe1557a2016-12-21 00:17:49 +0000693 ED->setIntegerType(Record.readType());
694 ED->setPromotionType(Record.readType());
695 ED->setNumPositiveBits(Record.readInt());
696 ED->setNumNegativeBits(Record.readInt());
697 ED->IsScoped = Record.readInt();
698 ED->IsScopedUsingClassTag = Record.readInt();
699 ED->IsFixed = Record.readInt();
Richard Smith4b38ded2012-03-14 23:13:10 +0000700
Richard Smith01a73372013-10-15 22:02:41 +0000701 // If this is a definition subject to the ODR, and we already have a
702 // definition, merge this one into it.
703 if (ED->IsCompleteDefinition &&
704 Reader.getContext().getLangOpts().Modules &&
705 Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith86dfc1e2015-06-18 22:07:00 +0000706 EnumDecl *&OldDef = Reader.EnumDefinitions[ED->getCanonicalDecl()];
707 if (!OldDef) {
708 // This is the first time we've seen an imported definition. Look for a
709 // local definition before deciding that we are the first definition.
710 for (auto *D : merged_redecls(ED->getCanonicalDecl())) {
711 if (!D->isFromASTFile() && D->isCompleteDefinition()) {
712 OldDef = D;
713 break;
714 }
715 }
716 }
717 if (OldDef) {
Richard Smith01a73372013-10-15 22:02:41 +0000718 Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
719 ED->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +0000720 Reader.mergeDefinitionVisibility(OldDef, ED);
Richard Smith01a73372013-10-15 22:02:41 +0000721 } else {
722 OldDef = ED;
723 }
724 }
725
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000726 if (EnumDecl *InstED = ReadDeclAs<EnumDecl>()) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000727 TemplateSpecializationKind TSK =
728 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000729 SourceLocation POI = ReadSourceLocation();
Richard Smith4b38ded2012-03-14 23:13:10 +0000730 ED->setInstantiationOfMemberEnum(Reader.getContext(), InstED, TSK);
731 ED->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
732 }
Chris Lattner487412d2009-04-27 05:27:42 +0000733}
734
Richard Smith1d209d02013-05-23 01:49:11 +0000735ASTDeclReader::RedeclarableResult
736ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
737 RedeclarableResult Redecl = VisitTagDecl(RD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000738 RD->setHasFlexibleArrayMember(Record.readInt());
739 RD->setAnonymousStructOrUnion(Record.readInt());
740 RD->setHasObjectMember(Record.readInt());
741 RD->setHasVolatileMember(Record.readInt());
Akira Hatanaka34fb2642018-03-13 18:58:25 +0000742 RD->setNonTrivialToPrimitiveDefaultInitialize(Record.readInt());
743 RD->setNonTrivialToPrimitiveCopy(Record.readInt());
744 RD->setNonTrivialToPrimitiveDestroy(Record.readInt());
Akira Hatanakad791e922018-03-19 17:38:40 +0000745 RD->setCanPassInRegisters(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000746 RD->setParamDestroyedInCallee(Record.readInt());
Richard Smith1d209d02013-05-23 01:49:11 +0000747 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +0000748}
749
Sebastian Redlb3298c32010-08-18 23:56:48 +0000750void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000751 VisitNamedDecl(VD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000752 VD->setType(Record.readType());
Chris Lattner487412d2009-04-27 05:27:42 +0000753}
754
Sebastian Redlb3298c32010-08-18 23:56:48 +0000755void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000756 VisitValueDecl(ECD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000757 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +0000758 ECD->setInitExpr(Record.readExpr());
759 ECD->setInitVal(Record.readAPSInt());
Richard Smith01a73372013-10-15 22:02:41 +0000760 mergeMergeable(ECD);
Chris Lattner487412d2009-04-27 05:27:42 +0000761}
762
Sebastian Redlb3298c32010-08-18 23:56:48 +0000763void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000764 VisitValueDecl(DD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000765 DD->setInnerLocStart(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000766 if (Record.readInt()) { // hasExtInfo
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000767 DeclaratorDecl::ExtInfo *Info
Douglas Gregor4163aca2011-09-09 21:34:22 +0000768 = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000769 ReadQualifierInfo(*Info);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000770 DD->DeclInfo = Info;
Jonathan D. Turner205c7d52011-06-03 23:11:16 +0000771 }
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000772}
773
Sebastian Redlb3298c32010-08-18 23:56:48 +0000774void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Douglas Gregorb2585692012-01-04 17:13:46 +0000775 RedeclarableResult Redecl = VisitRedeclarable(FD);
Douglas Gregor3e300102011-10-26 17:53:41 +0000776 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000777
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000778 ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName());
David L. Jonesbe1557a2016-12-21 00:17:49 +0000779 FD->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000780
Douglas Gregorb2585692012-01-04 17:13:46 +0000781 // FunctionDecl's body is handled last at ASTDeclReader::Visit,
782 // after everything else is read.
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000783
David L. Jonesbe1557a2016-12-21 00:17:49 +0000784 FD->SClass = (StorageClass)Record.readInt();
785 FD->IsInline = Record.readInt();
786 FD->IsInlineSpecified = Record.readInt();
Richard Smith78e3d702017-02-10 01:32:04 +0000787 FD->IsExplicitSpecified = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000788 FD->IsVirtualAsWritten = Record.readInt();
789 FD->IsPure = Record.readInt();
790 FD->HasInheritedPrototype = Record.readInt();
791 FD->HasWrittenPrototype = Record.readInt();
792 FD->IsDeleted = Record.readInt();
793 FD->IsTrivial = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000794 FD->IsTrivialForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000795 FD->IsDefaulted = Record.readInt();
796 FD->IsExplicitlyDefaulted = Record.readInt();
797 FD->HasImplicitReturnZero = Record.readInt();
798 FD->IsConstexpr = Record.readInt();
Reid Kleckner0d157382017-01-10 21:27:03 +0000799 FD->UsesSEHTry = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000800 FD->HasSkippedBody = Record.readInt();
Erich Keane281d20b2018-01-08 21:34:17 +0000801 FD->IsMultiVersion = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000802 FD->IsLateTemplateParsed = Record.readInt();
803 FD->setCachedLinkage(Linkage(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000804 FD->EndRangeLoc = ReadSourceLocation();
Douglas Gregorb2585692012-01-04 17:13:46 +0000805
Richard Trieue6caa262017-12-23 00:41:01 +0000806 FD->ODRHash = Record.readInt();
807 FD->HasODRHash = true;
808
David L. Jonesbe1557a2016-12-21 00:17:49 +0000809 switch ((FunctionDecl::TemplatedKind)Record.readInt()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000810 case FunctionDecl::TK_NonTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000811 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000812 break;
813 case FunctionDecl::TK_FunctionTemplate:
Richard Smithcd45dbc2014-04-19 03:48:30 +0000814 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000815 FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>());
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000816 break;
817 case FunctionDecl::TK_MemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000818 FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000819 TemplateSpecializationKind TSK =
820 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000821 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +0000822 FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000823 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Richard Smithcd45dbc2014-04-19 03:48:30 +0000824 mergeRedeclarable(FD, Redecl);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000825 break;
826 }
827 case FunctionDecl::TK_FunctionTemplateSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000828 FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000829 TemplateSpecializationKind TSK =
830 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000831
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000832 // Template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000833 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +0000834 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
Richard Smith2bb3c342015-08-09 01:05:31 +0000835
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000836 // Template args as written.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000837 SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000838 SourceLocation LAngleLoc, RAngleLoc;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000839 bool HasTemplateArgumentsAsWritten = Record.readInt();
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000840 if (HasTemplateArgumentsAsWritten) {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000841 unsigned NumTemplateArgLocs = Record.readInt();
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000842 TemplArgLocs.reserve(NumTemplateArgLocs);
843 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000844 TemplArgLocs.push_back(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000845
846 LAngleLoc = ReadSourceLocation();
847 RAngleLoc = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000848 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000849
850 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000851
Douglas Gregor4163aca2011-09-09 21:34:22 +0000852 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000853 TemplateArgumentList *TemplArgList
David Majnemer8b622692016-07-03 21:17:51 +0000854 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000855 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000856 for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000857 TemplArgsInfo.addArgument(TemplArgLocs[i]);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000858 FunctionTemplateSpecializationInfo *FTInfo
859 = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
860 TemplArgList,
Craig Toppera13603a2014-05-22 05:54:18 +0000861 HasTemplateArgumentsAsWritten ? &TemplArgsInfo
862 : nullptr,
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000863 POI);
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000864 FD->TemplateOrSpecialization = FTInfo;
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000865
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000866 if (FD->isCanonicalDecl()) { // if canonical add to template's set.
Argyrios Kyrtzidisf24d5692010-09-13 11:45:48 +0000867 // The template that contains the specializations set. It's not safe to
868 // use getCanonicalDecl on Template since it may still be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000869 FunctionTemplateDecl *CanonTemplate = ReadDeclAs<FunctionTemplateDecl>();
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +0000870 // Get the InsertPos by FindNodeOrInsertPos() instead of calling
871 // InsertNode(FTInfo) directly to avoid the getASTContext() call in
872 // FunctionTemplateSpecializationInfo's Profile().
873 // We avoid getASTContext because a decl in the parent hierarchy may
874 // be initializing.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000875 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000876 FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs, C);
Craig Toppera13603a2014-05-22 05:54:18 +0000877 void *InsertPos = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000878 FunctionTemplateDecl::Common *CommonPtr = CanonTemplate->getCommonPtr();
Richard Smithda6c2342015-07-01 23:19:58 +0000879 FunctionTemplateSpecializationInfo *ExistingInfo =
880 CommonPtr->Specializations.FindNodeOrInsertPos(ID, InsertPos);
Argyrios Kyrtzidise60e4082012-09-10 23:28:22 +0000881 if (InsertPos)
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000882 CommonPtr->Specializations.InsertNode(FTInfo, InsertPos);
883 else {
884 assert(Reader.getContext().getLangOpts().Modules &&
885 "already deserialized this template specialization");
Richard Smithda6c2342015-07-01 23:19:58 +0000886 mergeRedeclarable(FD, ExistingInfo->Function, Redecl);
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000887 }
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000888 }
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000889 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000890 }
891 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
892 // Templates.
893 UnresolvedSet<8> TemplDecls;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000894 unsigned NumTemplates = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000895 while (NumTemplates--)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000896 TemplDecls.addDecl(ReadDeclAs<NamedDecl>());
897
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000898 // Templates args.
899 TemplateArgumentListInfo TemplArgs;
David L. Jonesbe1557a2016-12-21 00:17:49 +0000900 unsigned NumArgs = Record.readInt();
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000901 while (NumArgs--)
David L. Jonesb6a8f022016-12-21 04:34:52 +0000902 TemplArgs.addArgument(Record.readTemplateArgumentLoc());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000903 TemplArgs.setLAngleLoc(ReadSourceLocation());
904 TemplArgs.setRAngleLoc(ReadSourceLocation());
905
Douglas Gregor4163aca2011-09-09 21:34:22 +0000906 FD->setDependentTemplateSpecialization(Reader.getContext(),
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000907 TemplDecls, TemplArgs);
Richard Smithda6c2342015-07-01 23:19:58 +0000908 // These are not merged; we don't need to merge redeclarations of dependent
909 // template friends.
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000910 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000911 }
912 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000913
Chris Lattnerca025db2010-05-07 21:43:38 +0000914 // Read in the parameters.
David L. Jonesbe1557a2016-12-21 00:17:49 +0000915 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000916 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000917 Params.reserve(NumParams);
918 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000919 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +0000920 FD->setParams(Reader.getContext(), Params);
Chris Lattner487412d2009-04-27 05:27:42 +0000921}
922
Sebastian Redlb3298c32010-08-18 23:56:48 +0000923void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000924 VisitNamedDecl(MD);
David L. Jonesbe1557a2016-12-21 00:17:49 +0000925 if (Record.readInt()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +0000926 // Load the body on-demand. Most clients won't care, because method
927 // definitions rarely show up in headers.
928 Reader.PendingBodies[MD] = GetCurrentCursorOffset();
929 HasPendingBody = true;
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000930 MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>());
931 MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +0000932 }
David L. Jonesbe1557a2016-12-21 00:17:49 +0000933 MD->setInstanceMethod(Record.readInt());
934 MD->setVariadic(Record.readInt());
935 MD->setPropertyAccessor(Record.readInt());
936 MD->setDefined(Record.readInt());
937 MD->IsOverriding = Record.readInt();
938 MD->HasSkippedBody = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000939
David L. Jonesbe1557a2016-12-21 00:17:49 +0000940 MD->IsRedeclaration = Record.readInt();
941 MD->HasRedeclaration = Record.readInt();
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000942 if (MD->HasRedeclaration)
943 Reader.getContext().setObjCMethodRedeclaration(MD,
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000944 ReadDeclAs<ObjCMethodDecl>());
Argyrios Kyrtzidisdb215962011-10-14 17:41:52 +0000945
David L. Jonesbe1557a2016-12-21 00:17:49 +0000946 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record.readInt());
947 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record.readInt());
948 MD->SetRelatedResultType(Record.readInt());
949 MD->setReturnType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000950 MD->setReturnTypeSourceInfo(GetTypeSourceInfo());
951 MD->DeclEndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +0000952 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000953 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +0000954 Params.reserve(NumParams);
955 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000956 Params.push_back(ReadDeclAs<ParmVarDecl>());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000957
David L. Jonesbe1557a2016-12-21 00:17:49 +0000958 MD->SelLocsKind = Record.readInt();
959 unsigned NumStoredSelLocs = Record.readInt();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000960 SmallVector<SourceLocation, 16> SelLocs;
961 SelLocs.reserve(NumStoredSelLocs);
962 for (unsigned i = 0; i != NumStoredSelLocs; ++i)
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000963 SelLocs.push_back(ReadSourceLocation());
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000964
965 MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
Chris Lattner487412d2009-04-27 05:27:42 +0000966}
967
Douglas Gregor85f3f952015-07-07 03:57:15 +0000968void ASTDeclReader::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
David Blaikie27a1bc02015-09-28 23:48:49 +0000969 VisitTypedefNameDecl(D);
Richard Smith9b88a4c2015-07-27 05:40:23 +0000970
David L. Jonesbe1557a2016-12-21 00:17:49 +0000971 D->Variance = Record.readInt();
972 D->Index = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000973 D->VarianceLoc = ReadSourceLocation();
974 D->ColonLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000975}
976
Sebastian Redlb3298c32010-08-18 23:56:48 +0000977void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +0000978 VisitNamedDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000979 CD->setAtStartLoc(ReadSourceLocation());
980 CD->setAtEndRange(ReadSourceRange());
Chris Lattner487412d2009-04-27 05:27:42 +0000981}
982
Douglas Gregor85f3f952015-07-07 03:57:15 +0000983ObjCTypeParamList *ASTDeclReader::ReadObjCTypeParamList() {
David L. Jonesbe1557a2016-12-21 00:17:49 +0000984 unsigned numParams = Record.readInt();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000985 if (numParams == 0)
986 return nullptr;
987
988 SmallVector<ObjCTypeParamDecl *, 4> typeParams;
989 typeParams.reserve(numParams);
990 for (unsigned i = 0; i != numParams; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000991 auto typeParam = ReadDeclAs<ObjCTypeParamDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000992 if (!typeParam)
993 return nullptr;
994
995 typeParams.push_back(typeParam);
996 }
997
David L. Jonesc4808b9e2016-12-15 20:53:26 +0000998 SourceLocation lAngleLoc = ReadSourceLocation();
999 SourceLocation rAngleLoc = ReadSourceLocation();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001000
1001 return ObjCTypeParamList::create(Reader.getContext(), lAngleLoc,
1002 typeParams, rAngleLoc);
1003}
1004
Manman Renec315f12016-09-09 23:48:27 +00001005void ASTDeclReader::ReadObjCDefinitionData(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001006 struct ObjCInterfaceDecl::DefinitionData &Data) {
Manman Renec315f12016-09-09 23:48:27 +00001007 // Read the superclass.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001008 Data.SuperClassTInfo = GetTypeSourceInfo();
Manman Renec315f12016-09-09 23:48:27 +00001009
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001010 Data.EndLoc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001011 Data.HasDesignatedInitializers = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001012
Manman Renec315f12016-09-09 23:48:27 +00001013 // Read the directly referenced protocols and their SourceLocations.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001014 unsigned NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001015 SmallVector<ObjCProtocolDecl *, 16> Protocols;
1016 Protocols.reserve(NumProtocols);
1017 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001018 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001019 SmallVector<SourceLocation, 16> ProtoLocs;
1020 ProtoLocs.reserve(NumProtocols);
1021 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001022 ProtoLocs.push_back(ReadSourceLocation());
Manman Renec315f12016-09-09 23:48:27 +00001023 Data.ReferencedProtocols.set(Protocols.data(), NumProtocols, ProtoLocs.data(),
1024 Reader.getContext());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001025
Manman Renec315f12016-09-09 23:48:27 +00001026 // Read the transitive closure of protocols referenced by this class.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001027 NumProtocols = Record.readInt();
Manman Renec315f12016-09-09 23:48:27 +00001028 Protocols.clear();
1029 Protocols.reserve(NumProtocols);
1030 for (unsigned I = 0; I != NumProtocols; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001031 Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>());
Manman Renec315f12016-09-09 23:48:27 +00001032 Data.AllReferencedProtocols.set(Protocols.data(), NumProtocols,
1033 Reader.getContext());
1034}
1035
1036void ASTDeclReader::MergeDefinitionData(ObjCInterfaceDecl *D,
1037 struct ObjCInterfaceDecl::DefinitionData &&NewDD) {
1038 // FIXME: odr checking?
1039}
1040
Sebastian Redlb3298c32010-08-18 23:56:48 +00001041void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
Douglas Gregor022857e2011-12-22 01:48:48 +00001042 RedeclarableResult Redecl = VisitRedeclarable(ID);
Chris Lattner487412d2009-04-27 05:27:42 +00001043 VisitObjCContainerDecl(ID);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001044 TypeIDForTypeDecl = Record.getGlobalTypeID(Record.readInt());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00001045 mergeRedeclarable(ID, Redecl);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001046
1047 ID->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001048 if (Record.readInt()) {
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001049 // Read the definition.
1050 ID->allocateDefinitionData();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001051
David L. Jonesbe1557a2016-12-21 00:17:49 +00001052 ReadObjCDefinitionData(ID->data());
Manman Renec315f12016-09-09 23:48:27 +00001053 ObjCInterfaceDecl *Canon = ID->getCanonicalDecl();
1054 if (Canon->Data.getPointer()) {
1055 // If we already have a definition, keep the definition invariant and
1056 // merge the data.
1057 MergeDefinitionData(Canon, std::move(ID->data()));
1058 ID->Data = Canon->Data;
1059 } else {
1060 // Set the definition data of the canonical declaration, so other
1061 // redeclarations will see it.
1062 ID->getCanonicalDecl()->Data = ID->Data;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001063
Manman Renec315f12016-09-09 23:48:27 +00001064 // We will rebuild this list lazily.
1065 ID->setIvarList(nullptr);
1066 }
Craig Toppera13603a2014-05-22 05:54:18 +00001067
Douglas Gregorc1a61fe2011-12-19 20:51:16 +00001068 // Note that we have deserialized a definition.
1069 Reader.PendingDefinitions.insert(ID);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001070
Douglas Gregor404cdde2012-01-27 01:47:08 +00001071 // Note that we've loaded this Objective-C class.
1072 Reader.ObjCClassesLoaded.push_back(ID);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001073 } else {
1074 ID->Data = ID->getCanonicalDecl()->Data;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001075 }
Chris Lattner487412d2009-04-27 05:27:42 +00001076}
1077
Sebastian Redlb3298c32010-08-18 23:56:48 +00001078void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001079 VisitFieldDecl(IVD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001080 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record.readInt());
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001081 // This field will be built lazily.
Craig Toppera13603a2014-05-22 05:54:18 +00001082 IVD->setNextIvar(nullptr);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001083 bool synth = Record.readInt();
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00001084 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +00001085}
1086
Graydon Hoaree0a68352017-06-28 18:36:27 +00001087void ASTDeclReader::ReadObjCDefinitionData(
1088 struct ObjCProtocolDecl::DefinitionData &Data) {
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001089
David L. Jonesbe1557a2016-12-21 00:17:49 +00001090 unsigned NumProtoRefs = Record.readInt();
Douglas Gregore6e48b12012-01-01 19:29:29 +00001091 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
1092 ProtoRefs.reserve(NumProtoRefs);
1093 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001094 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Douglas Gregore6e48b12012-01-01 19:29:29 +00001095 SmallVector<SourceLocation, 16> ProtoLocs;
1096 ProtoLocs.reserve(NumProtoRefs);
1097 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001098 ProtoLocs.push_back(ReadSourceLocation());
Graydon Hoaree0a68352017-06-28 18:36:27 +00001099 Data.ReferencedProtocols.set(ProtoRefs.data(), NumProtoRefs,
1100 ProtoLocs.data(), Reader.getContext());
1101}
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001102
Graydon Hoaree0a68352017-06-28 18:36:27 +00001103void ASTDeclReader::MergeDefinitionData(ObjCProtocolDecl *D,
1104 struct ObjCProtocolDecl::DefinitionData &&NewDD) {
1105 // FIXME: odr checking?
1106}
1107
1108void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
1109 RedeclarableResult Redecl = VisitRedeclarable(PD);
1110 VisitObjCContainerDecl(PD);
1111 mergeRedeclarable(PD, Redecl);
1112
1113 if (Record.readInt()) {
1114 // Read the definition.
1115 PD->allocateDefinitionData();
1116
1117 ReadObjCDefinitionData(PD->data());
1118
1119 ObjCProtocolDecl *Canon = PD->getCanonicalDecl();
1120 if (Canon->Data.getPointer()) {
1121 // If we already have a definition, keep the definition invariant and
1122 // merge the data.
1123 MergeDefinitionData(Canon, std::move(PD->data()));
1124 PD->Data = Canon->Data;
1125 } else {
1126 // Set the definition data of the canonical declaration, so other
1127 // redeclarations will see it.
1128 PD->getCanonicalDecl()->Data = PD->Data;
1129 }
Douglas Gregora715bff2012-01-01 19:51:50 +00001130 // Note that we have deserialized a definition.
1131 Reader.PendingDefinitions.insert(PD);
Douglas Gregorc03c52e2012-01-15 18:08:05 +00001132 } else {
1133 PD->Data = PD->getCanonicalDecl()->Data;
Douglas Gregore6e48b12012-01-01 19:29:29 +00001134 }
Chris Lattner487412d2009-04-27 05:27:42 +00001135}
1136
Sebastian Redlb3298c32010-08-18 23:56:48 +00001137void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001138 VisitFieldDecl(FD);
1139}
1140
Sebastian Redlb3298c32010-08-18 23:56:48 +00001141void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001142 VisitObjCContainerDecl(CD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001143 CD->setCategoryNameLoc(ReadSourceLocation());
1144 CD->setIvarLBraceLoc(ReadSourceLocation());
1145 CD->setIvarRBraceLoc(ReadSourceLocation());
1146
Douglas Gregor404cdde2012-01-27 01:47:08 +00001147 // Note that this category has been deserialized. We do this before
1148 // deserializing the interface declaration, so that it will consider this
1149 /// category.
1150 Reader.CategoriesDeserialized.insert(CD);
1151
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001152 CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>();
Douglas Gregor85f3f952015-07-07 03:57:15 +00001153 CD->TypeParamList = ReadObjCTypeParamList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001154 unsigned NumProtoRefs = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001155 SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
Chris Lattner487412d2009-04-27 05:27:42 +00001156 ProtoRefs.reserve(NumProtoRefs);
1157 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001158 ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001159 SmallVector<SourceLocation, 16> ProtoLocs;
Douglas Gregor002b6712010-01-16 15:02:53 +00001160 ProtoLocs.reserve(NumProtoRefs);
1161 for (unsigned I = 0; I != NumProtoRefs; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001162 ProtoLocs.push_back(ReadSourceLocation());
Douglas Gregor002b6712010-01-16 15:02:53 +00001163 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
Douglas Gregor4163aca2011-09-09 21:34:22 +00001164 Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +00001165}
1166
Sebastian Redlb3298c32010-08-18 23:56:48 +00001167void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001168 VisitNamedDecl(CAD);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001169 CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001170}
1171
Sebastian Redlb3298c32010-08-18 23:56:48 +00001172void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001173 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001174 D->setAtLoc(ReadSourceLocation());
1175 D->setLParenLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001176 QualType T = Record.readType();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001177 TypeSourceInfo *TSI = GetTypeSourceInfo();
Douglas Gregor813a0662015-06-19 18:14:38 +00001178 D->setType(T, TSI);
Chris Lattner487412d2009-04-27 05:27:42 +00001179 D->setPropertyAttributes(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001180 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001181 D->setPropertyAttributesAsWritten(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001182 (ObjCPropertyDecl::PropertyAttributeKind)Record.readInt());
Chris Lattner487412d2009-04-27 05:27:42 +00001183 D->setPropertyImplementation(
David L. Jonesbe1557a2016-12-21 00:17:49 +00001184 (ObjCPropertyDecl::PropertyControl)Record.readInt());
Argyrios Kyrtzidisc6c4ec82017-03-17 00:49:42 +00001185 DeclarationName GetterName = Record.readDeclarationName();
1186 SourceLocation GetterLoc = ReadSourceLocation();
1187 D->setGetterName(GetterName.getObjCSelector(), GetterLoc);
1188 DeclarationName SetterName = Record.readDeclarationName();
1189 SourceLocation SetterLoc = ReadSourceLocation();
1190 D->setSetterName(SetterName.getObjCSelector(), SetterLoc);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001191 D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1192 D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>());
1193 D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001194}
1195
Sebastian Redlb3298c32010-08-18 23:56:48 +00001196void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +00001197 VisitObjCContainerDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001198 D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>());
Chris Lattner487412d2009-04-27 05:27:42 +00001199}
1200
Sebastian Redlb3298c32010-08-18 23:56:48 +00001201void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001202 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001203 D->CategoryNameLoc = ReadSourceLocation();
Chris Lattner487412d2009-04-27 05:27:42 +00001204}
1205
Sebastian Redlb3298c32010-08-18 23:56:48 +00001206void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001207 VisitObjCImplDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001208 D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>());
1209 D->SuperLoc = ReadSourceLocation();
1210 D->setIvarLBraceLoc(ReadSourceLocation());
1211 D->setIvarRBraceLoc(ReadSourceLocation());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001212 D->setHasNonZeroConstructors(Record.readInt());
1213 D->setHasDestructors(Record.readInt());
1214 D->NumIvarInitializers = Record.readInt();
Richard Smithc2bb8182015-03-24 06:36:48 +00001215 if (D->NumIvarInitializers)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001216 D->IvarInitializers = ReadGlobalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00001217}
1218
Sebastian Redlb3298c32010-08-18 23:56:48 +00001219void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00001220 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001221 D->setAtLoc(ReadSourceLocation());
1222 D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>());
1223 D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>();
1224 D->IvarLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001225 D->setGetterCXXConstructor(Record.readExpr());
1226 D->setSetterCXXAssignment(Record.readExpr());
Chris Lattner487412d2009-04-27 05:27:42 +00001227}
1228
Sebastian Redlb3298c32010-08-18 23:56:48 +00001229void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +00001230 VisitDeclaratorDecl(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001231 FD->Mutable = Record.readInt();
Richard Smith6b8e3c02017-08-28 00:28:14 +00001232
1233 if (auto ISK = static_cast<FieldDecl::InitStorageKind>(Record.readInt())) {
1234 FD->InitStorage.setInt(ISK);
1235 FD->InitStorage.setPointer(ISK == FieldDecl::ISK_CapturedVLAType
1236 ? Record.readType().getAsOpaquePtr()
1237 : Record.readExpr());
Richard Smith2b013182012-06-10 03:12:00 +00001238 }
Richard Smith6b8e3c02017-08-28 00:28:14 +00001239
1240 if (auto *BW = Record.readExpr())
1241 FD->setBitWidth(BW);
1242
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001243 if (!FD->getDeclName()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001244 if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001245 Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001246 }
Richard Smith0b87e072013-10-07 08:02:11 +00001247 mergeMergeable(FD);
Chris Lattner487412d2009-04-27 05:27:42 +00001248}
1249
John McCall5e77d762013-04-16 07:28:30 +00001250void ASTDeclReader::VisitMSPropertyDecl(MSPropertyDecl *PD) {
1251 VisitDeclaratorDecl(PD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001252 PD->GetterId = Record.getIdentifierInfo();
1253 PD->SetterId = Record.getIdentifierInfo();
John McCall5e77d762013-04-16 07:28:30 +00001254}
1255
Francois Pichet783dd6e2010-11-21 06:08:52 +00001256void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
1257 VisitValueDecl(FD);
1258
David L. Jonesbe1557a2016-12-21 00:17:49 +00001259 FD->ChainingSize = Record.readInt();
Francois Pichet783dd6e2010-11-21 06:08:52 +00001260 assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
Douglas Gregor4163aca2011-09-09 21:34:22 +00001261 FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
Francois Pichet783dd6e2010-11-21 06:08:52 +00001262
1263 for (unsigned I = 0; I != FD->ChainingSize; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001264 FD->Chaining[I] = ReadDeclAs<NamedDecl>();
Richard Smith8cbd8952015-08-04 02:05:09 +00001265
1266 mergeMergeable(FD);
Francois Pichet783dd6e2010-11-21 06:08:52 +00001267}
1268
Larisse Voufo39a1e502013-08-06 01:03:05 +00001269ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00001270 RedeclarableResult Redecl = VisitRedeclarable(VD);
Douglas Gregor3e300102011-10-26 17:53:41 +00001271 VisitDeclaratorDecl(VD);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001272
David L. Jonesbe1557a2016-12-21 00:17:49 +00001273 VD->VarDeclBits.SClass = (StorageClass)Record.readInt();
1274 VD->VarDeclBits.TSCSpec = Record.readInt();
1275 VD->VarDeclBits.InitStyle = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001276 if (!isa<ParmVarDecl>(VD)) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00001277 VD->NonParmVarDeclBits.IsThisDeclarationADemotedDefinition =
1278 Record.readInt();
1279 VD->NonParmVarDeclBits.ExceptionVar = Record.readInt();
1280 VD->NonParmVarDeclBits.NRVOVariable = Record.readInt();
1281 VD->NonParmVarDeclBits.CXXForRangeDecl = Record.readInt();
George Karpenkovec38cf72018-03-29 00:56:24 +00001282 VD->NonParmVarDeclBits.ObjCForDecl = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001283 VD->NonParmVarDeclBits.ARCPseudoStrong = Record.readInt();
1284 VD->NonParmVarDeclBits.IsInline = Record.readInt();
1285 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
1286 VD->NonParmVarDeclBits.IsConstexpr = Record.readInt();
1287 VD->NonParmVarDeclBits.IsInitCapture = Record.readInt();
1288 VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope = Record.readInt();
Alexey Bataev56223232017-06-09 13:40:18 +00001289 VD->NonParmVarDeclBits.ImplicitParamKind = Record.readInt();
David Majnemerfa7bc782015-05-19 00:57:16 +00001290 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001291 Linkage VarLinkage = Linkage(Record.readInt());
Richard Smith541b38b2013-09-20 01:15:31 +00001292 VD->setCachedLinkage(VarLinkage);
1293
1294 // Reconstruct the one piece of the IdentifierNamespace that we need.
Ted Kremeneka683f632014-02-11 06:29:29 +00001295 if (VD->getStorageClass() == SC_Extern && VarLinkage != NoLinkage &&
Richard Smith541b38b2013-09-20 01:15:31 +00001296 VD->getLexicalDeclContext()->isFunctionOrMethod())
1297 VD->setLocalExternDecl();
Rafael Espindola50df3a02013-05-25 17:16:20 +00001298
David L. Jonesbe1557a2016-12-21 00:17:49 +00001299 if (uint64_t Val = Record.readInt()) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00001300 VD->setInit(Record.readExpr());
Vassil Vassilevd1a88132016-10-06 13:04:54 +00001301 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
Richard Smithd0b4dd62011-12-19 06:19:21 +00001302 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
1303 Eval->CheckedICE = true;
1304 Eval->IsICE = Val == 3;
1305 }
1306 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001307
Richard Smitha4653622017-09-06 20:01:14 +00001308 if (VD->getStorageDuration() == SD_Static && Record.readInt())
1309 Reader.DefinitionSource[VD] = Loc.F->Kind == ModuleKind::MK_MainFile;
1310
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001311 enum VarKind {
1312 VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1313 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001314 switch ((VarKind)Record.readInt()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001315 case VarNotTemplate:
Richard Smith9b88a4c2015-07-27 05:40:23 +00001316 // Only true variables (not parameters or implicit parameters) can be
1317 // merged; the other kinds are not really redeclarable at all.
Richard Smith0144f512015-08-22 02:09:38 +00001318 if (!isa<ParmVarDecl>(VD) && !isa<ImplicitParamDecl>(VD) &&
1319 !isa<VarTemplateSpecializationDecl>(VD))
Richard Smithf17fdbd2014-04-24 02:25:27 +00001320 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001321 break;
1322 case VarTemplate:
Richard Smithf17fdbd2014-04-24 02:25:27 +00001323 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001324 VD->setDescribedVarTemplate(ReadDeclAs<VarTemplateDecl>());
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001325 break;
1326 case StaticDataMemberSpecialization: { // HasMemberSpecializationInfo.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001327 VarDecl *Tmpl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001328 TemplateSpecializationKind TSK =
1329 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001330 SourceLocation POI = ReadSourceLocation();
Douglas Gregor4163aca2011-09-09 21:34:22 +00001331 Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
Richard Smithf17fdbd2014-04-24 02:25:27 +00001332 mergeRedeclarable(VD, Redecl);
Larisse Voufod8dd97c2013-08-14 03:09:19 +00001333 break;
1334 }
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +00001335 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001336
1337 return Redecl;
Chris Lattner487412d2009-04-27 05:27:42 +00001338}
1339
Sebastian Redlb3298c32010-08-18 23:56:48 +00001340void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001341 VisitVarDecl(PD);
1342}
1343
Sebastian Redlb3298c32010-08-18 23:56:48 +00001344void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001345 VisitVarDecl(PD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001346 unsigned isObjCMethodParam = Record.readInt();
1347 unsigned scopeDepth = Record.readInt();
1348 unsigned scopeIndex = Record.readInt();
1349 unsigned declQualifier = Record.readInt();
John McCall82490832011-05-02 00:30:12 +00001350 if (isObjCMethodParam) {
1351 assert(scopeDepth == 0);
1352 PD->setObjCMethodScopeInfo(scopeIndex);
1353 PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
1354 } else {
1355 PD->setScopeInfo(scopeDepth, scopeIndex);
1356 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00001357 PD->ParmVarDeclBits.IsKNRPromoted = Record.readInt();
1358 PD->ParmVarDeclBits.HasInheritedDefaultArg = Record.readInt();
1359 if (Record.readInt()) // hasUninstantiatedDefaultArg.
David L. Jonesb6a8f022016-12-21 04:34:52 +00001360 PD->setUninstantiatedDefaultArg(Record.readExpr());
Richard Smithbf78e642013-06-24 22:51:00 +00001361
1362 // FIXME: If this is a redeclaration of a function from another module, handle
1363 // inheritance of default arguments.
Chris Lattner487412d2009-04-27 05:27:42 +00001364}
1365
Richard Smith7b76d812016-08-12 02:21:25 +00001366void ASTDeclReader::VisitDecompositionDecl(DecompositionDecl *DD) {
1367 VisitVarDecl(DD);
1368 BindingDecl **BDs = DD->getTrailingObjects<BindingDecl*>();
1369 for (unsigned I = 0; I != DD->NumBindings; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001370 BDs[I] = ReadDeclAs<BindingDecl>();
Richard Smith7b76d812016-08-12 02:21:25 +00001371}
1372
1373void ASTDeclReader::VisitBindingDecl(BindingDecl *BD) {
1374 VisitValueDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001375 BD->Binding = Record.readExpr();
Richard Smith7b76d812016-08-12 02:21:25 +00001376}
1377
Sebastian Redlb3298c32010-08-18 23:56:48 +00001378void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001379 VisitDecl(AD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001380 AD->setAsmString(cast<StringLiteral>(Record.readExpr()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001381 AD->setRParenLoc(ReadSourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001382}
1383
Sebastian Redlb3298c32010-08-18 23:56:48 +00001384void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
Chris Lattner487412d2009-04-27 05:27:42 +00001385 VisitDecl(BD);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001386 BD->setBody(cast_or_null<CompoundStmt>(Record.readStmt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001387 BD->setSignatureAsWritten(GetTypeSourceInfo());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001388 unsigned NumParams = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001389 SmallVector<ParmVarDecl *, 16> Params;
Chris Lattner487412d2009-04-27 05:27:42 +00001390 Params.reserve(NumParams);
1391 for (unsigned I = 0; I != NumParams; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001392 Params.push_back(ReadDeclAs<ParmVarDecl>());
David Blaikie9c70e042011-09-21 18:16:56 +00001393 BD->setParams(Params);
John McCallc63de662011-02-02 13:00:07 +00001394
David L. Jonesbe1557a2016-12-21 00:17:49 +00001395 BD->setIsVariadic(Record.readInt());
1396 BD->setBlockMissingReturnType(Record.readInt());
1397 BD->setIsConversionFromLambda(Record.readInt());
John McCallcf6ce282012-04-13 17:33:29 +00001398
David L. Jonesbe1557a2016-12-21 00:17:49 +00001399 bool capturesCXXThis = Record.readInt();
1400 unsigned numCaptures = Record.readInt();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001401 SmallVector<BlockDecl::Capture, 16> captures;
John McCall351762c2011-02-07 10:33:21 +00001402 captures.reserve(numCaptures);
1403 for (unsigned i = 0; i != numCaptures; ++i) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001404 VarDecl *decl = ReadDeclAs<VarDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001405 unsigned flags = Record.readInt();
John McCall351762c2011-02-07 10:33:21 +00001406 bool byRef = (flags & 1);
1407 bool nested = (flags & 2);
David L. Jonesb6a8f022016-12-21 04:34:52 +00001408 Expr *copyExpr = ((flags & 4) ? Record.readExpr() : nullptr);
John McCall351762c2011-02-07 10:33:21 +00001409
1410 captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
1411 }
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00001412 BD->setCaptures(Reader.getContext(), captures, capturesCXXThis);
Chris Lattner487412d2009-04-27 05:27:42 +00001413}
1414
Ben Langmuirce914fc2013-05-03 19:20:19 +00001415void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
1416 VisitDecl(CD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001417 unsigned ContextParamPos = Record.readInt();
1418 CD->setNothrow(Record.readInt() != 0);
Ben Langmuirce914fc2013-05-03 19:20:19 +00001419 // Body is set by VisitCapturedStmt.
Alexey Bataev9959db52014-05-06 10:08:46 +00001420 for (unsigned I = 0; I < CD->NumParams; ++I) {
1421 if (I != ContextParamPos)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001422 CD->setParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001423 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001424 CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>());
Alexey Bataev9959db52014-05-06 10:08:46 +00001425 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001426}
1427
Sebastian Redlb3298c32010-08-18 23:56:48 +00001428void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001429 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001430 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001431 D->setExternLoc(ReadSourceLocation());
1432 D->setRBraceLoc(ReadSourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001433}
1434
Richard Smith8df390f2016-09-08 23:14:54 +00001435void ASTDeclReader::VisitExportDecl(ExportDecl *D) {
1436 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001437 D->RBraceLoc = ReadSourceLocation();
Richard Smith8df390f2016-09-08 23:14:54 +00001438}
1439
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001440void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
1441 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001442 D->setLocStart(ReadSourceLocation());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001443}
1444
Sebastian Redlb3298c32010-08-18 23:56:48 +00001445void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +00001446 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001447 VisitNamedDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001448 D->setInline(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001449 D->LocStart = ReadSourceLocation();
1450 D->RBraceLoc = ReadSourceLocation();
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00001451
Richard Smith15e32fd2015-03-17 02:23:11 +00001452 // Defer loading the anonymous namespace until we've finished merging
1453 // this namespace; loading it might load a later declaration of the
1454 // same namespace, and we have an invariant that older declarations
1455 // get merged before newer ones try to merge.
1456 GlobalDeclID AnonNamespace = 0;
Douglas Gregore57e7522012-01-07 09:11:48 +00001457 if (Redecl.getFirstID() == ThisDeclID) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001458 AnonNamespace = ReadDeclID();
Douglas Gregore57e7522012-01-07 09:11:48 +00001459 } else {
1460 // Link this namespace back to the first declaration, which has already
1461 // been deserialized.
Rafael Espindola8db352d2013-10-17 15:37:26 +00001462 D->AnonOrFirstNamespaceAndInline.setPointer(D->getFirstDecl());
Douglas Gregore57e7522012-01-07 09:11:48 +00001463 }
Richard Smith5e9e56a2014-07-15 03:37:06 +00001464
1465 mergeRedeclarable(D, Redecl);
Richard Smith15e32fd2015-03-17 02:23:11 +00001466
1467 if (AnonNamespace) {
1468 // Each module has its own anonymous namespace, which is disjoint from
1469 // any other module's anonymous namespaces, so don't attach the anonymous
1470 // namespace at all.
1471 NamespaceDecl *Anon = cast<NamespaceDecl>(Reader.GetDecl(AnonNamespace));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001472 if (!Record.isModule())
Richard Smith15e32fd2015-03-17 02:23:11 +00001473 D->setAnonymousNamespace(Anon);
1474 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001475}
1476
Sebastian Redlb3298c32010-08-18 23:56:48 +00001477void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Richard Smithf4634362014-09-03 23:11:22 +00001478 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001479 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001480 D->NamespaceLoc = ReadSourceLocation();
1481 D->IdentLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001482 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001483 D->Namespace = ReadDeclAs<NamedDecl>();
Richard Smithf4634362014-09-03 23:11:22 +00001484 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001485}
1486
Sebastian Redlb3298c32010-08-18 23:56:48 +00001487void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001488 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001489 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001490 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001491 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
1492 D->FirstUsingShadow.setPointer(ReadDeclAs<UsingShadowDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001493 D->setTypename(Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001494 if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>())
Douglas Gregor4163aca2011-09-09 21:34:22 +00001495 Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
Richard Smith32952e12014-10-14 02:00:47 +00001496 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001497}
1498
Richard Smith151c4562016-12-20 21:35:28 +00001499void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) {
1500 VisitNamedDecl(D);
1501 D->InstantiatedFrom = ReadDeclAs<NamedDecl>();
1502 NamedDecl **Expansions = D->getTrailingObjects<NamedDecl*>();
1503 for (unsigned I = 0; I != D->NumExpansions; ++I)
1504 Expansions[I] = ReadDeclAs<NamedDecl>();
1505 mergeMergeable(D);
1506}
1507
Sebastian Redlb3298c32010-08-18 23:56:48 +00001508void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Richard Smithfd8634a2013-10-23 02:17:46 +00001509 RedeclarableResult Redecl = VisitRedeclarable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001510 VisitNamedDecl(D);
Richard Smitha263c342018-01-06 01:07:05 +00001511 D->Underlying = ReadDeclAs<NamedDecl>();
1512 D->IdentifierNamespace = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001513 D->UsingOrNextShadow = ReadDeclAs<NamedDecl>();
1514 UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>();
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001515 if (Pattern)
Douglas Gregor4163aca2011-09-09 21:34:22 +00001516 Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
Richard Smithfd8634a2013-10-23 02:17:46 +00001517 mergeRedeclarable(D, Redecl);
Chris Lattnerca025db2010-05-07 21:43:38 +00001518}
1519
Richard Smith5179eb72016-06-28 19:03:57 +00001520void ASTDeclReader::VisitConstructorUsingShadowDecl(
1521 ConstructorUsingShadowDecl *D) {
1522 VisitUsingShadowDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001523 D->NominatedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
1524 D->ConstructedBaseClassShadowDecl = ReadDeclAs<ConstructorUsingShadowDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001525 D->IsVirtual = Record.readInt();
Richard Smith5179eb72016-06-28 19:03:57 +00001526}
1527
Sebastian Redlb3298c32010-08-18 23:56:48 +00001528void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001529 VisitNamedDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001530 D->UsingLoc = ReadSourceLocation();
1531 D->NamespaceLoc = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001532 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001533 D->NominatedNamespace = ReadDeclAs<NamedDecl>();
1534 D->CommonAncestor = ReadDeclAs<DeclContext>();
Chris Lattnerca025db2010-05-07 21:43:38 +00001535}
1536
Sebastian Redlb3298c32010-08-18 23:56:48 +00001537void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001538 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001539 D->setUsingLoc(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00001540 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001541 ReadDeclarationNameLoc(D->DNLoc, D->getDeclName());
Richard Smith151c4562016-12-20 21:35:28 +00001542 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001543 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001544}
1545
Sebastian Redlb3298c32010-08-18 23:56:48 +00001546void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00001547 UnresolvedUsingTypenameDecl *D) {
1548 VisitTypeDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001549 D->TypenameLocation = ReadSourceLocation();
David L. Jonesb6a8f022016-12-21 04:34:52 +00001550 D->QualifierLoc = Record.readNestedNameSpecifierLoc();
Richard Smith151c4562016-12-20 21:35:28 +00001551 D->EllipsisLoc = ReadSourceLocation();
Richard Smith32952e12014-10-14 02:00:47 +00001552 mergeMergeable(D);
Chris Lattnerca025db2010-05-07 21:43:38 +00001553}
1554
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001555void ASTDeclReader::ReadCXXDefinitionData(
David Blaikie1ac9c982017-04-11 21:13:37 +00001556 struct CXXRecordDecl::DefinitionData &Data, const CXXRecordDecl *D) {
Douglas Gregor99ae8062012-02-14 17:54:36 +00001557 // Note: the caller has deserialized the IsLambda bit already.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001558 Data.UserDeclaredConstructor = Record.readInt();
1559 Data.UserDeclaredSpecialMembers = Record.readInt();
1560 Data.Aggregate = Record.readInt();
1561 Data.PlainOldData = Record.readInt();
1562 Data.Empty = Record.readInt();
1563 Data.Polymorphic = Record.readInt();
1564 Data.Abstract = Record.readInt();
1565 Data.IsStandardLayout = Record.readInt();
1566 Data.HasNoNonEmptyBases = Record.readInt();
1567 Data.HasPrivateFields = Record.readInt();
1568 Data.HasProtectedFields = Record.readInt();
1569 Data.HasPublicFields = Record.readInt();
1570 Data.HasMutableFields = Record.readInt();
1571 Data.HasVariantMembers = Record.readInt();
1572 Data.HasOnlyCMembers = Record.readInt();
1573 Data.HasInClassInitializer = Record.readInt();
1574 Data.HasUninitializedReferenceMember = Record.readInt();
1575 Data.HasUninitializedFields = Record.readInt();
1576 Data.HasInheritedConstructor = Record.readInt();
1577 Data.HasInheritedAssignment = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001578 Data.NeedOverloadResolutionForCopyConstructor = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001579 Data.NeedOverloadResolutionForMoveConstructor = Record.readInt();
1580 Data.NeedOverloadResolutionForMoveAssignment = Record.readInt();
1581 Data.NeedOverloadResolutionForDestructor = Record.readInt();
Richard Smith96cd6712017-08-16 01:49:53 +00001582 Data.DefaultedCopyConstructorIsDeleted = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001583 Data.DefaultedMoveConstructorIsDeleted = Record.readInt();
1584 Data.DefaultedMoveAssignmentIsDeleted = Record.readInt();
1585 Data.DefaultedDestructorIsDeleted = Record.readInt();
1586 Data.HasTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001587 Data.HasTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001588 Data.DeclaredNonTrivialSpecialMembers = Record.readInt();
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001589 Data.DeclaredNonTrivialSpecialMembersForCall = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001590 Data.HasIrrelevantDestructor = Record.readInt();
1591 Data.HasConstexprNonCopyMoveConstructor = Record.readInt();
1592 Data.HasDefaultedDefaultConstructor = Record.readInt();
1593 Data.DefaultedDefaultConstructorIsConstexpr = Record.readInt();
1594 Data.HasConstexprDefaultConstructor = Record.readInt();
1595 Data.HasNonLiteralTypeFieldsOrBases = Record.readInt();
1596 Data.ComputedVisibleConversions = Record.readInt();
1597 Data.UserProvidedDefaultConstructor = Record.readInt();
1598 Data.DeclaredSpecialMembers = Record.readInt();
Richard Smithdf054d32017-02-25 23:53:05 +00001599 Data.ImplicitCopyConstructorCanHaveConstParamForVBase = Record.readInt();
1600 Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase = Record.readInt();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001601 Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
1602 Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
1603 Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Richard Trieub6adf542017-02-18 02:09:28 +00001604 Data.ODRHash = Record.readInt();
Richard Trieufd1acbb2017-04-11 21:31:00 +00001605 Data.HasODRHash = true;
Anders Carlsson703d6e62011-01-22 18:11:02 +00001606
Richard Smithcd4a7a42017-09-07 00:55:55 +00001607 if (Record.readInt())
1608 Reader.DefinitionSource[D] = Loc.F->Kind == ModuleKind::MK_MainFile;
David Blaikie1ac9c982017-04-11 21:13:37 +00001609
David L. Jonesbe1557a2016-12-21 00:17:49 +00001610 Data.NumBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001611 if (Data.NumBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001612 Data.Bases = ReadGlobalOffset();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001613 Data.NumVBases = Record.readInt();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001614 if (Data.NumVBases)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001615 Data.VBases = ReadGlobalOffset();
1616
David L. Jonesb6a8f022016-12-21 04:34:52 +00001617 Record.readUnresolvedSet(Data.Conversions);
1618 Record.readUnresolvedSet(Data.VisibleConversions);
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001619 assert(Data.Definition && "Data.Definition should be already set!");
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001620 Data.FirstFriend = ReadDeclID();
Richard Smith9dd9f032013-08-30 00:23:29 +00001621
Douglas Gregor99ae8062012-02-14 17:54:36 +00001622 if (Data.IsLambda) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001623 typedef LambdaCapture Capture;
Douglas Gregor99ae8062012-02-14 17:54:36 +00001624 CXXRecordDecl::LambdaDefinitionData &Lambda
1625 = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001626 Lambda.Dependent = Record.readInt();
1627 Lambda.IsGenericLambda = Record.readInt();
1628 Lambda.CaptureDefault = Record.readInt();
1629 Lambda.NumCaptures = Record.readInt();
1630 Lambda.NumExplicitCaptures = Record.readInt();
1631 Lambda.ManglingNumber = Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001632 Lambda.ContextDecl = ReadDeclID();
Richard Smithdbafb6c2017-06-29 23:23:46 +00001633 Lambda.Captures = (Capture *)Reader.getContext().Allocate(
1634 sizeof(Capture) * Lambda.NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001635 Capture *ToCapture = Lambda.Captures;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001636 Lambda.MethodTyInfo = GetTypeSourceInfo();
Douglas Gregor99ae8062012-02-14 17:54:36 +00001637 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001638 SourceLocation Loc = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001639 bool IsImplicit = Record.readInt();
1640 LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record.readInt());
Richard Smithba71c082013-05-16 06:20:58 +00001641 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001642 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00001643 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00001644 case LCK_VLAType:
Craig Toppera13603a2014-05-22 05:54:18 +00001645 *ToCapture++ = Capture(Loc, IsImplicit, Kind, nullptr,SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00001646 break;
1647 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001648 case LCK_ByRef:
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001649 VarDecl *Var = ReadDeclAs<VarDecl>();
1650 SourceLocation EllipsisLoc = ReadSourceLocation();
Richard Smithba71c082013-05-16 06:20:58 +00001651 *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
1652 break;
1653 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00001654 }
1655 }
Argyrios Kyrtzidis8b200a52010-10-24 17:26:27 +00001656}
1657
Richard Smithcd45dbc2014-04-19 03:48:30 +00001658void ASTDeclReader::MergeDefinitionData(
Richard Smith8a639892015-01-24 01:07:20 +00001659 CXXRecordDecl *D, struct CXXRecordDecl::DefinitionData &&MergeDD) {
Richard Smithb6483992016-05-17 22:44:15 +00001660 assert(D->DefinitionData &&
Richard Smith053f6c62014-05-16 23:01:30 +00001661 "merging class definition into non-definition");
Richard Smithb6483992016-05-17 22:44:15 +00001662 auto &DD = *D->DefinitionData;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001663
Richard Smith02793752015-03-27 21:16:39 +00001664 if (DD.Definition != MergeDD.Definition) {
Richard Smith97100e32015-06-20 00:22:34 +00001665 // Track that we merged the definitions.
1666 Reader.MergedDeclContexts.insert(std::make_pair(MergeDD.Definition,
1667 DD.Definition));
1668 Reader.PendingDefinitions.erase(MergeDD.Definition);
1669 MergeDD.Definition->IsCompleteDefinition = false;
Richard Smith6561f922016-09-12 21:06:40 +00001670 Reader.mergeDefinitionVisibility(DD.Definition, MergeDD.Definition);
Richard Smithd88a7f12015-09-01 20:35:42 +00001671 assert(Reader.Lookups.find(MergeDD.Definition) == Reader.Lookups.end() &&
1672 "already loaded pending lookups for merged definition");
Richard Smith02793752015-03-27 21:16:39 +00001673 }
1674
Richard Smith2a9e5c52015-02-03 03:32:14 +00001675 auto PFDI = Reader.PendingFakeDefinitionData.find(&DD);
1676 if (PFDI != Reader.PendingFakeDefinitionData.end() &&
1677 PFDI->second == ASTReader::PendingFakeDefinitionKind::Fake) {
Richard Smith8a639892015-01-24 01:07:20 +00001678 // We faked up this definition data because we found a class for which we'd
1679 // not yet loaded the definition. Replace it with the real thing now.
Richard Smith8a639892015-01-24 01:07:20 +00001680 assert(!DD.IsLambda && !MergeDD.IsLambda && "faked up lambda definition?");
Richard Smith2a9e5c52015-02-03 03:32:14 +00001681 PFDI->second = ASTReader::PendingFakeDefinitionKind::FakeLoaded;
Richard Smith8a639892015-01-24 01:07:20 +00001682
1683 // Don't change which declaration is the definition; that is required
1684 // to be invariant once we select it.
1685 auto *Def = DD.Definition;
1686 DD = std::move(MergeDD);
1687 DD.Definition = Def;
1688 return;
1689 }
1690
Richard Smithcd45dbc2014-04-19 03:48:30 +00001691 // FIXME: Move this out into a .def file?
Richard Smithcd45dbc2014-04-19 03:48:30 +00001692 bool DetectedOdrViolation = false;
1693#define OR_FIELD(Field) DD.Field |= MergeDD.Field;
1694#define MATCH_FIELD(Field) \
1695 DetectedOdrViolation |= DD.Field != MergeDD.Field; \
1696 OR_FIELD(Field)
1697 MATCH_FIELD(UserDeclaredConstructor)
1698 MATCH_FIELD(UserDeclaredSpecialMembers)
1699 MATCH_FIELD(Aggregate)
1700 MATCH_FIELD(PlainOldData)
1701 MATCH_FIELD(Empty)
1702 MATCH_FIELD(Polymorphic)
1703 MATCH_FIELD(Abstract)
1704 MATCH_FIELD(IsStandardLayout)
1705 MATCH_FIELD(HasNoNonEmptyBases)
1706 MATCH_FIELD(HasPrivateFields)
1707 MATCH_FIELD(HasProtectedFields)
1708 MATCH_FIELD(HasPublicFields)
1709 MATCH_FIELD(HasMutableFields)
1710 MATCH_FIELD(HasVariantMembers)
1711 MATCH_FIELD(HasOnlyCMembers)
1712 MATCH_FIELD(HasInClassInitializer)
1713 MATCH_FIELD(HasUninitializedReferenceMember)
Nico Weber6a6376b2016-02-19 01:52:46 +00001714 MATCH_FIELD(HasUninitializedFields)
Richard Smith12e79312016-05-13 06:47:56 +00001715 MATCH_FIELD(HasInheritedConstructor)
1716 MATCH_FIELD(HasInheritedAssignment)
Richard Smith96cd6712017-08-16 01:49:53 +00001717 MATCH_FIELD(NeedOverloadResolutionForCopyConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001718 MATCH_FIELD(NeedOverloadResolutionForMoveConstructor)
1719 MATCH_FIELD(NeedOverloadResolutionForMoveAssignment)
1720 MATCH_FIELD(NeedOverloadResolutionForDestructor)
Richard Smith96cd6712017-08-16 01:49:53 +00001721 MATCH_FIELD(DefaultedCopyConstructorIsDeleted)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001722 MATCH_FIELD(DefaultedMoveConstructorIsDeleted)
1723 MATCH_FIELD(DefaultedMoveAssignmentIsDeleted)
1724 MATCH_FIELD(DefaultedDestructorIsDeleted)
1725 OR_FIELD(HasTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001726 OR_FIELD(HasTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001727 OR_FIELD(DeclaredNonTrivialSpecialMembers)
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001728 OR_FIELD(DeclaredNonTrivialSpecialMembersForCall)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001729 MATCH_FIELD(HasIrrelevantDestructor)
1730 OR_FIELD(HasConstexprNonCopyMoveConstructor)
Nico Weber72c57f42016-02-24 20:58:14 +00001731 OR_FIELD(HasDefaultedDefaultConstructor)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001732 MATCH_FIELD(DefaultedDefaultConstructorIsConstexpr)
1733 OR_FIELD(HasConstexprDefaultConstructor)
1734 MATCH_FIELD(HasNonLiteralTypeFieldsOrBases)
1735 // ComputedVisibleConversions is handled below.
1736 MATCH_FIELD(UserProvidedDefaultConstructor)
1737 OR_FIELD(DeclaredSpecialMembers)
Richard Smithdf054d32017-02-25 23:53:05 +00001738 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForVBase)
1739 MATCH_FIELD(ImplicitCopyConstructorCanHaveConstParamForNonVBase)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001740 MATCH_FIELD(ImplicitCopyAssignmentHasConstParam)
1741 OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
1742 OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
1743 MATCH_FIELD(IsLambda)
1744#undef OR_FIELD
1745#undef MATCH_FIELD
1746
1747 if (DD.NumBases != MergeDD.NumBases || DD.NumVBases != MergeDD.NumVBases)
1748 DetectedOdrViolation = true;
1749 // FIXME: Issue a diagnostic if the base classes don't match when we come
1750 // to lazily load them.
1751
1752 // FIXME: Issue a diagnostic if the list of conversion functions doesn't
1753 // match when we come to lazily load them.
1754 if (MergeDD.ComputedVisibleConversions && !DD.ComputedVisibleConversions) {
1755 DD.VisibleConversions = std::move(MergeDD.VisibleConversions);
1756 DD.ComputedVisibleConversions = true;
1757 }
1758
1759 // FIXME: Issue a diagnostic if FirstFriend doesn't match when we come to
1760 // lazily load it.
1761
1762 if (DD.IsLambda) {
1763 // FIXME: ODR-checking for merging lambdas (this happens, for instance,
1764 // when they occur within the body of a function template specialization).
1765 }
1766
Richard Trieufd1acbb2017-04-11 21:31:00 +00001767 if (D->getODRHash() != MergeDD.ODRHash) {
1768 DetectedOdrViolation = true;
1769 }
1770
Richard Smithcd45dbc2014-04-19 03:48:30 +00001771 if (DetectedOdrViolation)
Richard Trieue13eabe2017-09-30 02:19:17 +00001772 Reader.PendingOdrMergeFailures[DD.Definition].push_back(
1773 {MergeDD.Definition, &MergeDD});
Richard Smithcd45dbc2014-04-19 03:48:30 +00001774}
1775
Richard Smith2a9e5c52015-02-03 03:32:14 +00001776void ASTDeclReader::ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00001777 struct CXXRecordDecl::DefinitionData *DD;
1778 ASTContext &C = Reader.getContext();
1779
1780 // Determine whether this is a lambda closure type, so that we can
1781 // allocate the appropriate DefinitionData structure.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001782 bool IsLambda = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001783 if (IsLambda)
Craig Toppera13603a2014-05-22 05:54:18 +00001784 DD = new (C) CXXRecordDecl::LambdaDefinitionData(D, nullptr, false, false,
Richard Smithcd45dbc2014-04-19 03:48:30 +00001785 LCD_None);
1786 else
1787 DD = new (C) struct CXXRecordDecl::DefinitionData(D);
1788
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001789 CXXRecordDecl *Canon = D->getCanonicalDecl();
1790 // Set decl definition data before reading it, so that during deserialization
1791 // when we read CXXRecordDecl, it already has definition data and we don't
1792 // set fake one.
1793 if (!Canon->DefinitionData)
1794 Canon->DefinitionData = DD;
1795 D->DefinitionData = Canon->DefinitionData;
David Blaikie1ac9c982017-04-11 21:13:37 +00001796 ReadCXXDefinitionData(*DD, D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001797
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001798 // We might already have a different definition for this record. This can
1799 // happen either because we're reading an update record, or because we've
1800 // already done some merging. Either way, just merge into it.
1801 if (Canon->DefinitionData != DD) {
Richard Smith8a639892015-01-24 01:07:20 +00001802 MergeDefinitionData(Canon, std::move(*DD));
Richard Smithcd45dbc2014-04-19 03:48:30 +00001803 return;
1804 }
1805
Richard Smith97100e32015-06-20 00:22:34 +00001806 // Mark this declaration as being a definition.
1807 D->IsCompleteDefinition = true;
Richard Smith2a9e5c52015-02-03 03:32:14 +00001808
Richard Smith97100e32015-06-20 00:22:34 +00001809 // If this is not the first declaration or is an update record, we can have
1810 // other redeclarations already. Make a note that we need to propagate the
1811 // DefinitionData pointer onto them.
Volodymyr Sapsai8a5943f2018-03-21 21:28:54 +00001812 if (Update || Canon != D)
Richard Smithcd45dbc2014-04-19 03:48:30 +00001813 Reader.PendingDefinitions.insert(D);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001814}
1815
Richard Smith1d209d02013-05-23 01:49:11 +00001816ASTDeclReader::RedeclarableResult
1817ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) {
1818 RedeclarableResult Redecl = VisitRecordDeclImpl(D);
Argyrios Kyrtzidisa41f6602010-10-20 00:11:15 +00001819
Douglas Gregor3a5ae562012-01-15 18:17:48 +00001820 ASTContext &C = Reader.getContext();
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001821
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001822 enum CXXRecKind {
1823 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1824 };
David L. Jonesbe1557a2016-12-21 00:17:49 +00001825 switch ((CXXRecKind)Record.readInt()) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001826 case CXXRecNotTemplate:
Richard Smithdc5523d2014-07-11 00:20:06 +00001827 // Merged when we merge the folding set entry in the primary template.
1828 if (!isa<ClassTemplateSpecializationDecl>(D))
1829 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001830 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001831 case CXXRecTemplate: {
1832 // Merged when we merge the template.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001833 ClassTemplateDecl *Template = ReadDeclAs<ClassTemplateDecl>();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001834 D->TemplateOrInstantiation = Template;
1835 if (!Template->getTemplatedDecl()) {
1836 // We've not actually loaded the ClassTemplateDecl yet, because we're
1837 // currently being loaded as its pattern. Rely on it to set up our
1838 // TypeForDecl (see VisitClassTemplateDecl).
1839 //
1840 // Beware: we do not yet know our canonical declaration, and may still
1841 // get merged once the surrounding class template has got off the ground.
1842 TypeIDForTypeDecl = 0;
1843 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001844 break;
Richard Smithf17fdbd2014-04-24 02:25:27 +00001845 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001846 case CXXRecMemberSpecialization: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001847 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001848 TemplateSpecializationKind TSK =
1849 (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001850 SourceLocation POI = ReadSourceLocation();
Argyrios Kyrtzidiscaf82482010-09-13 11:45:25 +00001851 MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
1852 MSI->setPointOfInstantiation(POI);
1853 D->TemplateOrInstantiation = MSI;
Richard Smithcd45dbc2014-04-19 03:48:30 +00001854 mergeRedeclarable(D, Redecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001855 break;
1856 }
1857 }
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001858
David L. Jonesbe1557a2016-12-21 00:17:49 +00001859 bool WasDefinition = Record.readInt();
Richard Smithcd45dbc2014-04-19 03:48:30 +00001860 if (WasDefinition)
Richard Smith2a9e5c52015-02-03 03:32:14 +00001861 ReadCXXRecordDefinition(D, /*Update*/false);
Richard Smithcd45dbc2014-04-19 03:48:30 +00001862 else
1863 // Propagate DefinitionData pointer from the canonical declaration.
1864 D->DefinitionData = D->getCanonicalDecl()->DefinitionData;
1865
Richard Smith676c4042013-08-29 23:59:27 +00001866 // Lazily load the key function to avoid deserializing every method so we can
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001867 // compute it.
Richard Smithd55889a2013-09-09 16:55:27 +00001868 if (WasDefinition) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001869 DeclID KeyFn = ReadDeclID();
Richard Smithd55889a2013-09-09 16:55:27 +00001870 if (KeyFn && D->IsCompleteDefinition)
Richard Smitha9a1c682014-07-07 06:38:20 +00001871 // FIXME: This is wrong for the ARM ABI, where some other module may have
1872 // made this function no longer be a key function. We need an update
1873 // record or similar for that case.
Richard Smith676c4042013-08-29 23:59:27 +00001874 C.KeyFunctions[D] = KeyFn;
Argyrios Kyrtzidis68431412010-10-14 20:14:38 +00001875 }
Richard Smith1d209d02013-05-23 01:49:11 +00001876
1877 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00001878}
1879
Richard Smithbc491202017-02-17 20:05:37 +00001880void ASTDeclReader::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
1881 VisitFunctionDecl(D);
Faisal Vali1f3a2af2017-11-11 18:02:29 +00001882 D->IsCopyDeductionCandidate = Record.readInt();
Richard Smithbc491202017-02-17 20:05:37 +00001883}
1884
Sebastian Redlb3298c32010-08-18 23:56:48 +00001885void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001886 VisitFunctionDecl(D);
Richard Smithb1108732014-08-26 23:29:11 +00001887
David L. Jonesbe1557a2016-12-21 00:17:49 +00001888 unsigned NumOverridenMethods = Record.readInt();
Richard Smithb1108732014-08-26 23:29:11 +00001889 if (D->isCanonicalDecl()) {
1890 while (NumOverridenMethods--) {
1891 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
1892 // MD may be initializing.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001893 if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>())
Richard Smithb1108732014-08-26 23:29:11 +00001894 Reader.getContext().addOverriddenMethod(D, MD->getCanonicalDecl());
1895 }
1896 } else {
1897 // We don't care about which declarations this used to override; we get
1898 // the relevant information from the canonical declaration.
David L. Jonesbe1557a2016-12-21 00:17:49 +00001899 Record.skipInts(NumOverridenMethods);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001900 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001901}
1902
Sebastian Redlb3298c32010-08-18 23:56:48 +00001903void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +00001904 // We need the inherited constructor information to merge the declaration,
1905 // so we have to read it before we call VisitCXXMethodDecl.
1906 if (D->isInheritingConstructor()) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001907 auto *Shadow = ReadDeclAs<ConstructorUsingShadowDecl>();
1908 auto *Ctor = ReadDeclAs<CXXConstructorDecl>();
Richard Smith5179eb72016-06-28 19:03:57 +00001909 *D->getTrailingObjects<InheritedConstructor>() =
1910 InheritedConstructor(Shadow, Ctor);
1911 }
1912
Chris Lattnerca025db2010-05-07 21:43:38 +00001913 VisitCXXMethodDecl(D);
1914}
1915
Sebastian Redlb3298c32010-08-18 23:56:48 +00001916void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001917 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +00001918
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001919 if (auto *OperatorDelete = ReadDeclAs<FunctionDecl>()) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00001920 CXXDestructorDecl *Canon = D->getCanonicalDecl();
Richard Smith5b349582017-10-13 01:55:36 +00001921 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00001922 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00001923 if (!Canon->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00001924 Canon->OperatorDelete = OperatorDelete;
Richard Smith5b349582017-10-13 01:55:36 +00001925 Canon->OperatorDeleteThisArg = ThisArg;
1926 }
Richard Smithf8134002015-03-10 01:41:22 +00001927 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001928}
1929
Sebastian Redlb3298c32010-08-18 23:56:48 +00001930void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +00001931 VisitCXXMethodDecl(D);
1932}
1933
Douglas Gregorba345522011-12-02 23:23:56 +00001934void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
1935 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001936 D->ImportedAndComplete.setPointer(readModule());
David L. Jonesbe1557a2016-12-21 00:17:49 +00001937 D->ImportedAndComplete.setInt(Record.readInt());
James Y Knight967eb202015-12-29 22:13:13 +00001938 SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
Douglas Gregorba345522011-12-02 23:23:56 +00001939 for (unsigned I = 0, N = Record.back(); I != N; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001940 StoredLocs[I] = ReadSourceLocation();
David L. Jones267b8842017-01-24 01:04:30 +00001941 Record.skipInts(1); // The number of stored source locations.
Douglas Gregorba345522011-12-02 23:23:56 +00001942}
1943
Sebastian Redlb3298c32010-08-18 23:56:48 +00001944void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
Abramo Bagnarad7340582010-06-05 05:09:32 +00001945 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001946 D->setColonLoc(ReadSourceLocation());
Abramo Bagnarad7340582010-06-05 05:09:32 +00001947}
1948
Sebastian Redlb3298c32010-08-18 23:56:48 +00001949void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00001950 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001951 if (Record.readInt()) // hasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001952 D->Friend = ReadDeclAs<NamedDecl>();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001953 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001954 D->Friend = GetTypeSourceInfo();
Enea Zaffanellaeb22c872013-01-31 09:54:08 +00001955 for (unsigned i = 0; i != D->NumTPLists; ++i)
James Y Knight967eb202015-12-29 22:13:13 +00001956 D->getTrailingObjects<TemplateParameterList *>()[i] =
David L. Jonesb6a8f022016-12-21 04:34:52 +00001957 Record.readTemplateParameterList();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001958 D->NextFriend = ReadDeclID();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001959 D->UnsupportedFriend = (Record.readInt() != 0);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001960 D->FriendLoc = ReadSourceLocation();
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001961}
1962
Sebastian Redlb3298c32010-08-18 23:56:48 +00001963void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001964 VisitDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00001965 unsigned NumParams = Record.readInt();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001966 D->NumParams = NumParams;
1967 D->Params = new TemplateParameterList*[NumParams];
1968 for (unsigned i = 0; i != NumParams; ++i)
David L. Jonesb6a8f022016-12-21 04:34:52 +00001969 D->Params[i] = Record.readTemplateParameterList();
David L. Jonesbe1557a2016-12-21 00:17:49 +00001970 if (Record.readInt()) // HasFriendDecl
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001971 D->Friend = ReadDeclAs<NamedDecl>();
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001972 else
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001973 D->Friend = GetTypeSourceInfo();
1974 D->FriendLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00001975}
1976
Richard Smithf17fdbd2014-04-24 02:25:27 +00001977DeclID ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001978 VisitNamedDecl(D);
1979
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001980 DeclID PatternID = ReadDeclID();
Richard Smithf17fdbd2014-04-24 02:25:27 +00001981 NamedDecl *TemplatedDecl = cast_or_null<NamedDecl>(Reader.GetDecl(PatternID));
David L. Jonesb6a8f022016-12-21 04:34:52 +00001982 TemplateParameterList *TemplateParams = Record.readTemplateParameterList();
Hubert Tong5a8ec4e2017-02-10 02:46:19 +00001983 // FIXME handle associated constraints
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001984 D->init(TemplatedDecl, TemplateParams);
Richard Smithbf78e642013-06-24 22:51:00 +00001985
Richard Smithf17fdbd2014-04-24 02:25:27 +00001986 return PatternID;
Chris Lattnerca025db2010-05-07 21:43:38 +00001987}
1988
David L. Jonesc4808b9e2016-12-15 20:53:26 +00001989ASTDeclReader::RedeclarableResult
Douglas Gregor54079202012-01-06 22:05:37 +00001990ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00001991 RedeclarableResult Redecl = VisitRedeclarable(D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001992
Douglas Gregor68444de2012-01-14 15:13:49 +00001993 // Make sure we've allocated the Common pointer first. We do this before
1994 // VisitTemplateDecl so that getCommonPtr() can be used during initialization.
1995 RedeclarableTemplateDecl *CanonD = D->getCanonicalDecl();
1996 if (!CanonD->Common) {
1997 CanonD->Common = CanonD->newCommon(Reader.getContext());
1998 Reader.PendingDefinitions.insert(CanonD);
1999 }
2000 D->Common = CanonD->Common;
Douglas Gregor022857e2011-12-22 01:48:48 +00002001
Douglas Gregor68444de2012-01-14 15:13:49 +00002002 // If this is the first declaration of the template, fill in the information
2003 // for the 'common' pointer.
2004 if (ThisDeclID == Redecl.getFirstID()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002005 if (RedeclarableTemplateDecl *RTD
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002006 = ReadDeclAs<RedeclarableTemplateDecl>()) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002007 assert(RTD->getKind() == D->getKind() &&
2008 "InstantiatedFromMemberTemplate kind mismatch");
Douglas Gregor68444de2012-01-14 15:13:49 +00002009 D->setInstantiatedFromMemberTemplate(RTD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002010 if (Record.readInt())
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002011 D->setMemberSpecialization();
2012 }
2013 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002014
Richard Smithf17fdbd2014-04-24 02:25:27 +00002015 DeclID PatternID = VisitTemplateDecl(D);
David L. Jonesbe1557a2016-12-21 00:17:49 +00002016 D->IdentifierNamespace = Record.readInt();
Axel Naumann866ba3e2012-10-01 09:18:00 +00002017
Richard Smithf17fdbd2014-04-24 02:25:27 +00002018 mergeRedeclarable(D, Redecl, PatternID);
Axel Naumann866ba3e2012-10-01 09:18:00 +00002019
Richard Smithd46d6de2013-10-13 23:50:45 +00002020 // If we merged the template with a prior declaration chain, merge the common
2021 // pointer.
2022 // FIXME: Actually merge here, don't just overwrite.
2023 D->Common = D->getCanonicalDecl()->Common;
2024
Douglas Gregor68444de2012-01-14 15:13:49 +00002025 return Redecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002026}
2027
Sebastian Redlb3298c32010-08-18 23:56:48 +00002028void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002029 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +00002030
Douglas Gregor68444de2012-01-14 15:13:49 +00002031 if (ThisDeclID == Redecl.getFirstID()) {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +00002032 // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
2033 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002034 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002035 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002036 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002037 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002038
2039 if (D->getTemplatedDecl()->TemplateOrInstantiation) {
2040 // We were loaded before our templated declaration was. We've not set up
2041 // its corresponding type yet (see VisitCXXRecordDeclImpl), so reconstruct
2042 // it now.
Richard Smithdbafb6c2017-06-29 23:23:46 +00002043 Reader.getContext().getInjectedClassNameType(
Richard Smitha1406fa2014-05-23 21:31:59 +00002044 D->getTemplatedDecl(), D->getInjectedClassNameSpecialization());
Richard Smithf17fdbd2014-04-24 02:25:27 +00002045 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002046}
2047
David Majnemerd9b1a4f2015-11-04 03:40:30 +00002048void ASTDeclReader::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) {
2049 llvm_unreachable("BuiltinTemplates are not serialized");
2050}
2051
Larisse Voufo30616382013-08-23 22:21:36 +00002052/// TODO: Unify with ClassTemplateDecl version?
2053/// May require unifying ClassTemplateDecl and
2054/// VarTemplateDecl beyond TemplateDecl...
Larisse Voufo39a1e502013-08-06 01:03:05 +00002055void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) {
2056 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
2057
2058 if (ThisDeclID == Redecl.getFirstID()) {
Larisse Voufod8dd97c2013-08-14 03:09:19 +00002059 // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of
Larisse Voufo39a1e502013-08-06 01:03:05 +00002060 // the specializations.
Richard Smith0b884372015-02-27 00:25:58 +00002061 SmallVector<serialization::DeclID, 32> SpecIDs;
Richard Smith0b884372015-02-27 00:25:58 +00002062 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002063 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002064 }
2065}
2066
Richard Smith1d209d02013-05-23 01:49:11 +00002067ASTDeclReader::RedeclarableResult
2068ASTDeclReader::VisitClassTemplateSpecializationDeclImpl(
2069 ClassTemplateSpecializationDecl *D) {
2070 RedeclarableResult Redecl = VisitCXXRecordDeclImpl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002071
Douglas Gregor4163aca2011-09-09 21:34:22 +00002072 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002073 if (Decl *InstD = ReadDecl()) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002074 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002075 D->SpecializedTemplate = CTD;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002076 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002077 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002078 Record.readTemplateArgumentList(TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002079 TemplateArgumentList *ArgList
David Majnemer8b622692016-07-03 21:17:51 +00002080 = TemplateArgumentList::CreateCopy(C, TemplArgs);
Argyrios Kyrtzidisb4c659b2010-09-13 11:45:32 +00002081 ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
2082 = new (C) ClassTemplateSpecializationDecl::
2083 SpecializedPartialSpecialization();
2084 PS->PartialSpecialization
2085 = cast<ClassTemplatePartialSpecializationDecl>(InstD);
2086 PS->TemplateArgs = ArgList;
2087 D->SpecializedTemplate = PS;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002088 }
2089 }
2090
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002091 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002092 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002093 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002094 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002095 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002096
David L. Jonesbe1557a2016-12-21 00:17:49 +00002097 bool writtenAsCanonicalDecl = Record.readInt();
Axel Naumanna31dee22012-10-01 07:34:47 +00002098 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002099 ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>();
Axel Naumanna31dee22012-10-01 07:34:47 +00002100 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smithd55889a2013-09-09 16:55:27 +00002101 // Set this as, or find, the canonical declaration for this specialization
2102 ClassTemplateSpecializationDecl *CanonSpec;
Richard Smith5de91b52013-06-25 01:25:15 +00002103 if (ClassTemplatePartialSpecializationDecl *Partial =
2104 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002105 CanonSpec = CanonPattern->getCommonPtr()->PartialSpecializations
Richard Smith5de91b52013-06-25 01:25:15 +00002106 .GetOrInsertNode(Partial);
Axel Naumanna31dee22012-10-01 07:34:47 +00002107 } else {
Richard Smithd55889a2013-09-09 16:55:27 +00002108 CanonSpec =
2109 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2110 }
2111 // If there was already a canonical specialization, merge into it.
2112 if (CanonSpec != D) {
2113 mergeRedeclarable<TagDecl>(D, CanonSpec, Redecl);
2114
2115 // This declaration might be a definition. Merge with any existing
2116 // definition.
Richard Smithb6483992016-05-17 22:44:15 +00002117 if (auto *DDD = D->DefinitionData) {
2118 if (CanonSpec->DefinitionData)
Richard Smith8a639892015-01-24 01:07:20 +00002119 MergeDefinitionData(CanonSpec, std::move(*DDD));
Richard Smith97100e32015-06-20 00:22:34 +00002120 else
Richard Smith053f6c62014-05-16 23:01:30 +00002121 CanonSpec->DefinitionData = D->DefinitionData;
Richard Smithd55889a2013-09-09 16:55:27 +00002122 }
Richard Smith547864d2014-07-11 18:22:58 +00002123 D->DefinitionData = CanonSpec->DefinitionData;
Axel Naumanna31dee22012-10-01 07:34:47 +00002124 }
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00002125 }
2126 }
Richard Smith1d209d02013-05-23 01:49:11 +00002127
Richard Smithd55889a2013-09-09 16:55:27 +00002128 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002129 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Richard Smithd55889a2013-09-09 16:55:27 +00002130 ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
2131 = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
2132 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002133 ExplicitInfo->ExternLoc = ReadSourceLocation();
2134 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Richard Smithd55889a2013-09-09 16:55:27 +00002135 D->ExplicitInfo = ExplicitInfo;
2136 }
2137
Richard Smith1d209d02013-05-23 01:49:11 +00002138 return Redecl;
Chris Lattnerca025db2010-05-07 21:43:38 +00002139}
2140
Sebastian Redlb3298c32010-08-18 23:56:48 +00002141void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +00002142 ClassTemplatePartialSpecializationDecl *D) {
Richard Smith1d209d02013-05-23 01:49:11 +00002143 RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002144
David L. Jonesb6a8f022016-12-21 04:34:52 +00002145 D->TemplateParams = Record.readTemplateParameterList();
2146 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002147
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002148 // These are read/set from/to the first declaration.
Richard Smith1d209d02013-05-23 01:49:11 +00002149 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis0e8b3ce2010-09-13 11:45:41 +00002150 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002151 ReadDeclAs<ClassTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002152 D->InstantiatedFromMember.setInt(Record.readInt());
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002153 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002154}
2155
Sebastian Redlb7448632011-08-31 13:59:56 +00002156void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
2157 ClassScopeFunctionSpecializationDecl *D) {
Francois Pichet09af8c32011-08-17 01:06:54 +00002158 VisitDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002159 D->Specialization = ReadDeclAs<CXXMethodDecl>();
Francois Pichet09af8c32011-08-17 01:06:54 +00002160}
2161
Sebastian Redlb3298c32010-08-18 23:56:48 +00002162void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor68444de2012-01-14 15:13:49 +00002163 RedeclarableResult Redecl = VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002164
Douglas Gregor68444de2012-01-14 15:13:49 +00002165 if (ThisDeclID == Redecl.getFirstID()) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002166 // This FunctionTemplateDecl owns a CommonPtr; read it.
Richard Smith0b884372015-02-27 00:25:58 +00002167 SmallVector<serialization::DeclID, 32> SpecIDs;
2168 ReadDeclIDList(SpecIDs);
Vassil Vassilev7d264622017-06-30 22:40:17 +00002169 ASTDeclReader::AddLazySpecializations(D, SpecIDs);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00002170 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002171}
2172
Larisse Voufo30616382013-08-23 22:21:36 +00002173/// TODO: Unify with ClassTemplateSpecializationDecl version?
2174/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2175/// VarTemplate(Partial)SpecializationDecl with a new data
2176/// structure Template(Partial)SpecializationDecl, and
2177/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002178ASTDeclReader::RedeclarableResult
2179ASTDeclReader::VisitVarTemplateSpecializationDeclImpl(
2180 VarTemplateSpecializationDecl *D) {
2181 RedeclarableResult Redecl = VisitVarDeclImpl(D);
2182
2183 ASTContext &C = Reader.getContext();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002184 if (Decl *InstD = ReadDecl()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002185 if (VarTemplateDecl *VTD = dyn_cast<VarTemplateDecl>(InstD)) {
2186 D->SpecializedTemplate = VTD;
2187 } else {
2188 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002189 Record.readTemplateArgumentList(TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002190 TemplateArgumentList *ArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00002191 C, TemplArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002192 VarTemplateSpecializationDecl::SpecializedPartialSpecialization *PS =
2193 new (C)
2194 VarTemplateSpecializationDecl::SpecializedPartialSpecialization();
2195 PS->PartialSpecialization =
2196 cast<VarTemplatePartialSpecializationDecl>(InstD);
2197 PS->TemplateArgs = ArgList;
2198 D->SpecializedTemplate = PS;
2199 }
2200 }
2201
2202 // Explicit info.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002203 if (TypeSourceInfo *TyInfo = GetTypeSourceInfo()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002204 VarTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo =
2205 new (C) VarTemplateSpecializationDecl::ExplicitSpecializationInfo;
2206 ExplicitInfo->TypeAsWritten = TyInfo;
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002207 ExplicitInfo->ExternLoc = ReadSourceLocation();
2208 ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002209 D->ExplicitInfo = ExplicitInfo;
2210 }
2211
2212 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00002213 Record.readTemplateArgumentList(TemplArgs, /*Canonicalize*/ true);
David Majnemer8b622692016-07-03 21:17:51 +00002214 D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002215 D->PointOfInstantiation = ReadSourceLocation();
David L. Jonesbe1557a2016-12-21 00:17:49 +00002216 D->SpecializationKind = (TemplateSpecializationKind)Record.readInt();
Richard Smith435e6472017-12-02 02:48:42 +00002217 D->IsCompleteDefinition = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002218
David L. Jonesbe1557a2016-12-21 00:17:49 +00002219 bool writtenAsCanonicalDecl = Record.readInt();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002220 if (writtenAsCanonicalDecl) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002221 VarTemplateDecl *CanonPattern = ReadDeclAs<VarTemplateDecl>();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002222 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Richard Smith9b88a4c2015-07-27 05:40:23 +00002223 // FIXME: If it's already present, merge it.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002224 if (VarTemplatePartialSpecializationDecl *Partial =
2225 dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002226 CanonPattern->getCommonPtr()->PartialSpecializations
2227 .GetOrInsertNode(Partial);
2228 } else {
2229 CanonPattern->getCommonPtr()->Specializations.GetOrInsertNode(D);
2230 }
2231 }
2232 }
2233
2234 return Redecl;
2235}
2236
Larisse Voufo30616382013-08-23 22:21:36 +00002237/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2238/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2239/// VarTemplate(Partial)SpecializationDecl with a new data
2240/// structure Template(Partial)SpecializationDecl, and
2241/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002242void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
2243 VarTemplatePartialSpecializationDecl *D) {
2244 RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
2245
David L. Jonesb6a8f022016-12-21 04:34:52 +00002246 D->TemplateParams = Record.readTemplateParameterList();
2247 D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002248
2249 // These are read/set from/to the first declaration.
2250 if (ThisDeclID == Redecl.getFirstID()) {
2251 D->InstantiatedFromMember.setPointer(
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002252 ReadDeclAs<VarTemplatePartialSpecializationDecl>());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002253 D->InstantiatedFromMember.setInt(Record.readInt());
Larisse Voufo39a1e502013-08-06 01:03:05 +00002254 }
2255}
2256
Sebastian Redlb3298c32010-08-18 23:56:48 +00002257void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002258 VisitTypeDecl(D);
2259
David L. Jonesbe1557a2016-12-21 00:17:49 +00002260 D->setDeclaredWithTypename(Record.readInt());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002261
David L. Jonesbe1557a2016-12-21 00:17:49 +00002262 if (Record.readInt())
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002263 D->setDefaultArgument(GetTypeSourceInfo());
Chris Lattnerca025db2010-05-07 21:43:38 +00002264}
2265
Sebastian Redlb3298c32010-08-18 23:56:48 +00002266void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
John McCallf4cd4f92011-02-09 01:13:10 +00002267 VisitDeclaratorDecl(D);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00002268 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002269 D->setDepth(Record.readInt());
2270 D->setPosition(Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002271 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002272 auto TypesAndInfos =
2273 D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002274 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00002275 new (&TypesAndInfos[I].first) QualType(Record.readType());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002276 TypesAndInfos[I].second = GetTypeSourceInfo();
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002277 }
2278 } else {
2279 // Rest of NonTypeTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002280 D->ParameterPack = Record.readInt();
2281 if (Record.readInt())
David L. Jonesb6a8f022016-12-21 04:34:52 +00002282 D->setDefaultArgument(Record.readExpr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002283 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002284}
2285
Sebastian Redlb3298c32010-08-18 23:56:48 +00002286void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00002287 VisitTemplateDecl(D);
2288 // TemplateParmPosition.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002289 D->setDepth(Record.readInt());
2290 D->setPosition(Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002291 if (D->isExpandedParameterPack()) {
James Y Knight967eb202015-12-29 22:13:13 +00002292 TemplateParameterList **Data =
2293 D->getTrailingObjects<TemplateParameterList *>();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002294 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2295 I != N; ++I)
David L. Jonesb6a8f022016-12-21 04:34:52 +00002296 Data[I] = Record.readTemplateParameterList();
Richard Smith1fde8ec2012-09-07 02:06:42 +00002297 } else {
2298 // Rest of TemplateTemplateParmDecl.
David L. Jonesbe1557a2016-12-21 00:17:49 +00002299 D->ParameterPack = Record.readInt();
2300 if (Record.readInt())
Richard Smith1469b912015-06-10 00:29:03 +00002301 D->setDefaultArgument(Reader.getContext(),
David L. Jonesb6a8f022016-12-21 04:34:52 +00002302 Record.readTemplateArgumentLoc());
Richard Smith1fde8ec2012-09-07 02:06:42 +00002303 }
Chris Lattnerca025db2010-05-07 21:43:38 +00002304}
2305
Richard Smith3f1b5d02011-05-05 21:57:07 +00002306void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
2307 VisitRedeclarableTemplateDecl(D);
2308}
2309
Sebastian Redlb3298c32010-08-18 23:56:48 +00002310void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00002311 VisitDecl(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00002312 D->AssertExprAndFailed.setPointer(Record.readExpr());
David L. Jonesbe1557a2016-12-21 00:17:49 +00002313 D->AssertExprAndFailed.setInt(Record.readInt());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002314 D->Message = cast_or_null<StringLiteral>(Record.readExpr());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002315 D->RParenLoc = ReadSourceLocation();
Chris Lattnerca025db2010-05-07 21:43:38 +00002316}
2317
Michael Han84324352013-02-22 17:15:32 +00002318void ASTDeclReader::VisitEmptyDecl(EmptyDecl *D) {
2319 VisitDecl(D);
2320}
2321
Mike Stump11289f42009-09-09 15:08:12 +00002322std::pair<uint64_t, uint64_t>
Sebastian Redlb3298c32010-08-18 23:56:48 +00002323ASTDeclReader::VisitDeclContext(DeclContext *DC) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002324 uint64_t LexicalOffset = ReadLocalOffset();
2325 uint64_t VisibleOffset = ReadLocalOffset();
Chris Lattner487412d2009-04-27 05:27:42 +00002326 return std::make_pair(LexicalOffset, VisibleOffset);
2327}
2328
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002329template <typename T>
Richard Smith5a4737c2015-02-06 02:42:59 +00002330ASTDeclReader::RedeclarableResult
Douglas Gregor022857e2011-12-22 01:48:48 +00002331ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002332 DeclID FirstDeclID = ReadDeclID();
Richard Smith5a4737c2015-02-06 02:42:59 +00002333 Decl *MergeWith = nullptr;
Richard Smithd8a83712015-08-22 01:47:18 +00002334
Richard Smith5fc18a92015-07-12 23:43:21 +00002335 bool IsKeyDecl = ThisDeclID == FirstDeclID;
Richard Smithd8a83712015-08-22 01:47:18 +00002336 bool IsFirstLocalDecl = false;
Richard Smith5a4737c2015-02-06 02:42:59 +00002337
Richard Smithd61d4ac2015-08-22 20:13:39 +00002338 uint64_t RedeclOffset = 0;
2339
Douglas Gregor358cd442012-01-15 16:58:34 +00002340 // 0 indicates that this declaration was the only declaration of its entity,
2341 // and is used for space optimization.
Richard Smith5fc18a92015-07-12 23:43:21 +00002342 if (FirstDeclID == 0) {
Douglas Gregor074a4092011-12-19 18:19:24 +00002343 FirstDeclID = ThisDeclID;
Richard Smith5fc18a92015-07-12 23:43:21 +00002344 IsKeyDecl = true;
Richard Smithd8a83712015-08-22 01:47:18 +00002345 IsFirstLocalDecl = true;
David L. Jonesbe1557a2016-12-21 00:17:49 +00002346 } else if (unsigned N = Record.readInt()) {
Richard Smithd8a83712015-08-22 01:47:18 +00002347 // This declaration was the first local declaration, but may have imported
2348 // other declarations.
2349 IsKeyDecl = N == 1;
2350 IsFirstLocalDecl = true;
Richard Smith5fc18a92015-07-12 23:43:21 +00002351
Richard Smithfe620d22015-03-05 23:24:12 +00002352 // We have some declarations that must be before us in our redeclaration
2353 // chain. Read them now, and remember that we ought to merge with one of
2354 // them.
2355 // FIXME: Provide a known merge target to the second and subsequent such
2356 // declaration.
Richard Smithd8a83712015-08-22 01:47:18 +00002357 for (unsigned I = 0; I != N - 1; ++I)
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002358 MergeWith = ReadDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00002359
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002360 RedeclOffset = ReadLocalOffset();
Richard Smithd8a83712015-08-22 01:47:18 +00002361 } else {
2362 // This declaration was not the first local declaration. Read the first
2363 // local declaration now, to trigger the import of other redeclarations.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002364 (void)ReadDecl();
Richard Smith5a4737c2015-02-06 02:42:59 +00002365 }
2366
Douglas Gregor358cd442012-01-15 16:58:34 +00002367 T *FirstDecl = cast_or_null<T>(Reader.GetDecl(FirstDeclID));
2368 if (FirstDecl != D) {
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00002369 // We delay loading of the redeclaration chain to avoid deeply nested calls.
2370 // We temporarily set the first (canonical) declaration as the previous one
2371 // which is the one that matters and mark the real previous DeclID to be
2372 // loaded & attached later on.
David Blaikie7e64fd92012-05-28 19:38:42 +00002373 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(FirstDecl);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002374 D->First = FirstDecl->getCanonicalDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002375 }
Richard Smithd8a83712015-08-22 01:47:18 +00002376
Richard Smithd8a83712015-08-22 01:47:18 +00002377 T *DAsT = static_cast<T*>(D);
Richard Smithd8a83712015-08-22 01:47:18 +00002378
2379 // Note that we need to load local redeclarations of this decl and build a
2380 // decl chain for them. This must happen *after* we perform the preloading
2381 // above; this ensures that the redeclaration chain is built in the correct
2382 // order.
2383 if (IsFirstLocalDecl)
Richard Smithd61d4ac2015-08-22 20:13:39 +00002384 Reader.PendingDeclChains.push_back(std::make_pair(DAsT, RedeclOffset));
Richard Smithd8a83712015-08-22 01:47:18 +00002385
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002386 return RedeclarableResult(MergeWith, FirstDeclID, IsKeyDecl);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002387}
2388
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002389/// \brief Attempts to merge the given declaration (D) with another declaration
2390/// of the same entity.
2391template<typename T>
Richard Smith5e9e56a2014-07-15 03:37:06 +00002392void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase,
Richard Smithf17fdbd2014-04-24 02:25:27 +00002393 RedeclarableResult &Redecl,
2394 DeclID TemplatePatternID) {
Douglas Gregore097d4b2012-01-03 17:31:38 +00002395 // If modules are not available, there is no reason to perform this merge.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002396 if (!Reader.getContext().getLangOpts().Modules)
Douglas Gregore097d4b2012-01-03 17:31:38 +00002397 return;
Richard Smithd55889a2013-09-09 16:55:27 +00002398
Richard Smith202850a2015-03-10 02:57:50 +00002399 // If we're not the canonical declaration, we don't need to merge.
2400 if (!DBase->isFirstDecl())
2401 return;
2402
Vassil Vassilev916d8be2016-10-06 13:18:06 +00002403 T *D = static_cast<T*>(DBase);
2404
Richard Smith5a4737c2015-02-06 02:42:59 +00002405 if (auto *Existing = Redecl.getKnownMergeTarget())
2406 // We already know of an existing declaration we should merge with.
2407 mergeRedeclarable(D, cast<T>(Existing), Redecl, TemplatePatternID);
2408 else if (FindExistingResult ExistingRes = findExisting(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002409 if (T *Existing = ExistingRes)
Richard Smithf17fdbd2014-04-24 02:25:27 +00002410 mergeRedeclarable(D, Existing, Redecl, TemplatePatternID);
2411}
2412
2413/// \brief "Cast" to type T, asserting if we don't have an implicit conversion.
2414/// We use this to put code in a template that will only be valid for certain
2415/// instantiations.
2416template<typename T> static T assert_cast(T t) { return t; }
2417template<typename T> static T assert_cast(...) {
2418 llvm_unreachable("bad assert_cast");
2419}
2420
2421/// \brief Merge together the pattern declarations from two template
2422/// declarations.
2423void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D,
2424 RedeclarableTemplateDecl *Existing,
Richard Smith5fc18a92015-07-12 23:43:21 +00002425 DeclID DsID, bool IsKeyDecl) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00002426 auto *DPattern = D->getTemplatedDecl();
2427 auto *ExistingPattern = Existing->getTemplatedDecl();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002428 RedeclarableResult Result(/*MergeWith*/ ExistingPattern,
2429 DPattern->getCanonicalDecl()->getGlobalID(),
Alexander Shaposhnikovfbc4d682016-09-24 04:21:53 +00002430 IsKeyDecl);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002431
Richard Smith547864d2014-07-11 18:22:58 +00002432 if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) {
2433 // Merge with any existing definition.
2434 // FIXME: This is duplicated in several places. Refactor.
2435 auto *ExistingClass =
2436 cast<CXXRecordDecl>(ExistingPattern)->getCanonicalDecl();
Richard Smithb6483992016-05-17 22:44:15 +00002437 if (auto *DDD = DClass->DefinitionData) {
2438 if (ExistingClass->DefinitionData) {
Richard Smith8a639892015-01-24 01:07:20 +00002439 MergeDefinitionData(ExistingClass, std::move(*DDD));
Richard Smith547864d2014-07-11 18:22:58 +00002440 } else {
2441 ExistingClass->DefinitionData = DClass->DefinitionData;
Richard Smith97100e32015-06-20 00:22:34 +00002442 // We may have skipped this before because we thought that DClass
2443 // was the canonical declaration.
Richard Smith7474dd922015-03-11 18:21:02 +00002444 Reader.PendingDefinitions.insert(DClass);
Richard Smith547864d2014-07-11 18:22:58 +00002445 }
2446 }
2447 DClass->DefinitionData = ExistingClass->DefinitionData;
2448
Richard Smithf17fdbd2014-04-24 02:25:27 +00002449 return mergeRedeclarable(DClass, cast<TagDecl>(ExistingPattern),
2450 Result);
Richard Smith547864d2014-07-11 18:22:58 +00002451 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00002452 if (auto *DFunction = dyn_cast<FunctionDecl>(DPattern))
2453 return mergeRedeclarable(DFunction, cast<FunctionDecl>(ExistingPattern),
2454 Result);
2455 if (auto *DVar = dyn_cast<VarDecl>(DPattern))
2456 return mergeRedeclarable(DVar, cast<VarDecl>(ExistingPattern), Result);
Richard Smithf59b7352014-07-28 21:16:37 +00002457 if (auto *DAlias = dyn_cast<TypeAliasDecl>(DPattern))
2458 return mergeRedeclarable(DAlias, cast<TypedefNameDecl>(ExistingPattern),
2459 Result);
Richard Smithf17fdbd2014-04-24 02:25:27 +00002460 llvm_unreachable("merged an unknown kind of redeclarable template");
Richard Smithd55889a2013-09-09 16:55:27 +00002461}
2462
2463/// \brief Attempts to merge the given declaration (D) with another declaration
2464/// of the same entity.
2465template<typename T>
Richard Smithf17fdbd2014-04-24 02:25:27 +00002466void ASTDeclReader::mergeRedeclarable(Redeclarable<T> *DBase, T *Existing,
2467 RedeclarableResult &Redecl,
2468 DeclID TemplatePatternID) {
2469 T *D = static_cast<T*>(DBase);
Richard Smithd55889a2013-09-09 16:55:27 +00002470 T *ExistingCanon = Existing->getCanonicalDecl();
Richard Smithf17fdbd2014-04-24 02:25:27 +00002471 T *DCanon = D->getCanonicalDecl();
Richard Smithd55889a2013-09-09 16:55:27 +00002472 if (ExistingCanon != DCanon) {
Richard Smith202850a2015-03-10 02:57:50 +00002473 assert(DCanon->getGlobalID() == Redecl.getFirstID() &&
2474 "already merged this declaration");
Richard Smith5e9e56a2014-07-15 03:37:06 +00002475
Richard Smithd55889a2013-09-09 16:55:27 +00002476 // Have our redeclaration link point back at the canonical declaration
Richard Smith5e9e56a2014-07-15 03:37:06 +00002477 // of the existing declaration, so that this declaration has the
Richard Smithd55889a2013-09-09 16:55:27 +00002478 // appropriate canonical declaration.
2479 D->RedeclLink = Redeclarable<T>::PreviousDeclLink(ExistingCanon);
Manuel Klimek093b2d42015-03-25 23:18:30 +00002480 D->First = ExistingCanon;
Vassil Vassilev928c8252016-04-28 14:13:28 +00002481 ExistingCanon->Used |= D->Used;
2482 D->Used = false;
Richard Smithd55889a2013-09-09 16:55:27 +00002483
2484 // When we merge a namespace, update its pointer to the first namespace.
Richard Smith15e32fd2015-03-17 02:23:11 +00002485 // We cannot have loaded any redeclarations of this declaration yet, so
2486 // there's nothing else that needs to be updated.
Richard Smithf17fdbd2014-04-24 02:25:27 +00002487 if (auto *Namespace = dyn_cast<NamespaceDecl>(D))
Richard Smithd55889a2013-09-09 16:55:27 +00002488 Namespace->AnonOrFirstNamespaceAndInline.setPointer(
Richard Smithf17fdbd2014-04-24 02:25:27 +00002489 assert_cast<NamespaceDecl*>(ExistingCanon));
2490
2491 // When we merge a template, merge its pattern.
2492 if (auto *DTemplate = dyn_cast<RedeclarableTemplateDecl>(D))
2493 mergeTemplatePattern(
2494 DTemplate, assert_cast<RedeclarableTemplateDecl*>(ExistingCanon),
Richard Smith5fc18a92015-07-12 23:43:21 +00002495 TemplatePatternID, Redecl.isKeyDecl());
Richard Smithd55889a2013-09-09 16:55:27 +00002496
Richard Smith5fc18a92015-07-12 23:43:21 +00002497 // If this declaration is a key declaration, make a note of that.
Richard Smithd8a83712015-08-22 01:47:18 +00002498 if (Redecl.isKeyDecl())
Richard Smith5fc18a92015-07-12 23:43:21 +00002499 Reader.KeyDecls[ExistingCanon].push_back(Redecl.getFirstID());
Douglas Gregor2c46b5b2012-01-03 17:27:13 +00002500 }
2501}
2502
Richard Smith0b87e072013-10-07 08:02:11 +00002503/// \brief Attempts to merge the given declaration (D) with another declaration
2504/// of the same entity, for the case where the entity is not actually
2505/// redeclarable. This happens, for instance, when merging the fields of
2506/// identical class definitions from two different modules.
2507template<typename T>
2508void ASTDeclReader::mergeMergeable(Mergeable<T> *D) {
2509 // If modules are not available, there is no reason to perform this merge.
2510 if (!Reader.getContext().getLangOpts().Modules)
2511 return;
2512
Richard Smith01a73372013-10-15 22:02:41 +00002513 // ODR-based merging is only performed in C++. In C, identically-named things
2514 // in different translation units are not redeclarations (but may still have
2515 // compatible types).
2516 if (!Reader.getContext().getLangOpts().CPlusPlus)
2517 return;
2518
Richard Smith0b87e072013-10-07 08:02:11 +00002519 if (FindExistingResult ExistingRes = findExisting(static_cast<T*>(D)))
2520 if (T *Existing = ExistingRes)
Richard Smithdbafb6c2017-06-29 23:23:46 +00002521 Reader.getContext().setPrimaryMergedDecl(static_cast<T *>(D),
2522 Existing->getCanonicalDecl());
Richard Smith0b87e072013-10-07 08:02:11 +00002523}
2524
Alexey Bataeva769e072013-03-22 06:34:35 +00002525void ASTDeclReader::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
2526 VisitDecl(D);
2527 unsigned NumVars = D->varlist_size();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002528 SmallVector<Expr *, 16> Vars;
Alexey Bataeva769e072013-03-22 06:34:35 +00002529 Vars.reserve(NumVars);
2530 for (unsigned i = 0; i != NumVars; ++i) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00002531 Vars.push_back(Record.readExpr());
Alexey Bataeva769e072013-03-22 06:34:35 +00002532 }
2533 D->setVars(Vars);
2534}
2535
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002536void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002537 VisitValueDecl(D);
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002538 D->setLocation(ReadSourceLocation());
David L. Jonesb6a8f022016-12-21 04:34:52 +00002539 D->setCombiner(Record.readExpr());
Alexey Bataev070f43a2017-09-06 14:49:58 +00002540 D->setInitializer(
2541 Record.readExpr(),
2542 static_cast<OMPDeclareReductionDecl::InitKind>(Record.readInt()));
David L. Jonesc4808b9e2016-12-15 20:53:26 +00002543 D->PrevDeclInScope = ReadDeclID();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002544}
2545
Alexey Bataev4244be22016-02-11 05:35:55 +00002546void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00002547 VisitVarDecl(D);
2548}
2549
Chris Lattner487412d2009-04-27 05:27:42 +00002550//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00002551// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00002552//===----------------------------------------------------------------------===//
2553
Chris Lattner8f63ab52009-04-27 06:01:06 +00002554/// \brief Reads attributes from the current stream position.
David L. Jones267b8842017-01-24 01:04:30 +00002555void ASTReader::ReadAttributes(ASTRecordReader &Record, AttrVec &Attrs) {
2556 for (unsigned i = 0, e = Record.readInt(); i != e; ++i) {
Craig Toppera13603a2014-05-22 05:54:18 +00002557 Attr *New = nullptr;
David L. Jones267b8842017-01-24 01:04:30 +00002558 attr::Kind Kind = (attr::Kind)Record.readInt();
2559 SourceRange Range = Record.readSourceRange();
Richard Smithdbafb6c2017-06-29 23:23:46 +00002560 ASTContext &Context = getContext();
Chris Lattner8f63ab52009-04-27 06:01:06 +00002561
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002562#include "clang/Serialization/AttrPCHRead.inc"
Chris Lattner8f63ab52009-04-27 06:01:06 +00002563
2564 assert(New && "Unable to decode attribute?");
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002565 Attrs.push_back(New);
Chris Lattner8f63ab52009-04-27 06:01:06 +00002566 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00002567}
2568
2569//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +00002570// ASTReader Implementation
Chris Lattner8f63ab52009-04-27 06:01:06 +00002571//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00002572
2573/// \brief Note that we have loaded the declaration with the given
2574/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00002575///
Chris Lattner487412d2009-04-27 05:27:42 +00002576/// This routine notes that this declaration has already been loaded,
2577/// so that future GetDecl calls will return this declaration rather
2578/// than trying to load a new declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002579inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
Chris Lattner487412d2009-04-27 05:27:42 +00002580 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2581 DeclsLoaded[Index] = D;
2582}
2583
2584
2585/// \brief Determine whether the consumer will be interested in seeing
2586/// this declaration (via HandleTopLevelDecl).
2587///
2588/// This routine should return true for anything that might affect
2589/// code generation, e.g., inline function definitions, Objective-C
2590/// declarations with metadata, etc.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002591static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00002592 // An ObjCMethodDecl is never considered as "interesting" because its
2593 // implementation container always is.
2594
Richard Smithcd4a7a42017-09-07 00:55:55 +00002595 // An ImportDecl or VarDecl imported from a module map module will get
2596 // emitted when we import the relevant module.
2597 if (isa<ImportDecl>(D) || isa<VarDecl>(D)) {
2598 auto *M = D->getImportedOwningModule();
2599 if (M && M->Kind == Module::ModuleMapModule &&
2600 Ctx.DeclMustBeEmitted(D))
2601 return false;
2602 }
Richard Smithdc1f0422016-07-20 19:10:16 +00002603
Douglas Gregor87d81242011-09-10 00:22:34 +00002604 if (isa<FileScopeAsmDecl>(D) ||
2605 isa<ObjCProtocolDecl>(D) ||
Ben Langmuir332aafe2014-01-31 01:06:56 +00002606 isa<ObjCImplDecl>(D) ||
Alexey Bataev97720002014-11-11 04:05:39 +00002607 isa<ImportDecl>(D) ||
Nico Weber66220292016-03-02 17:28:48 +00002608 isa<PragmaCommentDecl>(D) ||
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002609 isa<PragmaDetectMismatchDecl>(D))
Daniel Dunbar865c2a72009-09-17 03:06:44 +00002610 return true;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00002611 if (isa<OMPThreadPrivateDecl>(D) || isa<OMPDeclareReductionDecl>(D))
2612 return !D->getDeclContext()->isFunctionOrMethod();
Chris Lattner487412d2009-04-27 05:27:42 +00002613 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis4ba81b22010-08-05 09:47:59 +00002614 return Var->isFileVarDecl() &&
2615 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner487412d2009-04-27 05:27:42 +00002616 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00002617 return Func->doesThisDeclarationHaveABody() || HasBody;
David Blaikie1ac9c982017-04-11 21:13:37 +00002618
2619 if (auto *ES = D->getASTContext().getExternalSource())
2620 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
2621 return true;
2622
Douglas Gregor87d81242011-09-10 00:22:34 +00002623 return false;
Chris Lattner487412d2009-04-27 05:27:42 +00002624}
2625
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002626/// \brief Get the correct cursor and offset for loading a declaration.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002627ASTReader::RecordLocation
Richard Smithcb34bd32016-03-27 07:28:06 +00002628ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) {
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002629 GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
2630 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00002631 ModuleFile *M = I->second;
Richard Smith34da7512016-03-27 05:52:25 +00002632 const DeclOffset &DOffs =
2633 M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS];
Richard Smithcb34bd32016-03-27 07:28:06 +00002634 Loc = TranslateSourceLocation(*M, DOffs.getLocation());
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00002635 return RecordLocation(M, DOffs.BitOffset);
Sebastian Redl34627792010-07-20 22:46:15 +00002636}
2637
Douglas Gregord32f0352011-07-22 06:10:01 +00002638ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002639 ContinuousRangeMap<uint64_t, ModuleFile*, 4>::iterator I
Douglas Gregord32f0352011-07-22 06:10:01 +00002640 = GlobalBitOffsetsMap.find(GlobalOffset);
2641
2642 assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
2643 return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
2644}
2645
Douglas Gregorde3ef502011-11-30 23:21:26 +00002646uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint32_t LocalOffset) {
Douglas Gregorc27b2872011-08-04 00:01:48 +00002647 return LocalOffset + M.GlobalBitOffset;
2648}
2649
Richard Smithbf78e642013-06-24 22:51:00 +00002650static bool isSameTemplateParameterList(const TemplateParameterList *X,
2651 const TemplateParameterList *Y);
2652
2653/// \brief Determine whether two template parameters are similar enough
2654/// that they may be used in declarations of the same template.
2655static bool isSameTemplateParameter(const NamedDecl *X,
2656 const NamedDecl *Y) {
2657 if (X->getKind() != Y->getKind())
2658 return false;
2659
2660 if (const TemplateTypeParmDecl *TX = dyn_cast<TemplateTypeParmDecl>(X)) {
2661 const TemplateTypeParmDecl *TY = cast<TemplateTypeParmDecl>(Y);
2662 return TX->isParameterPack() == TY->isParameterPack();
2663 }
2664
2665 if (const NonTypeTemplateParmDecl *TX = dyn_cast<NonTypeTemplateParmDecl>(X)) {
2666 const NonTypeTemplateParmDecl *TY = cast<NonTypeTemplateParmDecl>(Y);
2667 return TX->isParameterPack() == TY->isParameterPack() &&
2668 TX->getASTContext().hasSameType(TX->getType(), TY->getType());
2669 }
2670
2671 const TemplateTemplateParmDecl *TX = cast<TemplateTemplateParmDecl>(X);
2672 const TemplateTemplateParmDecl *TY = cast<TemplateTemplateParmDecl>(Y);
2673 return TX->isParameterPack() == TY->isParameterPack() &&
2674 isSameTemplateParameterList(TX->getTemplateParameters(),
2675 TY->getTemplateParameters());
2676}
2677
Richard Smith32952e12014-10-14 02:00:47 +00002678static NamespaceDecl *getNamespace(const NestedNameSpecifier *X) {
2679 if (auto *NS = X->getAsNamespace())
2680 return NS;
2681 if (auto *NAS = X->getAsNamespaceAlias())
2682 return NAS->getNamespace();
2683 return nullptr;
2684}
2685
2686static bool isSameQualifier(const NestedNameSpecifier *X,
2687 const NestedNameSpecifier *Y) {
2688 if (auto *NSX = getNamespace(X)) {
2689 auto *NSY = getNamespace(Y);
2690 if (!NSY || NSX->getCanonicalDecl() != NSY->getCanonicalDecl())
2691 return false;
2692 } else if (X->getKind() != Y->getKind())
2693 return false;
2694
2695 // FIXME: For namespaces and types, we're permitted to check that the entity
2696 // is named via the same tokens. We should probably do so.
2697 switch (X->getKind()) {
2698 case NestedNameSpecifier::Identifier:
2699 if (X->getAsIdentifier() != Y->getAsIdentifier())
2700 return false;
2701 break;
2702 case NestedNameSpecifier::Namespace:
2703 case NestedNameSpecifier::NamespaceAlias:
2704 // We've already checked that we named the same namespace.
2705 break;
2706 case NestedNameSpecifier::TypeSpec:
2707 case NestedNameSpecifier::TypeSpecWithTemplate:
2708 if (X->getAsType()->getCanonicalTypeInternal() !=
2709 Y->getAsType()->getCanonicalTypeInternal())
2710 return false;
2711 break;
2712 case NestedNameSpecifier::Global:
2713 case NestedNameSpecifier::Super:
2714 return true;
2715 }
2716
2717 // Recurse into earlier portion of NNS, if any.
2718 auto *PX = X->getPrefix();
2719 auto *PY = Y->getPrefix();
2720 if (PX && PY)
2721 return isSameQualifier(PX, PY);
2722 return !PX && !PY;
2723}
2724
Richard Smithbf78e642013-06-24 22:51:00 +00002725/// \brief Determine whether two template parameter lists are similar enough
2726/// that they may be used in declarations of the same template.
2727static bool isSameTemplateParameterList(const TemplateParameterList *X,
2728 const TemplateParameterList *Y) {
2729 if (X->size() != Y->size())
2730 return false;
2731
2732 for (unsigned I = 0, N = X->size(); I != N; ++I)
2733 if (!isSameTemplateParameter(X->getParam(I), Y->getParam(I)))
2734 return false;
2735
2736 return true;
2737}
2738
George Burgess IV95845082017-02-15 22:43:27 +00002739/// Determine whether the attributes we can overload on are identical for A and
George Burgess IVb7760212017-02-24 02:49:47 +00002740/// B. Will ignore any overloadable attrs represented in the type of A and B.
George Burgess IV95845082017-02-15 22:43:27 +00002741static bool hasSameOverloadableAttrs(const FunctionDecl *A,
2742 const FunctionDecl *B) {
George Burgess IVb7760212017-02-24 02:49:47 +00002743 // Note that pass_object_size attributes are represented in the function's
2744 // ExtParameterInfo, so we don't need to check them here.
2745
George Burgess IV95845082017-02-15 22:43:27 +00002746 SmallVector<const EnableIfAttr *, 4> AEnableIfs;
2747 // Since this is an equality check, we can ignore that enable_if attrs show up
2748 // in reverse order.
2749 for (const auto *EIA : A->specific_attrs<EnableIfAttr>())
2750 AEnableIfs.push_back(EIA);
2751
2752 SmallVector<const EnableIfAttr *, 4> BEnableIfs;
2753 for (const auto *EIA : B->specific_attrs<EnableIfAttr>())
2754 BEnableIfs.push_back(EIA);
2755
2756 // Two very common cases: either we have 0 enable_if attrs, or we have an
2757 // unequal number of enable_if attrs.
2758 if (AEnableIfs.empty() && BEnableIfs.empty())
2759 return true;
2760
2761 if (AEnableIfs.size() != BEnableIfs.size())
2762 return false;
2763
2764 llvm::FoldingSetNodeID Cand1ID, Cand2ID;
2765 for (unsigned I = 0, E = AEnableIfs.size(); I != E; ++I) {
2766 Cand1ID.clear();
2767 Cand2ID.clear();
2768
2769 AEnableIfs[I]->getCond()->Profile(Cand1ID, A->getASTContext(), true);
2770 BEnableIfs[I]->getCond()->Profile(Cand2ID, B->getASTContext(), true);
2771 if (Cand1ID != Cand2ID)
2772 return false;
2773 }
2774
George Burgess IV95845082017-02-15 22:43:27 +00002775 return true;
2776}
2777
Douglas Gregor022857e2011-12-22 01:48:48 +00002778/// \brief Determine whether the two declarations refer to the same entity.
2779static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
2780 assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
Richard Smithf4634362014-09-03 23:11:22 +00002781
Douglas Gregor022857e2011-12-22 01:48:48 +00002782 if (X == Y)
2783 return true;
Richard Smithf4634362014-09-03 23:11:22 +00002784
Douglas Gregor022857e2011-12-22 01:48:48 +00002785 // Must be in the same context.
2786 if (!X->getDeclContext()->getRedeclContext()->Equals(
2787 Y->getDeclContext()->getRedeclContext()))
2788 return false;
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002789
2790 // Two typedefs refer to the same entity if they have the same underlying
2791 // type.
2792 if (TypedefNameDecl *TypedefX = dyn_cast<TypedefNameDecl>(X))
2793 if (TypedefNameDecl *TypedefY = dyn_cast<TypedefNameDecl>(Y))
2794 return X->getASTContext().hasSameType(TypedefX->getUnderlyingType(),
2795 TypedefY->getUnderlyingType());
Richard Smithf4634362014-09-03 23:11:22 +00002796
Douglas Gregor9b7b3912012-01-04 16:44:10 +00002797 // Must have the same kind.
2798 if (X->getKind() != Y->getKind())
2799 return false;
Richard Smithf4634362014-09-03 23:11:22 +00002800
Douglas Gregorda389302012-01-01 21:47:52 +00002801 // Objective-C classes and protocols with the same name always match.
2802 if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
Douglas Gregor022857e2011-12-22 01:48:48 +00002803 return true;
Richard Smith1d209d02013-05-23 01:49:11 +00002804
2805 if (isa<ClassTemplateSpecializationDecl>(X)) {
Richard Smithd55889a2013-09-09 16:55:27 +00002806 // No need to handle these here: we merge them when adding them to the
2807 // template.
Richard Smith1d209d02013-05-23 01:49:11 +00002808 return false;
2809 }
Richard Smithd55889a2013-09-09 16:55:27 +00002810
Douglas Gregor2009cee2012-01-03 22:46:00 +00002811 // Compatible tags match.
Joao Matose9a3ed42012-08-31 22:18:20 +00002812 if (TagDecl *TagX = dyn_cast<TagDecl>(X)) {
2813 TagDecl *TagY = cast<TagDecl>(Y);
2814 return (TagX->getTagKind() == TagY->getTagKind()) ||
2815 ((TagX->getTagKind() == TTK_Struct || TagX->getTagKind() == TTK_Class ||
2816 TagX->getTagKind() == TTK_Interface) &&
2817 (TagY->getTagKind() == TTK_Struct || TagY->getTagKind() == TTK_Class ||
2818 TagY->getTagKind() == TTK_Interface));
2819 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002820
Joao Matose9a3ed42012-08-31 22:18:20 +00002821 // Functions with the same type and linkage match.
Richard Smithf4634362014-09-03 23:11:22 +00002822 // FIXME: This needs to cope with merging of prototyped/non-prototyped
2823 // functions, etc.
Douglas Gregorb2585692012-01-04 17:13:46 +00002824 if (FunctionDecl *FuncX = dyn_cast<FunctionDecl>(X)) {
2825 FunctionDecl *FuncY = cast<FunctionDecl>(Y);
Richard Smith5179eb72016-06-28 19:03:57 +00002826 if (CXXConstructorDecl *CtorX = dyn_cast<CXXConstructorDecl>(X)) {
2827 CXXConstructorDecl *CtorY = cast<CXXConstructorDecl>(Y);
2828 if (CtorX->getInheritedConstructor() &&
2829 !isSameEntity(CtorX->getInheritedConstructor().getConstructor(),
2830 CtorY->getInheritedConstructor().getConstructor()))
2831 return false;
2832 }
Erich Keane281d20b2018-01-08 21:34:17 +00002833
2834 if (FuncX->isMultiVersion() != FuncY->isMultiVersion())
2835 return false;
2836
2837 // Multiversioned functions with different feature strings are represented
2838 // as separate declarations.
2839 if (FuncX->isMultiVersion()) {
2840 const auto *TAX = FuncX->getAttr<TargetAttr>();
2841 const auto *TAY = FuncY->getAttr<TargetAttr>();
2842 assert(TAX && TAY && "Multiversion Function without target attribute");
2843
2844 if (TAX->getFeaturesStr() != TAY->getFeaturesStr())
2845 return false;
2846 }
2847
Richard Smitha54d3242017-03-08 23:00:26 +00002848 ASTContext &C = FuncX->getASTContext();
2849 if (!C.hasSameType(FuncX->getType(), FuncY->getType())) {
2850 // We can get functions with different types on the redecl chain in C++17
2851 // if they have differing exception specifications and at least one of
2852 // the excpetion specs is unresolved.
2853 // FIXME: Do we need to check for C++14 deduced return types here too?
2854 auto *XFPT = FuncX->getType()->getAs<FunctionProtoType>();
2855 auto *YFPT = FuncY->getType()->getAs<FunctionProtoType>();
Aaron Ballmanc351fba2017-12-04 20:27:34 +00002856 if (C.getLangOpts().CPlusPlus17 && XFPT && YFPT &&
Richard Smitha54d3242017-03-08 23:00:26 +00002857 (isUnresolvedExceptionSpec(XFPT->getExceptionSpecType()) ||
2858 isUnresolvedExceptionSpec(YFPT->getExceptionSpecType())) &&
2859 C.hasSameFunctionTypeIgnoringExceptionSpec(FuncX->getType(),
2860 FuncY->getType()))
2861 return true;
2862 return false;
2863 }
George Burgess IV95845082017-02-15 22:43:27 +00002864 return FuncX->getLinkageInternal() == FuncY->getLinkageInternal() &&
George Burgess IV95845082017-02-15 22:43:27 +00002865 hasSameOverloadableAttrs(FuncX, FuncY);
Douglas Gregorb2585692012-01-04 17:13:46 +00002866 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002867
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002868 // Variables with the same type and linkage match.
2869 if (VarDecl *VarX = dyn_cast<VarDecl>(X)) {
2870 VarDecl *VarY = cast<VarDecl>(Y);
Yaron Kerene94da642016-01-22 19:03:27 +00002871 if (VarX->getLinkageInternal() == VarY->getLinkageInternal()) {
2872 ASTContext &C = VarX->getASTContext();
2873 if (C.hasSameType(VarX->getType(), VarY->getType()))
2874 return true;
2875
2876 // We can get decls with different types on the redecl chain. Eg.
2877 // template <typename T> struct S { static T Var[]; }; // #1
2878 // template <typename T> T S<T>::Var[sizeof(T)]; // #2
2879 // Only? happens when completing an incomplete array type. In this case
Vassil Vassilev4d75e8d2016-02-28 19:08:24 +00002880 // when comparing #1 and #2 we should go through their element type.
Yaron Kerene94da642016-01-22 19:03:27 +00002881 const ArrayType *VarXTy = C.getAsArrayType(VarX->getType());
2882 const ArrayType *VarYTy = C.getAsArrayType(VarY->getType());
2883 if (!VarXTy || !VarYTy)
2884 return false;
2885 if (VarXTy->isIncompleteArrayType() || VarYTy->isIncompleteArrayType())
2886 return C.hasSameType(VarXTy->getElementType(), VarYTy->getElementType());
2887 }
2888 return false;
Douglas Gregorb8c6f1e2012-01-04 17:21:36 +00002889 }
Richard Smith8f8f05c2013-06-24 04:45:28 +00002890
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00002891 // Namespaces with the same name and inlinedness match.
2892 if (NamespaceDecl *NamespaceX = dyn_cast<NamespaceDecl>(X)) {
2893 NamespaceDecl *NamespaceY = cast<NamespaceDecl>(Y);
2894 return NamespaceX->isInline() == NamespaceY->isInline();
2895 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002896
Richard Smith8f8f05c2013-06-24 04:45:28 +00002897 // Identical template names and kinds match if their template parameter lists
2898 // and patterns match.
2899 if (TemplateDecl *TemplateX = dyn_cast<TemplateDecl>(X)) {
Richard Smithbf78e642013-06-24 22:51:00 +00002900 TemplateDecl *TemplateY = cast<TemplateDecl>(Y);
Richard Smith8f8f05c2013-06-24 04:45:28 +00002901 return isSameEntity(TemplateX->getTemplatedDecl(),
Richard Smithbf78e642013-06-24 22:51:00 +00002902 TemplateY->getTemplatedDecl()) &&
2903 isSameTemplateParameterList(TemplateX->getTemplateParameters(),
2904 TemplateY->getTemplateParameters());
Richard Smith8f8f05c2013-06-24 04:45:28 +00002905 }
Axel Naumann866ba3e2012-10-01 09:18:00 +00002906
Richard Smith0b87e072013-10-07 08:02:11 +00002907 // Fields with the same name and the same type match.
2908 if (FieldDecl *FDX = dyn_cast<FieldDecl>(X)) {
2909 FieldDecl *FDY = cast<FieldDecl>(Y);
Richard Smith01a73372013-10-15 22:02:41 +00002910 // FIXME: Also check the bitwidth is odr-equivalent, if any.
Richard Smith0b87e072013-10-07 08:02:11 +00002911 return X->getASTContext().hasSameType(FDX->getType(), FDY->getType());
2912 }
2913
Richard Smith8cbd8952015-08-04 02:05:09 +00002914 // Indirect fields with the same target field match.
2915 if (auto *IFDX = dyn_cast<IndirectFieldDecl>(X)) {
2916 auto *IFDY = cast<IndirectFieldDecl>(Y);
2917 return IFDX->getAnonField()->getCanonicalDecl() ==
2918 IFDY->getAnonField()->getCanonicalDecl();
2919 }
2920
Richard Smith01a73372013-10-15 22:02:41 +00002921 // Enumerators with the same name match.
2922 if (isa<EnumConstantDecl>(X))
2923 // FIXME: Also check the value is odr-equivalent.
2924 return true;
2925
Richard Smithfd8634a2013-10-23 02:17:46 +00002926 // Using shadow declarations with the same target match.
2927 if (UsingShadowDecl *USX = dyn_cast<UsingShadowDecl>(X)) {
2928 UsingShadowDecl *USY = cast<UsingShadowDecl>(Y);
2929 return USX->getTargetDecl() == USY->getTargetDecl();
2930 }
2931
Richard Smith32952e12014-10-14 02:00:47 +00002932 // Using declarations with the same qualifier match. (We already know that
2933 // the name matches.)
2934 if (auto *UX = dyn_cast<UsingDecl>(X)) {
2935 auto *UY = cast<UsingDecl>(Y);
2936 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2937 UX->hasTypename() == UY->hasTypename() &&
2938 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2939 }
2940 if (auto *UX = dyn_cast<UnresolvedUsingValueDecl>(X)) {
2941 auto *UY = cast<UnresolvedUsingValueDecl>(Y);
2942 return isSameQualifier(UX->getQualifier(), UY->getQualifier()) &&
2943 UX->isAccessDeclaration() == UY->isAccessDeclaration();
2944 }
2945 if (auto *UX = dyn_cast<UnresolvedUsingTypenameDecl>(X))
2946 return isSameQualifier(
2947 UX->getQualifier(),
2948 cast<UnresolvedUsingTypenameDecl>(Y)->getQualifier());
2949
Richard Smithf4634362014-09-03 23:11:22 +00002950 // Namespace alias definitions with the same target match.
2951 if (auto *NAX = dyn_cast<NamespaceAliasDecl>(X)) {
2952 auto *NAY = cast<NamespaceAliasDecl>(Y);
2953 return NAX->getNamespace()->Equals(NAY->getNamespace());
2954 }
2955
Douglas Gregor022857e2011-12-22 01:48:48 +00002956 return false;
2957}
2958
Richard Smithd55889a2013-09-09 16:55:27 +00002959/// Find the context in which we should search for previous declarations when
2960/// looking for declarations to merge.
Richard Smith8a639892015-01-24 01:07:20 +00002961DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
2962 DeclContext *DC) {
Richard Smithd55889a2013-09-09 16:55:27 +00002963 if (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
2964 return ND->getOriginalNamespace();
2965
Richard Smith8a639892015-01-24 01:07:20 +00002966 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
2967 // Try to dig out the definition.
Richard Smithb6483992016-05-17 22:44:15 +00002968 auto *DD = RD->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002969 if (!DD)
Richard Smithb6483992016-05-17 22:44:15 +00002970 DD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith8a639892015-01-24 01:07:20 +00002971
2972 // If there's no definition yet, then DC's definition is added by an update
2973 // record, but we've not yet loaded that update record. In this case, we
2974 // commit to DC being the canonical definition now, and will fix this when
2975 // we load the update record.
2976 if (!DD) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00002977 DD = new (Reader.getContext()) struct CXXRecordDecl::DefinitionData(RD);
Richard Smith8a639892015-01-24 01:07:20 +00002978 RD->IsCompleteDefinition = true;
2979 RD->DefinitionData = DD;
2980 RD->getCanonicalDecl()->DefinitionData = DD;
2981
2982 // Track that we did this horrible thing so that we can fix it later.
Richard Smith2a9e5c52015-02-03 03:32:14 +00002983 Reader.PendingFakeDefinitionData.insert(
2984 std::make_pair(DD, ASTReader::PendingFakeDefinitionKind::Fake));
Richard Smith8a639892015-01-24 01:07:20 +00002985 }
2986
2987 return DD->Definition;
2988 }
Richard Smithd55889a2013-09-09 16:55:27 +00002989
Richard Smith01a73372013-10-15 22:02:41 +00002990 if (EnumDecl *ED = dyn_cast<EnumDecl>(DC))
Craig Toppera13603a2014-05-22 05:54:18 +00002991 return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
2992 : nullptr;
Richard Smith01a73372013-10-15 22:02:41 +00002993
Richard Smith4eca9b92015-02-04 23:37:59 +00002994 // We can see the TU here only if we have no Sema object. In that case,
2995 // there's no TU scope to look in, so using the DC alone is sufficient.
2996 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2997 return TU;
2998
Craig Toppera13603a2014-05-22 05:54:18 +00002999 return nullptr;
Richard Smithd55889a2013-09-09 16:55:27 +00003000}
3001
Douglas Gregor022857e2011-12-22 01:48:48 +00003002ASTDeclReader::FindExistingResult::~FindExistingResult() {
Richard Smithf1f4bc22015-01-22 02:21:23 +00003003 // Record that we had a typedef name for linkage whether or not we merge
3004 // with that declaration.
3005 if (TypedefNameForLinkage) {
3006 DeclContext *DC = New->getDeclContext()->getRedeclContext();
3007 Reader.ImportedTypedefNamesForLinkage.insert(
3008 std::make_pair(std::make_pair(DC, TypedefNameForLinkage), New));
3009 return;
3010 }
3011
Douglas Gregor9b47f942012-01-09 17:38:47 +00003012 if (!AddResult || Existing)
Douglas Gregor022857e2011-12-22 01:48:48 +00003013 return;
Richard Smith95d99302013-07-13 02:00:19 +00003014
Richard Smith70d58502014-08-30 00:04:23 +00003015 DeclarationName Name = New->getDeclName();
Richard Smith95d99302013-07-13 02:00:19 +00003016 DeclContext *DC = New->getDeclContext()->getRedeclContext();
Richard Smith2b560572015-02-07 03:11:11 +00003017 if (needsAnonymousDeclarationNumber(New)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003018 setAnonymousDeclForMerging(Reader, New->getLexicalDeclContext(),
3019 AnonymousDeclNumber, New);
Richard Smith10379092016-05-06 23:14:07 +00003020 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003021 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003022 if (Reader.getIdResolver().tryAddTopLevelDecl(New, Name))
Richard Smith9e2341d2015-03-23 03:25:59 +00003023 Reader.PendingFakeLookupResults[Name.getAsIdentifierInfo()]
3024 .push_back(New);
Richard Smith8a639892015-01-24 01:07:20 +00003025 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smith95d99302013-07-13 02:00:19 +00003026 // Add the declaration to its redeclaration context so later merging
3027 // lookups will find it.
Richard Smithd55889a2013-09-09 16:55:27 +00003028 MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true);
Douglas Gregor022857e2011-12-22 01:48:48 +00003029 }
3030}
3031
Richard Smith88ebade2014-08-23 01:45:27 +00003032/// Find the declaration that should be merged into, given the declaration found
3033/// by name lookup. If we're merging an anonymous declaration within a typedef,
3034/// we need a matching typedef, and we merge with the type inside it.
3035static NamedDecl *getDeclForMerging(NamedDecl *Found,
3036 bool IsTypedefNameForLinkage) {
3037 if (!IsTypedefNameForLinkage)
3038 return Found;
3039
3040 // If we found a typedef declaration that gives a name to some other
3041 // declaration, then we want that inner declaration. Declarations from
3042 // AST files are handled via ImportedTypedefNamesForLinkage.
Richard Smitha5230222015-03-27 01:37:43 +00003043 if (Found->isFromASTFile())
Hans Wennborgdcfba332015-10-06 23:40:43 +00003044 return nullptr;
Richard Smitha5230222015-03-27 01:37:43 +00003045
3046 if (auto *TND = dyn_cast<TypedefNameDecl>(Found))
Richard Smith3fb1a852016-08-30 19:13:18 +00003047 return TND->getAnonDeclWithTypedefName(/*AnyRedecl*/true);
Richard Smith88ebade2014-08-23 01:45:27 +00003048
Hans Wennborgdcfba332015-10-06 23:40:43 +00003049 return nullptr;
Richard Smith88ebade2014-08-23 01:45:27 +00003050}
3051
Richard Smithd08aeb62014-08-28 01:33:39 +00003052NamedDecl *ASTDeclReader::getAnonymousDeclForMerging(ASTReader &Reader,
3053 DeclContext *DC,
3054 unsigned Index) {
3055 // If the lexical context has been merged, look into the now-canonical
3056 // definition.
3057 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3058 DC = Merged;
3059
3060 // If we've seen this before, return the canonical declaration.
3061 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3062 if (Index < Previous.size() && Previous[Index])
3063 return Previous[Index];
3064
3065 // If this is the first time, but we have parsed a declaration of the context,
3066 // build the anonymous declaration list from the parsed declaration.
3067 if (!cast<Decl>(DC)->isFromASTFile()) {
Richard Smith2b560572015-02-07 03:11:11 +00003068 numberAnonymousDeclsWithin(DC, [&](NamedDecl *ND, unsigned Number) {
3069 if (Previous.size() == Number)
Richard Smithd08aeb62014-08-28 01:33:39 +00003070 Previous.push_back(cast<NamedDecl>(ND->getCanonicalDecl()));
3071 else
Richard Smith2b560572015-02-07 03:11:11 +00003072 Previous[Number] = cast<NamedDecl>(ND->getCanonicalDecl());
3073 });
Richard Smithd08aeb62014-08-28 01:33:39 +00003074 }
3075
3076 return Index < Previous.size() ? Previous[Index] : nullptr;
3077}
3078
3079void ASTDeclReader::setAnonymousDeclForMerging(ASTReader &Reader,
3080 DeclContext *DC, unsigned Index,
3081 NamedDecl *D) {
3082 if (auto *Merged = Reader.MergedDeclContexts.lookup(DC))
3083 DC = Merged;
3084
3085 auto &Previous = Reader.AnonymousDeclarationsForMerging[DC];
3086 if (Index >= Previous.size())
3087 Previous.resize(Index + 1);
3088 if (!Previous[Index])
3089 Previous[Index] = D;
3090}
3091
Douglas Gregor022857e2011-12-22 01:48:48 +00003092ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) {
Richard Smith70d58502014-08-30 00:04:23 +00003093 DeclarationName Name = TypedefNameForLinkage ? TypedefNameForLinkage
3094 : D->getDeclName();
Richard Smith88ebade2014-08-23 01:45:27 +00003095
Richard Smithd08aeb62014-08-28 01:33:39 +00003096 if (!Name && !needsAnonymousDeclarationNumber(D)) {
3097 // Don't bother trying to find unnamed declarations that are in
3098 // unmergeable contexts.
Richard Smith70d58502014-08-30 00:04:23 +00003099 FindExistingResult Result(Reader, D, /*Existing=*/nullptr,
3100 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor2009cee2012-01-03 22:46:00 +00003101 Result.suppress();
3102 return Result;
3103 }
Richard Smith95d99302013-07-13 02:00:19 +00003104
Douglas Gregor022857e2011-12-22 01:48:48 +00003105 DeclContext *DC = D->getDeclContext()->getRedeclContext();
Richard Smith70d58502014-08-30 00:04:23 +00003106 if (TypedefNameForLinkage) {
Richard Smith88ebade2014-08-23 01:45:27 +00003107 auto It = Reader.ImportedTypedefNamesForLinkage.find(
Richard Smith70d58502014-08-30 00:04:23 +00003108 std::make_pair(DC, TypedefNameForLinkage));
Richard Smith88ebade2014-08-23 01:45:27 +00003109 if (It != Reader.ImportedTypedefNamesForLinkage.end())
3110 if (isSameEntity(It->second, D))
Richard Smith70d58502014-08-30 00:04:23 +00003111 return FindExistingResult(Reader, D, It->second, AnonymousDeclNumber,
3112 TypedefNameForLinkage);
Richard Smith88ebade2014-08-23 01:45:27 +00003113 // Go on to check in other places in case an existing typedef name
3114 // was not imported.
3115 }
Richard Smithd08aeb62014-08-28 01:33:39 +00003116
Richard Smith2b560572015-02-07 03:11:11 +00003117 if (needsAnonymousDeclarationNumber(D)) {
Richard Smithd08aeb62014-08-28 01:33:39 +00003118 // This is an anonymous declaration that we may need to merge. Look it up
3119 // in its context by number.
Richard Smithd08aeb62014-08-28 01:33:39 +00003120 if (auto *Existing = getAnonymousDeclForMerging(
3121 Reader, D->getLexicalDeclContext(), AnonymousDeclNumber))
3122 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003123 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3124 TypedefNameForLinkage);
Richard Smith10379092016-05-06 23:14:07 +00003125 } else if (DC->isTranslationUnit() &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003126 !Reader.getContext().getLangOpts().CPlusPlus) {
Richard Smith10379092016-05-06 23:14:07 +00003127 IdentifierResolver &IdResolver = Reader.getIdResolver();
Douglas Gregor6168bd22013-02-18 15:53:43 +00003128
3129 // Temporarily consider the identifier to be up-to-date. We don't want to
3130 // cause additional lookups here.
3131 class UpToDateIdentifierRAII {
3132 IdentifierInfo *II;
3133 bool WasOutToDate;
3134
3135 public:
3136 explicit UpToDateIdentifierRAII(IdentifierInfo *II)
3137 : II(II), WasOutToDate(false)
3138 {
3139 if (II) {
3140 WasOutToDate = II->isOutOfDate();
3141 if (WasOutToDate)
3142 II->setOutOfDate(false);
3143 }
3144 }
3145
3146 ~UpToDateIdentifierRAII() {
3147 if (WasOutToDate)
3148 II->setOutOfDate(true);
3149 }
3150 } UpToDate(Name.getAsIdentifierInfo());
3151
Douglas Gregor2009cee2012-01-03 22:46:00 +00003152 for (IdentifierResolver::iterator I = IdResolver.begin(Name),
Douglas Gregor022857e2011-12-22 01:48:48 +00003153 IEnd = IdResolver.end();
3154 I != IEnd; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003155 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith87dacc72014-08-25 23:33:46 +00003156 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003157 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3158 TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003159 }
Richard Smith8a639892015-01-24 01:07:20 +00003160 } else if (DeclContext *MergeDC = getPrimaryContextForMerging(Reader, DC)) {
Richard Smithd55889a2013-09-09 16:55:27 +00003161 DeclContext::lookup_result R = MergeDC->noload_lookup(Name);
Richard Smith95d99302013-07-13 02:00:19 +00003162 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) {
Richard Smith70d58502014-08-30 00:04:23 +00003163 if (NamedDecl *Existing = getDeclForMerging(*I, TypedefNameForLinkage))
Richard Smith88ebade2014-08-23 01:45:27 +00003164 if (isSameEntity(Existing, D))
Richard Smith70d58502014-08-30 00:04:23 +00003165 return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber,
3166 TypedefNameForLinkage);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003167 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00003168 } else {
3169 // Not in a mergeable context.
3170 return FindExistingResult(Reader);
Douglas Gregor9b47f942012-01-09 17:38:47 +00003171 }
Richard Smithd55889a2013-09-09 16:55:27 +00003172
Richard Smith2b9e3e32013-10-18 06:05:18 +00003173 // If this declaration is from a merged context, make a note that we need to
3174 // check that the canonical definition of that context contains the decl.
Richard Smithcd45dbc2014-04-19 03:48:30 +00003175 //
3176 // FIXME: We should do something similar if we merge two definitions of the
3177 // same template specialization into the same CXXRecordDecl.
Richard Smith88126a22014-08-25 02:10:01 +00003178 auto MergedDCIt = Reader.MergedDeclContexts.find(D->getLexicalDeclContext());
3179 if (MergedDCIt != Reader.MergedDeclContexts.end() &&
3180 MergedDCIt->second == D->getDeclContext())
Richard Smith2b9e3e32013-10-18 06:05:18 +00003181 Reader.PendingOdrMergeChecks.push_back(D);
3182
Richard Smithd08aeb62014-08-28 01:33:39 +00003183 return FindExistingResult(Reader, D, /*Existing=*/nullptr,
Richard Smith70d58502014-08-30 00:04:23 +00003184 AnonymousDeclNumber, TypedefNameForLinkage);
Douglas Gregor022857e2011-12-22 01:48:48 +00003185}
3186
Richard Smithb321ecb2014-05-13 01:15:00 +00003187template<typename DeclT>
Richard Smithc3a53252015-02-28 05:57:02 +00003188Decl *ASTDeclReader::getMostRecentDeclImpl(Redeclarable<DeclT> *D) {
3189 return D->RedeclLink.getLatestNotUpdated();
3190}
3191Decl *ASTDeclReader::getMostRecentDeclImpl(...) {
3192 llvm_unreachable("getMostRecentDecl on non-redeclarable declaration");
3193}
3194
3195Decl *ASTDeclReader::getMostRecentDecl(Decl *D) {
3196 assert(D);
3197
3198 switch (D->getKind()) {
3199#define ABSTRACT_DECL(TYPE)
3200#define DECL(TYPE, BASE) \
3201 case Decl::TYPE: \
3202 return getMostRecentDeclImpl(cast<TYPE##Decl>(D));
3203#include "clang/AST/DeclNodes.inc"
3204 }
3205 llvm_unreachable("unknown decl kind");
3206}
3207
Richard Smith8ce51082015-03-11 01:44:51 +00003208Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
3209 return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
3210}
3211
Richard Smithc3a53252015-02-28 05:57:02 +00003212template<typename DeclT>
Richard Smith6de7a242014-07-31 23:46:44 +00003213void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
3214 Redeclarable<DeclT> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003215 Decl *Previous, Decl *Canon) {
Richard Smith053f6c62014-05-16 23:01:30 +00003216 D->RedeclLink.setPrevious(cast<DeclT>(Previous));
Manuel Klimek093b2d42015-03-25 23:18:30 +00003217 D->First = cast<DeclT>(Previous)->First;
Richard Smithb321ecb2014-05-13 01:15:00 +00003218}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003219
Richard Smith24d166c2014-07-31 23:52:38 +00003220namespace clang {
Richard Smith6de7a242014-07-31 23:46:44 +00003221template<>
3222void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smithedbc6e92016-10-14 21:41:24 +00003223 Redeclarable<VarDecl> *D,
3224 Decl *Previous, Decl *Canon) {
3225 VarDecl *VD = static_cast<VarDecl*>(D);
3226 VarDecl *PrevVD = cast<VarDecl>(Previous);
3227 D->RedeclLink.setPrevious(PrevVD);
3228 D->First = PrevVD->First;
3229
3230 // We should keep at most one definition on the chain.
3231 // FIXME: Cache the definition once we've found it. Building a chain with
3232 // N definitions currently takes O(N^2) time here.
3233 if (VD->isThisDeclarationADefinition() == VarDecl::Definition) {
3234 for (VarDecl *CurD = PrevVD; CurD; CurD = CurD->getPreviousDecl()) {
3235 if (CurD->isThisDeclarationADefinition() == VarDecl::Definition) {
3236 Reader.mergeDefinitionVisibility(CurD, VD);
3237 VD->demoteThisDefinitionToDeclaration();
3238 break;
3239 }
3240 }
3241 }
3242}
3243
3244template<>
3245void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
Richard Smith6de7a242014-07-31 23:46:44 +00003246 Redeclarable<FunctionDecl> *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003247 Decl *Previous, Decl *Canon) {
Richard Smith6de7a242014-07-31 23:46:44 +00003248 FunctionDecl *FD = static_cast<FunctionDecl*>(D);
3249 FunctionDecl *PrevFD = cast<FunctionDecl>(Previous);
3250
3251 FD->RedeclLink.setPrevious(PrevFD);
Manuel Klimek093b2d42015-03-25 23:18:30 +00003252 FD->First = PrevFD->First;
Richard Smith6de7a242014-07-31 23:46:44 +00003253
3254 // If the previous declaration is an inline function declaration, then this
3255 // declaration is too.
3256 if (PrevFD->IsInline != FD->IsInline) {
3257 // FIXME: [dcl.fct.spec]p4:
3258 // If a function with external linkage is declared inline in one
3259 // translation unit, it shall be declared inline in all translation
3260 // units in which it appears.
3261 //
3262 // Be careful of this case:
3263 //
3264 // module A:
3265 // template<typename T> struct X { void f(); };
3266 // template<typename T> inline void X<T>::f() {}
3267 //
3268 // module B instantiates the declaration of X<int>::f
3269 // module C instantiates the definition of X<int>::f
3270 //
3271 // If module B and C are merged, we do not have a violation of this rule.
3272 FD->IsInline = true;
3273 }
3274
Richard Smith9e2341d2015-03-23 03:25:59 +00003275 // If we need to propagate an exception specification along the redecl
3276 // chain, make a note of that so that we can do so later.
Richard Smith6de7a242014-07-31 23:46:44 +00003277 auto *FPT = FD->getType()->getAs<FunctionProtoType>();
3278 auto *PrevFPT = PrevFD->getType()->getAs<FunctionProtoType>();
Richard Smith80969752015-03-10 02:00:53 +00003279 if (FPT && PrevFPT) {
Richard Smith9e2341d2015-03-23 03:25:59 +00003280 bool IsUnresolved = isUnresolvedExceptionSpec(FPT->getExceptionSpecType());
3281 bool WasUnresolved =
3282 isUnresolvedExceptionSpec(PrevFPT->getExceptionSpecType());
3283 if (IsUnresolved != WasUnresolved)
3284 Reader.PendingExceptionSpecUpdates.insert(
3285 std::make_pair(Canon, IsUnresolved ? PrevFD : FD));
Richard Smith6de7a242014-07-31 23:46:44 +00003286 }
3287}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003288} // end namespace clang
3289
Richard Smith6de7a242014-07-31 23:46:44 +00003290void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader, ...) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003291 llvm_unreachable("attachPreviousDecl on non-redeclarable declaration");
3292}
3293
Richard Smith8346e522015-06-10 01:47:58 +00003294/// Inherit the default template argument from \p From to \p To. Returns
3295/// \c false if there is no default template for \p From.
3296template <typename ParmDecl>
3297static bool inheritDefaultTemplateArgument(ASTContext &Context, ParmDecl *From,
3298 Decl *ToD) {
3299 auto *To = cast<ParmDecl>(ToD);
3300 if (!From->hasDefaultArgument())
3301 return false;
Richard Smithe7bd6de2015-06-10 20:30:23 +00003302 To->setInheritedDefaultArgument(Context, From);
Richard Smith8346e522015-06-10 01:47:58 +00003303 return true;
3304}
3305
3306static void inheritDefaultTemplateArguments(ASTContext &Context,
3307 TemplateDecl *From,
3308 TemplateDecl *To) {
3309 auto *FromTP = From->getTemplateParameters();
3310 auto *ToTP = To->getTemplateParameters();
3311 assert(FromTP->size() == ToTP->size() && "merged mismatched templates?");
3312
3313 for (unsigned I = 0, N = FromTP->size(); I != N; ++I) {
3314 NamedDecl *FromParam = FromTP->getParam(N - I - 1);
Richard Smith3d987032016-02-04 22:54:41 +00003315 if (FromParam->isParameterPack())
3316 continue;
Richard Smith8346e522015-06-10 01:47:58 +00003317 NamedDecl *ToParam = ToTP->getParam(N - I - 1);
3318
3319 if (auto *FTTP = dyn_cast<TemplateTypeParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003320 if (!inheritDefaultTemplateArgument(Context, FTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003321 break;
3322 } else if (auto *FNTTP = dyn_cast<NonTypeTemplateParmDecl>(FromParam)) {
Richard Smithafe800c2015-06-17 22:13:23 +00003323 if (!inheritDefaultTemplateArgument(Context, FNTTP, ToParam))
Richard Smith8346e522015-06-10 01:47:58 +00003324 break;
3325 } else {
Richard Smithafe800c2015-06-17 22:13:23 +00003326 if (!inheritDefaultTemplateArgument(
Richard Smith8346e522015-06-10 01:47:58 +00003327 Context, cast<TemplateTemplateParmDecl>(FromParam), ToParam))
3328 break;
3329 }
3330 }
3331}
3332
Richard Smith6de7a242014-07-31 23:46:44 +00003333void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
Richard Smith9e2341d2015-03-23 03:25:59 +00003334 Decl *Previous, Decl *Canon) {
Richard Smithb321ecb2014-05-13 01:15:00 +00003335 assert(D && Previous);
3336
3337 switch (D->getKind()) {
3338#define ABSTRACT_DECL(TYPE)
Richard Smith9e2341d2015-03-23 03:25:59 +00003339#define DECL(TYPE, BASE) \
3340 case Decl::TYPE: \
3341 attachPreviousDeclImpl(Reader, cast<TYPE##Decl>(D), Previous, Canon); \
Richard Smithb321ecb2014-05-13 01:15:00 +00003342 break;
3343#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003344 }
Richard Smith7ecc31b2013-08-02 01:09:12 +00003345
3346 // If the declaration was visible in one module, a redeclaration of it in
3347 // another module remains visible even if it wouldn't be visible by itself.
3348 //
3349 // FIXME: In this case, the declaration should only be visible if a module
3350 // that makes it visible has been imported.
Richard Smith7ecc31b2013-08-02 01:09:12 +00003351 D->IdentifierNamespace |=
Richard Smithb321ecb2014-05-13 01:15:00 +00003352 Previous->IdentifierNamespace &
Richard Smith7ecc31b2013-08-02 01:09:12 +00003353 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Richard Smith195d8ef2014-05-29 03:15:31 +00003354
Richard Smith8346e522015-06-10 01:47:58 +00003355 // If the declaration declares a template, it may inherit default arguments
3356 // from the previous declaration.
3357 if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
3358 inheritDefaultTemplateArguments(Reader.getContext(),
3359 cast<TemplateDecl>(Previous), TD);
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00003360}
3361
Richard Smithb321ecb2014-05-13 01:15:00 +00003362template<typename DeclT>
3363void ASTDeclReader::attachLatestDeclImpl(Redeclarable<DeclT> *D, Decl *Latest) {
Richard Smith053f6c62014-05-16 23:01:30 +00003364 D->RedeclLink.setLatest(cast<DeclT>(Latest));
Richard Smithb321ecb2014-05-13 01:15:00 +00003365}
3366void ASTDeclReader::attachLatestDeclImpl(...) {
3367 llvm_unreachable("attachLatestDecl on non-redeclarable declaration");
3368}
3369
Douglas Gregor05f10352011-12-17 23:38:30 +00003370void ASTDeclReader::attachLatestDecl(Decl *D, Decl *Latest) {
3371 assert(D && Latest);
Richard Smithb321ecb2014-05-13 01:15:00 +00003372
3373 switch (D->getKind()) {
3374#define ABSTRACT_DECL(TYPE)
Richard Smith053f6c62014-05-16 23:01:30 +00003375#define DECL(TYPE, BASE) \
3376 case Decl::TYPE: \
Richard Smithb321ecb2014-05-13 01:15:00 +00003377 attachLatestDeclImpl(cast<TYPE##Decl>(D), Latest); \
3378 break;
3379#include "clang/AST/DeclNodes.inc"
Douglas Gregor05f10352011-12-17 23:38:30 +00003380 }
3381}
3382
Richard Smith851072e2014-05-19 20:59:20 +00003383template<typename DeclT>
3384void ASTDeclReader::markIncompleteDeclChainImpl(Redeclarable<DeclT> *D) {
3385 D->RedeclLink.markIncomplete();
3386}
3387void ASTDeclReader::markIncompleteDeclChainImpl(...) {
3388 llvm_unreachable("markIncompleteDeclChain on non-redeclarable declaration");
3389}
3390
3391void ASTReader::markIncompleteDeclChain(Decl *D) {
3392 switch (D->getKind()) {
3393#define ABSTRACT_DECL(TYPE)
3394#define DECL(TYPE, BASE) \
3395 case Decl::TYPE: \
3396 ASTDeclReader::markIncompleteDeclChainImpl(cast<TYPE##Decl>(D)); \
3397 break;
3398#include "clang/AST/DeclNodes.inc"
3399 }
3400}
3401
Sebastian Redlb3298c32010-08-18 23:56:48 +00003402/// \brief Read the declaration at the given offset from the AST file.
Douglas Gregorf7180622011-08-03 15:48:04 +00003403Decl *ASTReader::ReadDeclRecord(DeclID ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00003404 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
Richard Smithcb34bd32016-03-27 07:28:06 +00003405 SourceLocation DeclLoc;
3406 RecordLocation Loc = DeclCursorForID(ID, DeclLoc);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003407 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Chris Lattner487412d2009-04-27 05:27:42 +00003408 // Keep track of where we are in the stream, then jump back there
3409 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00003410 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00003411
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003412 ReadingKindTracker ReadingKind(Read_Decl, *this);
3413
Douglas Gregor1342e842009-07-06 18:54:52 +00003414 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003415 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00003416
Sebastian Redl2c373b92010-10-05 15:59:54 +00003417 DeclsCursor.JumpToBit(Loc.Offset);
David L. Jonesbe1557a2016-12-21 00:17:49 +00003418 ASTRecordReader Record(*this, *Loc.F);
3419 ASTDeclReader Reader(*this, Record, Loc, ID, DeclLoc);
Chris Lattner1de76db2009-04-27 05:58:23 +00003420 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00003421
Richard Smithdbafb6c2017-06-29 23:23:46 +00003422 ASTContext &Context = getContext();
Craig Toppera13603a2014-05-22 05:54:18 +00003423 Decl *D = nullptr;
David L. Jonesbe1557a2016-12-21 00:17:49 +00003424 switch ((DeclCode)Record.readRecord(DeclsCursor, Code)) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003425 case DECL_CONTEXT_LEXICAL:
3426 case DECL_CONTEXT_VISIBLE:
David Blaikie83d382b2011-09-23 05:06:16 +00003427 llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
Sebastian Redl539c5062010-08-18 23:57:32 +00003428 case DECL_TYPEDEF:
Douglas Gregor72172e92012-01-05 21:55:30 +00003429 D = TypedefDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003430 break;
Richard Smithdda56e42011-04-15 14:24:37 +00003431 case DECL_TYPEALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003432 D = TypeAliasDecl::CreateDeserialized(Context, ID);
Richard Smithdda56e42011-04-15 14:24:37 +00003433 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003434 case DECL_ENUM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003435 D = EnumDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003436 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003437 case DECL_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003438 D = RecordDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003439 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003440 case DECL_ENUM_CONSTANT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003441 D = EnumConstantDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003442 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003443 case DECL_FUNCTION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003444 D = FunctionDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003445 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003446 case DECL_LINKAGE_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003447 D = LinkageSpecDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003448 break;
Richard Smith8df390f2016-09-08 23:14:54 +00003449 case DECL_EXPORT:
3450 D = ExportDecl::CreateDeserialized(Context, ID);
3451 break;
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003452 case DECL_LABEL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003453 D = LabelDecl::CreateDeserialized(Context, ID);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00003454 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003455 case DECL_NAMESPACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003456 D = NamespaceDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003457 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003458 case DECL_NAMESPACE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003459 D = NamespaceAliasDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003460 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003461 case DECL_USING:
Douglas Gregor72172e92012-01-05 21:55:30 +00003462 D = UsingDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003463 break;
Richard Smith151c4562016-12-20 21:35:28 +00003464 case DECL_USING_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003465 D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith151c4562016-12-20 21:35:28 +00003466 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003467 case DECL_USING_SHADOW:
Douglas Gregor72172e92012-01-05 21:55:30 +00003468 D = UsingShadowDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003469 break;
Richard Smith5179eb72016-06-28 19:03:57 +00003470 case DECL_CONSTRUCTOR_USING_SHADOW:
3471 D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID);
3472 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003473 case DECL_USING_DIRECTIVE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003474 D = UsingDirectiveDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003475 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003476 case DECL_UNRESOLVED_USING_VALUE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003477 D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003478 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003479 case DECL_UNRESOLVED_USING_TYPENAME:
Douglas Gregor72172e92012-01-05 21:55:30 +00003480 D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003481 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003482 case DECL_CXX_RECORD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003483 D = CXXRecordDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003484 break;
Richard Smithbc491202017-02-17 20:05:37 +00003485 case DECL_CXX_DEDUCTION_GUIDE:
3486 D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID);
3487 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003488 case DECL_CXX_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003489 D = CXXMethodDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003490 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003491 case DECL_CXX_CONSTRUCTOR:
Richard Smith5179eb72016-06-28 19:03:57 +00003492 D = CXXConstructorDecl::CreateDeserialized(Context, ID, false);
3493 break;
3494 case DECL_CXX_INHERITED_CONSTRUCTOR:
3495 D = CXXConstructorDecl::CreateDeserialized(Context, ID, true);
Chris Lattnerca025db2010-05-07 21:43:38 +00003496 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003497 case DECL_CXX_DESTRUCTOR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003498 D = CXXDestructorDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003499 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003500 case DECL_CXX_CONVERSION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003501 D = CXXConversionDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003502 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003503 case DECL_ACCESS_SPEC:
Douglas Gregor72172e92012-01-05 21:55:30 +00003504 D = AccessSpecDecl::CreateDeserialized(Context, ID);
Abramo Bagnarad7340582010-06-05 05:09:32 +00003505 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003506 case DECL_FRIEND:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003507 D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt());
Chris Lattnerca025db2010-05-07 21:43:38 +00003508 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003509 case DECL_FRIEND_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003510 D = FriendTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003511 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003512 case DECL_CLASS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003513 D = ClassTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003514 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003515 case DECL_CLASS_TEMPLATE_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003516 D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003517 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003518 case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003519 D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003520 break;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003521 case DECL_VAR_TEMPLATE:
3522 D = VarTemplateDecl::CreateDeserialized(Context, ID);
3523 break;
3524 case DECL_VAR_TEMPLATE_SPECIALIZATION:
3525 D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID);
3526 break;
3527 case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION:
3528 D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID);
3529 break;
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003530 case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003531 D = ClassScopeFunctionSpecializationDecl::CreateDeserialized(Context, ID);
Francois Pichet00c7e6c2011-08-14 03:52:19 +00003532 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003533 case DECL_FUNCTION_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003534 D = FunctionTemplateDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003535 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003536 case DECL_TEMPLATE_TYPE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003537 D = TemplateTypeParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003538 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003539 case DECL_NON_TYPE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003540 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003541 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003542 case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003543 D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID,
3544 Record.readInt());
Douglas Gregor0231d8d2011-01-19 20:10:05 +00003545 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003546 case DECL_TEMPLATE_TEMPLATE_PARM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003547 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003548 break;
Richard Smith1fde8ec2012-09-07 02:06:42 +00003549 case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK:
3550 D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003551 Record.readInt());
Richard Smith1fde8ec2012-09-07 02:06:42 +00003552 break;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003553 case DECL_TYPE_ALIAS_TEMPLATE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003554 D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID);
Richard Smith3f1b5d02011-05-05 21:57:07 +00003555 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003556 case DECL_STATIC_ASSERT:
Douglas Gregor72172e92012-01-05 21:55:30 +00003557 D = StaticAssertDecl::CreateDeserialized(Context, ID);
Chris Lattnerca025db2010-05-07 21:43:38 +00003558 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003559 case DECL_OBJC_METHOD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003560 D = ObjCMethodDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003561 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003562 case DECL_OBJC_INTERFACE:
Douglas Gregor72172e92012-01-05 21:55:30 +00003563 D = ObjCInterfaceDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003564 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003565 case DECL_OBJC_IVAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003566 D = ObjCIvarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003567 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003568 case DECL_OBJC_PROTOCOL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003569 D = ObjCProtocolDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003570 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003571 case DECL_OBJC_AT_DEFS_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003572 D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003573 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003574 case DECL_OBJC_CATEGORY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003575 D = ObjCCategoryDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003576 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003577 case DECL_OBJC_CATEGORY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003578 D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003579 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003580 case DECL_OBJC_IMPLEMENTATION:
Douglas Gregor72172e92012-01-05 21:55:30 +00003581 D = ObjCImplementationDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003582 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003583 case DECL_OBJC_COMPATIBLE_ALIAS:
Douglas Gregor72172e92012-01-05 21:55:30 +00003584 D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003585 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003586 case DECL_OBJC_PROPERTY:
Douglas Gregor72172e92012-01-05 21:55:30 +00003587 D = ObjCPropertyDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003588 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003589 case DECL_OBJC_PROPERTY_IMPL:
Douglas Gregor72172e92012-01-05 21:55:30 +00003590 D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003591 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003592 case DECL_FIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003593 D = FieldDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003594 break;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003595 case DECL_INDIRECTFIELD:
Douglas Gregor72172e92012-01-05 21:55:30 +00003596 D = IndirectFieldDecl::CreateDeserialized(Context, ID);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003597 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003598 case DECL_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003599 D = VarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003600 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003601 case DECL_IMPLICIT_PARAM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003602 D = ImplicitParamDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003603 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003604 case DECL_PARM_VAR:
Douglas Gregor72172e92012-01-05 21:55:30 +00003605 D = ParmVarDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003606 break;
Richard Smith7b76d812016-08-12 02:21:25 +00003607 case DECL_DECOMPOSITION:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003608 D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt());
Richard Smith7b76d812016-08-12 02:21:25 +00003609 break;
3610 case DECL_BINDING:
3611 D = BindingDecl::CreateDeserialized(Context, ID);
3612 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003613 case DECL_FILE_SCOPE_ASM:
Douglas Gregor72172e92012-01-05 21:55:30 +00003614 D = FileScopeAsmDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003615 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003616 case DECL_BLOCK:
Douglas Gregor72172e92012-01-05 21:55:30 +00003617 D = BlockDecl::CreateDeserialized(Context, ID);
Chris Lattner487412d2009-04-27 05:27:42 +00003618 break;
John McCall5e77d762013-04-16 07:28:30 +00003619 case DECL_MS_PROPERTY:
3620 D = MSPropertyDecl::CreateDeserialized(Context, ID);
3621 break;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003622 case DECL_CAPTURED:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003623 D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt());
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003624 break;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003625 case DECL_CXX_BASE_SPECIFIERS:
3626 Error("attempt to read a C++ base-specifier record as a declaration");
Craig Toppera13603a2014-05-22 05:54:18 +00003627 return nullptr;
Richard Smithc2bb8182015-03-24 06:36:48 +00003628 case DECL_CXX_CTOR_INITIALIZERS:
3629 Error("attempt to read a C++ ctor initializer record as a declaration");
3630 return nullptr;
Douglas Gregorba345522011-12-02 23:23:56 +00003631 case DECL_IMPORT:
3632 // Note: last entry of the ImportDecl record is the number of stored source
3633 // locations.
Douglas Gregor72172e92012-01-05 21:55:30 +00003634 D = ImportDecl::CreateDeserialized(Context, ID, Record.back());
Douglas Gregorba345522011-12-02 23:23:56 +00003635 break;
Alexey Bataeva769e072013-03-22 06:34:35 +00003636 case DECL_OMP_THREADPRIVATE:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003637 D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, Record.readInt());
Alexey Bataeva769e072013-03-22 06:34:35 +00003638 break;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003639 case DECL_OMP_DECLARE_REDUCTION:
3640 D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID);
3641 break;
Alexey Bataev4244be22016-02-11 05:35:55 +00003642 case DECL_OMP_CAPTUREDEXPR:
3643 D = OMPCapturedExprDecl::CreateDeserialized(Context, ID);
Alexey Bataev90c228f2016-02-08 09:29:13 +00003644 break;
Nico Weber66220292016-03-02 17:28:48 +00003645 case DECL_PRAGMA_COMMENT:
David L. Jonesbe1557a2016-12-21 00:17:49 +00003646 D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt());
Nico Weber66220292016-03-02 17:28:48 +00003647 break;
Nico Webercbbaeb12016-03-02 19:28:54 +00003648 case DECL_PRAGMA_DETECT_MISMATCH:
3649 D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID,
David L. Jonesbe1557a2016-12-21 00:17:49 +00003650 Record.readInt());
Nico Webercbbaeb12016-03-02 19:28:54 +00003651 break;
Michael Han84324352013-02-22 17:15:32 +00003652 case DECL_EMPTY:
3653 D = EmptyDecl::CreateDeserialized(Context, ID);
3654 break;
Douglas Gregor85f3f952015-07-07 03:57:15 +00003655 case DECL_OBJC_TYPE_PARAM:
3656 D = ObjCTypeParamDecl::CreateDeserialized(Context, ID);
3657 break;
Chris Lattner487412d2009-04-27 05:27:42 +00003658 }
Chris Lattner487412d2009-04-27 05:27:42 +00003659
Sebastian Redlb3298c32010-08-18 23:56:48 +00003660 assert(D && "Unknown declaration reading AST file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00003661 LoadedDecl(Index, D);
Argyrios Kyrtzidis6c075472012-02-09 06:02:44 +00003662 // Set the DeclContext before doing any deserialization, to make sure internal
3663 // calls to Decl::getASTContext() by Decl's methods will find the
3664 // TranslationUnitDecl without crashing.
3665 D->setDeclContext(Context.getTranslationUnitDecl());
Chris Lattner8f63ab52009-04-27 06:01:06 +00003666 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00003667
3668 // If this declaration is also a declaration context, get the
3669 // offsets for its tables of lexical and visible declarations.
3670 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
3671 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003672 if (Offsets.first &&
3673 ReadLexicalDeclContextStorage(*Loc.F, DeclsCursor, Offsets.first, DC))
3674 return nullptr;
3675 if (Offsets.second &&
3676 ReadVisibleDeclContextStorage(*Loc.F, DeclsCursor, Offsets.second, ID))
3677 return nullptr;
Chris Lattner487412d2009-04-27 05:27:42 +00003678 }
David L. Jonesbe1557a2016-12-21 00:17:49 +00003679 assert(Record.getIdx() == Record.size());
Chris Lattner487412d2009-04-27 05:27:42 +00003680
Douglas Gregordab42432011-08-12 00:15:20 +00003681 // Load any relevant update records.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003682 PendingUpdateRecords.push_back(
3683 PendingUpdateRecord(ID, D, /*JustLoaded=*/true));
Argyrios Kyrtzidis7d268c32011-11-14 07:07:59 +00003684
Douglas Gregor404cdde2012-01-27 01:47:08 +00003685 // Load the categories after recursive loading is finished.
3686 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Manman Renec315f12016-09-09 23:48:27 +00003687 // If we already have a definition when deserializing the ObjCInterfaceDecl,
3688 // we put the Decl in PendingDefinitions so we can pull the categories here.
3689 if (Class->isThisDeclarationADefinition() ||
3690 PendingDefinitions.count(Class))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003691 loadObjCCategories(ID, Class);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003692
Richard Smith13fb8602016-07-15 21:33:46 +00003693 // If we have deserialized a declaration that has a definition the
3694 // AST consumer might need to know about, queue it.
3695 // We don't pass it to the consumer immediately because we may be in recursive
3696 // loading, and some declarations may still be initializing.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003697 PotentiallyInterestingDecls.push_back(
3698 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith13fb8602016-07-15 21:33:46 +00003699
Douglas Gregordab42432011-08-12 00:15:20 +00003700 return D;
3701}
3702
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003703void ASTReader::PassInterestingDeclsToConsumer() {
3704 assert(Consumer);
3705
3706 if (PassingDeclsToConsumer)
3707 return;
3708
3709 // Guard variable to avoid recursively redoing the process of passing
3710 // decls to consumer.
3711 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
3712 true);
3713
3714 // Ensure that we've loaded all potentially-interesting declarations
3715 // that need to be eagerly loaded.
3716 for (auto ID : EagerlyDeserializedDecls)
3717 GetDecl(ID);
3718 EagerlyDeserializedDecls.clear();
3719
3720 while (!PotentiallyInterestingDecls.empty()) {
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003721 InterestingDecl D = PotentiallyInterestingDecls.front();
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003722 PotentiallyInterestingDecls.pop_front();
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003723 if (isConsumerInterestedIn(getContext(), D.getDecl(), D.hasPendingBody()))
3724 PassInterestingDeclToConsumer(D.getDecl());
Vassil Vassilev46afbbb2017-04-12 21:56:05 +00003725 }
3726}
3727
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003728void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003729 // The declaration may have been modified by files later in the chain.
3730 // If this is the case, read the record containing the updates from each file
3731 // and pass it to ASTDeclReader to make the modifications.
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003732 serialization::GlobalDeclID ID = Record.ID;
3733 Decl *D = Record.D;
Vassil Vassilev19765fb2016-07-22 21:08:24 +00003734 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003735 DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
Vassil Vassilev7d264622017-06-30 22:40:17 +00003736
3737 llvm::SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs;
3738
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003739 if (UpdI != DeclUpdateOffsets.end()) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003740 auto UpdateOffsets = std::move(UpdI->second);
3741 DeclUpdateOffsets.erase(UpdI);
3742
Vassil Vassilev74c3e8c2017-05-19 16:46:06 +00003743 // Check if this decl was interesting to the consumer. If we just loaded
3744 // the declaration, then we know it was interesting and we skip the call
3745 // to isConsumerInterestedIn because it is unsafe to call in the
3746 // current ASTReader state.
3747 bool WasInteresting =
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003748 Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
Richard Smith0f4e2c42015-08-06 04:23:48 +00003749 for (auto &FileAndOffset : UpdateOffsets) {
3750 ModuleFile *F = FileAndOffset.first;
3751 uint64_t Offset = FileAndOffset.second;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003752 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3753 SavedStreamPosition SavedPosition(Cursor);
3754 Cursor.JumpToBit(Offset);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003755 unsigned Code = Cursor.ReadCode();
David L. Jonesbe1557a2016-12-21 00:17:49 +00003756 ASTRecordReader Record(*this, *F);
3757 unsigned RecCode = Record.readRecord(Cursor, Code);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003758 (void)RecCode;
3759 assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
Richard Smith04d05b52014-03-23 00:27:18 +00003760
David L. Jonesbe1557a2016-12-21 00:17:49 +00003761 ASTDeclReader Reader(*this, Record, RecordLocation(F, Offset), ID,
3762 SourceLocation());
Vassil Vassilev7d264622017-06-30 22:40:17 +00003763 Reader.UpdateDecl(D, PendingLazySpecializationIDs);
Richard Smith04d05b52014-03-23 00:27:18 +00003764
3765 // We might have made this declaration interesting. If so, remember that
3766 // we need to hand it off to the consumer.
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00003767 if (!WasInteresting &&
3768 isConsumerInterestedIn(getContext(), D, Reader.hasPendingBody())) {
3769 PotentiallyInterestingDecls.push_back(
3770 InterestingDecl(D, Reader.hasPendingBody()));
Richard Smith04d05b52014-03-23 00:27:18 +00003771 WasInteresting = true;
3772 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003773 }
3774 }
Vassil Vassilev7d264622017-06-30 22:40:17 +00003775 // Add the lazy specializations to the template.
3776 assert((PendingLazySpecializationIDs.empty() || isa<ClassTemplateDecl>(D) ||
3777 isa<FunctionTemplateDecl>(D) || isa<VarTemplateDecl>(D)) &&
3778 "Must not have pending specializations");
3779 if (auto *CTD = dyn_cast<ClassTemplateDecl>(D))
3780 ASTDeclReader::AddLazySpecializations(CTD, PendingLazySpecializationIDs);
3781 else if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
3782 ASTDeclReader::AddLazySpecializations(FTD, PendingLazySpecializationIDs);
3783 else if (auto *VTD = dyn_cast<VarTemplateDecl>(D))
3784 ASTDeclReader::AddLazySpecializations(VTD, PendingLazySpecializationIDs);
3785 PendingLazySpecializationIDs.clear();
Richard Smith1e8e91b2016-04-08 20:53:26 +00003786
3787 // Load the pending visible updates for this decl context, if it has any.
3788 auto I = PendingVisibleUpdates.find(ID);
3789 if (I != PendingVisibleUpdates.end()) {
3790 auto VisibleUpdates = std::move(I->second);
3791 PendingVisibleUpdates.erase(I);
3792
3793 auto *DC = cast<DeclContext>(D)->getPrimaryContext();
3794 for (const PendingVisibleUpdate &Update : VisibleUpdates)
3795 Lookups[DC].Table.add(
3796 Update.Mod, Update.Data,
3797 reader::ASTDeclContextNameLookupTrait(*this, *Update.Mod));
3798 DC->setHasExternalVisibleStorage(true);
3799 }
Chris Lattner487412d2009-04-27 05:27:42 +00003800}
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003801
Richard Smithd61d4ac2015-08-22 20:13:39 +00003802void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
3803 // Attach FirstLocal to the end of the decl chain.
Richard Smithd8a83712015-08-22 01:47:18 +00003804 Decl *CanonDecl = FirstLocal->getCanonicalDecl();
Richard Smithd61d4ac2015-08-22 20:13:39 +00003805 if (FirstLocal != CanonDecl) {
3806 Decl *PrevMostRecent = ASTDeclReader::getMostRecentDecl(CanonDecl);
3807 ASTDeclReader::attachPreviousDecl(
3808 *this, FirstLocal, PrevMostRecent ? PrevMostRecent : CanonDecl,
3809 CanonDecl);
3810 }
3811
3812 if (!LocalOffset) {
3813 ASTDeclReader::attachLatestDecl(CanonDecl, FirstLocal);
3814 return;
3815 }
3816
3817 // Load the list of other redeclarations from this module file.
3818 ModuleFile *M = getOwningModuleFile(FirstLocal);
3819 assert(M && "imported decl from no module file");
3820
3821 llvm::BitstreamCursor &Cursor = M->DeclsCursor;
3822 SavedStreamPosition SavedPosition(Cursor);
3823 Cursor.JumpToBit(LocalOffset);
3824
3825 RecordData Record;
3826 unsigned Code = Cursor.ReadCode();
3827 unsigned RecCode = Cursor.readRecord(Code, Record);
3828 (void)RecCode;
3829 assert(RecCode == LOCAL_REDECLARATIONS && "expected LOCAL_REDECLARATIONS record!");
3830
3831 // FIXME: We have several different dispatches on decl kind here; maybe
3832 // we should instead generate one loop per kind and dispatch up-front?
3833 Decl *MostRecent = FirstLocal;
3834 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3835 auto *D = GetLocalDecl(*M, Record[N - I - 1]);
Richard Smithe2f8ce92015-07-15 00:02:40 +00003836 ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
3837 MostRecent = D;
Douglas Gregor05f10352011-12-17 23:38:30 +00003838 }
Richard Smithc3a53252015-02-28 05:57:02 +00003839 ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
Douglas Gregor05f10352011-12-17 23:38:30 +00003840}
3841
3842namespace {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003843 /// \brief Given an ObjC interface, goes through the modules and links to the
3844 /// interface all the categories for it.
Douglas Gregor404cdde2012-01-27 01:47:08 +00003845 class ObjCCategoriesVisitor {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003846 ASTReader &Reader;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003847 ObjCInterfaceDecl *Interface;
Craig Topper4dd9b432014-08-17 23:49:53 +00003848 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003849 ObjCCategoryDecl *Tail;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003850 llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003851 serialization::GlobalDeclID InterfaceID;
3852 unsigned PreviousGeneration;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003853
3854 void add(ObjCCategoryDecl *Cat) {
3855 // Only process each category once.
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00003856 if (!Deserialized.erase(Cat))
Douglas Gregor404cdde2012-01-27 01:47:08 +00003857 return;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003858
3859 // Check for duplicate categories.
3860 if (Cat->getDeclName()) {
3861 ObjCCategoryDecl *&Existing = NameCategoryMap[Cat->getDeclName()];
3862 if (Existing &&
3863 Reader.getOwningModuleFile(Existing)
3864 != Reader.getOwningModuleFile(Cat)) {
3865 // FIXME: We should not warn for duplicates in diamond:
3866 //
3867 // MT //
3868 // / \ //
3869 // ML MR //
3870 // \ / //
3871 // MB //
3872 //
3873 // If there are duplicates in ML/MR, there will be warning when
3874 // creating MB *and* when importing MB. We should not warn when
3875 // importing.
3876 Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
3877 << Interface->getDeclName() << Cat->getDeclName();
3878 Reader.Diag(Existing->getLocation(), diag::note_previous_definition);
3879 } else if (!Existing) {
3880 // Record this category.
3881 Existing = Cat;
3882 }
3883 }
3884
3885 // Add this category to the end of the chain.
3886 if (Tail)
3887 ASTDeclReader::setNextObjCCategory(Tail, Cat);
3888 else
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003889 Interface->setCategoryListRaw(Cat);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003890 Tail = Cat;
3891 }
3892
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003893 public:
Douglas Gregor404cdde2012-01-27 01:47:08 +00003894 ObjCCategoriesVisitor(ASTReader &Reader,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003895 ObjCInterfaceDecl *Interface,
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003896 llvm::SmallPtrSetImpl<ObjCCategoryDecl *> &Deserialized,
3897 serialization::GlobalDeclID InterfaceID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003898 unsigned PreviousGeneration)
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003899 : Reader(Reader), Interface(Interface), Deserialized(Deserialized),
3900 Tail(nullptr), InterfaceID(InterfaceID),
3901 PreviousGeneration(PreviousGeneration)
Douglas Gregor404cdde2012-01-27 01:47:08 +00003902 {
3903 // Populate the name -> category map with the set of known categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003904 for (auto *Cat : Interface->known_categories()) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003905 if (Cat->getDeclName())
Aaron Ballman15063e12014-03-13 21:35:02 +00003906 NameCategoryMap[Cat->getDeclName()] = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003907
3908 // Keep track of the tail of the category list.
Aaron Ballman15063e12014-03-13 21:35:02 +00003909 Tail = Cat;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003910 }
3911 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003912
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003913 bool operator()(ModuleFile &M) {
Douglas Gregor404cdde2012-01-27 01:47:08 +00003914 // If we've loaded all of the category information we care about from
3915 // this module file, we're done.
3916 if (M.Generation <= PreviousGeneration)
3917 return true;
3918
3919 // Map global ID of the definition down to the local ID used in this
3920 // module file. If there is no such mapping, we'll find nothing here
3921 // (or in any module it imports).
3922 DeclID LocalID = Reader.mapGlobalIDToModuleFileGlobalID(M, InterfaceID);
3923 if (!LocalID)
3924 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003925
Douglas Gregor404cdde2012-01-27 01:47:08 +00003926 // Perform a binary search to find the local redeclarations for this
3927 // declaration (if any).
Benjamin Kramera6f39502014-03-15 14:21:58 +00003928 const ObjCCategoriesInfo Compare = { LocalID, 0 };
Douglas Gregor404cdde2012-01-27 01:47:08 +00003929 const ObjCCategoriesInfo *Result
3930 = std::lower_bound(M.ObjCCategoriesMap,
3931 M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
Benjamin Kramera6f39502014-03-15 14:21:58 +00003932 Compare);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003933 if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
3934 Result->DefinitionID != LocalID) {
3935 // We didn't find anything. If the class definition is in this module
3936 // file, then the module files it depends on cannot have any categories,
3937 // so suppress further lookup.
3938 return Reader.isDeclIDFromModule(InterfaceID, M);
3939 }
3940
3941 // We found something. Dig out all of the categories.
3942 unsigned Offset = Result->Offset;
3943 unsigned N = M.ObjCCategories[Offset];
3944 M.ObjCCategories[Offset++] = 0; // Don't try to deserialize again
3945 for (unsigned I = 0; I != N; ++I)
3946 add(cast_or_null<ObjCCategoryDecl>(
3947 Reader.GetLocalDecl(M, M.ObjCCategories[Offset++])));
3948 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003949 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003950 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00003951} // end anonymous namespace
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003952
Douglas Gregor404cdde2012-01-27 01:47:08 +00003953void ASTReader::loadObjCCategories(serialization::GlobalDeclID ID,
3954 ObjCInterfaceDecl *D,
3955 unsigned PreviousGeneration) {
Alexander Shaposhnikov96cbe7b2016-09-24 02:07:19 +00003956 ObjCCategoriesVisitor Visitor(*this, D, CategoriesDeserialized, ID,
Douglas Gregor404cdde2012-01-27 01:47:08 +00003957 PreviousGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00003958 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003959}
Douglas Gregordab42432011-08-12 00:15:20 +00003960
Richard Smithd6db68c2014-08-07 20:58:41 +00003961template<typename DeclT, typename Fn>
3962static void forAllLaterRedecls(DeclT *D, Fn F) {
3963 F(D);
3964
3965 // Check whether we've already merged D into its redeclaration chain.
3966 // MostRecent may or may not be nullptr if D has not been merged. If
3967 // not, walk the merged redecl chain and see if it's there.
3968 auto *MostRecent = D->getMostRecentDecl();
3969 bool Found = false;
3970 for (auto *Redecl = MostRecent; Redecl && !Found;
3971 Redecl = Redecl->getPreviousDecl())
3972 Found = (Redecl == D);
3973
3974 // If this declaration is merged, apply the functor to all later decls.
3975 if (Found) {
3976 for (auto *Redecl = MostRecent; Redecl != D;
3977 Redecl = Redecl->getPreviousDecl())
3978 F(Redecl);
3979 }
3980}
3981
Vassil Vassilev7d264622017-06-30 22:40:17 +00003982void ASTDeclReader::UpdateDecl(Decl *D,
3983 llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) {
David L. Jonesbe1557a2016-12-21 00:17:49 +00003984 while (Record.getIdx() < Record.size()) {
3985 switch ((DeclUpdateKind)Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003986 case UPD_CXX_ADDED_IMPLICIT_MEMBER: {
Richard Smith1b65dbc2015-01-22 03:50:31 +00003987 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithd6db68c2014-08-07 20:58:41 +00003988 // FIXME: If we also have an update record for instantiating the
3989 // definition of D, we need that to happen before we get here.
David L. Jonesb6a8f022016-12-21 04:34:52 +00003990 Decl *MD = Record.readDecl();
Richard Smithcd45dbc2014-04-19 03:48:30 +00003991 assert(MD && "couldn't read decl from update record");
Richard Smith46bb5812014-08-01 01:56:39 +00003992 // FIXME: We should call addHiddenDecl instead, to add the member
3993 // to its DeclContext.
Richard Smith1b65dbc2015-01-22 03:50:31 +00003994 RD->addedMember(MD);
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003995 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003996 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003997
3998 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
Vassil Vassilev7d264622017-06-30 22:40:17 +00003999 // It will be added to the template's lazy specialization set.
4000 PendingLazySpecializationIDs.push_back(ReadDeclID());
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004001 break;
4002
4003 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004004 NamespaceDecl *Anon = ReadDeclAs<NamespaceDecl>();
4005
Douglas Gregor540fd812012-01-09 18:07:24 +00004006 // Each module has its own anonymous namespace, which is disjoint from
4007 // any other module's anonymous namespaces, so don't attach the anonymous
4008 // namespace at all.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004009 if (!Record.isModule()) {
Douglas Gregor540fd812012-01-09 18:07:24 +00004010 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
4011 TU->setAnonymousNamespace(Anon);
4012 else
4013 cast<NamespaceDecl>(D)->setAnonymousNamespace(Anon);
4014 }
Sebastian Redlfa1f3702011-04-24 16:28:13 +00004015 break;
4016 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004017
Richard Smith891fc7f2017-12-05 01:31:47 +00004018 case UPD_CXX_ADDED_VAR_DEFINITION: {
Richard Smith05a21352017-06-22 22:18:46 +00004019 VarDecl *VD = cast<VarDecl>(D);
Richard Smithf5017592017-11-02 01:06:00 +00004020 VD->NonParmVarDeclBits.IsInline = Record.readInt();
4021 VD->NonParmVarDeclBits.IsInlineSpecified = Record.readInt();
Richard Smith05a21352017-06-22 22:18:46 +00004022 uint64_t Val = Record.readInt();
4023 if (Val && !VD->getInit()) {
4024 VD->setInit(Record.readExpr());
4025 if (Val > 1) { // IsInitKnownICE = 1, IsInitNotICE = 2, IsInitICE = 3
4026 EvaluatedStmt *Eval = VD->ensureEvaluatedStmt();
4027 Eval->CheckedICE = true;
4028 Eval->IsICE = Val == 3;
4029 }
4030 }
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004031 break;
Richard Smith05a21352017-06-22 22:18:46 +00004032 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004033
Richard Smith891fc7f2017-12-05 01:31:47 +00004034 case UPD_CXX_POINT_OF_INSTANTIATION: {
4035 SourceLocation POI = Record.readSourceLocation();
4036 if (VarTemplateSpecializationDecl *VTSD =
4037 dyn_cast<VarTemplateSpecializationDecl>(D)) {
4038 VTSD->setPointOfInstantiation(POI);
4039 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
4040 VD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
4041 } else {
4042 auto *FD = cast<FunctionDecl>(D);
4043 if (auto *FTSInfo = FD->TemplateOrSpecialization
4044 .dyn_cast<FunctionTemplateSpecializationInfo *>())
4045 FTSInfo->setPointOfInstantiation(POI);
4046 else
4047 FD->TemplateOrSpecialization.get<MemberSpecializationInfo *>()
4048 ->setPointOfInstantiation(POI);
4049 }
4050 break;
4051 }
4052
John McCall32791cc2016-01-06 22:34:54 +00004053 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT: {
4054 auto Param = cast<ParmVarDecl>(D);
4055
4056 // We have to read the default argument regardless of whether we use it
4057 // so that hypothetical further update records aren't messed up.
4058 // TODO: Add a function to skip over the next expr record.
David L. Jonesb6a8f022016-12-21 04:34:52 +00004059 auto DefaultArg = Record.readExpr();
John McCall32791cc2016-01-06 22:34:54 +00004060
4061 // Only apply the update if the parameter still has an uninstantiated
4062 // default argument.
4063 if (Param->hasUninstantiatedDefaultArg())
4064 Param->setDefaultArg(DefaultArg);
4065 break;
4066 }
4067
Richard Smith4b054b22016-08-24 21:25:37 +00004068 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
4069 auto FD = cast<FieldDecl>(D);
David L. Jonesb6a8f022016-12-21 04:34:52 +00004070 auto DefaultInit = Record.readExpr();
Richard Smith4b054b22016-08-24 21:25:37 +00004071
4072 // Only apply the update if the field still has an uninstantiated
4073 // default member initializer.
4074 if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
4075 if (DefaultInit)
4076 FD->setInClassInitializer(DefaultInit);
4077 else
4078 // Instantiation failed. We can get here if we serialized an AST for
4079 // an invalid program.
4080 FD->removeInClassInitializer();
4081 }
4082 break;
4083 }
4084
Richard Smith4d235792014-08-07 18:53:08 +00004085 case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
Richard Smithd28ac5b2014-03-22 23:33:22 +00004086 FunctionDecl *FD = cast<FunctionDecl>(D);
Daniel Jasper4a6d5b72017-10-11 07:47:54 +00004087 if (Reader.PendingBodies[FD]) {
4088 // FIXME: Maybe check for ODR violations.
4089 // It's safe to stop now because this update record is always last.
4090 return;
4091 }
4092
David L. Jonesbe1557a2016-12-21 00:17:49 +00004093 if (Record.readInt()) {
Richard Smith195d8ef2014-05-29 03:15:31 +00004094 // Maintain AST consistency: any later redeclarations of this function
4095 // are inline if this one is. (We might have merged another declaration
4096 // into this one.)
Richard Smithd6db68c2014-08-07 20:58:41 +00004097 forAllLaterRedecls(FD, [](FunctionDecl *FD) {
4098 FD->setImplicitlyInline();
4099 });
Richard Smith195d8ef2014-05-29 03:15:31 +00004100 }
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004101 FD->setInnerLocStart(ReadSourceLocation());
David Blaikieac4345c2017-02-12 18:45:31 +00004102 ReadFunctionDefinition(FD);
David L. Jonesbe1557a2016-12-21 00:17:49 +00004103 assert(Record.getIdx() == Record.size() && "lazy body must be last");
Richard Smithd28ac5b2014-03-22 23:33:22 +00004104 break;
4105 }
4106
Richard Smithcd45dbc2014-04-19 03:48:30 +00004107 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4108 auto *RD = cast<CXXRecordDecl>(D);
Richard Smithb6483992016-05-17 22:44:15 +00004109 auto *OldDD = RD->getCanonicalDecl()->DefinitionData;
Richard Smith2a9e5c52015-02-03 03:32:14 +00004110 bool HadRealDefinition =
Richard Smith7483d202015-02-04 01:23:46 +00004111 OldDD && (OldDD->Definition != RD ||
4112 !Reader.PendingFakeDefinitionData.count(OldDD));
Akira Hatanakad791e922018-03-19 17:38:40 +00004113 RD->setCanPassInRegisters(Record.readInt());
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00004114 RD->setParamDestroyedInCallee(Record.readInt());
Richard Smith2a9e5c52015-02-03 03:32:14 +00004115 ReadCXXRecordDefinition(RD, /*Update*/true);
4116
Richard Smithcd45dbc2014-04-19 03:48:30 +00004117 // Visible update is handled separately.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004118 uint64_t LexicalOffset = ReadLocalOffset();
Richard Smith8a639892015-01-24 01:07:20 +00004119 if (!HadRealDefinition && LexicalOffset) {
David L. Jonesb6a8f022016-12-21 04:34:52 +00004120 Record.readLexicalDeclContextStorage(LexicalOffset, RD);
Richard Smith2a9e5c52015-02-03 03:32:14 +00004121 Reader.PendingFakeDefinitionData.erase(OldDD);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004122 }
4123
David L. Jonesbe1557a2016-12-21 00:17:49 +00004124 auto TSK = (TemplateSpecializationKind)Record.readInt();
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004125 SourceLocation POI = ReadSourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004126 if (MemberSpecializationInfo *MSInfo =
4127 RD->getMemberSpecializationInfo()) {
4128 MSInfo->setTemplateSpecializationKind(TSK);
4129 MSInfo->setPointOfInstantiation(POI);
4130 } else {
4131 ClassTemplateSpecializationDecl *Spec =
4132 cast<ClassTemplateSpecializationDecl>(RD);
4133 Spec->setTemplateSpecializationKind(TSK);
4134 Spec->setPointOfInstantiation(POI);
Richard Smithdf352052014-05-22 20:59:29 +00004135
David L. Jonesbe1557a2016-12-21 00:17:49 +00004136 if (Record.readInt()) {
Richard Smithdf352052014-05-22 20:59:29 +00004137 auto PartialSpec =
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004138 ReadDeclAs<ClassTemplatePartialSpecializationDecl>();
Richard Smithdf352052014-05-22 20:59:29 +00004139 SmallVector<TemplateArgument, 8> TemplArgs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004140 Record.readTemplateArgumentList(TemplArgs);
Richard Smithdf352052014-05-22 20:59:29 +00004141 auto *TemplArgList = TemplateArgumentList::CreateCopy(
David Majnemer8b622692016-07-03 21:17:51 +00004142 Reader.getContext(), TemplArgs);
Richard Smith72544f82014-08-14 03:30:27 +00004143
4144 // FIXME: If we already have a partial specialization set,
4145 // check that it matches.
4146 if (!Spec->getSpecializedTemplateOrPartial()
4147 .is<ClassTemplatePartialSpecializationDecl *>())
4148 Spec->setInstantiationOf(PartialSpec, TemplArgList);
Richard Smithdf352052014-05-22 20:59:29 +00004149 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004150 }
4151
David L. Jonesbe1557a2016-12-21 00:17:49 +00004152 RD->setTagKind((TagTypeKind)Record.readInt());
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004153 RD->setLocation(ReadSourceLocation());
4154 RD->setLocStart(ReadSourceLocation());
4155 RD->setBraceRange(ReadSourceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004156
David L. Jonesbe1557a2016-12-21 00:17:49 +00004157 if (Record.readInt()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00004158 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004159 Record.readAttributes(Attrs);
Richard Smith842e46e2016-10-26 02:31:56 +00004160 // If the declaration already has attributes, we assume that some other
4161 // AST file already loaded them.
4162 if (!D->hasAttrs())
4163 D->setAttrsImpl(Attrs, Reader.getContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004164 }
4165 break;
4166 }
4167
Richard Smithf8134002015-03-10 01:41:22 +00004168 case UPD_CXX_RESOLVED_DTOR_DELETE: {
4169 // Set the 'operator delete' directly to avoid emitting another update
4170 // record.
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004171 auto *Del = ReadDeclAs<FunctionDecl>();
Richard Smithf8134002015-03-10 01:41:22 +00004172 auto *First = cast<CXXDestructorDecl>(D->getCanonicalDecl());
Richard Smith5b349582017-10-13 01:55:36 +00004173 auto *ThisArg = Record.readExpr();
Richard Smithf8134002015-03-10 01:41:22 +00004174 // FIXME: Check consistency if we have an old and new operator delete.
Richard Smith5b349582017-10-13 01:55:36 +00004175 if (!First->OperatorDelete) {
Richard Smithf8134002015-03-10 01:41:22 +00004176 First->OperatorDelete = Del;
Richard Smith5b349582017-10-13 01:55:36 +00004177 First->OperatorDeleteThisArg = ThisArg;
4178 }
Richard Smithf8134002015-03-10 01:41:22 +00004179 break;
4180 }
4181
Richard Smith564417a2014-03-20 21:47:22 +00004182 case UPD_CXX_RESOLVED_EXCEPTION_SPEC: {
Richard Smith8acb4282014-07-31 21:57:55 +00004183 FunctionProtoType::ExceptionSpecInfo ESI;
Richard Smith6de7a242014-07-31 23:46:44 +00004184 SmallVector<QualType, 8> ExceptionStorage;
David L. Jonesbe1557a2016-12-21 00:17:49 +00004185 Record.readExceptionSpec(ExceptionStorage, ESI);
Richard Smith9e2341d2015-03-23 03:25:59 +00004186
4187 // Update this declaration's exception specification, if needed.
4188 auto *FD = cast<FunctionDecl>(D);
4189 auto *FPT = FD->getType()->castAs<FunctionProtoType>();
4190 // FIXME: If the exception specification is already present, check that it
4191 // matches.
4192 if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
Richard Smithdbafb6c2017-06-29 23:23:46 +00004193 FD->setType(Reader.getContext().getFunctionType(
Richard Smith6de7a242014-07-31 23:46:44 +00004194 FPT->getReturnType(), FPT->getParamTypes(),
4195 FPT->getExtProtoInfo().withExceptionSpec(ESI)));
Richard Smith9e2341d2015-03-23 03:25:59 +00004196
4197 // When we get to the end of deserializing, see if there are other decls
4198 // that we need to propagate this exception specification onto.
4199 Reader.PendingExceptionSpecUpdates.insert(
4200 std::make_pair(FD->getCanonicalDecl(), FD));
Richard Smith6de7a242014-07-31 23:46:44 +00004201 }
Richard Smith564417a2014-03-20 21:47:22 +00004202 break;
4203 }
4204
Richard Smith1fa5d642013-05-11 05:45:24 +00004205 case UPD_CXX_DEDUCED_RETURN_TYPE: {
Richard Smithd6db68c2014-08-07 20:58:41 +00004206 // FIXME: Also do this when merging redecls.
David L. Jonesbe1557a2016-12-21 00:17:49 +00004207 QualType DeducedResultType = Record.readType();
Richard Smithd6db68c2014-08-07 20:58:41 +00004208 for (auto *Redecl : merged_redecls(D)) {
4209 // FIXME: If the return type is already deduced, check that it matches.
4210 FunctionDecl *FD = cast<FunctionDecl>(Redecl);
Richard Smithdbafb6c2017-06-29 23:23:46 +00004211 Reader.getContext().adjustDeducedFunctionResultType(FD,
4212 DeducedResultType);
Richard Smithd6db68c2014-08-07 20:58:41 +00004213 }
Richard Smith1fa5d642013-05-11 05:45:24 +00004214 break;
4215 }
Eli Friedman276dd182013-09-05 00:02:25 +00004216
4217 case UPD_DECL_MARKED_USED: {
Richard Smith675d2792014-06-16 20:26:19 +00004218 // Maintain AST consistency: any later redeclarations are used too.
Richard Smithdbafb6c2017-06-29 23:23:46 +00004219 D->markUsed(Reader.getContext());
Eli Friedman276dd182013-09-05 00:02:25 +00004220 break;
4221 }
Richard Smith5652c0f2014-03-21 01:48:23 +00004222
4223 case UPD_MANGLING_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004224 Reader.getContext().setManglingNumber(cast<NamedDecl>(D),
4225 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004226 break;
4227
4228 case UPD_STATIC_LOCAL_NUMBER:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004229 Reader.getContext().setStaticLocalNumber(cast<VarDecl>(D),
4230 Record.readInt());
Richard Smith5652c0f2014-03-21 01:48:23 +00004231 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004232
Alexey Bataev97720002014-11-11 04:05:39 +00004233 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smithdbafb6c2017-06-29 23:23:46 +00004234 D->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(Reader.getContext(),
4235 ReadSourceRange()));
Alexey Bataev97720002014-11-11 04:05:39 +00004236 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004237
Alex Denisovfde64952015-06-26 05:28:36 +00004238 case UPD_DECL_EXPORTED: {
David L. Jonesc4808b9e2016-12-15 20:53:26 +00004239 unsigned SubmoduleID = readSubmoduleID();
Richard Smithbeb44782015-06-20 01:05:19 +00004240 auto *Exported = cast<NamedDecl>(D);
4241 if (auto *TD = dyn_cast<TagDecl>(Exported))
4242 Exported = TD->getDefinition();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004243 Module *Owner = SubmoduleID ? Reader.getSubmodule(SubmoduleID) : nullptr;
Richard Smith42413142015-05-15 20:05:43 +00004244 if (Reader.getContext().getLangOpts().ModulesLocalVisibility) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00004245 Reader.getContext().mergeDefinitionIntoModule(cast<NamedDecl>(Exported),
4246 Owner);
Richard Smith1e02a5a2015-06-25 21:42:33 +00004247 Reader.PendingMergedDefinitionsToDeduplicate.insert(
4248 cast<NamedDecl>(Exported));
Richard Smith42413142015-05-15 20:05:43 +00004249 } else if (Owner && Owner->NameVisibility != Module::AllVisible) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00004250 // If Owner is made visible at some later point, make this declaration
4251 // visible too.
Richard Smith1e02a5a2015-06-25 21:42:33 +00004252 Reader.HiddenNamesMap[Owner].push_back(Exported);
Richard Smith65ebb4a2015-03-26 04:09:53 +00004253 } else {
4254 // The declaration is now visible.
Richard Smith90dc5252017-06-23 01:04:34 +00004255 Exported->setVisibleDespiteOwningModule();
Richard Smith65ebb4a2015-03-26 04:09:53 +00004256 }
4257 break;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004258 }
Alex Denisovfde64952015-06-26 05:28:36 +00004259
Dmitry Polukhind69b5052016-05-09 14:59:13 +00004260 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
Alex Denisovfde64952015-06-26 05:28:36 +00004261 case UPD_ADDED_ATTR_TO_RECORD:
4262 AttrVec Attrs;
David L. Jonesb6a8f022016-12-21 04:34:52 +00004263 Record.readAttributes(Attrs);
Alex Denisovfde64952015-06-26 05:28:36 +00004264 assert(Attrs.size() == 1);
4265 D->addAttr(Attrs[0]);
4266 break;
4267 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004268 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004269}